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;
249 return static_cast<const ScalarNode* const
>(
this)->stringValue_;
255 typedef std::map<std::string, ValueNodePtr> Container;
267 virtual Mapping* cloneMapping()
const;
269 bool empty()
const {
return values.empty(); }
270 int size()
const {
return static_cast<int>(values.size()); }
273 void setFlowStyle(
bool isFlowStyle =
true) { isFlowStyle_ = isFlowStyle; }
276 void setFloatingNumberFormat(
const char* format);
279 [[deprecated(
"Use Mapping::setFloatingNumberFormat")]]
281 [[deprecated(
"Use Mapping::floatingNumberFormat")]]
286 ValueNode* find(
const std::string& key)
const;
287 ValueNode* find(std::initializer_list<const char*> keys)
const;
288 Mapping* findMapping(
const std::string& key)
const;
289 Mapping* findMapping(std::initializer_list<const char*> keys)
const;
290 Listing* findListing(
const std::string& key)
const;
291 Listing* findListing(std::initializer_list<const char*> keys)
const;
294 ValueNodePtr extract(std::initializer_list<const char*> keys);
296 bool extract(
const std::string& key,
double& out_value);
297 bool extract(
const std::string& key, std::string& out_value);
299 ValueNode& get(
const std::string& key)
const;
305 void insert(
const std::string& key,
ValueNode* node);
307 void insert(
const Mapping* other);
310 return openMapping_(key,
false);
314 return openFlowStyleMapping_(key,
false);
318 return openMapping_(key,
true);
322 return openFlowStyleMapping_(key,
true);
326 return openListing_(key,
false);
330 return openFlowStyleListing_(key,
false);
334 return openListing_(key,
true);
338 return openFlowStyleListing_(key,
true);
341 bool remove(
const std::string& key);
343 bool read(
const std::string& key, std::string& out_value)
const;
344 bool read(
const std::string& key,
bool& out_value)
const;
345 bool read(
const std::string& key,
int& out_value)
const;
346 bool read(
const std::string& key,
double& out_value)
const;
347 bool read(
const std::string& key,
float& out_value)
const;
350 bool read(std::initializer_list<const char*> keys, T& out_value)
const {
351 for(
auto& key : keys){
352 if(this->
read(key, out_value)){
359 bool readAngle(
const std::string& key,
double& out_angle,
const ValueNode* unitAttrNode =
nullptr)
const;
360 bool readAngle(
const std::string& key,
float& out_angle,
const ValueNode* unitAttrNode =
nullptr)
const;
361 bool readAngle(std::initializer_list<const char*> keys,
double& out_angle,
const ValueNode* unitAttrNode =
nullptr)
const;
362 bool readAngle(std::initializer_list<const char*> keys,
float& out_angle,
const ValueNode* unitAttrNode =
nullptr)
const;
364 template <
class T> T
get(
const std::string& key)
const {
366 if(
read(key, value)){
369 throwKeyNotFoundException(key);
375 T
get(
const std::string& key,
const T& defaultValue)
const {
377 if(
read(key, value)){
384 std::string
get(
const std::string& key,
const char* defaultValue)
const {
386 if(
read(key, value)){
394 T
get(std::initializer_list<const char*> keys,
const T& defaultValue)
const {
396 if(
read(keys, value)){
405 write(key, std::string(value), stringStyle);
408 void write(
const std::string& key,
bool value);
409 void write(
const std::string& key,
int value);
410 void write(
const std::string& key,
double value);
411 void writePath(
const std::string &key,
const std::string& value);
413 template<
class ArrayType>
void writeAsListing(
const std::string& key,
const ArrayType& container);
415 typedef enum { READ_MODE, WRITE_MODE } AssignMode;
420 void assign(
const std::string& key, T& io_value,
const T& defaultValue){
423 if(!
read(key, io_value)){
424 io_value = defaultValue;
428 write(key, io_value);
438 void throwKeyNotFoundException(
const std::string& key)
const;
443 template <
class T> T
read(
const std::string& key)
const {
return get<T>(key); }
445 #ifdef CNOID_BACKWARD_COMPATIBILITY
446 Listing* findSequence(
const std::string& key)
const {
return findListing(key); }
447 Listing* openSequence(
const std::string& key) {
return openListing(key); }
448 Listing* openFlowStyleSequence(
const std::string& key){
return openFlowStyleListing(key); }
449 Listing* createSequence(
const std::string& key){
return createListing(key); }
450 Listing* createFlowStyleSequence(
const std::string& key){
return createFlowStyleListing(key); }
455 Mapping(
const Mapping& org);
456 Mapping& operator=(
const Mapping&);
458 Mapping* openMapping_(
const std::string& key,
bool doOverwrite);
459 Mapping* openFlowStyleMapping_(
const std::string& key,
bool doOverwrite);
460 Listing* openListing_(
const std::string& key,
bool doOverwrite);
461 Listing* openFlowStyleListing_(
const std::string& key,
bool doOverwrite);
463 inline void insertSub(
const std::string& key, ValueNode* node);
465 void writeSub(
const std::string &key,
const char* text,
size_t length,
StringStyle stringStyle);
470 const char* floatingNumberFormat_;
475 friend class YAMLReaderImpl;
488 typedef std::vector<ValueNodePtr> Container;
501 bool empty()
const {
return values.empty(); }
502 int size()
const {
return static_cast<int>(values.size()); }
504 void reserve(
int size);
506 void setFlowStyle(
bool isFlowStyle =
true) { isFlowStyle_ = isFlowStyle; }
509 void setFloatingNumberFormat(
const char* format);
512 [[deprecated(
"Use Mapping::setFloatingNumberFormat")]]
514 [[deprecated(
"Use Mapping::floatingNumberFormat")]]
518 return values.front();
522 return values.back();
536 void write(
int i,
int value);
552 values.push_back(node);
557 void append(
int value);
564 void append(
int value,
int maxColumns,
int numValues = 0) {
565 insertLF(maxColumns, numValues);
569 void append(
size_t value);
570 void append(
double value);
577 void append(
double value,
int maxColumns,
int numValues = 0) {
578 insertLF(maxColumns, numValues);
590 insertLF(maxColumns, numValues);
591 append(value, stringStyle);
604 Listing(
int line,
int column,
int reservedSize);
609 void insertLF(
int maxColumns,
int numValues);
612 const char* floatingNumberFormat_;
614 bool doInsertLFBeforeNextElement;
617 friend class YAMLReaderImpl;
621 template<
class ArrayType>
625 for(
auto& value : container){
626 listing->append(value);
633 #ifdef CNOID_BACKWARD_COMPATIBILITY
bool isFlowStyle() const
Definition: ValueTree.h:507
@ LISTING
Definition: YAMLWriter.cpp:18
ValueNode(TypeBit type)
Definition: ValueTree.h:182
iterator begin()
Definition: ValueTree.h:433
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:253
T get(const std::string &key) const
Definition: ValueTree.h:364
Listing * openListing(const std::string &key)
Definition: ValueTree.h:325
void setFlowStyle(bool isFlowStyle=true)
Definition: ValueTree.h:273
int size() const
Definition: ValueTree.h:270
Listing * openFlowStyleListing(const std::string &key)
Definition: ValueTree.h:329
ValueNode & operator[](const std::string &key) const
Definition: ValueTree.h:301
void append(const std::string &value, int maxColumns, int numValues=0, StringStyle stringStyle=PLAIN_STRING)
Definition: ValueTree.h:589
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:440
const std::string & toString() const
Definition: ValueTree.h:244
@ 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:435
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:597
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:350
bool empty() const
Definition: ValueTree.h:269
int indexInMapping() const
Definition: ValueTree.h:176
TypeBit
Definition: ValueTree.h:44
Mapping * createMapping(const std::string &key)
Definition: ValueTree.h:317
int typeBits
Definition: ValueTree.h:190
T get(const std::string &key, const T &defaultValue) const
Definition: ValueTree.h:375
ValueNode * front() const
Definition: ValueTree.h:517
void setDoubleFormat(const char *format)
Definition: ValueTree.h:280
const char * doubleFormat()
Definition: ValueTree.h:282
void throwNotScalrException() const
Definition: ValueTree.cpp:414
bool empty() const
Definition: ValueTree.h:501
const_iterator end() const
Definition: ValueTree.h:599
float toFloat() const
Definition: ValueTree.cpp:240
Definition: ValueTree.h:148
ValueNode * back() const
Definition: ValueTree.h:521
@ 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:598
Definition: ValueTree.h:157
Listing * createFlowStyleListing(const std::string &key)
Definition: ValueTree.h:337
const char * floatingNumberFormat()
Definition: ValueTree.h:510
T get(std::initializer_list< const char * > keys, const T &defaultValue) const
Definition: ValueTree.h:394
int toInt() const
Definition: ValueTree.cpp:166
@ SINGLE_QUOTED
Definition: ValueTree.h:24
Container::const_iterator const_iterator
Definition: ValueTree.h:499
Container::const_iterator const_iterator
Definition: ValueTree.h:260
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:384
ref_ptr< ValueNode > ValueNodePtr
Definition: ValueTree.h:213
TypeBit nodeType() const
Definition: ValueTree.h:58
ref_ptr< ScalarNode > ScalarNodePtr
Definition: ValueTree.h:241
Mapping * createFlowStyleMapping(const std::string &key)
Definition: ValueTree.h:321
int size() const
Definition: ValueTree.h:502
const char * doubleFormat()
Definition: ValueTree.h:515
Definition: ValueTree.h:486
Definition: AbstractSceneLoader.h:11
@ LITERAL_STRING
Definition: ValueTree.h:24
Mapping * openMapping(const std::string &key)
Definition: ValueTree.h:309
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:436
@ PLAIN_STRING
Definition: ValueTree.h:24
Listing * createListing(const std::string &key)
Definition: ValueTree.h:333
T read(const std::string &key) const
Definition: ValueTree.h:443
ValueNode()
Definition: ValueTree.h:181
double toDouble() const
Definition: ValueTree.cpp:217
void append(int value, int maxColumns, int numValues=0)
Definition: ValueTree.h:564
void append(ValueNode *node)
Definition: ValueTree.h:551
bool isMapping() const
Definition: ValueTree.h:91
iterator begin()
Definition: ValueTree.h:596
void setForcedRadianMode(bool on=true)
Definition: ValueTree.h:83
Definition: Referenced.h:54
AssignMode
Definition: ValueTree.h:415
void setKey(const std::string &key)
Definition: ValueTree.h:143
void setAssignMode(AssignMode mode)
Definition: ValueTree.h:417
Definition: ValueTree.h:169
const std::string & stringValue() const
Definition: ValueTree.h:224
const char * floatingNumberFormat()
Definition: ValueTree.h:277
Definition: ValueTree.h:172
void setFlowStyle(bool isFlowStyle=true)
Definition: ValueTree.h:506
int column() const
Definition: ValueTree.h:113
Mapping * openFlowStyleMapping(const std::string &key)
Definition: ValueTree.h:313
Definition: ValueTree.h:216
Definition: ValueTree.h:154
Container::iterator iterator
Definition: ValueTree.h:498
iterator end()
Definition: ValueTree.h:434
int line() const
Definition: ValueTree.h:124
Definition: ValueTree.h:151
void setDoubleFormat(const char *format)
Definition: ValueTree.h:513
void setDegreeMode()
Definition: ValueTree.h:87
ValueNode * at(int i) const
Definition: ValueTree.h:525
ValueNode & operator[](int i) const
Definition: ValueTree.h:542
Definition: ValueTree.h:140
Container::iterator iterator
Definition: ValueTree.h:259
ref_ptr< Listing > ListingPtr
Definition: ValueTree.h:631
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:577
void assign(const std::string &key, T &io_value, const T &defaultValue)
Definition: ValueTree.h:420
bool isFlowStyle() const
Definition: ValueTree.h:274
void write(const std::string &key, const char *value, StringStyle stringStyle=PLAIN_STRING)
Definition: ValueTree.h:404
ValueNode & get(int i) const
Definition: ValueTree.h:532
void writeAsListing(const std::string &key, const ArrayType &container)
Definition: ValueTree.h:622