LCOV - code coverage report
Current view: top level - cereal - access.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 45 47 95.7 %
Date: 2022-01-16 21:05:07 Functions: 456 467 97.6 %

          Line data    Source code
       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 the copyright holder 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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        1608 :       T * operator->()
     186             :       {
     187        1608 :         if( !itsValid )
     188           0 :           throw Exception("Object must be initialized prior to accessing members");
     189             : 
     190        1608 :         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           4 :       T * ptr()
     202             :       {
     203           4 :         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       11608 :       construct( T * p ) : itsPtr( p ), itsEnableSharedRestoreFunction( [](){} ), itsValid( false ) {}
     211        2400 :       construct( T * p, std::function<void()> enableSharedFunc ) : // g++4.7 ice with default lambda to std func
     212        2400 :         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     5141140 :       static auto member_serialize(Archive & ar, T & t) -> decltype(t.CEREAL_SERIALIZE_FUNCTION_NAME(ar))
     246     5141140 :       { return t.CEREAL_SERIALIZE_FUNCTION_NAME(ar); }
     247             : 
     248             :       template<class Archive, class T> inline
     249      844979 :       static auto member_save(Archive & ar, T const & t) -> decltype(t.CEREAL_SAVE_FUNCTION_NAME(ar))
     250      844979 :       { 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      844979 :       static auto member_load(Archive & ar, T & t) -> decltype(t.CEREAL_LOAD_FUNCTION_NAME(ar))
     258      844979 :       { return t.CEREAL_LOAD_FUNCTION_NAME(ar); }
     259             : 
     260             :       template<class Archive, class T> inline
     261        1200 :       static auto member_save_minimal(Archive const & ar, T const & t) -> decltype(t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar))
     262        1200 :       { 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        1200 :       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        1200 :       { return t.CEREAL_LOAD_MINIMAL_FUNCTION_NAME(ar, std::forward<U>(u)); }
     271             : 
     272             :       // ####### Versioned Serialization #######################################
     273             :       template<class Archive, class T> inline
     274        4000 :       static auto member_serialize(Archive & ar, T & t, const std::uint32_t version ) -> decltype(t.CEREAL_SERIALIZE_FUNCTION_NAME(ar, version))
     275        4000 :       { return t.CEREAL_SERIALIZE_FUNCTION_NAME(ar, version); }
     276             : 
     277             :       template<class Archive, class T> inline
     278        1200 :       static auto member_save(Archive & ar, T const & t, const std::uint32_t version ) -> decltype(t.CEREAL_SAVE_FUNCTION_NAME(ar, version))
     279        1200 :       { 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        1200 :       static auto member_load(Archive & ar, T & t, const std::uint32_t version ) -> decltype(t.CEREAL_LOAD_FUNCTION_NAME(ar, version))
     287        1200 :       { return t.CEREAL_LOAD_FUNCTION_NAME(ar, version); }
     288             : 
     289             :       template<class Archive, class T> inline
     290         800 :       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         800 :       { 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         800 :       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         800 :       { 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        8004 :       static void construct( T *& ptr, Args && ... args )
     309             :       {
     310        8004 :         new (ptr) T( std::forward<Args>( args )... );
     311        8004 :       }
     312             : 
     313             :       // for non-placement new with a default constructor
     314             :       template <class T> inline
     315       85608 :       static T * construct()
     316             :       {
     317       85608 :         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        5200 :       static auto load_and_construct(Archive & ar, ::cereal::construct<T> & construct) -> decltype(T::load_and_construct(ar, construct))
     326             :       {
     327        5200 :         T::load_and_construct( ar, construct );
     328        4800 :       }
     329             : 
     330             :       template<class T, class Archive> inline
     331         800 :       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         800 :         T::load_and_construct( ar, construct, version );
     334         800 :       }
     335             :   }; // end class access
     336             : 
     337             :   // ######################################################################
     338             :   // Deferred Implementation, see construct for more information
     339             :   template <class T> template <class ... Args> inline
     340        8004 :   void construct<T>::operator()( Args && ... args )
     341             :   {
     342        8004 :     if( itsValid )
     343           0 :       throw Exception("Attempting to construct an already initialized object");
     344             : 
     345        8004 :     ::cereal::access::construct( itsPtr, std::forward<Args>( args )... );
     346        8004 :     itsEnableSharedRestoreFunction();
     347        8004 :     itsValid = true;
     348        8004 :   }
     349             : } // namespace cereal
     350             : 
     351             : #endif // CEREAL_ACCESS_HPP_

Generated by: LCOV version 1.14