5 #ifndef CNOID_UTIL_VALUE_TREE_H 6 #define CNOID_UTIL_VALUE_TREE_H 23 #ifndef CNOID_BACKWARD_COMPATIBILITY 39 static Initializer initializer;
45 #ifndef CNOID_BACKWARD_COMPATIBILITY 46 enum TypeBit { INVALID_NODE = 0, SCALAR = 1, MAPPING = 2, LISTING = 4, INSERT_LF = 8, APPEND_LF = 16 };
48 enum TypeBit { INVALID_NODE = 0, SCALAR = 1, MAPPING = 2, LISTING = 4, SEQUENCE = 4, INSERT_LF = 8, APPEND_LF = 16 };
56 double toDouble()
const;
59 bool isScalar()
const {
return typeBits & SCALAR; }
60 bool isString()
const {
return typeBits & SCALAR; }
63 const std::string toString()
const;
64 const std::string toUTF8String()
const;
66 operator const std::string ()
const {
70 const std::string& toString()
const;
71 const std::string& toUTF8String()
const;
73 operator const std::string& ()
const {
79 template<
typename T> T to()
const;
81 bool isMapping()
const {
return typeBits & MAPPING; }
82 const Mapping* toMapping()
const;
85 bool isListing()
const {
return typeBits & LISTING; }
86 const Listing* toListing()
const;
89 #ifdef CNOID_BACKWARD_COMPATIBILITY 90 bool isSequence()
const {
return typeBits & LISTING; }
91 const Listing* toSequence()
const {
return toListing(); }
92 Listing* toSequence() {
return toListing(); }
95 bool read(
int &out_value)
const;
96 bool read(
double &out_value)
const;
97 bool read(
bool &out_value)
const;
98 bool read(std::string &out_value)
const;
99 bool readUTF8String(std::string& out_value)
const;
102 int line()
const {
return line_ + 1; }
103 int column()
const {
return column_ + 1; }
105 void throwException(
const std::string& message)
const;
114 int line()
const {
return line_; }
116 std::string message()
const;
127 std::string message_;
132 const std::string&
key() {
return key_; }
133 void setKey(
const std::string& key) { key_ = key; }
172 void throwNotScalrException()
const;
173 void throwNotMappingException()
const;
174 void throwNotListingException()
const;
186 friend class YAMLReaderImpl;
193 template<>
inline double ValueNode::to<double>()
const {
return toDouble(); }
194 template<>
inline int ValueNode::to<int>()
const {
return toInt(); }
195 template<>
inline std::string ValueNode::to<std::string>()
const {
return toString(); }
213 std::string stringValue;
216 friend class YAMLReaderImpl;
226 typedef std::map<std::string, ValueNodePtr> Container;
239 bool empty()
const {
return values.empty(); }
240 int size()
const {
return values.size(); }
243 void setFlowStyle(
bool isFlowStyle =
true) { isFlowStyle_ = isFlowStyle; }
246 void setDoubleFormat(
const char* format);
251 ValueNode* find(
const std::string& key)
const;
252 Mapping* findMapping(
const std::string& key)
const;
253 Listing* findListing(
const std::string& key)
const;
255 ValueNodePtr extract(
const std::string& key);
257 ValueNode&
get(
const std::string& key)
const;
263 void insert(
const std::string& key,
ValueNode* node);
265 void insert(
const Mapping* other);
268 return openMapping(key,
false);
272 return openFlowStyleMapping(key,
false);
276 return openMapping(key,
true);
280 return openFlowStyleMapping(key,
true);
284 return openListing(key,
false);
288 return openFlowStyleListing(key,
false);
292 return openListing(key,
true);
296 return openFlowStyleListing(key,
true);
299 bool remove(
const std::string& key);
301 bool read(
const std::string &key, std::string &out_value)
const;
302 bool readUTF8(
const std::string &key, std::string &out_value)
const;
303 bool read(
const std::string &key,
bool &out_value)
const;
304 bool read(
const std::string &key,
int &out_value)
const;
305 bool read(
const std::string &key,
double &out_value)
const;
308 T
read(
const std::string& key)
const {
310 if(
read(key, value)){
313 throwKeyNotFoundException(key);
318 T
get(
const std::string& key,
const T& defaultValue)
const {
320 if(
read(key, value)){
327 std::string
get(
const std::string& key,
const char* defaultValue)
const {
329 if(
read(key, value)){
339 writeUTF8(key,
toUTF8(value), stringStyle);
343 writeUTF8(key, std::string(value), stringStyle);
347 write(key, std::string(value), stringStyle);
350 void write(
const std::string &key,
bool value);
351 void write(
const std::string &key,
int value);
352 void write(
const std::string &key,
double value);
353 void writePath(
const std::string &key,
const std::string& value);
360 void assign(
const std::string& key, T& io_value,
const T& defaultValue){
363 if(!
read(key, io_value)){
364 io_value = defaultValue;
368 write(key, io_value);
373 iterator
begin() {
return values.begin(); }
374 iterator
end() {
return values.end(); }
375 const_iterator
begin()
const {
return values.begin(); }
376 const_iterator
end()
const {
return values.end(); }
378 void throwKeyNotFoundException(
const std::string& key)
const;
380 #ifdef CNOID_BACKWARD_COMPATIBILITY 381 Listing* findSequence(
const std::string& key)
const {
return findListing(key); }
382 Listing* openSequence(
const std::string& key) {
return openListing(key); }
383 Listing* openFlowStyleSequence(
const std::string& key){
return openFlowStyleListing(key); }
384 Listing* createSequence(
const std::string& key){
return createListing(key); }
385 Listing* createFlowStyleSequence(
const std::string& key){
return createFlowStyleListing(key); }
393 Mapping* openMapping(
const std::string& key,
bool doOverwrite);
394 Mapping* openFlowStyleMapping(
const std::string& key,
bool doOverwrite);
395 Listing* openListing(
const std::string& key,
bool doOverwrite);
396 Listing* openFlowStyleListing(
const std::string& key,
bool doOverwrite);
398 inline void insertSub(
const std::string& key,
ValueNode* node);
400 void writeSub(
const std::string &key,
const char* text,
size_t length,
StringStyle stringStyle);
407 const char* doubleFormat_;
412 friend class YAMLReaderImpl;
426 typedef std::vector<ValueNodePtr> Container;
439 bool empty()
const {
return values.empty(); }
440 int size()
const {
return values.size(); }
442 void reserve(
int size);
444 void setFlowStyle(
bool isFlowStyle =
true) { isFlowStyle_ = isFlowStyle; }
447 void setDoubleFormat(
const char* format);
451 return values.front();
455 return values.back();
469 void write(
int i,
int value);
485 values.push_back(node);
490 void append(
int value);
497 void append(
int value,
int maxColumns,
int numValues = 0) {
498 insertLF(maxColumns, numValues);
502 void append(
size_t value);
516 void append(
double value);
523 void append(
double value,
int maxColumns,
int numValues = 0) {
524 insertLF(maxColumns, numValues);
536 insertLF(maxColumns, numValues);
537 append(value, stringStyle);
542 iterator
begin() {
return values.begin(); }
543 iterator
end() {
return values.end(); }
544 const_iterator
begin()
const {
return values.begin(); }
545 const_iterator
end()
const {
return values.end(); };
550 Listing(
int line,
int column,
int reservedSize);
555 void insertLF(
int maxColumns,
int numValues);
558 const char* doubleFormat_;
560 bool doInsertLFBeforeNextElement;
563 friend class YAMLReaderImpl;
569 #ifdef CNOID_BACKWARD_COMPATIBILITY 571 typedef ValueNodePtr YamlNodePtr;
574 typedef MappingPtr YamlMappingPtr;
576 typedef ListingPtr YamlSequencePtr;
578 typedef ListingPtr SequencePtr;
void setKey(const std::string &key)
Definition: ValueTree.h:133
bool isString() const
Definition: ValueTree.h:60
void setAssignMode(AssignMode mode)
Definition: ValueTree.h:357
Definition: ValueTree.h:141
bool isValid() const
Definition: ValueTree.h:51
const std::string & toUTF8(const std::string &text)
Definition: UTF8.h:19
ValueNode * front() const
Definition: ValueTree.h:450
Listing * openListing(const std::string &key)
Definition: ValueTree.h:283
bool isMapping() const
Definition: ValueTree.h:81
Listing * openFlowStyleListing(const std::string &key)
Definition: ValueTree.h:287
const char * doubleFormat()
Definition: ValueTree.h:247
void append(int value, int maxColumns, int numValues=0)
Definition: ValueTree.h:497
Definition: ValueTree.h:224
Definition: ValueTree.h:156
void append(ValueNode *node)
Definition: ValueTree.h:484
iterator begin()
Definition: ValueTree.h:542
T read(const std::string &key) const
Definition: ValueTree.h:308
Container::const_iterator const_iterator
Definition: ValueTree.h:231
const char * doubleFormat()
Definition: ValueTree.h:448
int column() const
Definition: ValueTree.h:115
ValueNode(TypeBit type)
Definition: ValueTree.h:168
void write(const std::string &key, const char *value, StringStyle stringStyle=PLAIN_STRING)
Definition: ValueTree.h:346
ValueNode()
Definition: ValueTree.h:167
Definition: YAMLWriter.h:18
Mapping * createMapping(const std::string &key)
Definition: ValueTree.h:275
void setFlowStyle(bool isFlowStyle=true)
Definition: ValueTree.h:444
StringStyle
Definition: ValueTree.h:24
Definition: Referenced.h:67
Definition: ValueTree.h:162
Container::iterator iterator
Definition: ValueTree.h:436
Definition: ValueTree.h:138
bool empty() const
Definition: ValueTree.h:239
int size() const
Definition: ValueTree.h:240
Listing & write(Mapping &mapping, const std::string &key, const Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:62
bool hasLineInfo() const
Definition: ValueTree.h:101
Definition: ValueTree.h:144
const_iterator begin() const
Definition: ValueTree.h:375
Definition: ValueTree.h:24
void append(double value, int maxColumns, int numValues=0)
Definition: ValueTree.h:523
ValueNode & operator[](int i) const
Definition: ValueTree.h:475
Mapping * openMapping(const std::string &key)
Definition: ValueTree.h:267
virtual ~ValueNode()
Definition: ValueTree.h:170
Definition: ValueTree.h:147
const std::string & key()
Definition: ValueTree.h:132
TypeBit LFType() const
Definition: ValueTree.h:52
Definition: ValueTree.h:159
void writeUTF8(const std::string &key, const char *value, StringStyle stringStyle=PLAIN_STRING)
Definition: ValueTree.h:342
Definition: ValueTree.h:153
Listing * createListing(const std::string &key)
Definition: ValueTree.h:291
int size() const
Definition: ValueTree.h:440
Definition: ValueTree.h:424
Listing * createFlowStyleListing(const std::string &key)
Definition: ValueTree.h:295
void append(const std::string &value, int maxColumns, int numValues=0, StringStyle stringStyle=PLAIN_STRING)
Definition: ValueTree.h:535
ref_ptr< ValueNode > ValueNodePtr
Definition: ValueTree.h:197
Mapping * openFlowStyleMapping(const std::string &key)
Definition: ValueTree.h:271
ref_ptr< Listing > ListingPtr
Definition: ValueTree.h:567
void read(const Listing &listing, Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:17
bool empty() const
Definition: ValueTree.h:439
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
Definition: ValueTree.h:34
int line() const
Definition: ValueTree.h:114
Definition: ValueTree.h:24
Mapping * createFlowStyleMapping(const std::string &key)
Definition: ValueTree.h:279
Definition: ValueTree.h:150
TypeBit nodeType() const
Definition: ValueTree.h:53
Definition: ValueTree.h:130
void setPosition(int line, int column)
Definition: ValueTree.h:117
Definition: ValueTree.h:24
void setMessage(const std::string &m)
Definition: ValueTree.h:121
Container::const_iterator const_iterator
Definition: ValueTree.h:437
Definition: ValueTree.h:110
iterator end()
Definition: ValueTree.h:374
int column() const
Definition: ValueTree.h:103
bool isListing() const
Definition: ValueTree.h:85
ref_ptr< Mapping > MappingPtr
Definition: ValueTree.h:416
void setFlowStyle(bool isFlowStyle=true)
Definition: ValueTree.h:243
const_iterator begin() const
Definition: ValueTree.h:544
#define CNOID_EXPORT
Definition: Util/exportdecl.h:37
const_iterator end() const
Definition: ValueTree.h:545
int line() const
Definition: ValueTree.h:102
Definition: ValueTree.h:24
iterator begin()
Definition: ValueTree.h:373
ValueNode * back() const
Definition: ValueTree.h:454
const_iterator end() const
Definition: ValueTree.h:376
Container::iterator iterator
Definition: ValueTree.h:230
iterator end()
Definition: ValueTree.h:543
AssignMode
Definition: ValueTree.h:355
int typeBits
Definition: ValueTree.h:176
ValueNode & operator[](const std::string &key) const
Definition: ValueTree.h:259
bool isFlowStyle() const
Definition: ValueTree.h:244
ValueNode * at(int i) const
Definition: ValueTree.h:458
void assign(const std::string &key, T &io_value, const T &defaultValue)
Definition: ValueTree.h:360
Definition: ValueTree.h:200
Definition: ValueTree.h:24
bool isFlowStyle() const
Definition: ValueTree.h:445
void write(const std::string &key, const std::string &value, StringStyle stringStyle=PLAIN_STRING)
Definition: ValueTree.h:338
bool isScalar() const
Definition: ValueTree.h:59
TypeBit
Definition: ValueTree.h:46