View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

object_impl.h
Go to the documentation of this file.
1#ifndef OBJECT_IMPL_H
2#define OBJECT_IMPL_H
3
4#include "serialize.h"
5#include "archives_config.h"
6
7#ifdef USE_BOOST_ARCHIVE
8#include <boost/archive/basic_archive.hpp>
9
10namespace boost {
11namespace serialization {
12
13template<>
14V_COREEXPORT void access::destroy(const vistle::shm<char>::string *t);
15
16template<>
17V_COREEXPORT void access::construct(vistle::shm<char>::string *t);
18
19template<>
20V_COREEXPORT void access::destroy(const vistle::Object::Data::AttributeList *t);
21
22template<>
23V_COREEXPORT void access::construct(vistle::Object::Data::AttributeList *t);
24
25template<>
27
28template<>
30
31} // namespace serialization
32} // namespace boost
33#endif
34
35
36namespace vistle {
37
38template<class Archive>
39void Object::serialize(Archive &ar)
40{
41 d()->serialize<Archive>(ar);
42}
43
44template<class Archive>
45void Object::Data::save(Archive &ar) const
46{
47 ar &V_NAME(ar, "meta", meta);
48 StdAttributeMap attrMap;
49 auto attrs = getAttributeList();
50 for (auto a: attrs) {
51 attrMap[a] = getAttributes(a);
52 }
53 ar &V_NAME(ar, "attributes", attrMap);
54}
55
56template<class Archive>
57void Object::Data::load(Archive &ar)
58{
59 ar &V_NAME(ar, "meta", meta);
60 attributes.clear();
61 StdAttributeMap attrMap;
62 ar &V_NAME(ar, "attributes", attrMap);
63 for (auto &kv: attrMap) {
64 auto &a = kv.first;
65 auto &vallist = kv.second;
66 setAttributeList(a, vallist);
67 }
68}
69
70template<class Archive>
71void Object::saveObject(Archive &ar) const
72{
73 ar &V_NAME(ar, "object_name", this->getName());
74 ar &V_NAME(ar, "object_type", (int)this->getType());
75 this->saveToArchive(ar);
76}
77
78template<class Archive>
80{
81 Object *obj = nullptr;
82 try {
83 std::string arname;
84 ar &V_NAME(ar, "object_name", arname);
85 std::string name = ar.translateObjectName(arname);
86 int type;
87 ar &V_NAME(ar, "object_type", type);
89 Object::Data *objData = nullptr;
90 if (!name.empty())
91 objData = Shm::the().getObjectDataFromName(name);
92 if (objData && objData->isComplete()) {
93 objData->ref();
95 obj = Object::create(objData);
96 if (!ar.currentObject())
97 ar.setCurrentObject(objData);
98 objData->unref();
99 assert(obj->refcount() >= 1);
100 } else {
101 if (objData) {
102 std::cerr << "Object::loadObject: have " << name << ", but incomplete, refcount=" << objData->refcount()
103 << std::endl;
104 obj = Object::create(objData);
105 } else {
106 auto funcs = ObjectTypeRegistry::getType(type);
107 obj = funcs.createEmpty(name);
108 }
110 name = obj->getName();
111 ar.registerObjectNameTranslation(arname, name);
112 obj->loadFromArchive(ar);
113 assert(obj->refcount() >= 1);
114 }
115#ifdef USE_BOOST_ARCHIVE
116 } catch (const boost::archive::archive_exception &ex) {
117 std::cerr << "Boost.Archive exception: " << ex.what() << std::endl;
118 if (ex.code == boost::archive::archive_exception::unsupported_version) {
119 std::cerr << "***" << std::endl;
120 std::cerr << "*** received Boost archive in unsupported format, supported is up to "
121 << boost::archive::BOOST_ARCHIVE_VERSION() << std::endl;
122 std::cerr << "***" << std::endl;
123 }
124 return obj;
125#endif
126 } catch (std::exception &ex) {
127 std::cerr << "exception during object loading: " << ex.what() << std::endl;
128 return obj;
129 } catch (...) {
130 throw;
131 }
132 assert(ar.currentObject() == obj->d());
133 if (obj->d()->unresolvedReferences == 0) {
134 obj->refresh();
135 obj->check();
136 if (ar.objectCompletionHandler())
137 ar.objectCompletionHandler()();
138 } else {
139 //std::cerr << "LOADED " << obj->d()->name << " (" << obj->d()->type << "): " << obj->d()->unresolvedReferences << " unresolved references" << std::endl;
140 }
141 return obj;
142}
143
144} // namespace vistle
145
146#endif
#define V_NAME(ar, name, obj)
Definition: archives_config.h:441
static const struct FunctionTable & getType(int id)
Definition: object.cpp:733
Definition: object.h:58
int refcount() const
Definition: object.cpp:344
Data * d() const
Definition: object.h:211
static Object * createEmpty(const std::string &name=std::string())
Definition: object.cpp:290
static Object * loadObject(Archive &ar)
Definition: object_impl.h:79
virtual void refresh() const
refresh cached pointers from shm
Definition: object.cpp:301
void saveObject(Archive &ar) const
Definition: object_impl.h:71
Type getType() const
Definition: object.cpp:380
static Object * create(Data *)
Definition: object.cpp:132
virtual bool check() const
Definition: object.cpp:304
std::string getName() const
Definition: object.cpp:385
ObjectData * getObjectDataFromName(const std::string &name) const
Definition: shm.cpp:581
void unlockObjects() const
Definition: shm.cpp:203
static Shm & the()
Definition: shm.cpp:315
void lockObjects() const
Definition: shm.cpp:191
#define V_COREEXPORT
Definition: export.h:9
Definition: serialize.h:35
Definition: allobjects.cpp:30
Definition: object.h:233
std::map< std::string, std::vector< std::string > > StdAttributeMap
Definition: object.h:251
std::atomic< int > unresolvedReferences
no. of not-yet-available arrays and referenced objects
Definition: object.h:236
std::pair< const Key, AttributeList > AttributeMapValueType
Definition: object.h:243
ARCHIVE_ACCESS_SPLIT void save(Archive &ar) const
V_COREEXPORT bool isComplete() const
Definition: object.cpp:204
V_COREEXPORT std::vector< std::string > getAttributeList() const
Definition: object.cpp:620
Meta meta
Definition: object.h:238
void load(Archive &ar)
Definition: object_impl.h:57
V_COREEXPORT std::vector< std::string > getAttributes(const std::string &key) const
Definition: object.cpp:605
AttributeMap attributes
Definition: object.h:250
V_COREEXPORT int unref() const
Definition: object.cpp:364
V_COREEXPORT void setAttributeList(const std::string &key, const std::vector< std::string > &values)
Definition: object.cpp:551
V_COREEXPORT int ref() const
Definition: object.cpp:359
shm< Attribute >::vector AttributeList
Definition: object.h:242
int refcount() const
Definition: shmdata.h:35
boost::interprocess::basic_string< T, std::char_traits< T >, allocator > string
Definition: shm.h:112