NAP
sequenceeditorguiclipboard.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include <rtti/rtti.h>
9 #include <rtti/object.h>
10 #include <rtti/jsonreader.h>
11 #include <rtti/defaultlinkresolver.h>
12 #include <fstream>
13 #include <rtti/objectptr.h>
14 #include <nap/logger.h>
15 
16 namespace nap
17 {
18  namespace sequenceguiclipboard
19  {
21 
28  class NAPAPI Clipboard
29  {
30  RTTI_ENABLE()
31 
32  public:
37  explicit Clipboard(const rttr::type& trackType);
38 
42  virtual ~Clipboard() = default;
43 
50  void addObject(const rtti::Object* object, const std::string& sequenceName, utility::ErrorState& errorState);
51 
57  void addObject(const rtti::Object* object, utility::ErrorState& errorState);
58 
66  template<typename T>
67  std::vector<T*> deserialize(std::vector<std::unique_ptr<rtti::Object>>& createdObjects, utility::ErrorState& errorState);
68 
69 
75  template<typename T>
76  bool isClipboard()
77  {
78  return this->get_type() == RTTI_OF(T);
79  }
80 
81 
88  template<typename T>
90  {
91  assert(isClipboard<T>());
92  return static_cast<T*>(this);
93  }
94 
95 
100  std::vector<std::string> getObjectIDs() const;
101 
108  bool containsObject(const std::string& objectID, const std::string& sequenceName) const;
109 
114  void removeObject(const std::string& objectID);
115 
120  int getObjectCount() const{ return mSerializedObjects.size(); }
121 
126  rttr::type getTrackType() const{ return mTrackType; }
127 
135  bool save(const std::string& filePath, utility::ErrorState& errorState);
136 
143  bool load(const std::string& filePath, utility::ErrorState& errorState);
144 
145  protected:
146  // the serialized objects
147  std::map<std::string, std::string> mSerializedObjects;
148  rttr::type mTrackType;
149  std::string mSequenceName;
150  };
151 
152  // shortcut
153  using SequenceClipboardPtr = std::unique_ptr<Clipboard>;
154 
155 
156  // use this method to create a clipboard
157  template<typename T, typename ... Args>
158  static SequenceClipboardPtr createClipboard(Args &&... args)
159  {
160  return std::make_unique<T>(std::forward<Args>(args)...);
161  }
162 
163 
167  class NAPAPI Empty : public Clipboard
168  {
169  RTTI_ENABLE()
170  public:
171  Empty() : Clipboard(rttr::type::empty())
172  {}
173  };
174 
175 
177  // Template definitions
179 
180  template<typename T>
181  std::vector<T*> Clipboard::deserialize(std::vector<std::unique_ptr<rtti::Object>>& createdObjects, utility::ErrorState& errorState)
182  {
183  std::vector<T *> deserialized_objects;
184 
185  createdObjects.clear();
186  for(auto &pair: mSerializedObjects)
187  {
188  // get the serialized object from the map
189  std::string serialized_object = pair.second;
190 
191  // De-serialize
193  rtti::Factory factory;
195  rtti::EPointerPropertyMode::NoRawPointers, factory, result, errorState))
196  return std::vector<T *>();
197 
198  // Resolve links
200  return std::vector<T *>();
201 
202  // Make sure we deserialized something
203  T *root_object = nullptr;
204  if(!errorState.check(!result.mReadObjects.empty(), "No objects deserialized"))
205  return std::vector<T *>();
206 
207  auto *first_object = result.mReadObjects[0].get();
208  if(!errorState.check(first_object->get_type() == RTTI_OF(T) ||
209  first_object->get_type().is_derived_from<T>(), "Root object not of correct type"))
210  return std::vector<T *>();
211 
212  // Move ownership of read objects
213  root_object = static_cast<T *>(first_object);
214  for(auto &read_object: result.mReadObjects)
215  {
216  createdObjects.emplace_back(std::move(read_object));
217  }
218 
219  // init objects
220  for(auto &object_ptr: createdObjects)
221  {
222  if(!errorState.check(object_ptr->init(errorState), "Error initializing object : %s ", errorState.toString().c_str()))
223  return std::vector<T *>();
224  }
225 
226  if(!errorState.check(root_object != nullptr, "return object is null"))
227  return std::vector<T *>();
228 
229  deserialized_objects.emplace_back(root_object);
230  }
231 
232  return deserialized_objects;
233  }
234  }
235 }
nap::rtti::EPointerPropertyMode::NoRawPointers
@ NoRawPointers
Raw pointers cannot be used to point to other object, only ObjectPtr, ResourcePtr or ComponentPtr,...
nap::sequenceguiclipboard::Clipboard::mSerializedObjects
std::map< std::string, std::string > mSerializedObjects
Definition: sequenceeditorguiclipboard.h:147
nap::utility::ErrorState::check
bool check(bool successCondition, T &&errorMessage)
Definition: errorstate.h:36
nap::sequenceguiclipboard::Clipboard::isClipboard
bool isClipboard()
Definition: sequenceeditorguiclipboard.h:76
nap::rtti::Object
Definition: object.h:30
nap::sequenceguiclipboard::Clipboard::getTrackType
rttr::type getTrackType() const
Definition: sequenceeditorguiclipboard.h:126
nap::utility::ErrorState
Definition: errorstate.h:19
nap::sequenceguiclipboard::Clipboard::getDerived
T * getDerived()
Definition: sequenceeditorguiclipboard.h:89
nap::rtti::deserializeJSON
bool NAPAPI deserializeJSON(const std::string &json, EPropertyValidationMode propertyValidationMode, EPointerPropertyMode pointerPropertyMode, Factory &factory, DeserializeResult &result, utility::ErrorState &errorState)
nap::sequenceguiclipboard::Clipboard::deserialize
std::vector< T * > deserialize(std::vector< std::unique_ptr< rtti::Object >> &createdObjects, utility::ErrorState &errorState)
Definition: sequenceeditorguiclipboard.h:181
nap::sequenceguiclipboard::Clipboard::mTrackType
rttr::type mTrackType
Definition: sequenceeditorguiclipboard.h:148
nap::utility::ErrorState::toString
const std::string toString() const
nap::rtti::DeserializeResult::mUnresolvedPointers
UnresolvedPointerList mUnresolvedPointers
Definition: deserializeresult.h:48
nap::rtti::DeserializeResult
Definition: deserializeresult.h:40
nap::sequenceguiclipboard::Empty::Empty
Empty()
Definition: sequenceeditorguiclipboard.h:171
nap::sequenceguiclipboard::Clipboard
Definition: sequenceeditorguiclipboard.h:28
nap::rtti::Factory
Definition: factory.h:78
nap::sequenceguiclipboard::SequenceClipboardPtr
std::unique_ptr< Clipboard > SequenceClipboardPtr
Definition: sequenceeditorguiclipboard.h:153
nap::sequenceguiclipboard::Clipboard::getObjectCount
int getObjectCount() const
Definition: sequenceeditorguiclipboard.h:120
nap
Definition: templateapp.h:17
nap::rtti::DeserializeResult::mReadObjects
OwnedObjectList mReadObjects
Definition: deserializeresult.h:46
nap::sequenceguiclipboard::Empty
Definition: sequenceeditorguiclipboard.h:167
nap::sequenceguiclipboard::Clipboard::mSequenceName
std::string mSequenceName
Definition: sequenceeditorguiclipboard.h:149
nap::rtti::EPropertyValidationMode::DisallowMissingProperties
@ DisallowMissingProperties
When a required property is missing from the file, an error will be returned.