ecto
registry.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #pragma once
30 
31 #if BOOST_VERSION <= 104000
32 #pragma GCC diagnostic ignored "-Wsign-compare"
33 // TODO EAR meh noisy -- We should make a portable stable archive type.
34 //#pragma message "Ignoring signed-unsigned comparison in boost::serialization in 1.40"
35 #endif
36 
37 #include <boost/archive/binary_iarchive.hpp>
38 #include <boost/archive/binary_oarchive.hpp>
39 #include <ecto/util.hpp>
40 #include <ecto/tendril.hpp>
41 #include <boost/tuple/tuple.hpp>
42 #include <map>
43 #include <ecto/log.hpp>
44 namespace ecto
45 {
46  namespace serialization
47  {
48  template<typename T, typename Archive>
49  struct writer_
50  {
51  typedef T value_type;
52  void
53  operator()(Archive& ar, const tendril& t) const
54  {
55  ECTO_LOG_DEBUG("%s", "");
56  ar << t.get<T>();
57  }
58  };
59 
60  template<typename T, typename Archive>
61  struct reader_
62  {
63  typedef T value_type;
64  void
65  operator()(Archive& ar, tendril& t) const
66  {
67  ECTO_LOG_DEBUG("%s", "");
68  if (!t.is_type<T>())
69  t << tendril(T(), ""); //don't want to lose docs.
70  ar >> t.get<T>();
71  }
72  };
73 
74  template<typename Archive>
75  struct registry: boost::noncopyable
76  {
77  typedef boost::function<void(Archive&, tendril&)> serial_fn_t;
78  typedef std::map<std::string, serial_fn_t> serial_map_t;
79 
80  template<typename Serializer>
81  void
82  add(const Serializer& s)
83  {
84  ECTO_LOG_DEBUG("%s", "");
85  typedef typename Serializer::value_type value_type;
86  const std::string& name = name_of<value_type>();
87  serial_fn_t fnc = serial_fn_t(s);
88  add(name, fnc);
89  }
90 
91  void
92  add(const std::string& name, serial_fn_t fnc);
93  void
94  serialize(const std::string& key, Archive& ar, tendril& t) const;
95 
96  serial_map_t serial_map;
97 
98  static registry<Archive>& instance();
99 
100  private:
101  registry();
102  };
103 
104  extern template struct registry<boost::archive::binary_oarchive> ;
105  extern template struct registry<boost::archive::binary_iarchive> ;
106 
109 
110 
111  template<typename T>
112  struct register_serializer: boost::noncopyable
113  {
116  private:
119  {
120  serialization::registry_binary_oa::instance().add(writer_binary_oa());
121  serialization::registry_binary_ia::instance().add(reader_binary_ia());
122  }
123  };
124 
125  template<typename T>
127 
128  }
129 
130 }
131 
132 #define ECTO_REGISTER_SERIALIZERS(Type) \
133 namespace ecto{ \
134  namespace serialization{ \
135  template struct register_serializer<Type>; \
136  } \
137 }
138 
139 #define ECTO_INSTANTIATE_SERIALIZATION(T) \
140  template void T::serialize(boost::archive::binary_oarchive&, const unsigned int); \
141  template void T::serialize(boost::archive::binary_iarchive&, const unsigned int);
142 
static const register_serializer instance
Definition: registry.hpp:117
std::map< std::string, serial_fn_t > serial_map_t
Definition: registry.hpp:78
#define ECTO_LOG_DEBUG(fmg, args)
Definition: log.hpp:66
Definition: registry.hpp:112
Definition: registry.hpp:49
bool is_type() const
runtime check if the tendril is of the given type.
Definition: tendril.hpp:217
boost::function< void(Archive &, tendril &)> serial_fn_t
Definition: registry.hpp:77
Definition: parameters.hpp:11
bool add(const ecto::tendril &t)
Definition: tendril.hpp:65
T value_type
Definition: registry.hpp:63
static registry< Archive > & instance()
Definition: registry.hpp:75
reader_< T, boost::archive::binary_iarchive > reader_binary_ia
Definition: registry.hpp:115
serial_map_t serial_map
Definition: registry.hpp:96
T value_type
Definition: registry.hpp:51
Definition: registry.hpp:61
A tendril is the slender, winding organ of the ecto::cell that gives it its awesome type erasure and ...
Definition: tendril.hpp:84
void add(const Serializer &s)
Definition: registry.hpp:82
registry< boost::archive::binary_oarchive > registry_binary_oa
Definition: registry.hpp:107
register_serializer()
Definition: registry.hpp:118
void operator()(Archive &ar, tendril &t) const
Definition: registry.hpp:65
writer_< T, boost::archive::binary_oarchive > writer_binary_oa
Definition: registry.hpp:114
const T & get() const
Definition: tendril.hpp:179
void operator()(Archive &ar, const tendril &t) const
Definition: registry.hpp:53
registry< boost::archive::binary_iarchive > registry_binary_ia
Definition: registry.hpp:108