You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
14 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*! \file helpers.hpp
  2. \brief Internal helper functionality
  3. \ingroup Internal */
  4. /*
  5. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  6. All rights reserved.
  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. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef CEREAL_DETAILS_HELPERS_HPP_
  29. #define CEREAL_DETAILS_HELPERS_HPP_
  30. #include <type_traits>
  31. #include <cstdint>
  32. #include <utility>
  33. #include <memory>
  34. #include <unordered_map>
  35. #include <stdexcept>
  36. #include "cereal/macros.hpp"
  37. #include "cereal/details/static_object.hpp"
  38. namespace cereal
  39. {
  40. // ######################################################################
  41. //! An exception class thrown when things go wrong at runtime
  42. /*! @ingroup Utility */
  43. struct Exception : public std::runtime_error
  44. {
  45. explicit Exception( const std::string & what_ ) : std::runtime_error(what_) {}
  46. explicit Exception( const char * what_ ) : std::runtime_error(what_) {}
  47. };
  48. // ######################################################################
  49. //! The size type used by cereal
  50. /*! To ensure compatability between 32, 64, etc bit machines, we need to use
  51. a fixed size type instead of size_t, which may vary from machine to
  52. machine.
  53. The default value for CEREAL_SIZE_TYPE is specified in cereal/macros.hpp */
  54. using size_type = CEREAL_SIZE_TYPE;
  55. // forward decls
  56. class BinaryOutputArchive;
  57. class BinaryInputArchive;
  58. // ######################################################################
  59. namespace detail
  60. {
  61. struct NameValuePairCore {}; //!< Traits struct for NVPs
  62. }
  63. //! For holding name value pairs
  64. /*! This pairs a name (some string) with some value such that an archive
  65. can potentially take advantage of the pairing.
  66. In serialization functions, NameValuePairs are usually created like so:
  67. @code{.cpp}
  68. struct MyStruct
  69. {
  70. int a, b, c, d, e;
  71. template<class Archive>
  72. void serialize(Archive & archive)
  73. {
  74. archive( CEREAL_NVP(a),
  75. CEREAL_NVP(b),
  76. CEREAL_NVP(c),
  77. CEREAL_NVP(d),
  78. CEREAL_NVP(e) );
  79. }
  80. };
  81. @endcode
  82. Alternatively, you can give you data members custom names like so:
  83. @code{.cpp}
  84. struct MyStruct
  85. {
  86. int a, b, my_embarrassing_variable_name, d, e;
  87. template<class Archive>
  88. void serialize(Archive & archive)
  89. {
  90. archive( CEREAL_NVP(a),
  91. CEREAL_NVP(b),
  92. cereal::make_nvp("var", my_embarrassing_variable_name) );
  93. CEREAL_NVP(d),
  94. CEREAL_NVP(e) );
  95. }
  96. };
  97. @endcode
  98. There is a slight amount of overhead to creating NameValuePairs, so there
  99. is a third method which will elide the names when they are not used by
  100. the Archive:
  101. @code{.cpp}
  102. struct MyStruct
  103. {
  104. int a, b;
  105. template<class Archive>
  106. void serialize(Archive & archive)
  107. {
  108. archive( cereal::make_nvp<Archive>(a),
  109. cereal::make_nvp<Archive>(b) );
  110. }
  111. };
  112. @endcode
  113. This third method is generally only used when providing generic type
  114. support. Users writing their own serialize functions will normally
  115. explicitly control whether they want to use NVPs or not.
  116. @internal */
  117. template <class T>
  118. class NameValuePair : detail::NameValuePairCore
  119. {
  120. private:
  121. // If we get passed an array, keep the type as is, otherwise store
  122. // a reference if we were passed an l value reference, else copy the value
  123. using Type = typename std::conditional<std::is_array<typename std::remove_reference<T>::type>::value,
  124. typename std::remove_cv<T>::type,
  125. typename std::conditional<std::is_lvalue_reference<T>::value,
  126. T,
  127. typename std::decay<T>::type>::type>::type;
  128. // prevent nested nvps
  129. static_assert( !std::is_base_of<detail::NameValuePairCore, T>::value,
  130. "Cannot pair a name to a NameValuePair" );
  131. NameValuePair & operator=( NameValuePair const & ) = delete;
  132. public:
  133. //! Constructs a new NameValuePair
  134. /*! @param n The name of the pair
  135. @param v The value to pair. Ideally this should be an l-value reference so that
  136. the value can be both loaded and saved to. If you pass an r-value reference,
  137. the NameValuePair will store a copy of it instead of a reference. Thus you should
  138. only pass r-values in cases where this makes sense, such as the result of some
  139. size() call.
  140. @internal */
  141. NameValuePair( char const * n, T && v ) : name(n), value(std::forward<T>(v)) {}
  142. char const * name;
  143. Type value;
  144. };
  145. //! A specialization of make_nvp<> that simply forwards the value for binary archives
  146. /*! @relates NameValuePair
  147. @internal */
  148. template<class Archive, class T> inline
  149. typename
  150. std::enable_if<std::is_same<Archive, ::cereal::BinaryInputArchive>::value ||
  151. std::is_same<Archive, ::cereal::BinaryOutputArchive>::value,
  152. T && >::type
  153. make_nvp( const char *, T && value )
  154. {
  155. return std::forward<T>(value);
  156. }
  157. //! A specialization of make_nvp<> that actually creates an nvp for non-binary archives
  158. /*! @relates NameValuePair
  159. @internal */
  160. template<class Archive, class T> inline
  161. typename
  162. std::enable_if<!std::is_same<Archive, ::cereal::BinaryInputArchive>::value &&
  163. !std::is_same<Archive, ::cereal::BinaryOutputArchive>::value,
  164. NameValuePair<T> >::type
  165. make_nvp( const char * name, T && value)
  166. {
  167. return {name, std::forward<T>(value)};
  168. }
  169. //! Convenience for creating a templated NVP
  170. /*! For use in internal generic typing functions which have an
  171. Archive type declared
  172. @internal */
  173. #define CEREAL_NVP_(name, value) ::cereal::make_nvp<Archive>(name, value)
  174. // ######################################################################
  175. //! A wrapper around data that can be serialized in a binary fashion
  176. /*! This class is used to demarcate data that can safely be serialized
  177. as a binary chunk of data. Individual archives can then choose how
  178. best represent this during serialization.
  179. @internal */
  180. template <class T>
  181. struct BinaryData
  182. {
  183. //! Internally store the pointer as a void *, keeping const if created with
  184. //! a const pointer
  185. using PT = typename std::conditional<std::is_const<typename std::remove_pointer<T>::type>::value,
  186. const void *,
  187. void *>::type;
  188. BinaryData( T && d, uint64_t s ) : data(std::forward<T>(d)), size(s) {}
  189. PT data; //!< pointer to beginning of data
  190. uint64_t size; //!< size in bytes
  191. };
  192. // ######################################################################
  193. namespace detail
  194. {
  195. // base classes for type checking
  196. /* The rtti virtual function only exists to enable an archive to
  197. be used in a polymorphic fashion, if necessary. See the
  198. archive adapters for an example of this */
  199. class OutputArchiveBase
  200. {
  201. public:
  202. OutputArchiveBase() = default;
  203. OutputArchiveBase( OutputArchiveBase && ) CEREAL_NOEXCEPT {}
  204. OutputArchiveBase & operator=( OutputArchiveBase && ) CEREAL_NOEXCEPT { return *this; }
  205. virtual ~OutputArchiveBase() CEREAL_NOEXCEPT = default;
  206. private:
  207. virtual void rtti() {}
  208. };
  209. class InputArchiveBase
  210. {
  211. public:
  212. InputArchiveBase() = default;
  213. InputArchiveBase( InputArchiveBase && ) CEREAL_NOEXCEPT {}
  214. InputArchiveBase & operator=( InputArchiveBase && ) CEREAL_NOEXCEPT { return *this; }
  215. virtual ~InputArchiveBase() CEREAL_NOEXCEPT = default;
  216. private:
  217. virtual void rtti() {}
  218. };
  219. // forward decls for polymorphic support
  220. template <class Archive, class T> struct polymorphic_serialization_support;
  221. struct adl_tag;
  222. // used during saving pointers
  223. static const int32_t msb_32bit = 0x80000000;
  224. static const int32_t msb2_32bit = 0x40000000;
  225. }
  226. // ######################################################################
  227. //! A wrapper around size metadata
  228. /*! This class provides a way for archives to have more flexibility over how
  229. they choose to serialize size metadata for containers. For some archive
  230. types, the size may be implicitly encoded in the output (e.g. JSON) and
  231. not need an explicit entry. Specializing serialize or load/save for
  232. your archive and SizeTags allows you to choose what happens.
  233. @internal */
  234. template <class T>
  235. class SizeTag
  236. {
  237. private:
  238. // Store a reference if passed an lvalue reference, otherwise
  239. // make a copy of the data
  240. using Type = typename std::conditional<std::is_lvalue_reference<T>::value,
  241. T,
  242. typename std::decay<T>::type>::type;
  243. SizeTag & operator=( SizeTag const & ) = delete;
  244. public:
  245. SizeTag( T && sz ) : size(std::forward<T>(sz)) {}
  246. Type size;
  247. };
  248. // ######################################################################
  249. //! A wrapper around a key and value for serializing data into maps.
  250. /*! This class just provides a grouping of keys and values into a struct for
  251. human readable archives. For example, XML archives will use this wrapper
  252. to write maps like so:
  253. @code{.xml}
  254. <mymap>
  255. <item0>
  256. <key>MyFirstKey</key>
  257. <value>MyFirstValue</value>
  258. </item0>
  259. <item1>
  260. <key>MySecondKey</key>
  261. <value>MySecondValue</value>
  262. </item1>
  263. </mymap>
  264. @endcode
  265. \sa make_map_item
  266. @internal */
  267. template <class Key, class Value>
  268. struct MapItem
  269. {
  270. using KeyType = typename std::conditional<
  271. std::is_lvalue_reference<Key>::value,
  272. Key,
  273. typename std::decay<Key>::type>::type;
  274. using ValueType = typename std::conditional<
  275. std::is_lvalue_reference<Value>::value,
  276. Value,
  277. typename std::decay<Value>::type>::type;
  278. //! Construct a MapItem from a key and a value
  279. /*! @internal */
  280. MapItem( Key && key_, Value && value_ ) : key(std::forward<Key>(key_)), value(std::forward<Value>(value_)) {}
  281. MapItem & operator=( MapItem const & ) = delete;
  282. KeyType key;
  283. ValueType value;
  284. //! Serialize the MapItem with the NVPs "key" and "value"
  285. template <class Archive> inline
  286. void CEREAL_SERIALIZE_FUNCTION_NAME(Archive & archive)
  287. {
  288. archive( make_nvp<Archive>("key", key),
  289. make_nvp<Archive>("value", value) );
  290. }
  291. };
  292. //! Create a MapItem so that human readable archives will group keys and values together
  293. /*! @internal
  294. @relates MapItem */
  295. template <class KeyType, class ValueType> inline
  296. MapItem<KeyType, ValueType> make_map_item(KeyType && key, ValueType && value)
  297. {
  298. return {std::forward<KeyType>(key), std::forward<ValueType>(value)};
  299. }
  300. namespace detail
  301. {
  302. //! Tag for Version, which due to its anonymous namespace, becomes a different
  303. //! type in each translation unit
  304. /*! This allows CEREAL_CLASS_VERSION to be safely called in a header file */
  305. namespace{ struct version_binding_tag {}; }
  306. // ######################################################################
  307. //! Version information class
  308. /*! This is the base case for classes that have not been explicitly
  309. registered */
  310. template <class T, class BindingTag = version_binding_tag> struct Version
  311. {
  312. static const std::uint32_t version = 0;
  313. // we don't need to explicitly register these types since they
  314. // always get a version number of 0
  315. };
  316. //! Holds all registered version information
  317. struct Versions
  318. {
  319. std::unordered_map<std::size_t, std::uint32_t> mapping;
  320. std::uint32_t find( std::size_t hash, std::uint32_t version )
  321. {
  322. const auto result = mapping.emplace( hash, version );
  323. return result.first->second;
  324. }
  325. }; // struct Versions
  326. } // namespace detail
  327. } // namespace cereal
  328. #endif // CEREAL_DETAILS_HELPERS_HPP_