Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TypeIdMemory.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2015 "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_TYPEIDMEMORY_H_
20 #define OMNI_TYPEIDMEMORY_H_
21 
22 #include <map>
23 #include <QBuffer>
24 #include <QByteArray>
25 #include <QDataStream>
26 #include <omni/TypeIdInterface.h>
27 
28 namespace omni {
29  /**@brief Helper class to store objects with a certain type id in QBuffer
30  @detail Used in GUI for storing previously selected mapping or canvas types
31  **/
32  template<typename INTERFACE>
33  class TypeIdMemory {
34  public:
35  /// Our interface type
36  typedef INTERFACE interface_type;
37 
38  /// Clear memory
39  void clear() {
40  memory_.clear();
41  }
42 
43  /// Store object with interface in memory
44  void store(interface_type const *_t) {
45  auto _id = _t->getTypeId();
46 
47  QBuffer _buf;
48  /// Serialize to QByteArray
49  _buf.open(QIODevice::WriteOnly);
50  QDataStream _s(&_buf);
51  _t->toStream(_s);
52  memory_[_id] = _buf.data();
53  }
54 
55  /// Restore object with interface in memory, if exists
56  void restore(interface_type *_t) const {
57  auto _id = _t->getTypeId();
58 
59  /// No exception is thrown
60  if (memory_.count(_id) == 0) return;
61 
62  /// Deserialize from QByteArray
63  QDataStream _s(memory_.at(_id));
64  _t->fromStream(_s);
65  }
66 
67  private:
68  std::map<Id, QByteArray> memory_;
69  };
70 }
71 
72 #endif /* OMNI_TYPEIDMEMORY_H_ */
std::map< Id, QByteArray > memory_
Definition: TypeIdMemory.h:68
virtual Id getTypeId() const =0
Returns type id of object.
void restore(interface_type *_t) const
Restore object with interface in memory, if exists.
Definition: TypeIdMemory.h:56
void clear()
Clear memory.
Definition: TypeIdMemory.h:39
void store(interface_type const *_t)
Store object with interface in memory.
Definition: TypeIdMemory.h:44
Helper class to store objects with a certain type id in QBuffer Used in GUI for storing previously s...
Definition: TypeIdMemory.h:33
Mapping interface with one or several inputs and shader Holds inputs and shader. ...
Definition: Interface.h:54
INTERFACE interface_type
Our interface type.
Definition: TypeIdMemory.h:36