1 // Copyright (c) 2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #ifndef _Standard_Dump_HeaderFile
15 #define _Standard_Dump_HeaderFile
17 #include <NCollection_IndexedDataMap.hxx>
18 #include <NCollection_List.hxx>
19 #include <Standard_OStream.hxx>
20 #include <Standard_SStream.hxx>
21 #include <TCollection_AsciiString.hxx>
24 //! The file contains interface to prepare dump output for OCCT objects. Format of the dump is JSON.
26 //! To prepare this output, implement method DumpJson in the object and use macro functions from this file.
27 //! Macros have one parameter for both, key and the value. It is a field of the current class. Macro has internal analyzer that
28 //! uses the variable name to generate key. If the parameter has prefix symbols "&", "*" and "my", it is cut.
30 //! - OCCT_DUMP_FIELD_VALUE_NUMERICAL. Use it for fields of numerical C++ types, like int, float, double. It creates a pair "key", "value",
31 //! - OCCT_DUMP_FIELD_VALUE_NUMERICAL_INC. Use it for fields of numerical C++ types, like int, float, double.
32 //! It creates a pair "key_inc", "value",
33 //! - OCCT_DUMP_FIELD_VALUE_STRING. Use it for char* type. It creates a pair "key", "value",
34 //! - OCCT_DUMP_FIELD_VALUE_POINTER. Use it for pointer fields. It creates a pair "key", "value", where the value is the pointer address,
35 //! - OCCT_DUMP_FIELD_VALUES_DUMPED. Use it for fields that has own Dump implementation. It expects the pointer to the class instance.
36 //! It creates "key": { result of dump of the field }
37 //! - OCCT_DUMP_FIELD_VALUES_DUMPED_INC. Use it for fields that has own Dump implementation. It expects the pointer to the class instance.
38 //! It creates "key_inc": { result of dump of the field }
39 //! - OCCT_DUMP_STREAM_VALUE_DUMPED. Use it for Standard_SStream. It creates "key": { text of stream }
40 //! - OCCT_DUMP_FIELD_VALUES_NUMERICAL. Use it for unlimited list of fields of C++ double type.
41 //! It creates massive of values [value_1, value_2, ...]
42 //! - OCCT_DUMP_FIELD_VALUES_STRING. Use it for unlimited list of fields of TCollection_AsciiString types.
43 //! It creates massive of values ["value_1", "value_2", ...]
44 //! - OCCT_DUMP_BASE_CLASS. Use if Dump implementation of the class is virtual, to perform ClassName::Dump() of the parent class,
45 //! expected parameter is the parent class name.
46 //! It creates "key": { result of dump of the field }
47 //! - OCCT_DUMP_VECTOR_CLASS. Use it as a single row in some object dump to have one row in output.
48 //! It's possible to use it without necessity of OCCT_DUMP_CLASS_BEGIN call.
49 //! It creates massive of values [value_1, value_2, ...]
51 //! The Dump result prepared by these macros is an output stream, it is not arranged with spaces and line feed.
52 //! To have output in a more readable way, use ConvertToAlignedString method for obtained stream.
54 //! Converts the class type into a string value
55 #define OCCT_CLASS_NAME(theClass) #theClass
57 //! @def OCCT_DUMP_CLASS_BEGIN
58 //! Creates an instance of Sentry to cover the current Dump implementation with keys of start and end.
59 //! This row should be inserted before other macros. The end key will be added by the sentry remove,
60 //! (exit of the method).
61 #define OCCT_DUMP_CLASS_BEGIN(theOStream, theField) \
63 const char* className = OCCT_CLASS_NAME(theField); \
64 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, className) \
67 //! @def OCCT_DUMP_TRANSIENT_CLASS_BEGIN
68 //! Creates an instance of Sentry to cover the current Dump implementation with keys of start and end.
69 //! This row should be inserted before other macros. The end key will be added by the sentry remove,
70 //! (exit of the method).
71 #define OCCT_DUMP_TRANSIENT_CLASS_BEGIN(theOStream) \
73 const char* className = get_type_name(); \
74 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, className) \
77 //! @def OCCT_DUMP_FIELD_VALUE_NUMERICAL
78 //! Append into output value: "Name": Field
79 #define OCCT_DUMP_FIELD_VALUE_NUMERICAL(theOStream, theField) \
81 TCollection_AsciiString aName = Standard_Dump::DumpFieldToName (#theField); \
82 Standard_Dump::AddValuesSeparator (theOStream); \
83 theOStream << "\"" << aName << "\": " << theField; \
86 //! @def OCCT_DUMP_FIELD_VALUE_NUMERICAL
87 //! Append into output value: "Name": Field
88 //! Inc name value added to the key to provide unique keys
89 #define OCCT_DUMP_FIELD_VALUE_NUMERICAL_INC(theOStream, theField, theIncName) \
91 TCollection_AsciiString aName = Standard_Dump::DumpFieldToName (#theField) + theIncName; \
92 Standard_Dump::AddValuesSeparator (theOStream); \
93 theOStream << "\"" << aName << "\": " << theField; \
96 //! @def OCCT_INIT_FIELD_VALUE_REAL
97 //! Append vector values into output value: "Name": [value_1, value_2, ...]
98 //! This macro is intended to have only one row for dumped object in Json.
99 //! It's possible to use it without necessity of OCCT_DUMP_CLASS_BEGIN call, but pay attention that it should be only one row in the object dump.
100 #define OCCT_INIT_FIELD_VALUE_REAL(theOStream, theStreamPos, theField) \
102 Standard_Integer aStreamPos = theStreamPos; \
103 if (!Standard_Dump::ProcessFieldName (theOStream, #theField, aStreamPos)) \
104 return Standard_False; \
105 TCollection_AsciiString aValueText; \
106 if (!Standard_Dump::InitValue (theOStream, aStreamPos, aValueText) || !aValueText.IsRealValue()) \
107 return Standard_False; \
108 theField = aValueText.RealValue(); \
109 theStreamPos = aStreamPos; \
112 //! @def OCCT_INIT_FIELD_VALUE_NUMERICAL
113 //! Append vector values into output value: "Name": [value_1, value_2, ...]
114 //! This macro is intended to have only one row for dumped object in Json.
115 //! It's possible to use it without necessity of OCCT_DUMP_CLASS_BEGIN call, but pay attention that it should be only one row in the object dump.
116 #define OCCT_INIT_FIELD_VALUE_INTEGER(theOStream, theStreamPos, theField) \
118 Standard_Integer aStreamPos = theStreamPos; \
119 if (!Standard_Dump::ProcessFieldName (theOStream, #theField, aStreamPos)) \
120 return Standard_False; \
121 TCollection_AsciiString aValueText; \
122 if (!Standard_Dump::InitValue (theOStream, aStreamPos, aValueText) || !aValueText.IsIntegerValue()) \
123 return Standard_False; \
124 theField = aValueText.IntegerValue(); \
125 theStreamPos = aStreamPos; \
128 //! @def OCCT_DUMP_FIELD_VALUE_STRING
129 //! Append into output value: "Name": "Field"
130 #define OCCT_DUMP_FIELD_VALUE_STRING(theOStream, theField) \
132 TCollection_AsciiString aName = Standard_Dump::DumpFieldToName (#theField); \
133 Standard_Dump::AddValuesSeparator (theOStream); \
134 theOStream << "\"" << aName << "\": \"" << theField << "\""; \
137 //! @def OCCT_DUMP_FIELD_VALUE_POINTER
138 //! Append into output value: "Name": "address of the pointer"
139 #define OCCT_DUMP_FIELD_VALUE_POINTER(theOStream, theField) \
141 TCollection_AsciiString aName = Standard_Dump::DumpFieldToName (#theField); \
142 Standard_Dump::AddValuesSeparator (theOStream); \
143 theOStream << "\"" << aName << "\": \"" << Standard_Dump::GetPointerInfo (theField) << "\""; \
146 //! @def OCCT_DUMP_FIELD_VALUE_STRING
147 //! Append into output value: "Name": "Field"
148 #define OCCT_DUMP_FIELD_VALUE_GUID(theOStream, theField) \
150 TCollection_AsciiString aName = Standard_Dump::DumpFieldToName (#theField); \
151 Standard_Dump::AddValuesSeparator (theOStream); \
152 char aStr[Standard_GUID_SIZE_ALLOC]; \
153 theField.ToCString (aStr); \
154 theOStream << "\"" << aName << "\": \"" << aStr << "\""; \
157 //! @def OCCT_DUMP_FIELD_VALUES_DUMPED
158 //! Append into output value: "Name": { field dumped values }
159 //! It computes Dump of the fields. The expected field is a pointer.
160 //! Use this macro for fields of the dumped class which has own Dump implementation.
161 //! The macros is recursive. Recursion is stopped when the depth value becomes equal to zero.
162 //! Depth = -1 is the default value, dump here is unlimited.
163 #define OCCT_DUMP_FIELD_VALUES_DUMPED(theOStream, theDepth, theField) \
165 if (theDepth != 0 && (void*)(theField) != NULL) \
167 Standard_SStream aFieldStream; \
168 (theField)->DumpJson (aFieldStream, theDepth - 1); \
169 TCollection_AsciiString aName = Standard_Dump::DumpFieldToName (#theField); \
170 Standard_Dump::DumpKeyToClass (theOStream, aName, Standard_Dump::Text (aFieldStream)); \
174 //! @def OCCT_DUMP_FIELD_VALUES_DUMPED_INC
175 //! Append into output value: "Name": { field dumped values }
176 //! It computes Dump of the fields. The expected field is a pointer.
177 //! Use this macro for fields of the dumped class which has own Dump implementation.
178 //! The macros is recursive. Recursion is stopped when the depth value becomes equal to zero.
179 //! Depth = -1 is the default value, dump here is unlimited.
180 //! Inc name value added to the key to provide unique keys
181 #define OCCT_DUMP_FIELD_VALUES_DUMPED_INC(theOStream, theDepth, theField, theIncName) \
183 if (theDepth != 0 && (void*)(theField) != NULL) \
185 Standard_SStream aFieldStream; \
186 (theField)->DumpJson (aFieldStream, theDepth - 1); \
187 TCollection_AsciiString aName = Standard_Dump::DumpFieldToName (#theField) + theIncName; \
188 Standard_Dump::DumpKeyToClass (theOStream, aName, Standard_Dump::Text (aFieldStream)); \
192 //! @def OCCT_INIT_FIELD_VALUES_DUMPED
193 //! Append into output value: "Name": { field dumped values }
194 //! It computes Dump of the fields. The expected field is a pointer.
195 //! Use this macro for fields of the dumped class which has own Dump implementation.
196 //! The macros is recursive. Recursion is stopped when the depth value becomes equal to zero.
197 //! Depth = -1 is the default value, dump here is unlimited.
198 #define OCCT_INIT_FIELD_VALUES_DUMPED(theSStream, theStreamPos, theField) \
200 if ((theField) == NULL || !(theField)->InitFromJson (theSStream, theStreamPos)) \
201 return Standard_False; \
204 //! @def OCCT_DUMP_STREAM_VALUE_DUMPED
205 //! Append into output value: "Name": { stream text }
206 //! It computes Dump of the fields. The expected field is a pointer.
207 //! Use this macro for Standard_SStream field.
208 #define OCCT_DUMP_STREAM_VALUE_DUMPED(theOStream, theField) \
210 TCollection_AsciiString aName = Standard_Dump::DumpFieldToName (#theField); \
211 Standard_Dump::DumpKeyToClass (theOStream, aName, Standard_Dump::Text (theField)); \
214 //! @def OCCT_DUMP_FIELD_VALUES_NUMERICAL
215 //! Append real values into output values in an order: [value_1, value_2, ...]
216 //! It computes Dump of the parent. The expected field is a parent class name to call ClassName::Dump.
217 #define OCCT_DUMP_FIELD_VALUES_NUMERICAL(theOStream, theName, theCount, ...) \
219 Standard_Dump::AddValuesSeparator (theOStream); \
220 theOStream << "\"" << theName << "\": ["; \
221 Standard_Dump::DumpRealValues (theOStream, theCount, __VA_ARGS__);\
225 //! @def OCCT_DUMP_FIELD_VALUES_STRING
226 //! Append real values into output values in an order: ["value_1", "value_2", ...]
227 //! It computes Dump of the parent. The expected field is a parent class name to call ClassName::Dump.
228 #define OCCT_DUMP_FIELD_VALUES_STRING(theOStream, theName, theCount, ...) \
230 Standard_Dump::AddValuesSeparator (theOStream); \
231 theOStream << "\"" << theName << "\": ["; \
232 Standard_Dump::DumpCharacterValues (theOStream, theCount, __VA_ARGS__);\
236 //! @def OCCT_DUMP_BASE_CLASS
237 //! Append into output value: "Name": { field dumped values }
238 //! It computes Dump of the parent. The expected field is a parent class name to call ClassName::Dump.
239 //! Use this macro for parent of the current class.
240 //! The macros is recursive. Recursive is stopped when the depth value becomes equal to zero.
241 //! Depth = -1 is the default value, dump here is unlimited.
242 #define OCCT_DUMP_BASE_CLASS(theOStream, theDepth, theField) \
246 Standard_Dump::AddValuesSeparator (theOStream); \
247 theField::DumpJson (theOStream, theDepth - 1); \
251 //! @def OCCT_DUMP_VECTOR_CLASS
252 //! Append vector values into output value: "Name": [value_1, value_2, ...]
253 //! This macro is intended to have only one row for dumped object in Json.
254 //! It's possible to use it without necessity of OCCT_DUMP_CLASS_BEGIN call, but pay attention that it should be only one row in the object dump.
255 #define OCCT_DUMP_VECTOR_CLASS(theOStream, theName, theCount, ...) \
257 Standard_Dump::AddValuesSeparator (theOStream); \
258 theOStream << "\"" << theName << "\": ["; \
259 Standard_Dump::DumpRealValues (theOStream, theCount, __VA_ARGS__);\
263 //! @def OCCT_INIT_VECTOR_CLASS
264 //! Append vector values into output value: "Name": [value_1, value_2, ...]
265 //! This macro is intended to have only one row for dumped object in Json.
266 //! It's possible to use it without necessity of OCCT_DUMP_CLASS_BEGIN call, but pay attention that it should be only one row in the object dump.
267 #define OCCT_INIT_VECTOR_CLASS(theOStream, theName, theStreamPos, theCount, ...) \
269 Standard_Integer aStreamPos = theStreamPos; \
270 if (!Standard_Dump::ProcessStreamName (theOStream, theName, aStreamPos)) \
271 return Standard_False; \
272 if (!Standard_Dump::InitRealValues (theOStream, aStreamPos, theCount, __VA_ARGS__)) \
273 return Standard_False; \
274 theStreamPos = aStreamPos; \
277 //! Kind of key in Json string
278 enum Standard_JsonKey
280 Standard_JsonKey_None, //!< no key
281 Standard_JsonKey_OpenChild, //!< "{"
282 Standard_JsonKey_CloseChild, //!< "}"
283 Standard_JsonKey_OpenContainer, //!< "["
284 Standard_JsonKey_CloseContainer, //!< "]"
285 Standard_JsonKey_Quote, //!< "\""
286 Standard_JsonKey_SeparatorKeyToValue, //!< ": "
287 Standard_JsonKey_SeparatorValueToValue //!< ", "
290 //! Type for storing a dump value with the stream position
291 struct Standard_DumpValue
293 Standard_DumpValue() : myStartPosition (0) {}
294 Standard_DumpValue (const TCollection_AsciiString& theValue, const Standard_Integer theStartPos)
295 : myValue (theValue), myStartPosition (theStartPos) {}
297 TCollection_AsciiString myValue; //!< current string value
298 Standard_Integer myStartPosition; //!< position of the value first char in the whole stream
301 //! This interface has some tool methods for stream (in JSON format) processing.
305 //! Converts stream value to string value. The result is original stream value.
306 //! @param theStream source value
307 //! @return text presentation
308 Standard_EXPORT static TCollection_AsciiString Text (const Standard_SStream& theStream);
310 //! Converts stream value to string value. Improves the text presentation with the following cases:
311 //! - for '{' append after '\n' and indent to the next value, increment current indent value
312 //! - for '}' append '\n' and current indent before it, decrement indent value
313 //! - for ',' append after '\n' and indent to the next value. If the current symbol is in massive container [], do nothing
314 //! Covers result with opened and closed brackets on the top level, if it has no symbols there.
315 //! @param theStream source value
316 //! @param theIndent count of ' ' symbols to apply hierarchical indent of the text values
317 //! @return text presentation
318 Standard_EXPORT static TCollection_AsciiString FormatJson (const Standard_SStream& theStream, const Standard_Integer theIndent = 3);
320 //! Converts stream into map of values.
322 //! The one level stream example: 'key_1: value_1, key_2: value_2'
323 //! In output: values contain 'key_1: value_1' and 'key_2: value_2'.
325 //! The two level stream example: 'key_1: value_1, key_2: value_2, key_3: {sublevel_key_1: sublevel_value_1}, key_4: value_4'
326 //! In output values contain 'key_1: value_1', 'key_2: value_2', 'key_3: {sublevel_key_1: sublevel_value_1}' and 'key_4: value_4'.
327 //! The sublevel value might be processed later using the same method.
329 //! @param theStreamStr stream value
330 //! @param theKeyToValues [out] container of split values. It contains key to value and position of the value in the stream text
331 Standard_EXPORT static Standard_Boolean SplitJson (const TCollection_AsciiString& theStreamStr,
332 NCollection_IndexedDataMap<TCollection_AsciiString, Standard_DumpValue>& theKeyToValues);
334 //! Returns container of indices in values, that has hierarchical value
335 Standard_EXPORT static NCollection_List<Standard_Integer> HierarchicalValueIndices (
336 const NCollection_IndexedDataMap<TCollection_AsciiString, TCollection_AsciiString>& theValues);
338 //! Returns true if the value has bracket key
339 Standard_EXPORT static Standard_Boolean HasChildKey (const TCollection_AsciiString& theSourceValue);
341 //! Returns key value for enum type
342 Standard_EXPORT static Standard_CString JsonKeyToString (const Standard_JsonKey theKey);
344 //! Returns length value for enum type
345 Standard_EXPORT static Standard_Integer JsonKeyLength (const Standard_JsonKey theKey);
347 //! @param theOStream source value
348 static Standard_EXPORT void AddValuesSeparator (Standard_OStream& theOStream);
350 //! Returns default prefix added for each pointer info string if short presentation of pointer used
351 static TCollection_AsciiString GetPointerPrefix() { return "0x"; }
353 //! Convert handle pointer to address of the pointer. If the handle is NULL, the result is an empty string.
354 //! @param thePointer a pointer
355 //! @param isShortInfo if true, all '0' symbols in the beginning of the pointer are skipped
356 //! @return the string value
357 Standard_EXPORT static TCollection_AsciiString GetPointerInfo (const Handle(Standard_Transient)& thePointer,
358 const bool isShortInfo = true);
360 //! Convert pointer to address of the pointer. If the handle is NULL, the result is an empty string.
361 //! @param thePointer a pointer
362 //! @param isShortInfo if true, all '0' symbols in the beginning of the pointer are skipped
363 //! @return the string value
364 Standard_EXPORT static TCollection_AsciiString GetPointerInfo (const void* thePointer,
365 const bool isShortInfo = true);
367 //! Append into output value: "Name": { Field }
368 //! @param theOStream [out] stream to be fill with values
369 //! @param theKey a source value
370 //! @param theField stream value
371 Standard_EXPORT static void DumpKeyToClass (Standard_OStream& theOStream,
372 const TCollection_AsciiString& theKey,
373 const TCollection_AsciiString& theField);
375 //! Unite values in one value using template: "value_1", "value_2", ..., "value_n"
376 //! @param theOStream [out] stream to be fill with values
377 //! @param theCount [in] number of values
378 Standard_EXPORT static void DumpCharacterValues (Standard_OStream& theOStream, int theCount, ...);
380 //! Unite values in one value using template: value_1, value_2, ..., value_n
381 //! @param theOStream [out] stream to be fill with values
382 //! @param theCount [in] number of values
383 Standard_EXPORT static void DumpRealValues (Standard_OStream& theOStream, int theCount, ...);
385 //! Check whether the parameter name is equal to the name in the stream at position
386 //! @param[in] theStreamStr stream with values
387 //! @param[in] theName stream key value
388 //! @param[out] theStreamPos current position in the stream
389 Standard_EXPORT static Standard_Boolean ProcessStreamName (const TCollection_AsciiString& theStreamStr,
390 const TCollection_AsciiString& theName,
391 Standard_Integer& theStreamPos);
393 //! Check whether the field name is equal to the name in the stream at position
394 //! @param[in] theStreamStr stream with values
395 //! @param[in] theName stream key field value
396 //! @param[out] theStreamPos current position in the stream
397 Standard_EXPORT static Standard_Boolean ProcessFieldName (const TCollection_AsciiString& theStreamStr,
398 const TCollection_AsciiString& theName,
399 Standard_Integer& theStreamPos);
401 //! Unite values in one value using template: value_1, value_2, ..., value_n
402 //! @param[in] theStreamStr stream with values
403 //! @param[out] theStreamPos current position in the stream
404 //! @param[in] theCount number of values
405 Standard_EXPORT static Standard_Boolean InitRealValues (const TCollection_AsciiString& theStreamStr,
406 Standard_Integer& theStreamPos,
409 //! Returns real value
410 //! @param[in] theStreamStr stream with values
411 //! @param[out] theStreamPos current position in the stream
412 //! @param[out] theValue stream value
413 Standard_EXPORT static Standard_Boolean InitValue (const TCollection_AsciiString& theStreamStr,
414 Standard_Integer& theStreamPos,
415 TCollection_AsciiString& theValue);
417 //! Convert field name into dump text value, removes "&" and "my" prefixes
418 //! An example, for field myValue, theName is Value, for &myCLass, the name is Class
419 //! @param theField a source value
420 Standard_EXPORT static TCollection_AsciiString DumpFieldToName (const TCollection_AsciiString& theField);
423 //! Extracts from the string value a pair (key, value), add it into output container, update index value
425 //! stream string starting the index position contains: ..."key": <value>...
426 //! a pair key, value will be added into theValues
427 //! at beginning theIndex is the position of the quota before <key>, after the index is the next position after the value
428 //! splitDumped(aString) gives theSplitValue = "abc", theTailValue = "defg", theKey = "key"
429 Standard_EXPORT static Standard_Boolean splitKeyToValue (const TCollection_AsciiString& theStreamStr,
430 Standard_Integer theStartIndex,
431 Standard_Integer& theNextIndex,
432 NCollection_IndexedDataMap<TCollection_AsciiString, Standard_DumpValue>& theValues);
435 //! Returns key of json in the index position. Incement the index position to the next symbol in the row
436 Standard_EXPORT static Standard_Boolean jsonKey (const TCollection_AsciiString& theStreamStr,
437 Standard_Integer theStartIndex,
438 Standard_Integer& theNextIndex,
439 Standard_JsonKey& theKey);
441 //! Find position in the source string of the symbol close after the start position.
442 //! Ignore combination <symbol open> ... <symbol close> between the close symbol.
443 //! Example, for case ... { ... { ... } ...} ... } it returns the position of the forth brace
444 Standard_EXPORT static Standard_Integer nextClosePosition (const TCollection_AsciiString& theSourceValue,
445 const Standard_Integer theStartPosition,
446 const Standard_JsonKey theCloseKey,
447 const Standard_JsonKey theOpenKey);
451 #endif // _Standard_Dump_HeaderFile