/*! @internal */ template <class Archive, class T, class D> inline typename std::enable_if<traits::has_load_and_construct<T, Archive>::value, void>::type load( Archive & ar, memory_detail::PtrWrapper<std::unique_ptr<T, D> &> & wrapper ) { uint8_t isValid; ar( _CEREAL_NVP("valid", isValid) ); auto & ptr = wrapper.ptr; if( isValid ) { // Storage type for the pointer - since we can't default construct this type, // we'll allocate it using std::aligned_storage using ST = typename std::aligned_storage<sizeof(T)>::type; // Allocate storage - note the ST type so that deleter is correct if // an exception is thrown before we are initialized std::unique_ptr<ST> stPtr( new ST() ); // Use wrapper to enter into "data" nvp of ptr_wrapper memory_detail::LoadAndConstructLoadWrapper<Archive, T> loadWrapper( reinterpret_cast<T *>( stPtr.get() ) ); // Initialize storage ar( _CEREAL_NVP("data", loadWrapper) ); // Transfer ownership to correct unique_ptr type ptr.reset( reinterpret_cast<T *>( stPtr.release() ) ); } else ptr.reset( nullptr ); }
/*! @param ar The archive @param ptr Raw pointer held by the shared_ptr @internal */ template <class Archive, class T> inline void loadAndConstructSharedPtr( Archive & ar, T * ptr, std::true_type /* has_shared_from_this */ ) { memory_detail::LoadAndConstructLoadWrapper<Archive, T> loadWrapper( ptr ); memory_detail::EnableSharedStateHelper<T> state( ptr ); // let the user perform their initialization ar( _CEREAL_NVP("data", loadWrapper) ); }
/*! @param ar The archive @param ptr Raw pointer held by the shared_ptr @internal */ template <class Archive, class T> inline void loadAndConstructSharedPtr( Archive & ar, T * ptr, std::true_type /* has_shared_from_this */ ) { memory_detail::EnableSharedStateHelper<T> state( ptr ); memory_detail::LoadAndConstructLoadWrapper<Archive, T> loadWrapper( ptr, [&](){ state.restore(); } ); // let the user perform their initialization, shared state will be restored as soon as construct() // is called ar( CEREAL_NVP_("data", loadWrapper) ); }
void doThings(lua_State* L) { // load the function. load(L, "functions.lua"); // load the Unit Wrapper. loadWrapper(L); ApplyDamageFunction damageFunction("applyDamage"); Unit unit1(12, 30); Unit unit2(7, 40); std::cout << "Health of unit1 and unit2 before unit1 deals damage to unit2 : [Unit1 : " << unit1.health << "] [Unit2 : " << unit2.health << "]" << std::endl; damageFunction.applyDamage(L, unit1, unit2); std::cout << "Health of unit1 and unit2 after unit1 deals damage to unit2 : [Unit1 : " << unit1.health << "] [Unit2 : " << unit2.health << "]" << std::endl; damageFunction.applyDamage(L, unit2, unit1); std::cout << "Health of unit1 and unit2 after unit2 deals damage to unit1 : [Unit1 : " << unit1.health << "] [Unit2 : " << unit2.health << "]" << std::endl; }
/*! This is the typical case, where we simply pass the load wrapper to the archive. @param ar The archive @param ptr Raw pointer held by the shared_ptr @internal */ template <class Archive, class T> inline void loadAndConstructSharedPtr( Archive & ar, T * ptr, std::false_type /* has_shared_from_this */ ) { memory_detail::LoadAndConstructLoadWrapper<Archive, T> loadWrapper( ptr ); ar( _CEREAL_NVP("data", loadWrapper) ); }
bool Script::enable(const std::string &namespaceName) { return loadWrapper(L, namespaceName.c_str()); }