AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
powerLineSerialization.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
19#include <agx/version.h>
24
25namespace 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.
91
92
93
95 {
96 return in.getSerializationVersion() >= MARKER_SERIALIZATION_VERSION;
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
153
154
159
160
165
166
168 {
169 out << UNIT_END_MARKER;
170 }
171
172
177
178
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
246 typedef void (agxStream::Serializable::*RestoreMemFn)(agxStream::StorageStream&);
247
248
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
304 {
306
307 bool hasConstraint = object != nullptr;
308 out << hasConstraint;
309 if (hasConstraint)
310 object->storeLightData(out);
311
313 }
314
315
316
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
344 return false;
345
346 return true;
347 }
348
349
350
366 template<typename T>
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
#define LOGGER_WARNING()
Definition Logger.h:23
#define LOGGER_ENDL()
Definition Logger.h:27
#define AGXSTREAM_SERIALIZATION_VERSION_POWER_LINE_ROTAT_LOCAL_OR_WORLD_DIRECTION
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Abstract base class for storing/restoring a line/drums with version control.
#define agxAbort1(msg)
Definition debug.h:165
bool hasMarkers(agxStream::StorageStream &in)
void storePowerLineEndMarker(agxStream::StorageStream &out)
const agx::UInt16 MARKER_SERIALIZATION_VERSION
void storeStorablesCount(const Container &components, agxStream::StorageStream &out)
bool restoreConnectorEndMarker(agxStream::StorageStream &in)
bool shouldReadRotationalActuatorConnectorData(size_t archiveAgxVersion, agx::UInt16 archiveSerializationVersion)
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.
const agx::UInt16 LIGHT_DATA_END_MARKER
const agx::UInt16 POWERLINE_BEGIN_MARKER
const agx::UInt16 POWERLINE_END_MARKER
bool restoreUnitEndMarker(agxStream::StorageStream &in)
const agx::UInt16 LIGHT_DATA_BEGIN_MARKER
const agx::UInt16 UNIT_END_MARKER
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.
bool restoreLightDataEndMarker(agxStream::StorageStream &in)
bool restoreRestorablesCount(const Container &components, agxStream::StorageStream &in)
void storeUnitEndMarker(agxStream::StorageStream &out)
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...
void storeLightDataEndMarker(agxStream::StorageStream &out)
void storeLightDataBeginMarker(agxStream::StorageStream &out)
void storeConnectorEndMarker(agxStream::StorageStream &out)
void optionallyStoreLightData(agxStream::StorageStream &out, agxStream::Serializable *object)
const agx::UInt16 CONNECTOR_END_MARKER
void storePowerLineBeginMarker(agxStream::StorageStream &out)
bool restorePowerLineBeginMarker(agxStream::StorageStream &in)
bool restoreMarker(agxStream::StorageStream &in, agx::UInt16 marker, const char *name)
bool shouldReadRotationalDimensionDirectionReference(size_t archiveAgxVersion, agx::UInt16 archiveSerializationVersion)
bool restoreLightDataBeginMarker(agxStream::StorageStream &in)
size_t countStoreRestorable(const Container &components)
bool shouldStore(const Component &c)
bool restorePowerLineEndMarker(agxStream::StorageStream &in)
void(agxStream::Serializable::* RestoreMemFn)(agxStream::StorageStream &)
Member function signature for methods that read data from a StorageStream.
agxPowerLine contains a collection of connected components that transport power across a system.
Definition MotorUnit.h:24
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
uint16_t UInt16
Definition Integer.h:31
STL namespace.
AGXCORE_EXPORT agx::UInt32 AGX_CALC_VERSION(agx::UInt8 generation, agx::UInt8 major, agx::UInt8 minor, agx::UInt8 patch)