Another tests that I have done by changing the pointer_wrapper
Only you have to change name to do tests Signed-off-by: Omar Shrit <omar@shrit.me>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#ifndef CEREAL_PONTER_WRAPPER_HPP
|
||||
#define CEREAL_PONTER_WRAPPER_HPP
|
||||
|
||||
/*
|
||||
* The objective of this class is to create a wrapper for
|
||||
* raw pointer by encapsulating them in a smart pointer unique_ptr
|
||||
* This will allow to serialize raw pointer in cereal as a smart pointer
|
||||
* because it will be difficult to change all pointer type in mlpack
|
||||
*/
|
||||
|
||||
#include <cereal/archives/binary.hpp>
|
||||
#include <cereal/archives/json.hpp>
|
||||
#include <cereal/archives/portable_binary.hpp>
|
||||
#include <cereal/archives/xml.hpp>
|
||||
#include <cereal/types/memory.hpp>
|
||||
|
||||
namespace cereal {
|
||||
|
||||
template<class T>
|
||||
class pointer_wrapper
|
||||
{
|
||||
public:
|
||||
pointer_wrapper(T*& pointer)
|
||||
: localPointer(&pointer)
|
||||
{
|
||||
std::cout << "address of pointer" << &pointer << std::endl;
|
||||
std::cout << "address of pointer" << *localPointer << std::endl;
|
||||
std::cout << "address saved by localPointer: " << localPointer << std::endl;
|
||||
if (pointer == nullptr) {
|
||||
*localPointer = new T();
|
||||
std::cout << "address pointed by the pointer" << pointer << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void save(Archive& ar, const unsigned int /*version*/) const
|
||||
{
|
||||
std::unique_ptr<T> smartPointer = std::make_unique<T>(*this->localPointer);
|
||||
ar(CEREAL_NVP(smartPointer));
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void load(Archive& ar, const unsigned int /*version*/)
|
||||
{
|
||||
std::unique_ptr<T> smartPointer;
|
||||
ar(CEREAL_NVP(smartPointer));
|
||||
|
||||
moved_pointer = std::move(smartPointer);
|
||||
|
||||
std::cout << "The value of localPointer is : " << *moved_pointer
|
||||
<< std::endl;
|
||||
auto p = *moved_pointer;
|
||||
std::cout << "p" << p << std::endl;
|
||||
release_pointer();
|
||||
}
|
||||
|
||||
void release_pointer()
|
||||
{
|
||||
std::cout << "The addresss of pointed by localpointer : "
|
||||
<< this->localPointer << std::endl;
|
||||
// double i =100;
|
||||
**localPointer = 27;
|
||||
std::cout << "The value for d should be: "
|
||||
<< **this->localPointer << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
T** localPointer;
|
||||
std::unique_ptr<T> moved_pointer;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
inline pointer_wrapper<T>
|
||||
make_pointer(T* t)
|
||||
{
|
||||
pointer_wrapper<T> a(t);
|
||||
return a;
|
||||
}
|
||||
|
||||
} // end namespace cereal
|
||||
|
||||
#endif // CEREAL_POINTER_WRAPPER_HPP
|
||||
Reference in New Issue
Block a user