View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

findobjectreferenceoarchive.h
Go to the documentation of this file.
1//-------------------------------------------------------------------------
2// FIND OBJECT REFERENCE OARCHIVE H
3// *
4// *
5// * Sever Topan, 2016
6//-------------------------------------------------------------------------
7
8#ifndef FIND_OBJECT_REFERENCE_OARCHIVE_H
9#define FIND_OBJECT_REFERENCE_OARCHIVE_H
10
11#include "archives_config.h"
12
13#ifdef USE_INTROSPECTION_ARCHIVE
14
15#include <cstddef>
16
17#include <boost/config.hpp>
18#include <boost/type_traits/is_enum.hpp>
19#include <boost/mpl/bool.hpp>
20#include <boost/mpl/eval_if.hpp>
21#include <boost/mpl/int.hpp>
22#include <boost/mpl/equal_to.hpp>
23#include <boost/serialization/nvp.hpp>
24#include <boost/serialization/array.hpp>
25#include <boost/serialization/string.hpp>
26#include <boost/serialization/vector.hpp>
27#include <boost/serialization/access.hpp>
28#include <boost/serialization/version.hpp>
29
30#include <typeinfo>
31#include <typeindex>
32#include <type_traits>
33
34#include "export.h"
35#include "object.h"
36#include "shm.h"
37
38namespace vistle {
39
40
41// FORWARD DECLARATIONS
42//-------------------------------------------------------------------------
43template<class T>
44class shm_obj_ref;
45
46//-------------------------------------------------------------------------
47// FIND OBJECT REFERENCE OARCHIVE CLASS DECLARATION
48//-------------------------------------------------------------------------
49class V_COREEXPORT FindObjectReferenceOArchive {
50public:
51 enum ReferenceType { ShmVector, ObjectReference };
52
53 struct ReferenceData;
54
55private:
56 // private member variables
57 std::vector<ReferenceData> m_referenceDataVector;
58 std::string m_currNvpTag;
59 unsigned m_indexHint;
60
61 // specialized save static structs
62 template<class Archive>
63 struct save_enum_type;
64
65 template<class Archive>
66 struct save_primitive;
67
68 template<class Archive>
69 struct save_only;
70
71 // unspecialized save static structs
72 template<class T>
73 void save(const T &t);
74
75public:
76 // Implement requirements for archive concept
77 typedef boost::mpl::bool_<false> is_loading;
78 typedef boost::mpl::bool_<true> is_saving;
79
80 template<class T>
81 void register_type(const T * = NULL)
82 {}
83
84 unsigned int get_library_version() { return 0; }
85 void save_binary(const void *address, std::size_t count) {}
86
87
88 // the << operators
89 template<class T>
90 FindObjectReferenceOArchive &operator<<(T const &t);
91 template<class T>
92 FindObjectReferenceOArchive &operator<<(T *const t);
93 template<class T, int N>
94 FindObjectReferenceOArchive &operator<<(const T (&t)[N]);
95 template<class T>
96 FindObjectReferenceOArchive &operator<<(const boost::serialization::nvp<T> &t);
97 template<class T>
98 FindObjectReferenceOArchive &operator<<(vistle::ShmVector<T> &t);
99 template<class T>
100 FindObjectReferenceOArchive &operator<<(vistle::shm_obj_ref<T> &t);
101
102 // the & operator
103 template<class T>
104 FindObjectReferenceOArchive &operator&(const T &t);
105
106 // constructor
107 FindObjectReferenceOArchive(): m_indexHint(0) {}
108
109 // get functions
110 std::vector<ReferenceData> &getVector() { return m_referenceDataVector; }
111 ReferenceData *getVectorEntryByNvpName(std::string name);
112
113 // public member variables
114 static const std::string nullObjectReferenceName;
115
116 template<class T>
117 void saveArray(const vistle::ShmVector<T> &)
118 {}
119
120 template<class T>
121 void saveObject(const vistle::shm_obj_ref<T> &)
122 {}
123};
124
125
126//-------------------------------------------------------------------------
127// FIND OBJECT REFERENCE OARCHIVE CLASS DEFINITION
128//-------------------------------------------------------------------------
129
130// ARRAY DATA STRUCT
131//-------------------------------------------------------------------------
132struct FindObjectReferenceOArchive::ReferenceData {
133 std::string nvpName;
134 std::string referenceName;
135 FindObjectReferenceOArchive::ReferenceType referenceType;
136 void *ref;
137
138 ReferenceData(std::string _nvpName, std::string _referenceName,
139 FindObjectReferenceOArchive::ReferenceType _referenceType, void *_ref)
140 : nvpName(_nvpName), referenceName(_referenceName), referenceType(_referenceType), ref(_ref)
141 {}
142};
143
144// SPECIALIZED SAVE FUNCTION: ENUMS
145//-------------------------------------------------------------------------
146template<class Archive>
147struct FindObjectReferenceOArchive::save_enum_type {
148 template<class T>
149 static void invoke(Archive &ar, const T &t)
150 {
151 // do nothing
152
153 return;
154 }
155};
156
157// SPECIALIZED SAVE FUNCTION: PRIMITIVES
158//-------------------------------------------------------------------------
159template<class Archive>
160struct FindObjectReferenceOArchive::save_primitive {
161 template<class T>
162 static void invoke(Archive &ar, const T &t)
163 {
164 // do nothing
165
166 return;
167 }
168};
169
170// SPECIALIZED SAVE FUNCTION
171// * calls serialize on all non-primitive/non-enum types
172//-------------------------------------------------------------------------
173template<class Archive>
174struct FindObjectReferenceOArchive::save_only {
175 template<class T>
176 static void invoke(Archive &ar, const T &t)
177 {
178 // call serialization delegate
179 boost::serialization::serialize_adl(ar, const_cast<T &>(t), ::boost::serialization::version<T>::value);
180
181 return;
182 }
183};
184
185// UNSPECIALIZED SAVE FUNCTION
186// * delegates saving functionality based on the type of the incoming variable.
187//-------------------------------------------------------------------------
188template<class T>
189void FindObjectReferenceOArchive::save(const T &t)
190{
191 typedef BOOST_DEDUCED_TYPENAME
192 boost::mpl::eval_if<boost::is_enum<T>, boost::mpl::identity<save_enum_type<FindObjectReferenceOArchive>>,
193 //else
194 BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
195 // if its primitive
196 boost::mpl::equal_to<boost::serialization::implementation_level<T>,
197 boost::mpl::int_<boost::serialization::primitive_type>>,
198 boost::mpl::identity<save_primitive<FindObjectReferenceOArchive>>,
199 // else
200 boost::mpl::identity<save_only<FindObjectReferenceOArchive>>>>::type typex;
201 typex::invoke(*this, t);
202}
203
204
205// << OPERATOR: UNSPECIALIZED
206//-------------------------------------------------------------------------
207template<class T>
208FindObjectReferenceOArchive &FindObjectReferenceOArchive::operator<<(T const &t)
209{
210 save(t);
211
212 return *this;
213}
214
215// << OPERATOR: POINTERS
216//-------------------------------------------------------------------------
217template<class T>
218FindObjectReferenceOArchive &FindObjectReferenceOArchive::operator<<(T *const t)
219{
220 if (t != nullptr) {
221 *this << *t;
222 }
223
224 return *this;
225}
226
227// << OPERATOR: ARRAYS
228//-------------------------------------------------------------------------
229template<class T, int N>
230FindObjectReferenceOArchive &FindObjectReferenceOArchive::operator<<(const T (&t)[N])
231{
232 return *this << &t;
233}
234
235// << OPERATOR: NVP
236//-------------------------------------------------------------------------
237template<class T>
238FindObjectReferenceOArchive &FindObjectReferenceOArchive::operator<<(const boost::serialization::nvp<T> &t)
239{
240 m_currNvpTag = t.name();
241
242 return *this << t.value();
243}
244
245// << OPERATOR: SHMVECTOR
246//-------------------------------------------------------------------------
247template<class T>
249{
250 // check for object validity
251 assert(t);
252
253 // save shmName to archive
254 m_referenceDataVector.push_back(
255 ReferenceData(m_currNvpTag, std::string(t.name().name.data()), ReferenceType::ShmVector, &t));
256
257 return *this;
258}
259
260// << OPERATOR: SHM OBJECT REFERENCE
261//-------------------------------------------------------------------------
262template<class T>
264{
265 // save shmName to archive
266 if (auto obj = t.getObject()) {
267 m_referenceDataVector.push_back(
268 ReferenceData(m_currNvpTag, obj->getName(), ReferenceType::ObjectReference, &t));
269 } else {
270 m_referenceDataVector.push_back(
271 ReferenceData(m_currNvpTag, nullObjectReferenceName, ReferenceType::ObjectReference, &t));
272 }
273
274 return *this;
275}
276
277
278// & OPERATOR
279//-------------------------------------------------------------------------
280template<class T>
281FindObjectReferenceOArchive &FindObjectReferenceOArchive::operator&(const T &t)
282{
283 // delegate to appropriate << operator
284 return *this << t;
285}
286
287} // namespace vistle
288
289
290#endif /* USE_INTROSPECTION_ARCHIVE */
291#endif /* FIND_OBJECT_REFERENCE_OARCHIVE_H */
Definition: shm_reference.h:15
const shm_name_t & name() const
Definition: shm_reference_impl.h:129
Definition: shm_obj_ref.h:15
ObjType::const_ptr getObject() const
Definition: shm_obj_ref_impl.h:110
#define V_COREEXPORT
Definition: export.h:9
V_COREEXPORT std::ostream & operator<<(std::ostream &s, const Message &msg)
Definition: messages.cpp:1718
Definition: allobjects.cpp:30
std::ostream & operator<<(std::ostream &out, const Meta &meta)
Definition: objectmeta.cpp:45
shm_array_ref< shm_array< T, typename shm< T >::allocator > > ShmVector
Definition: shm.h:127
std::array< char, 32 > name
Definition: shmname.h:12