Go to the documentation of this file.
5 #ifndef CNOID_UTIL_VALUE_TREE_H
6 #define CNOID_UTIL_VALUE_TREE_H
12 #include <initializer_list>
13 #include "exportdecl.h"
23 #ifndef CNOID_BACKWARD_COMPATIBILITY
39 static Initializer initializer;
51 FORCED_RADIAN_MODE = 32
55 explicit operator bool()
const {
return isValid(); }
61 double toDouble()
const;
62 float toFloat()
const;
64 [[deprecated(
"Check isForcedRadianMode() of the top node.")]]
65 double toAngle()
const;
67 bool isScalar()
const {
return typeBits & SCALAR; }
68 bool isString()
const {
return typeBits & SCALAR; }
71 const std::string& toString()
const;
73 operator const std::string& ()
const {
84 [[deprecated(
"Use 'isForcedRadianMode' to determine the angle unit.")]]
86 [[deprecated(
"Use ''setForcedRadianMode' to specify the angle unit.")]]
89 template<
typename T> T to()
const;
92 const Mapping* toMapping()
const;
96 const Listing* toListing()
const;
99 #ifdef CNOID_BACKWARD_COMPATIBILITY
100 bool isSequence()
const {
return typeBits &
LISTING; }
101 const Listing* toSequence()
const {
return toListing(); }
102 Listing* toSequence() {
return toListing(); }
105 bool read(
int &out_value)
const;
106 bool read(
double &out_value)
const;
107 bool read(
float &out_value)
const;
108 bool read(
bool &out_value)
const;
109 bool read(std::string &out_value)
const;
112 int line()
const {
return line_ + 1; }
113 int column()
const {
return column_ + 1; }
115 void throwException(
const std::string& message)
const;
124 int line()
const {
return line_; }
126 std::string message()
const;
137 std::string message_;
142 const std::string&
key() {
return key_; }
143 void setKey(
const std::string& key) { key_ = key; }
186 void throwNotScalrException()
const;
187 void throwNotMappingException()
const;
188 void throwNotListingException()
const;
202 friend class YAMLReaderImpl;
208 template<>
inline double ValueNode::to<double>()
const {
return toDouble(); }
209 template<>
inline float ValueNode::to<float>()
const {
return toFloat(); }
210 template<>
inline int ValueNode::to<int>()
const {
return toInt(); }
211 template<>
inline std::string ValueNode::to<std::string>()
const {
return toString(); }
232 std::string stringValue_;
235 friend class YAMLReaderImpl;
247 return static_cast<const ScalarNode* const
>(
this)->stringValue_;
253 typedef std::map<std::string, ValueNodePtr> Container;
265 virtual Mapping* cloneMapping()
const;
267 bool empty()
const {
return values.empty(); }
268 int size()
const {
return static_cast<int>(values.size()); }
271 void setFlowStyle(
bool isFlowStyle =
true) { isFlowStyle_ = isFlowStyle; }
274 void setFloatingNumberFormat(
const char* format);
277 [[deprecated(
"Use Mapping::setFloatingNumberFormat")]]
279 [[deprecated(
"Use Mapping::floatingNumberFormat")]]
284 ValueNode* find(
const std::string& key)
const;
285 ValueNode* find(std::initializer_list<const char*> keys)
const;
286 Mapping* findMapping(
const std::string& key)
const;
287 Mapping* findMapping(std::initializer_list<const char*> keys)
const;
288 Listing* findListing(
const std::string& key)
const;
289 Listing* findListing(std::initializer_list<const char*> keys)
const;
292 ValueNodePtr extract(std::initializer_list<const char*> keys);
294 bool extract(
const std::string& key,
double& out_value);
295 bool extract(
const std::string& key, std::string& out_value);
297 ValueNode& get(
const std::string& key)
const;
303 void insert(
const std::string& key,
ValueNode* node);
305 void insert(
const Mapping* other);
308 return openMapping_(key,
false);
312 return openFlowStyleMapping_(key,
false);
316 return openMapping_(key,
true);
320 return openFlowStyleMapping_(key,
true);
324 return openListing_(key,
false);
328 return openFlowStyleListing_(key,
false);
332 return openListing_(key,
true);
336 return openFlowStyleListing_(key,
true);
339 bool remove(
const std::string& key);
341 bool read(
const std::string& key, std::string& out_value)
const;
342 bool read(
const std::string& key,
bool& out_value)
const;
343 bool read(
const std::string& key,
int& out_value)
const;
344 bool read(
const std::string& key,
double& out_value)
const;
345 bool read(
const std::string& key,
float& out_value)
const;
348 bool read(std::initializer_list<const char*> keys, T& out_value)
const {
349 for(
auto& key : keys){
350 if(this->
read(key, out_value)){
357 bool readAngle(
const std::string& key,
double& out_angle,
const ValueNode* unitAttrNode =
nullptr)
const;
358 bool readAngle(
const std::string& key,
float& out_angle,
const ValueNode* unitAttrNode =
nullptr)
const;
359 bool readAngle(std::initializer_list<const char*> keys,
double& out_angle,
const ValueNode* unitAttrNode =
nullptr)
const;
360 bool readAngle(std::initializer_list<const char*> keys,
float& out_angle,
const ValueNode* unitAttrNode =
nullptr)
const;
362 template <
class T> T
get(
const std::string& key)
const {
364 if(
read(key, value)){
367 throwKeyNotFoundException(key);
373 T
get(
const std::string& key,
const T& defaultValue)
const {
375 if(
read(key, value)){
382 std::string
get(
const std::string& key,
const char* defaultValue)
const {
384 if(
read(key, value)){
392 T
get(std::initializer_list<const char*> keys,
const T& defaultValue)
const {
394 if(
read(keys, value)){
403 write(key, std::string(value), stringStyle);
406 void write(
const std::string& key,
bool value);
407 void write(
const std::string& key,
int value);
408 void write(
const std::string& key,
double value);
409 void writePath(
const std::string &key,
const std::string& value);
411 template<
class ArrayType>
void writeAsListing(
const std::string& key,
const ArrayType& container);
413 typedef enum { READ_MODE, WRITE_MODE } AssignMode;
418 void assign(
const std::string& key, T& io_value,
const T& defaultValue){
421 if(!
read(key, io_value)){
422 io_value = defaultValue;
426 write(key, io_value);
436 void throwKeyNotFoundException(
const std::string& key)
const;
441 template <
class T> T
read(
const std::string& key)
const {
return get<T>(key); }
443 #ifdef CNOID_BACKWARD_COMPATIBILITY
444 Listing* findSequence(
const std::string& key)
const {
return findListing(key); }
445 Listing* openSequence(
const std::string& key) {
return openListing(key); }
446 Listing* openFlowStyleSequence(
const std::string& key){
return openFlowStyleListing(key); }
447 Listing* createSequence(
const std::string& key){
return createListing(key); }
448 Listing* createFlowStyleSequence(
const std::string& key){
return createFlowStyleListing(key); }
453 Mapping(
const Mapping& org);
454 Mapping& operator=(
const Mapping&);
456 Mapping* openMapping_(
const std::string& key,
bool doOverwrite);
457 Mapping* openFlowStyleMapping_(
const std::string& key,
bool doOverwrite);
458 Listing* openListing_(
const std::string& key,
bool doOverwrite);
459 Listing* openFlowStyleListing_(
const std::string& key,
bool doOverwrite);
461 inline void insertSub(
const std::string& key, ValueNode* node);
463 void writeSub(
const std::string &key,
const char* text,
size_t length,
StringStyle stringStyle);
468 const char* floatingNumberFormat_;
473 friend class YAMLReaderImpl;
486 typedef std::vector<ValueNodePtr> Container;
499 bool empty()
const {
return values.empty(); }
500 int size()
const {
return static_cast<int>(values.size()); }
502 void reserve(
int size);
504 void setFlowStyle(
bool isFlowStyle =
true) { isFlowStyle_ = isFlowStyle; }
507 void setFloatingNumberFormat(
const char* format);
510 [[deprecated(
"Use Mapping::setFloatingNumberFormat")]]
512 [[deprecated(
"Use Mapping::floatingNumberFormat")]]
516 return values.front();
520 return values.back();
534 void write(
int i,
int value);
550 values.push_back(node);
555 void append(
int value);
562 void append(
int value,
int maxColumns,
int numValues = 0) {
563 insertLF(maxColumns, numValues);
567 void append(
size_t value);
568 void append(
double value);
575 void append(
double value,
int maxColumns,
int numValues = 0) {
576 insertLF(maxColumns, numValues);
588 insertLF(maxColumns, numValues);
589 append(value, stringStyle);
602 Listing(
int line,
int column,
int reservedSize);
607 void insertLF(
int maxColumns,
int numValues);
610 const char* floatingNumberFormat_;
612 bool doInsertLFBeforeNextElement;
615 friend class YAMLReaderImpl;
619 template<
class ArrayType>
623 for(
auto& value : container){
624 listing->append(value);
631 #ifdef CNOID_BACKWARD_COMPATIBILITY
bool isFlowStyle() const
Definition: ValueTree.h:505
@ LISTING
Definition: YAMLWriter.cpp:18
ValueNode(TypeBit type)
Definition: ValueTree.h:182
iterator begin()
Definition: ValueTree.h:431
bool isValid() const
Definition: ValueTree.h:54
virtual ~ValueNode()
Definition: ValueTree.h:184
bool isScalar() const
Definition: ValueTree.h:67
int column() const
Definition: ValueTree.h:125
Definition: ValueTree.h:251
T get(const std::string &key) const
Definition: ValueTree.h:362
Listing * openListing(const std::string &key)
Definition: ValueTree.h:323
void setFlowStyle(bool isFlowStyle=true)
Definition: ValueTree.h:271
int size() const
Definition: ValueTree.h:268
Listing * openFlowStyleListing(const std::string &key)
Definition: ValueTree.h:327
ValueNode & operator[](const std::string &key) const
Definition: ValueTree.h:299
void append(const std::string &value, int maxColumns, int numValues=0, StringStyle stringStyle=PLAIN_STRING)
Definition: ValueTree.h:587
bool isDegreeMode() const
Definition: ValueTree.h:85
Listing * write(Mapping *mapping, const std::string &key, const Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:145
void setPosition(int line, int column)
Definition: ValueTree.h:127
bool isCollection() const
Definition: ValueTree.h:69
StringStyle keyStringStyle() const
Definition: ValueTree.h:438
const std::string & toString() const
Definition: ValueTree.h:242
@ DOUBLE_QUOTED
Definition: ValueTree.h:24
bool hasLineInfo() const
Definition: ValueTree.h:111
@ FOLDED_STRING
Definition: ValueTree.h:24
const_iterator begin() const
Definition: ValueTree.h:433
bool isForcedRadianMode() const
Definition: ValueTree.h:82
const std::string & key()
Definition: ValueTree.h:142
StringStyle stringStyle() const
Definition: ValueTree.h:225
iterator end()
Definition: ValueTree.h:595
TypeBit LFType() const
Definition: ValueTree.h:57
Definition: ValueTree.h:34
bool read(std::initializer_list< const char * > keys, T &out_value) const
Definition: ValueTree.h:348
bool empty() const
Definition: ValueTree.h:267
int indexInMapping() const
Definition: ValueTree.h:176
TypeBit
Definition: ValueTree.h:44
Mapping * createMapping(const std::string &key)
Definition: ValueTree.h:315
int typeBits
Definition: ValueTree.h:190
T get(const std::string &key, const T &defaultValue) const
Definition: ValueTree.h:373
ValueNode * front() const
Definition: ValueTree.h:515
void setDoubleFormat(const char *format)
Definition: ValueTree.h:278
const char * doubleFormat()
Definition: ValueTree.h:280
void throwNotScalrException() const
Definition: ValueTree.cpp:414
bool empty() const
Definition: ValueTree.h:499
const_iterator end() const
Definition: ValueTree.h:597
float toFloat() const
Definition: ValueTree.cpp:240
Definition: ValueTree.h:148
ValueNode * back() const
Definition: ValueTree.h:519
@ MAPPING
Definition: YAMLWriter.cpp:18
bool read(const Mapping *mapping, const std::string &key, Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:43
bool isListing() const
Definition: ValueTree.h:95
Definition: ValueTree.h:163
const_iterator begin() const
Definition: ValueTree.h:596
Definition: ValueTree.h:157
Listing * createFlowStyleListing(const std::string &key)
Definition: ValueTree.h:335
const char * floatingNumberFormat()
Definition: ValueTree.h:508
T get(std::initializer_list< const char * > keys, const T &defaultValue) const
Definition: ValueTree.h:392
int toInt() const
Definition: ValueTree.cpp:166
@ SINGLE_QUOTED
Definition: ValueTree.h:24
Container::const_iterator const_iterator
Definition: ValueTree.h:497
Container::const_iterator const_iterator
Definition: ValueTree.h:258
int line() const
Definition: ValueTree.h:112
Definition: ValueTree.h:120
std::string get(const std::string &key, const char *defaultValue) const
Definition: ValueTree.h:382
ref_ptr< ValueNode > ValueNodePtr
Definition: ValueTree.h:213
TypeBit nodeType() const
Definition: ValueTree.h:58
Mapping * createFlowStyleMapping(const std::string &key)
Definition: ValueTree.h:319
int size() const
Definition: ValueTree.h:500
const char * doubleFormat()
Definition: ValueTree.h:513
Definition: ValueTree.h:484
Definition: AbstractSceneLoader.h:11
@ LITERAL_STRING
Definition: ValueTree.h:24
Mapping * openMapping(const std::string &key)
Definition: ValueTree.h:307
StringStyle
Definition: ValueTree.h:24
Definition: ValueTree.h:166
bool isString() const
Definition: ValueTree.h:68
Definition: ValueTree.h:160
const_iterator end() const
Definition: ValueTree.h:434
@ PLAIN_STRING
Definition: ValueTree.h:24
Listing * createListing(const std::string &key)
Definition: ValueTree.h:331
T read(const std::string &key) const
Definition: ValueTree.h:441
ValueNode()
Definition: ValueTree.h:181
double toDouble() const
Definition: ValueTree.cpp:217
void append(int value, int maxColumns, int numValues=0)
Definition: ValueTree.h:562
void append(ValueNode *node)
Definition: ValueTree.h:549
bool isMapping() const
Definition: ValueTree.h:91
iterator begin()
Definition: ValueTree.h:594
void setForcedRadianMode(bool on=true)
Definition: ValueTree.h:83
Definition: Referenced.h:54
AssignMode
Definition: ValueTree.h:413
void setKey(const std::string &key)
Definition: ValueTree.h:143
void setAssignMode(AssignMode mode)
Definition: ValueTree.h:415
Definition: ValueTree.h:169
const std::string & stringValue() const
Definition: ValueTree.h:224
const char * floatingNumberFormat()
Definition: ValueTree.h:275
Definition: ValueTree.h:172
void setFlowStyle(bool isFlowStyle=true)
Definition: ValueTree.h:504
int column() const
Definition: ValueTree.h:113
Mapping * openFlowStyleMapping(const std::string &key)
Definition: ValueTree.h:311
Definition: ValueTree.h:216
Definition: ValueTree.h:154
Container::iterator iterator
Definition: ValueTree.h:496
iterator end()
Definition: ValueTree.h:432
int line() const
Definition: ValueTree.h:124
Definition: ValueTree.h:151
void setDoubleFormat(const char *format)
Definition: ValueTree.h:511
void setDegreeMode()
Definition: ValueTree.h:87
ValueNode * at(int i) const
Definition: ValueTree.h:523
ValueNode & operator[](int i) const
Definition: ValueTree.h:540
Definition: ValueTree.h:140
Container::iterator iterator
Definition: ValueTree.h:257
ref_ptr< Listing > ListingPtr
Definition: ValueTree.h:629
void setMessage(const std::string &m)
Definition: ValueTree.h:131
ref_ptr< Mapping > MappingPtr
Definition: StdSceneWriter.h:12
void setAsHeaderInMapping(int priority=1)
Definition: ValueTree.h:177
void append(double value, int maxColumns, int numValues=0)
Definition: ValueTree.h:575
void assign(const std::string &key, T &io_value, const T &defaultValue)
Definition: ValueTree.h:418
bool isFlowStyle() const
Definition: ValueTree.h:272
void write(const std::string &key, const char *value, StringStyle stringStyle=PLAIN_STRING)
Definition: ValueTree.h:402
ValueNode & get(int i) const
Definition: ValueTree.h:530
void writeAsListing(const std::string &key, const ArrayType &container)
Definition: ValueTree.h:620