From 9ff99799ea7a41450fc33fbe4923d3bdc4cf20fa Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 18 Jun 2020 20:29:04 +0200 Subject: [PATCH] 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 --- .../core/cereal/pointer_wrapper_test.hpp | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/mlpack/core/cereal/pointer_wrapper_test.hpp diff --git a/src/mlpack/core/cereal/pointer_wrapper_test.hpp b/src/mlpack/core/cereal/pointer_wrapper_test.hpp new file mode 100644 index 0000000000..430754b88c --- /dev/null +++ b/src/mlpack/core/cereal/pointer_wrapper_test.hpp @@ -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 +#include +#include +#include +#include + +namespace cereal { + +template +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 + void save(Archive& ar, const unsigned int /*version*/) const + { + std::unique_ptr smartPointer = std::make_unique(*this->localPointer); + ar(CEREAL_NVP(smartPointer)); + } + + template + void load(Archive& ar, const unsigned int /*version*/) + { + std::unique_ptr 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 moved_pointer; +}; + +template +inline pointer_wrapper +make_pointer(T* t) +{ + pointer_wrapper a(t); + return a; +} + +} // end namespace cereal + +#endif // CEREAL_POINTER_WRAPPER_HPP