AGX Dynamics  2.37.2.0
powerLineSerialization.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/version.h>
21 #include <agxStream/Serializable.h>
24 
25 namespace agxPowerLine
26 {
27  namespace detail
28  {
33  inline bool shouldReadRotationalActuatorConnectorData(size_t archiveAgxVersion, agx::UInt16 archiveSerializationVersion)
34  {
35 
36  /*
37  The RotationalActuatorConnector was given serialization version 71 and merged
38  to the RC along with a bump to AGX version 2.13.4.
39  */
40  agx::UInt16 const serializationVersionInRc = 58;
41  agx::UInt16 const serializationVersionAtChange = 71;
42  size_t const agxVersionInRc = AGX_CALC_VERSION(2, 13, 4, 0);
43  size_t const agxVersionInTrunkAfterRc = AGX_CALC_VERSION(2, 14, 0, 0);
44 
45  return agxStream::detail::shouldReadDataFromArchive(
46  archiveAgxVersion,
47  archiveSerializationVersion,
48  serializationVersionInRc,
49  serializationVersionAtChange,
50  agxVersionInRc,
51  agxVersionInTrunkAfterRc);
52  }
53 
54 
55 
60  inline bool shouldReadRotationalDimensionDirectionReference(size_t archiveAgxVersion, agx::UInt16 archiveSerializationVersion)
61  {
62  /*
63  The direction reference for RotationalDimension was given serizalizeation
64  version 87 and merged to the RC along with a bump to AGX version 2.15.0.6.
65  */
66  const agx::UInt16 serializationVersionInRc = 80;
68  const size_t agxVersionInRc = AGX_CALC_VERSION(2, 15, 0, 6);
69  const size_t agxVersionInTrunkAfterRc = AGX_CALC_VERSION(2, 16, 0, 0);
70 
71  return agxStream::detail::shouldReadDataFromArchive(
72  archiveAgxVersion,
73  archiveSerializationVersion,
74  serializationVersionInRc,
75  serializationVersionAtChange,
76  agxVersionInRc,
77  agxVersionInTrunkAfterRc);
78  }
79 
80 
81 
83 
84  // A bunch of randomly selected numbers.
88  const agx::UInt16 UNIT_END_MARKER = 0x4a73;
91 
92 
93 
95  {
97  }
98 
99 
100  template<typename Component>
101  bool shouldStore(const Component& c)
102  {
103  return !c->getIgnoreForStoreRestoreStream();
104  }
105 
106 
107  template<typename Container>
108  size_t countStoreRestorable(const Container& components)
109  {
110  using Component = typename Container::value_type;
111  using namespace std;
112  size_t count = count_if(begin(components), end(components), shouldStore<Component>);
113 
114  return count;
115  }
116 
117 
118  template<typename Container>
119  void storeStorablesCount(const Container& components, agxStream::StorageStream& out)
120  {
121  size_t numStorables = countStoreRestorable(components);
122  out << numStorables;
123  }
124 
125 
126  template<typename Container>
127  bool restoreRestorablesCount(const Container& components, agxStream::StorageStream& in)
128  {
129  if (!hasMarkers(in))
130  return true;
131 
132  size_t numStored;
133  in >> numStored;
134 
135  size_t numRestorable = countStoreRestorable(components);
136 
137  bool match = numStored == numRestorable;
138  if (!match)
139  {
140  LOGGER_WARNING() << "PowerLine stream restore given stream with incorrect number of connectors." << LOGGER_ENDL();
141  LOGGER_WARNING() << " Stream contains " << numStored << " and power line contains " << numRestorable << " restorable connectors." << LOGGER_ENDL();
142  LOGGER_WARNING() << " Aborting restore." << LOGGER_ENDL();
143  return false;
144  }
145  return match;
146  }
147 
148 
150  {
151  out << POWERLINE_BEGIN_MARKER;
152  }
153 
154 
156  {
157  out << POWERLINE_END_MARKER;
158  }
159 
160 
162  {
163  out << CONNECTOR_END_MARKER;
164  }
165 
166 
168  {
169  out << UNIT_END_MARKER;
170  }
171 
172 
174  {
176  }
177 
178 
180  {
181  out << LIGHT_DATA_END_MARKER;
182  }
183 
184 
185 
186 
187  inline bool restoreMarker(agxStream::StorageStream& in, agx::UInt16 marker, const char* name)
188  {
189  if (!hasMarkers(in))
190  return true;
191 
192  uint16_t endMarker;
193  in >> endMarker;
194 
195  bool match = endMarker == marker;
196  if (!match)
197  LOGGER_WARNING() << "PowerLine stream restore did not find " << name << " marker. Aborting restore." << LOGGER_ENDL();
198  return match;
199  }
200 
201 
203  {
204  return restoreMarker(in, POWERLINE_BEGIN_MARKER, "start-of-powerline");
205  }
206 
207 
209  {
210  return restoreMarker(in, POWERLINE_END_MARKER, "end-of-powerline");
211  }
212 
213 
215  {
216  return restoreMarker(in, CONNECTOR_END_MARKER, "end-of-connector");
217  }
218 
219 
221  {
222  return restoreMarker(in, UNIT_END_MARKER, "end-of-unit");
223  }
224 
225 
227  {
228  return restoreMarker(in, LIGHT_DATA_BEGIN_MARKER, "start-of-light-data");
229  }
230 
231 
233  {
234  return restoreMarker(in, LIGHT_DATA_END_MARKER, "end-of-light-data");
235  }
236 
237 
238 
239 
247 
248 
255  agxStream::StorageStream& stream,
256  agxStream::Serializable* object,
257  RestoreMemFn restore,
258  const char* const parentName,
259  const char* const dataName)
260  {
261  // Determine who wants to read and who does not.
262  const bool parentHasObject = object != nullptr;
263  bool streamHasObject;
264  stream >> streamHasObject;
265 
266  // Check if they agree on whether we should read or not.
267  bool const bothHaveObject = parentHasObject && streamHasObject;
268  bool const neitherHasObject = !parentHasObject && !streamHasObject;
269  bool const onlyParentHasObject = parentHasObject && !streamHasObject;
270  bool const onlyStreamHasObject = !parentHasObject && streamHasObject;
271 
272  if (bothHaveObject)
273  {
274  (object->*restore)(stream);
275  return true;
276  }
277  else if (neitherHasObject)
278  {
279  // Nothing to do.
280  return true;
281  }
282  else if (onlyParentHasObject)
283  {
284  LOGGER_WARNING() << parentName << " found unexpected " << dataName << " data in StorageStream." << LOGGER_ENDL();
285  return false;
286  }
287  else if (onlyStreamHasObject)
288  {
289  LOGGER_WARNING() << parentName << " expected " << dataName << " data in StoageStream, but there were none." << LOGGER_ENDL();
290  return false;
291  }
292  else
293  {
294  agxAbort1("Logic error. All possible cases should have been handled already.");
295  return false;
296  }
297  }
298 
299 
300 
303  agxStream::Serializable* object)
304  {
306 
307  bool hasConstraint = object != nullptr;
308  out << hasConstraint;
309  if (hasConstraint)
310  object->storeLightData(out);
311 
313  }
314 
315 
316 
333  agxStream::Serializable* object,
334  const char * const parentName,
335  const char * const dataName)
336  {
338  return false;
339 
340  if (!optionallyRestore_helper(in, object, &agxStream::Serializable::restoreLightData, parentName, dataName))
341  return false;
342 
343  if (!restoreLightDataEndMarker(in))
344  return false;
345 
346  return true;
347  }
348 
349 
350 
366  template<typename T>
368  agxStream::StorageStream& stream,
369  T* object,
370  const char* const parentName,
371  const char* const dataName)
372  {
373  const bool parentHasObject = object != nullptr;
374  bool streamHasObject;
375  stream >> streamHasObject;
376 
377  bool const bothHaveObject = parentHasObject && streamHasObject;
378  bool const neitherHasObject = !parentHasObject && !streamHasObject;
379  bool const onlyParentHasObject = parentHasObject && !streamHasObject;
380  bool const onlyStreamHasObject = !parentHasObject && streamHasObject;
381 
382  if (bothHaveObject)
383  {
384  return object->restore(stream);
385  }
386  else if (neitherHasObject)
387  {
388  // Nothing to do.
389  return true;
390  }
391  else if (onlyParentHasObject)
392  {
393  LOGGER_WARNING() << parentName << " found unexpected " << dataName << " data in StorageStream." << LOGGER_ENDL();
394  return false;
395  }
396  else if (onlyStreamHasObject)
397  {
398  LOGGER_WARNING() << parentName << " expected " << dataName << " data in StoageStream, but there were none." << LOGGER_ENDL();
399  return false;
400  }
401  else
402  {
403  agxAbort1("Logic error. All possible cases should have been handled already.");
404  return false;
405  }
406  }
407 
408 
409  } // namespace detail.
410 } // namespace agxPowerLine.
411 
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
#define AGXSTREAM_SERIALIZATION_VERSION_POWER_LINE_ROTAT_LOCAL_OR_WORLD_DIRECTION
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive...
bool restoreConnectorEndMarker(agxStream::StorageStream &in)
#define LOGGER_ENDL()
Definition: Logger.h:27
const agx::UInt16 POWERLINE_END_MARKER
void storePowerLineEndMarker(agxStream::StorageStream &out)
bool shouldStore(const Component &c)
void optionallyStoreLightData(agxStream::StorageStream &out, agxStream::Serializable *object)
bool optionallyRestoreLightData(agxStream::StorageStream &in, agxStream::Serializable *object, const char *const parentName, const char *const dataName)
Restores light data for the given object if the object is not nullptr and the stream contains data fo...
STL namespace.
bool restoreMarker(agxStream::StorageStream &in, agx::UInt16 marker, const char *name)
const agx::UInt16 LIGHT_DATA_BEGIN_MARKER
bool optionallyRestore(agxStream::StorageStream &stream, T *object, const char *const parentName, const char *const dataName)
Restores data for the given object if the object is not nullptr and the stream contains data for it...
void storeConnectorEndMarker(agxStream::StorageStream &out)
bool optionallyRestore_helper(agxStream::StorageStream &stream, agxStream::Serializable *object, RestoreMemFn restore, const char *const parentName, const char *const dataName)
Main worker helper for optinally restore.
agxPowerLine contains a collection of connected components that transport power across a system...
Definition: MotorUnit.h:23
#define LOGGER_WARNING()
Definition: Logger.h:23
bool restorePowerLineEndMarker(agxStream::StorageStream &in)
void storePowerLineBeginMarker(agxStream::StorageStream &out)
bool restoreLightDataBeginMarker(agxStream::StorageStream &in)
bool hasMarkers(agxStream::StorageStream &in)
AGXCORE_EXPORT agx::UInt32 AGX_CALC_VERSION(agx::UInt8 generation, agx::UInt8 major, agx::UInt8 minor, agx::UInt8 patch)
const agx::UInt16 LIGHT_DATA_END_MARKER
bool restorePowerLineBeginMarker(agxStream::StorageStream &in)
bool restoreRestorablesCount(const Container &components, agxStream::StorageStream &in)
const agx::UInt16 UNIT_END_MARKER
void storeLightDataEndMarker(agxStream::StorageStream &out)
bool restoreUnitEndMarker(agxStream::StorageStream &in)
const agx::UInt16 CONNECTOR_END_MARKER
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
agx::UInt16 getSerializationVersion() const
const agx::UInt16 POWERLINE_BEGIN_MARKER
const agx::UInt16 MARKER_SERIALIZATION_VERSION
bool shouldReadRotationalActuatorConnectorData(size_t archiveAgxVersion, agx::UInt16 archiveSerializationVersion)
Abstract base class for storing/restoring a line/drums with version control.
Definition: StorageStream.h:48
bool shouldReadRotationalDimensionDirectionReference(size_t archiveAgxVersion, agx::UInt16 archiveSerializationVersion)
void storeUnitEndMarker(agxStream::StorageStream &out)
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
bool restoreLightDataEndMarker(agxStream::StorageStream &in)
void storeLightDataBeginMarker(agxStream::StorageStream &out)
void storeStorablesCount(const Container &components, agxStream::StorageStream &out)
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
uint16_t UInt16
Definition: Integer.h:39
void(agxStream::Serializable::* RestoreMemFn)(agxStream::StorageStream &)
Member function signature for methods that read data from a StorageStream.
size_t countStoreRestorable(const Container &components)
#define agxAbort1(msg)
Definition: debug.h:165