Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/utility - serialize_shared_ptr.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 14 14 100.0 %
Date: 2012-04-09 Functions: 8 8 100.0 %
Branches: 3 4 75.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines serialization functions for boost::shared_ptr
      20                 :            :  * to avoid the memory leaks in the boost 1.41.0 implementation and taking
      21                 :            :  * advantage that Wispers does not require object tracking or serialization
      22                 :            :  * of polymorphic types via base class pointers.
      23                 :            :  */
      24                 :            : 
      25                 :            : #ifndef __SSRC_WSPR_UTILITY_SERIALIZE_SHARED_PTR_H
      26                 :            : #define __SSRC_WSPR_UTILITY_SERIALIZE_SHARED_PTR_H
      27                 :            : 
      28                 :            : #include <ssrc/wispers-packages.h>
      29                 :            : 
      30                 :            : #include <boost/mpl/integral_c.hpp>
      31                 :            : #include <boost/mpl/integral_c_tag.hpp>
      32                 :            : 
      33                 :            : #include <boost/shared_ptr.hpp>
      34                 :            : #include <boost/serialization/split_free.hpp>
      35                 :            : #include <boost/serialization/nvp.hpp>
      36                 :            : #include <boost/serialization/version.hpp>
      37                 :            : 
      38                 :            : namespace boost {
      39                 :            :   namespace serialization {
      40                 :            : 
      41                 :            :     template<typename T>
      42                 :            :     struct version<boost::shared_ptr<T> > {
      43                 :            :       typedef boost::mpl::integral_c_tag tag;
      44                 :            :       typedef boost::mpl::int_<1> type;
      45                 :            :       BOOST_STATIC_CONSTANT(int, value = type::value);
      46                 :            :     };
      47                 :            : 
      48                 :            :     template<class T>
      49                 :            :     struct tracking_level<boost::shared_ptr<T> > {
      50                 :            :       typedef boost::mpl::integral_c_tag tag;
      51                 :            :       typedef mpl::int_<boost::serialization::track_never> type;
      52                 :            :       BOOST_STATIC_CONSTANT(int, value = type::value);
      53                 :            :     };
      54                 :            : 
      55                 :            :     template<class Archive, class T>
      56                 :         17 :     inline void save(Archive & ar, const boost::shared_ptr<T> & value,
      57                 :            :                      const unsigned int /*version*/)
      58                 :            :     {
      59                 :         17 :       const T *v = value.get();
      60                 :         17 :       ar << boost::serialization::make_nvp("px", v);
      61                 :         17 :     }
      62                 :            : 
      63                 :            :     template<class Archive, class T>
      64                 :         17 :     inline void load(Archive & ar, boost::shared_ptr<T> & value,
      65                 :            :                      const unsigned int /*version*/)
      66                 :            :     {
      67                 :         17 :       T *v = 0;
      68                 :         17 :       ar >> boost::serialization::make_nvp("px", v);
      69                 :            : 
      70   [ +  +  -  + ]:         17 :       if(v == 0) {
      71                 :          4 :         value.reset();
      72                 :            :       } else {
      73                 :         13 :         value.reset(v);
      74                 :            :       }
      75                 :         17 :     }
      76                 :            : 
      77                 :            :     template<class Archive, class T>
      78                 :         34 :     inline void serialize(Archive & ar, boost::shared_ptr<T> & value,
      79                 :            :                           const unsigned int version)
      80                 :            :     {
      81                 :         34 :       boost::serialization::split_free(ar, value, version);
      82                 :         34 :     }
      83                 :            :   }
      84                 :            : }
      85                 :            : 
      86                 :            : #endif