isotree 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/LICENSE.txt +2 -1
  4. data/README.md +57 -6
  5. data/ext/isotree/ext.cpp +170 -39
  6. data/ext/isotree/extconf.rb +3 -3
  7. data/lib/isotree.rb +2 -0
  8. data/lib/isotree/dataset.rb +73 -0
  9. data/lib/isotree/isolation_forest.rb +182 -29
  10. data/lib/isotree/version.rb +1 -1
  11. data/vendor/cereal/LICENSE +24 -0
  12. data/vendor/cereal/README.md +85 -0
  13. data/vendor/cereal/include/cereal/access.hpp +351 -0
  14. data/vendor/cereal/include/cereal/archives/adapters.hpp +163 -0
  15. data/vendor/cereal/include/cereal/archives/binary.hpp +169 -0
  16. data/vendor/cereal/include/cereal/archives/json.hpp +1019 -0
  17. data/vendor/cereal/include/cereal/archives/portable_binary.hpp +334 -0
  18. data/vendor/cereal/include/cereal/archives/xml.hpp +956 -0
  19. data/vendor/cereal/include/cereal/cereal.hpp +1089 -0
  20. data/vendor/cereal/include/cereal/details/helpers.hpp +422 -0
  21. data/vendor/cereal/include/cereal/details/polymorphic_impl.hpp +796 -0
  22. data/vendor/cereal/include/cereal/details/polymorphic_impl_fwd.hpp +65 -0
  23. data/vendor/cereal/include/cereal/details/static_object.hpp +127 -0
  24. data/vendor/cereal/include/cereal/details/traits.hpp +1411 -0
  25. data/vendor/cereal/include/cereal/details/util.hpp +84 -0
  26. data/vendor/cereal/include/cereal/external/base64.hpp +134 -0
  27. data/vendor/cereal/include/cereal/external/rapidjson/allocators.h +284 -0
  28. data/vendor/cereal/include/cereal/external/rapidjson/cursorstreamwrapper.h +78 -0
  29. data/vendor/cereal/include/cereal/external/rapidjson/document.h +2652 -0
  30. data/vendor/cereal/include/cereal/external/rapidjson/encodedstream.h +299 -0
  31. data/vendor/cereal/include/cereal/external/rapidjson/encodings.h +716 -0
  32. data/vendor/cereal/include/cereal/external/rapidjson/error/en.h +74 -0
  33. data/vendor/cereal/include/cereal/external/rapidjson/error/error.h +161 -0
  34. data/vendor/cereal/include/cereal/external/rapidjson/filereadstream.h +99 -0
  35. data/vendor/cereal/include/cereal/external/rapidjson/filewritestream.h +104 -0
  36. data/vendor/cereal/include/cereal/external/rapidjson/fwd.h +151 -0
  37. data/vendor/cereal/include/cereal/external/rapidjson/internal/biginteger.h +290 -0
  38. data/vendor/cereal/include/cereal/external/rapidjson/internal/diyfp.h +271 -0
  39. data/vendor/cereal/include/cereal/external/rapidjson/internal/dtoa.h +245 -0
  40. data/vendor/cereal/include/cereal/external/rapidjson/internal/ieee754.h +78 -0
  41. data/vendor/cereal/include/cereal/external/rapidjson/internal/itoa.h +308 -0
  42. data/vendor/cereal/include/cereal/external/rapidjson/internal/meta.h +186 -0
  43. data/vendor/cereal/include/cereal/external/rapidjson/internal/pow10.h +55 -0
  44. data/vendor/cereal/include/cereal/external/rapidjson/internal/regex.h +740 -0
  45. data/vendor/cereal/include/cereal/external/rapidjson/internal/stack.h +232 -0
  46. data/vendor/cereal/include/cereal/external/rapidjson/internal/strfunc.h +69 -0
  47. data/vendor/cereal/include/cereal/external/rapidjson/internal/strtod.h +290 -0
  48. data/vendor/cereal/include/cereal/external/rapidjson/internal/swap.h +46 -0
  49. data/vendor/cereal/include/cereal/external/rapidjson/istreamwrapper.h +128 -0
  50. data/vendor/cereal/include/cereal/external/rapidjson/memorybuffer.h +70 -0
  51. data/vendor/cereal/include/cereal/external/rapidjson/memorystream.h +71 -0
  52. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/inttypes.h +316 -0
  53. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/stdint.h +300 -0
  54. data/vendor/cereal/include/cereal/external/rapidjson/ostreamwrapper.h +81 -0
  55. data/vendor/cereal/include/cereal/external/rapidjson/pointer.h +1414 -0
  56. data/vendor/cereal/include/cereal/external/rapidjson/prettywriter.h +277 -0
  57. data/vendor/cereal/include/cereal/external/rapidjson/rapidjson.h +656 -0
  58. data/vendor/cereal/include/cereal/external/rapidjson/reader.h +2230 -0
  59. data/vendor/cereal/include/cereal/external/rapidjson/schema.h +2497 -0
  60. data/vendor/cereal/include/cereal/external/rapidjson/stream.h +223 -0
  61. data/vendor/cereal/include/cereal/external/rapidjson/stringbuffer.h +121 -0
  62. data/vendor/cereal/include/cereal/external/rapidjson/writer.h +709 -0
  63. data/vendor/cereal/include/cereal/external/rapidxml/license.txt +52 -0
  64. data/vendor/cereal/include/cereal/external/rapidxml/manual.html +406 -0
  65. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml.hpp +2624 -0
  66. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_iterators.hpp +175 -0
  67. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_print.hpp +428 -0
  68. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_utils.hpp +123 -0
  69. data/vendor/cereal/include/cereal/macros.hpp +154 -0
  70. data/vendor/cereal/include/cereal/specialize.hpp +139 -0
  71. data/vendor/cereal/include/cereal/types/array.hpp +79 -0
  72. data/vendor/cereal/include/cereal/types/atomic.hpp +55 -0
  73. data/vendor/cereal/include/cereal/types/base_class.hpp +203 -0
  74. data/vendor/cereal/include/cereal/types/bitset.hpp +176 -0
  75. data/vendor/cereal/include/cereal/types/boost_variant.hpp +164 -0
  76. data/vendor/cereal/include/cereal/types/chrono.hpp +72 -0
  77. data/vendor/cereal/include/cereal/types/common.hpp +129 -0
  78. data/vendor/cereal/include/cereal/types/complex.hpp +56 -0
  79. data/vendor/cereal/include/cereal/types/concepts/pair_associative_container.hpp +73 -0
  80. data/vendor/cereal/include/cereal/types/deque.hpp +62 -0
  81. data/vendor/cereal/include/cereal/types/forward_list.hpp +68 -0
  82. data/vendor/cereal/include/cereal/types/functional.hpp +43 -0
  83. data/vendor/cereal/include/cereal/types/list.hpp +62 -0
  84. data/vendor/cereal/include/cereal/types/map.hpp +36 -0
  85. data/vendor/cereal/include/cereal/types/memory.hpp +425 -0
  86. data/vendor/cereal/include/cereal/types/optional.hpp +66 -0
  87. data/vendor/cereal/include/cereal/types/polymorphic.hpp +483 -0
  88. data/vendor/cereal/include/cereal/types/queue.hpp +132 -0
  89. data/vendor/cereal/include/cereal/types/set.hpp +103 -0
  90. data/vendor/cereal/include/cereal/types/stack.hpp +76 -0
  91. data/vendor/cereal/include/cereal/types/string.hpp +61 -0
  92. data/vendor/cereal/include/cereal/types/tuple.hpp +123 -0
  93. data/vendor/cereal/include/cereal/types/unordered_map.hpp +36 -0
  94. data/vendor/cereal/include/cereal/types/unordered_set.hpp +99 -0
  95. data/vendor/cereal/include/cereal/types/utility.hpp +47 -0
  96. data/vendor/cereal/include/cereal/types/valarray.hpp +89 -0
  97. data/vendor/cereal/include/cereal/types/variant.hpp +109 -0
  98. data/vendor/cereal/include/cereal/types/vector.hpp +112 -0
  99. data/vendor/cereal/include/cereal/version.hpp +52 -0
  100. data/vendor/isotree/LICENSE +1 -1
  101. data/vendor/isotree/README.md +7 -2
  102. data/vendor/isotree/src/RcppExports.cpp +44 -4
  103. data/vendor/isotree/src/Rwrapper.cpp +141 -51
  104. data/vendor/isotree/src/crit.cpp +1 -1
  105. data/vendor/isotree/src/dealloc.cpp +1 -1
  106. data/vendor/isotree/src/dist.cpp +6 -6
  107. data/vendor/isotree/src/extended.cpp +5 -5
  108. data/vendor/isotree/src/fit_model.cpp +27 -5
  109. data/vendor/isotree/src/helpers_iforest.cpp +26 -11
  110. data/vendor/isotree/src/impute.cpp +7 -7
  111. data/vendor/isotree/src/isoforest.cpp +7 -7
  112. data/vendor/isotree/src/isotree.hpp +27 -5
  113. data/vendor/isotree/src/merge_models.cpp +1 -1
  114. data/vendor/isotree/src/mult.cpp +1 -1
  115. data/vendor/isotree/src/predict.cpp +20 -16
  116. data/vendor/isotree/src/serialize.cpp +1 -1
  117. data/vendor/isotree/src/sql.cpp +545 -0
  118. data/vendor/isotree/src/utils.cpp +36 -44
  119. metadata +102 -81
