Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pointer.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2016 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  **/
19 #ifndef OMNI_SERIALIZATION_POINTER_H_
20 #define OMNI_SERIALIZATION_POINTER_H_
21 
22 #include <memory>
23 #include <functional>
24 #include <omni/Id.h>
26 
27 namespace omni {
28  namespace serialization {
29  namespace traits {
30  struct ReadPtr {
31  template<typename STREAM, typename F>
32  STREAM& operator()(STREAM& _stream, F _f) {
33  auto _id = deserializeReturn<Id>(_stream, Id("none"));
34 
35  if (_id == "none") return _stream;
36 
37  auto *_obj = _f(_id);
38 
39  if (_obj)
40  {
41  _obj->fromStream(_stream);
42  }
43  else
44  {
46  }
47  return _stream;
48  }
49  };
50 
51  template<typename T, bool BASE_OF_SERIALIZATION_INTERFACE>
52  struct Write<T *, BASE_OF_SERIALIZATION_INTERFACE>{
53  template<typename STREAM, typename OBJ>
54  STREAM& operator()(STREAM& _stream, OBJ const& _t) {
55  if (!_t)
56  {
57  serialize(_stream, Id("none"));
58  return _stream;
59  }
60  serialize(_stream, _t->getTypeId());
61  _t->toStream(_stream);
62  return _stream;
63  }
64  };
65 
66  template<typename T, typename DELETER, bool BASE_OF_SERIALIZATION_INTERFACE>
67  struct Write<std::unique_ptr<T, DELETER>, BASE_OF_SERIALIZATION_INTERFACE>:
68  Write<T *, BASE_OF_SERIALIZATION_INTERFACE>{};
69  }
70 
71  /**@brief Deserialize a pointer from stream.
72  @detail Functor f must return a pointer which is constructed
73  from a factory with an id.
74  If pointer is not null, it will be deserialized from stream.
75  Otherwise, a serialization exception is thrown.
76  **/
77  template<typename STREAM, typename F, typename ... ARGS>
78  STREAM& deserializePtr(STREAM& _stream,
79  F _f,
80  ARGS&& ... _args) {
81  return traits::ReadPtr(_args ...)(_stream, _f);
82  }
83  }
84 
86 }
87 
88 #endif /* OMNI_SERIALIZATION_POINTER_H_ */
Definition: traits.h:82
STREAM & operator()(STREAM &_stream, OBJ const &_t)
Definition: pointer.h:54
Id type for classes An Id must only contain alpha numerical characters and must begin with a letter...
Definition: Id.h:34
STREAM & serialize(STREAM &_stream, T const &_t)
Serialize object to stream.
Definition: traits.h:140
STREAM & deserializePtr(STREAM &_stream, F _f, ARGS &&..._args)
Deserialize a pointer from stream. Functor f must return a pointer which is constructed from a facto...
Definition: pointer.h:78
Definition: pointer.h:30
STREAM & operator()(STREAM &_stream, F _f)
Definition: pointer.h:32
An exception that occurs during Serialization.
Definition: exception.h:86