Преглед на файлове

Better handling of function copy (still buggy issue #1 )

devel
Ludovic 'Archivist' Lagouardette преди 4 години
родител
ревизия
236d5ea5f1
променени са 2 файла, в които са добавени 42 реда и са изтрити 11 реда
  1. +2
    -2
      Makefile
  2. +40
    -9
      include/gp/function.hpp

+ 2
- 2
Makefile Целия файл

@ -1,8 +1,8 @@
CXX= clang++-8 CXX= clang++-8
CXXFLAGS= --std=c++17 -O0 -pthread -DGP_TESTS -DFUZZ_STRENGTH=500 -pedantic -Werror \ CXXFLAGS= --std=c++17 -O0 -pthread -DGP_TESTS -DFUZZ_STRENGTH=500 -pedantic -Werror \
-Wno-unknown-attributes -frtti \ -Wno-unknown-attributes -frtti \
-g -fprofile-instr-generate -fcoverage-mapping
# -fsanitize=address -fno-omit-frame-pointer
-g -fprofile-instr-generate -fcoverage-mapping \
-fsanitize=address -fno-omit-frame-pointer
all: tests all: tests
tests: bin/tests tests: bin/tests

+ 40
- 9
include/gp/function.hpp Целия файл

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "gp/exception.hpp" #include "gp/exception.hpp"
#include "gp/algorithm/tmp_manip.hpp"
namespace gp{ namespace gp{
template <typename> template <typename>
@ -10,6 +10,8 @@ namespace gp{
class function<ret(args...)>{ class function<ret(args...)>{
struct virtual_callable struct virtual_callable
{ {
virtual void inplace_copy(char*) = 0;
virtual virtual_callable* all_copy() = 0;
virtual ~virtual_callable() = default; virtual ~virtual_callable() = default;
virtual ret operator() (args...) = 0; virtual ret operator() (args...) = 0;
}; };
@ -22,8 +24,18 @@ namespace gp{
: internal_representation{func} : internal_representation{func}
{} {}
callable(callable&) = default;
virtual ~callable() override = default; virtual ~callable() override = default;
virtual void inplace_copy(char* ptr) override {
new(ptr) callable(*this);
}
virtual virtual_callable* all_copy() override {
return new callable(*this);
}
ret operator() (args... arg_list) override ret operator() (args... arg_list) override
{ {
return internal_representation(arg_list...); return internal_representation(arg_list...);
@ -59,16 +71,21 @@ namespace gp{
delete self.functor; delete self.functor;
} }
} }
if constexpr (sizeof(callable<T>) <= sizeof(self))
{
new((void*)self.inplace) callable(t);
state = (state_t)(ACTIVE | SOO);
}
else
if(t.state | ACTIVE)
{ {
self.functor = new callable<T>(t);
state = (state_t)(ACTIVE | NO_SOO);
if constexpr (sizeof(callable<T>) <= sizeof(self))
{
t.self.functor->inplace_copy(self.inplace);
state = (state_t)(ACTIVE | SOO);
}
else
{
self.functor = t.self.functor->all_copy();
state = (state_t)(ACTIVE | NO_SOO);
}
return;
} }
state = INACTIVE;
} }
function() function()
@ -91,6 +108,20 @@ namespace gp{
} }
} }
template<>
function<function>(function& t) {
if(t.state & SOO)
{
t.self.functor->inplace_copy(self.inplace);
state = (state_t)(ACTIVE | SOO);
}
else
{
self.functor = t.self.functor->all_copy();
state = (state_t)(ACTIVE | NO_SOO);
}
}
template <typename T> template <typename T>
function(T& t) function(T& t)
{ {

Зареждане…
Отказ
Запис