@@ -0,0 +1,351 @@
1
+ /*! \file access.hpp
2
+ \brief Access control and default construction */
3
+ /*
4
+ Copyright (c) 2014, Randolph Voorhies, Shane Grant
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are met:
9
+ * Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+ * Neither the name of cereal nor the
15
+ names of its contributors may be used to endorse or promote products
16
+ derived from this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
22
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+ #ifndef CEREAL_ACCESS_HPP_
30
+ #define CEREAL_ACCESS_HPP_
31
+
32
+ #include <type_traits>
33
+ #include <iostream>
34
+ #include <cstdint>
35
+ #include <functional>
36
+
37
+ #include "cereal/macros.hpp"
38
+ #include "cereal/specialize.hpp"
39
+ #include "cereal/details/helpers.hpp"
40
+
41
+ namespace cereal
42
+ {
43
+ // ######################################################################
44
+ //! A class that allows cereal to load smart pointers to types that have no default constructor
45
+ /*! If your class does not have a default constructor, cereal will not be able
46
+ to load any smart pointers to it unless you overload LoadAndConstruct
47
+ for your class, and provide an appropriate load_and_construct method. You can also
48
+ choose to define a member static function instead of specializing this class.
49
+
50
+ The specialization of LoadAndConstruct must be placed within the cereal namespace:
51
+
52
+ @code{.cpp}
53
+ struct MyType
54
+ {
55
+ MyType( int x ); // note: no default ctor
56
+ int myX;
57
+
58
+ // Define a serialize or load/save pair as you normally would
59
+ template <class Archive>
60
+ void serialize( Archive & ar )
61
+ {
62
+ ar( myX );
63
+ }
64
+ };
65
+
66
+ // Provide a specialization for LoadAndConstruct for your type
67
+ namespace cereal
68
+ {
69
+ template <> struct LoadAndConstruct<MyType>
70
+ {
71
+ // load_and_construct will be passed the archive that you will be loading
72
+ // from as well as a construct object which you can use as if it were the
73
+ // constructor for your type. cereal will handle all memory management for you.
74
+ template <class Archive>
75
+ static void load_and_construct( Archive & ar, cereal::construct<MyType> & construct )
76
+ {
77
+ int x;
78
+ ar( x );
79
+ construct( x );
80
+ }
81
+
82
+ // if you require versioning, simply add a const std::uint32_t as the final parameter, e.g.:
83
+ // load_and_construct( Archive & ar, cereal::construct<MyType> & construct, std::uint32_t const version )
84
+ };
85
+ } // end namespace cereal
86
+ @endcode
87
+
88
+ Please note that just as in using external serialization functions, you cannot get
89
+ access to non-public members of your class by befriending cereal::access. If you
90
+ have the ability to modify the class you wish to serialize, it is recommended that you
91
+ use member serialize functions and a static member load_and_construct function.
92
+
93
+ load_and_construct functions, regardless of whether they are static members of your class or
94
+ whether you create one in the LoadAndConstruct specialization, have the following signature:
95
+
96
+ @code{.cpp}
97
+ // generally Archive will be templated, but it can be specific if desired
98
+ template <class Archive>
99
+ static void load_and_construct( Archive & ar, cereal::construct<MyType> & construct );
100
+ // with an optional last parameter specifying the version: const std::uint32_t version
101
+ @endcode
102
+
103
+ Versioning behaves the same way as it does for standard serialization functions.
104
+
105
+ @tparam T The type to specialize for
106
+ @ingroup Access */
107
+ template <class T>
108
+ struct LoadAndConstruct
109
+ { };
110
+
111
+ // forward decl for construct
112
+ //! @cond PRIVATE_NEVERDEFINED
113
+ namespace memory_detail{ template <class Ar, class T> struct LoadAndConstructLoadWrapper; }
114
+ namespace boost_variant_detail{ template <class Ar, class T> struct LoadAndConstructLoadWrapper; }
115
+ //! @endcond
116
+
117
+ //! Used to construct types with no default constructor
118
+ /*! When serializing a type that has no default constructor, cereal
119
+ will attempt to call either the class static function load_and_construct
120
+ or the appropriate template specialization of LoadAndConstruct. cereal
121
+ will pass that function a reference to the archive as well as a reference
122
+ to a construct object which should be used to perform the allocation once
123
+ data has been appropriately loaded.
124
+
125
+ @code{.cpp}
126
+ struct MyType
127
+ {
128
+ // note the lack of default constructor
129
+ MyType( int xx, int yy );
130
+
131
+ int x, y;
132
+ double notInConstructor;
133
+
134
+ template <class Archive>
135
+ void serialize( Archive & ar )
136
+ {
137
+ ar( x, y );
138
+ ar( notInConstructor );
139
+ }
140
+
141
+ template <class Archive>
142
+ static void load_and_construct( Archive & ar, cereal::construct<MyType> & construct )
143
+ {
144
+ int x, y;
145
+ ar( x, y );
146
+
147
+ // use construct object to initialize with loaded data
148
+ construct( x, y );
149
+
150
+ // access to member variables and functions via -> operator
151
+ ar( construct->notInConstructor );
152
+
153
+ // could also do the above section by:
154
+ double z;
155
+ ar( z );
156
+ construct->notInConstructor = z;
157
+ }
158
+ };
159
+ @endcode
160
+
161
+ @tparam T The class type being serialized
162
+ */
163
+ template <class T>
164
+ class construct
165
+ {
166
+ public:
167
+ //! Construct and initialize the type T with the given arguments
168
+ /*! This will forward all arguments to the underlying type T,
169
+ calling an appropriate constructor.
170
+
171
+ Calling this function more than once will result in an exception
172
+ being thrown.
173
+
174
+ @param args The arguments to the constructor for T
175
+ @throw Exception If called more than once */
176
+ template <class ... Args>
177
+ void operator()( Args && ... args );
178
+ // implementation deferred due to reliance on cereal::access
179
+
180
+ //! Get a reference to the initialized underlying object
181
+ /*! This must be called after the object has been initialized.
182
+
183
+ @return A reference to the initialized object
184
+ @throw Exception If called before initialization */
185
+ T * operator->()
186
+ {
187
+ if( !itsValid )
188
+ throw Exception("Object must be initialized prior to accessing members");
189
+
190
+ return itsPtr;
191
+ }
192
+
193
+ //! Returns a raw pointer to the initialized underlying object
194
+ /*! This is mainly intended for use with passing an instance of
195
+ a constructed object to cereal::base_class.
196
+
197
+ It is strongly recommended to avoid using this function in
198
+ any other circumstance.
199
+
200
+ @return A raw pointer to the initialized type */
201
+ T * ptr()
202
+ {
203
+ return operator->();
204
+ }
205
+
206
+ private:
207
+ template <class Ar, class TT> friend struct ::cereal::memory_detail::LoadAndConstructLoadWrapper;
208
+ template <class Ar, class TT> friend struct ::cereal::boost_variant_detail::LoadAndConstructLoadWrapper;
209
+
210
+ construct( T * p ) : itsPtr( p ), itsEnableSharedRestoreFunction( [](){} ), itsValid( false ) {}
211
+ construct( T * p, std::function<void()> enableSharedFunc ) : // g++4.7 ice with default lambda to std func
212
+ itsPtr( p ), itsEnableSharedRestoreFunction( enableSharedFunc ), itsValid( false ) {}
213
+ construct( construct const & ) = delete;
214
+ construct & operator=( construct const & ) = delete;
215
+
216
+ T * itsPtr;
217
+ std::function<void()> itsEnableSharedRestoreFunction;
218
+ bool itsValid;
219
+ };
220
+
221
+ // ######################################################################
222
+ //! A class that can be made a friend to give cereal access to non public functions
223
+ /*! If you desire non-public serialization functions within a class, cereal can only
224
+ access these if you declare cereal::access a friend.
225
+
226
+ @code{.cpp}
227
+ class MyClass
228
+ {
229
+ private:
230
+ friend class cereal::access; // gives access to the private serialize
231
+
232
+ template <class Archive>
233
+ void serialize( Archive & ar )
234
+ {
235
+ // some code
236
+ }
237
+ };
238
+ @endcode
239
+ @ingroup Access */
240
+ class access
241
+ {
242
+ public:
243
+ // ####### Standard Serialization ########################################
244
+ template<class Archive, class T> inline
245
+ static auto member_serialize(Archive & ar, T & t) -> decltype(t.CEREAL_SERIALIZE_FUNCTION_NAME(ar))
246
+ { return t.CEREAL_SERIALIZE_FUNCTION_NAME(ar); }
247
+
248
+ template<class Archive, class T> inline
249
+ static auto member_save(Archive & ar, T const & t) -> decltype(t.CEREAL_SAVE_FUNCTION_NAME(ar))
250
+ { return t.CEREAL_SAVE_FUNCTION_NAME(ar); }
251
+
252
+ template<class Archive, class T> inline
253
+ static auto member_save_non_const(Archive & ar, T & t) -> decltype(t.CEREAL_SAVE_FUNCTION_NAME(ar))
254
+ { return t.CEREAL_SAVE_FUNCTION_NAME(ar); }
255
+
256
+ template<class Archive, class T> inline
257
+ static auto member_load(Archive & ar, T & t) -> decltype(t.CEREAL_LOAD_FUNCTION_NAME(ar))
258
+ { return t.CEREAL_LOAD_FUNCTION_NAME(ar); }
259
+
260
+ template<class Archive, class T> inline
261
+ static auto member_save_minimal(Archive const & ar, T const & t) -> decltype(t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar))
262
+ { return t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar); }
263
+
264
+ template<class Archive, class T> inline
265
+ static auto member_save_minimal_non_const(Archive const & ar, T & t) -> decltype(t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar))
266
+ { return t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar); }
267
+
268
+ template<class Archive, class T, class U> inline
269
+ static auto member_load_minimal(Archive const & ar, T & t, U && u) -> decltype(t.CEREAL_LOAD_MINIMAL_FUNCTION_NAME(ar, std::forward<U>(u)))
270
+ { return t.CEREAL_LOAD_MINIMAL_FUNCTION_NAME(ar, std::forward<U>(u)); }
271
+
272
+ // ####### Versioned Serialization #######################################
273
+ template<class Archive, class T> inline
274
+ static auto member_serialize(Archive & ar, T & t, const std::uint32_t version ) -> decltype(t.CEREAL_SERIALIZE_FUNCTION_NAME(ar, version))
275
+ { return t.CEREAL_SERIALIZE_FUNCTION_NAME(ar, version); }
276
+
277
+ template<class Archive, class T> inline
278
+ static auto member_save(Archive & ar, T const & t, const std::uint32_t version ) -> decltype(t.CEREAL_SAVE_FUNCTION_NAME(ar, version))
279
+ { return t.CEREAL_SAVE_FUNCTION_NAME(ar, version); }
280
+
281
+ template<class Archive, class T> inline
282
+ static auto member_save_non_const(Archive & ar, T & t, const std::uint32_t version ) -> decltype(t.CEREAL_SAVE_FUNCTION_NAME(ar, version))
283
+ { return t.CEREAL_SAVE_FUNCTION_NAME(ar, version); }
284
+
285
+ template<class Archive, class T> inline
286
+ static auto member_load(Archive & ar, T & t, const std::uint32_t version ) -> decltype(t.CEREAL_LOAD_FUNCTION_NAME(ar, version))
287
+ { return t.CEREAL_LOAD_FUNCTION_NAME(ar, version); }
288
+
289
+ template<class Archive, class T> inline
290
+ static auto member_save_minimal(Archive const & ar, T const & t, const std::uint32_t version) -> decltype(t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar, version))
291
+ { return t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar, version); }
292
+
293
+ template<class Archive, class T> inline
294
+ static auto member_save_minimal_non_const(Archive const & ar, T & t, const std::uint32_t version) -> decltype(t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar, version))
295
+ { return t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar, version); }
296
+
297
+ template<class Archive, class T, class U> inline
298
+ static auto member_load_minimal(Archive const & ar, T & t, U && u, const std::uint32_t version) -> decltype(t.CEREAL_LOAD_MINIMAL_FUNCTION_NAME(ar, std::forward<U>(u), version))
299
+ { return t.CEREAL_LOAD_MINIMAL_FUNCTION_NAME(ar, std::forward<U>(u), version); }
300
+
301
+ // ####### Other Functionality ##########################################
302
+ // for detecting inheritance from enable_shared_from_this
303
+ template <class T> inline
304
+ static auto shared_from_this(T & t) -> decltype(t.shared_from_this());
305
+
306
+ // for placement new
307
+ template <class T, class ... Args> inline
308
+ static void construct( T *& ptr, Args && ... args )
309
+ {
310
+ new (ptr) T( std::forward<Args>( args )... );
311
+ }
312
+
313
+ // for non-placement new with a default constructor
314
+ template <class T> inline
315
+ static T * construct()
316
+ {
317
+ return new T();
318
+ }
319
+
320
+ template <class T> inline
321
+ static std::false_type load_and_construct(...)
322
+ { return std::false_type(); }
323
+
324
+ template<class T, class Archive> inline
325
+ static auto load_and_construct(Archive & ar, ::cereal::construct<T> & construct) -> decltype(T::load_and_construct(ar, construct))
326
+ {
327
+ T::load_and_construct( ar, construct );
328
+ }
329
+
330
+ template<class T, class Archive> inline
331
+ static auto load_and_construct(Archive & ar, ::cereal::construct<T> & construct, const std::uint32_t version) -> decltype(T::load_and_construct(ar, construct, version))
332
+ {
333
+ T::load_and_construct( ar, construct, version );
334
+ }
335
+ }; // end class access
336
+
337
+ // ######################################################################
338
+ // Deferred Implementation, see construct for more information
339
+ template <class T> template <class ... Args> inline
340
+ void construct<T>::operator()( Args && ... args )
341
+ {
342
+ if( itsValid )
343
+ throw Exception("Attempting to construct an already initialized object");
344
+
345
+ ::cereal::access::construct( itsPtr, std::forward<Args>( args )... );
346
+ itsEnableSharedRestoreFunction();
347
+ itsValid = true;
348
+ }
349
+ } // namespace cereal
350
+
351
+ #endif // CEREAL_ACCESS_HPP_
@@ -0,0 +1,163 @@
1
+ /*! \file adapters.hpp
2
+ \brief Archive adapters that provide additional functionality
3
+ on top of an existing archive */
4
+ /*
5
+ Copyright (c) 2014, Randolph Voorhies, Shane Grant
6
+ All rights reserved.
7
+
8
+ Redistribution and use in source and binary forms, with or without
9
+ modification, are permitted provided that the following conditions are met:
10
+ * Redistributions of source code must retain the above copyright
11
+ notice, this list of conditions and the following disclaimer.
12
+ * Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in the
14
+ documentation and/or other materials provided with the distribution.
15
+ * Neither the name of cereal nor the
16
+ names of its contributors may be used to endorse or promote products
17
+ derived from this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
23
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+ #ifndef CEREAL_ARCHIVES_ADAPTERS_HPP_
31
+ #define CEREAL_ARCHIVES_ADAPTERS_HPP_
32
+
33
+ #include "cereal/details/helpers.hpp"
34
+ #include <utility>
35
+
36
+ namespace cereal
37
+ {
38
+ #ifdef CEREAL_FUTURE_EXPERIMENTAL
39
+
40
+ // Forward declaration for friend access
41
+ template <class U, class A> U & get_user_data( A & );
42
+
43
+ //! Wraps an archive and gives access to user data
44
+ /*! This adapter is useful if you require access to
45
+ either raw pointers or references within your
46
+ serialization functions.
47
+
48
+ While cereal does not directly support serialization
49
+ raw pointers or references, it is sometimes the case
50
+ that you may want to supply something such as a raw
51
+ pointer or global reference to some constructor.
52
+ In this situation this adapter would likely be used
53
+ with the construct class to allow for non-default
54
+ constructors.
55
+
56
+ @note This feature is experimental and may be altered or removed in a future release. See issue #46.
57
+
58
+ @code{.cpp}
59
+ struct MyUserData
60
+ {
61
+ int * myRawPointer;
62
+ std::reference_wrapper<MyOtherType> myReference;
63
+ };
64
+
65
+ struct MyClass
66
+ {
67
+ // Note the raw pointer parameter
68
+ MyClass( int xx, int * rawP );
69
+
70
+ int x;
71
+
72
+ template <class Archive>
73
+ void serialize( Archive & ar )
74
+ { ar( x ); }
75
+
76
+ template <class Archive>
77
+ static void load_and_construct( Archive & ar, cereal::construct<MyClass> & construct )
78
+ {
79
+ int xx;
80
+ ar( xx );
81
+ // note the need to use get_user_data to retrieve user data from the archive
82
+ construct( xx, cereal::get_user_data<MyUserData>( ar ).myRawPointer );
83
+ }
84
+ };
85
+
86
+ int main()
87
+ {
88
+ {
89
+ MyUserData md;
90
+ md.myRawPointer = &something;
91
+ md.myReference = someInstanceOfType;
92
+
93
+ std::ifstream is( "data.xml" );
94
+ cereal::UserDataAdapter<MyUserData, cereal::XMLInputArchive> ar( md, is );
95
+
96
+ std::unique_ptr<MyClass> sc;
97
+ ar( sc ); // use as normal
98
+ }
99
+
100
+ return 0;
101
+ }
102
+ @endcode
103
+
104
+ @relates get_user_data
105
+
106
+ @tparam UserData The type to give the archive access to
107
+ @tparam Archive The archive to wrap */
108
+ template <class UserData, class Archive>
109
+ class UserDataAdapter : public Archive
110
+ {
111
+ public:
112
+ //! Construct the archive with some user data struct
113
+ /*! This will forward all arguments (other than the user
114
+ data) to the wrapped archive type. The UserDataAdapter
115
+ can then be used identically to the wrapped archive type
116
+
117
+ @tparam Args The arguments to pass to the constructor of
118
+ the archive. */
119
+ template <class ... Args>
120
+ UserDataAdapter( UserData & ud, Args && ... args ) :
121
+ Archive( std::forward<Args>( args )... ),
122
+ userdata( ud )
123
+ { }
124
+
125
+ private:
126
+ //! Overload the rtti function to enable dynamic_cast
127
+ void rtti() {}
128
+ friend UserData & get_user_data<UserData>( Archive & ar );
129
+ UserData & userdata; //!< The actual user data
130
+ };
131
+
132
+ //! Retrieves user data from an archive wrapped by UserDataAdapter
133
+ /*! This will attempt to retrieve the user data associated with
134
+ some archive wrapped by UserDataAdapter. If this is used on
135
+ an archive that is not wrapped, a run-time exception will occur.
136
+
137
+ @note This feature is experimental and may be altered or removed in a future release. See issue #46.
138
+
139
+ @note The correct use of this function cannot be enforced at compile
140
+ time.
141
+
142
+ @relates UserDataAdapter
143
+ @tparam UserData The data struct contained in the archive
144
+ @tparam Archive The archive, which should be wrapped by UserDataAdapter
145
+ @param ar The archive
146
+ @throws Exception if the archive this is used upon is not wrapped with
147
+ UserDataAdapter. */
148
+ template <class UserData, class Archive>
149
+ UserData & get_user_data( Archive & ar )
150
+ {
151
+ try
152
+ {
153
+ return dynamic_cast<UserDataAdapter<UserData, Archive> &>( ar ).userdata;
154
+ }
155
+ catch( std::bad_cast const & )
156
+ {
157
+ throw ::cereal::Exception("Attempting to get user data from archive not wrapped in UserDataAdapter");
158
+ }
159
+ }
160
+ #endif // CEREAL_FUTURE_EXPERIMENTAL
161
+ } // namespace cereal
162
+
163
+ #endif // CEREAL_ARCHIVES_ADAPTERS_HPP_