@@ -90,16 +90,21 @@ class G3Pickler
9090{
9191public:
9292 template <class T > static
93- auto getstate (const py::object &self ) {
93+ auto dumpstate (const T &obj ) {
9494 std::vector<char > buffer;
9595 G3BufferOutputStream os (buffer);
9696 {
9797 cereal::PortableBinaryOutputArchive ar (os);
98- ar << self. cast < const T &>() ;
98+ ar << obj ;
9999 }
100100 os.flush ();
101101
102- py::bytes data (buffer.data (), buffer.size ());
102+ return py::bytes (buffer.data (), buffer.size ());
103+ }
104+
105+ template <class T > static
106+ auto getstate (const py::object &self) {
107+ py::bytes data = dumpstate<T>(self.cast <const T &>());
103108
104109 py::dict py_state;
105110 if (py::hasattr (self, " __dict__" ))
@@ -108,27 +113,40 @@ class G3Pickler
108113 return py::make_tuple (py_state, data);
109114 }
110115
116+ template <class T > static
117+ auto loadstate (T &obj, const std::string_view &buffer) {
118+ G3BufferInputStream fis ((char *)&buffer[0 ], buffer.size ());
119+ cereal::PortableBinaryInputArchive ar (fis);
120+
121+ ar >> obj;
122+ }
123+
111124 template <class T > static
112125 auto setstate (const py::tuple &state) {
113126 auto py_state = state[0 ].cast <py::dict>();
114-
115127 auto buffer = state[1 ].cast <std::string_view>();
116- G3BufferInputStream fis ((char *)&buffer[0 ], buffer.size ());
117- cereal::PortableBinaryInputArchive ar (fis);
118128
119129 T obj;
120- ar >> obj;
130+ loadstate<T>( obj, buffer) ;
121131
122132 return std::make_pair (std::move (obj), py_state);
123133 }
124134};
125135
126136// Call this function in a class .def method to enable pickling
127137template <class T >
128- auto
129- g3frameobject_picklesuite ()
138+ struct g3frameobject_picklesuite
130139{
131- return py::pickle (&G3Pickler::getstate<T>, &G3Pickler::setstate<T>);
140+ static constexpr bool op_enable_if_hook = true ;
141+
142+ template <typename Class>
143+ void execute (Class &cl) const {
144+ cl.def (py::pickle (&G3Pickler::getstate<T>, &G3Pickler::setstate<T>));
145+ cl.def (" _cereal_loads" , &G3Pickler::loadstate<T>,
146+ " Populate this instance from a serialized data buffer" );
147+ cl.def (" _cereal_dumps" , &G3Pickler::dumpstate<T>,
148+ " Save the state of this instance to a serialized data buffer" );
149+ }
132150};
133151
134152// Register a G3FrameObject-derived class. Includes a copy constructor,
0 commit comments