C++ 14 for Archiver

This commit is contained in:
Matthias Hochsteger 2018-12-03 18:07:52 +01:00
parent b2a2c64845
commit 2ec3bb0df1
2 changed files with 45 additions and 71 deletions

View File

@ -205,7 +205,7 @@ macro(get_dll_from_lib dll_path lib_path)
get_filename_component(lib_name ${lib} name) get_filename_component(lib_name ${lib} name)
endmacro() endmacro()
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 14)
if(WIN32) if(WIN32)
get_WIN32_WINNT(ver) get_WIN32_WINNT(ver)
add_definitions(-D_WIN32_WINNT=${ver} -DWNT -DWNT_WINDOW -DNOMINMAX) add_definitions(-D_WIN32_WINNT=${ver} -DWNT -DWNT_WINDOW -DNOMINMAX)

View File

@ -152,8 +152,6 @@ namespace ngcore
bool neededDowncast = false; bool neededDowncast = false;
// Downcasting is only possible for our registered classes // Downcasting is only possible for our registered classes
if(typeid(T) != typeid(*ptr)) if(typeid(T) != typeid(*ptr))
{
if constexpr(has_DoArchive<T>::value)
{ {
if(GetArchiveRegister().count(std::string(typeid(*ptr).name())) == 0) if(GetArchiveRegister().count(std::string(typeid(*ptr).name())) == 0)
throw std::runtime_error(std::string("Archive error: Polymorphic type ") throw std::runtime_error(std::string("Archive error: Polymorphic type ")
@ -164,7 +162,6 @@ namespace ngcore
if(reg_ptr != (void*) ptr.get()) if(reg_ptr != (void*) ptr.get())
neededDowncast = true; neededDowncast = true;
} }
}
auto pos = shared_ptr2nr.find(reg_ptr); auto pos = shared_ptr2nr.find(reg_ptr);
// if not found store -1 and the pointer // if not found store -1 and the pointer
if(pos == shared_ptr2nr.end()) if(pos == shared_ptr2nr.end())
@ -225,8 +222,6 @@ namespace ngcore
{ {
// if there was a downcast we can expect the class to be registered (since archiving // if there was a downcast we can expect the class to be registered (since archiving
// wouldn't have worked else) // wouldn't have worked else)
if constexpr(has_DoArchive<T>::value)
{
std::string name; std::string name;
(*this) & name; (*this) & name;
auto info = GetArchiveRegister()[name]; auto info = GetArchiveRegister()[name];
@ -236,9 +231,6 @@ namespace ngcore
info.upcaster(typeid(T), info.upcaster(typeid(T),
other.get()))); other.get())));
} }
else
throw std::runtime_error("Shouldn't get here...");
}
else else
ptr = std::static_pointer_cast<T>(other); ptr = std::static_pointer_cast<T>(other);
} }
@ -257,8 +249,6 @@ namespace ngcore
return (*this) << -2; return (*this) << -2;
void* reg_ptr = (void*)p; void* reg_ptr = (void*)p;
if(typeid(T) != typeid(*p)) if(typeid(T) != typeid(*p))
{
if constexpr(has_DoArchive<T>::value)
{ {
if(GetArchiveRegister().count(std::string(typeid(*p).name())) == 0) if(GetArchiveRegister().count(std::string(typeid(*p).name())) == 0)
throw std::runtime_error(std::string("Archive error: Polymorphic type ") throw std::runtime_error(std::string("Archive error: Polymorphic type ")
@ -267,14 +257,13 @@ namespace ngcore
else else
reg_ptr = GetArchiveRegister()[typeid(*p).name()].downcaster(typeid(T), p); reg_ptr = GetArchiveRegister()[typeid(*p).name()].downcaster(typeid(T), p);
} }
}
auto pos = ptr2nr.find(reg_ptr); auto pos = ptr2nr.find(reg_ptr);
// if the pointer is not found in the map create a new entry // if the pointer is not found in the map create a new entry
if (pos == ptr2nr.end()) if (pos == ptr2nr.end())
{ {
ptr2nr[reg_ptr] = ptr_count++; ptr2nr[reg_ptr] = ptr_count++;
if(typeid(*p) == typeid(T)) if(typeid(*p) == typeid(T))
if constexpr (std::is_constructible<T>::value) if (std::is_constructible<T>::value)
{ {
return (*this) << -1 & (*p); return (*this) << -1 & (*p);
} }
@ -287,8 +276,6 @@ namespace ngcore
// to avoid compile time issues we allow this behaviour only for "our" classes that // to avoid compile time issues we allow this behaviour only for "our" classes that
// implement a void DoArchive(Archive&) member function // implement a void DoArchive(Archive&) member function
// To recreate the object we need to store the true type of it // To recreate the object we need to store the true type of it
if constexpr(has_DoArchive<T>::value)
{
if(GetArchiveRegister().count(std::string(typeid(*p).name())) == 0) if(GetArchiveRegister().count(std::string(typeid(*p).name())) == 0)
throw std::runtime_error(std::string("Archive error: Polymorphic type ") throw std::runtime_error(std::string("Archive error: Polymorphic type ")
+ typeid(*p).name() + typeid(*p).name()
@ -296,11 +283,6 @@ namespace ngcore
else else
return (*this) << -3 << std::string(typeid(*p).name()) & (*p); return (*this) << -3 << std::string(typeid(*p).name()) & (*p);
} }
else
throw std::runtime_error(std::string("Archive error: Class ")
+ typeid(*p).name()
+ " is polymorphic but not registered for archiving");
}
} }
else else
{ {
@ -318,7 +300,7 @@ namespace ngcore
p = nullptr; p = nullptr;
else if (nr == -1) // create a new pointer of standard type (no virtual or multiple inheritance,...) else if (nr == -1) // create a new pointer of standard type (no virtual or multiple inheritance,...)
{ {
if constexpr (std::is_constructible<T>::value) if (std::is_constructible<T>::value)
{ {
p = new T; p = new T;
nr2ptr.push_back(p); nr2ptr.push_back(p);
@ -331,8 +313,6 @@ namespace ngcore
else if(nr == -3) // restore one of our registered classes that can have multiple inheritance,... else if(nr == -3) // restore one of our registered classes that can have multiple inheritance,...
{ {
// As stated above, we want this special behaviour only for our classes that implement DoArchive // As stated above, we want this special behaviour only for our classes that implement DoArchive
if constexpr(has_DoArchive<T>::value)
{
std::string name; std::string name;
(*this) & name; (*this) & name;
auto info = GetArchiveRegister()[name]; auto info = GetArchiveRegister()[name];
@ -344,9 +324,6 @@ namespace ngcore
nr2ptr.push_back(info.downcaster(typeid(T),p)); nr2ptr.push_back(info.downcaster(typeid(T),p));
(*this) & *p; (*this) & *p;
} }
else
throw std::runtime_error("Class isn't registered properly");
}
else else
{ {
bool downcasted; bool downcasted;
@ -355,12 +332,9 @@ namespace ngcore
if(downcasted) if(downcasted)
{ {
// if the class has been downcasted we can assume it is in the register // if the class has been downcasted we can assume it is in the register
if constexpr(has_DoArchive<T>::value)
{
auto info = GetArchiveRegister()[name]; auto info = GetArchiveRegister()[name];
p = (T*) info.upcaster(typeid(T), nr2ptr[nr]); p = (T*) info.upcaster(typeid(T), nr2ptr[nr]);
} }
}
else else
p = (T*) nr2ptr[nr]; p = (T*) nr2ptr[nr];
} }
@ -386,7 +360,7 @@ namespace ngcore
public: public:
RegisterClassForArchive() RegisterClassForArchive()
{ {
static_assert(std::is_constructible_v<T>, "Class registered for archive must be default constructible"); static_assert(std::is_constructible<T>::value, "Class registered for archive must be default constructible");
ClassArchiveInfo info; ClassArchiveInfo info;
info.creator = [this,&info](const std::type_info& ti) -> void* info.creator = [this,&info](const std::type_info& ti) -> void*
{ return typeid(T) == ti ? new T : Caster<T, Bases...>::tryUpcast(ti, new T); }; { return typeid(T) == ti ? new T : Caster<T, Bases...>::tryUpcast(ti, new T); };