5 #ifndef CNOID_UTIL_YAML_NODES_H_INCLUDED
6 #define CNOID_UTIL_YAML_NODES_H_INCLUDED
13 #include <boost/intrusive_ptr.hpp>
48 double toDouble()
const;
54 const std::string toString()
const;
55 const std::string toUtf8String()
const;
57 inline operator std::string ()
const {
61 const std::string& toString()
const;
62 const std::string& toUtf8String()
const;
64 inline operator const std::string& ()
const {
79 bool read(
int &out_value)
const;
83 inline int line()
const {
return line_ + 1; }
84 inline int column()
const {
return column_ + 1; }
89 int line()
const {
return line_; }
90 int column()
const {
return column_; }
91 const std::string&
message()
const {
return message_; }
102 std::string message_;
107 const std::string&
key() {
return key_; }
108 void setKey(
const std::string& key) { key_ = key; }
142 void throwNotScalrException()
const;
143 void throwNotMappingException()
const;
144 void throwNotSequenceException()
const;
158 friend class YamlReaderImpl;
178 std::string stringValue;
181 friend class YamlReaderImpl;
194 inline void setFlowStyle(
bool isFlowStyle =
true) { isFlowStyle_ = isFlowStyle; }
197 void setDoubleFormat(
const char* format);
214 typedef std::map<std::string, YamlNodePtr> Container;
225 inline bool empty()
const {
return values.empty(); }
226 inline size_t size()
const {
return values.size(); }
231 YamlNode* find(
const std::string& key)
const;
232 YamlMapping* findMapping(
const std::string& key)
const;
233 YamlSequence* findSequence(
const std::string& key)
const;
235 YamlNode&
get(
const std::string& key)
const;
241 void insert(
const std::string& key,
YamlNodePtr node);
244 return openMapping(key,
false);
248 return openFlowStyleMapping(key,
false);
252 return openMapping(key,
true);
256 return openFlowStyleMapping(key,
true);
260 return openSequence(key,
false);
264 return openFlowStyleSequence(key,
false);
268 return openSequence(key,
true);
272 return openFlowStyleSequence(key,
true);
275 bool read(
const std::string &key, std::string &out_value)
const;
276 bool readUtf8(
const std::string &key, std::string &out_value)
const;
277 bool read(
const std::string &key,
bool &out_value)
const;
278 bool read(
const std::string &key,
int &out_value)
const;
279 bool read(
const std::string &key,
double &out_value)
const;
282 inline T
read(
const std::string& key)
const {
284 if(
read(key, value)){
287 throwKeyNotFoundException(key);
292 inline T
get(
const std::string& key,
const T& defaultValue)
const {
294 if(
read(key, value)){
301 inline std::string
get(
const std::string& key,
const char* defaultValue)
const {
303 if(
read(key, value)){
313 writeUtf8(key,
toUtf8(value), stringStyle);
317 writeUtf8(key, std::string(value), stringStyle);
321 write(key, std::string(value), stringStyle);
324 void write(
const std::string &key,
bool value);
325 void write(
const std::string &key,
int value);
326 void write(
const std::string &key,
double value);
327 void writePath(
const std::string &key,
const std::string& value);
329 typedef enum { READ_MODE, WRITE_MODE } AssignMode;
334 void assign(
const std::string& key, T& io_value,
const T& defaultValue){
337 if(!
read(key, io_value)){
338 io_value = defaultValue;
342 write(key, io_value);
352 void throwKeyNotFoundException(
const std::string& key)
const;
359 YamlMapping* openMapping(
const std::string& key,
bool doOverwrite);
360 YamlMapping* openFlowStyleMapping(
const std::string& key,
bool doOverwrite);
361 YamlSequence* openSequence(
const std::string& key,
bool doOverwrite);
362 YamlSequence* openFlowStyleSequence(
const std::string& key,
bool doOverwrite);
364 inline void insertSub(
const std::string& key,
YamlNode* node);
366 void writeSub(
const std::string &key,
const char* text,
size_t length,
YamlStringStyle stringStyle);
376 friend class YamlReaderImpl;
388 typedef std::vector<YamlNodePtr> Container;
399 inline bool empty()
const {
return values.empty(); }
400 inline int size()
const {
return values.size(); }
402 void reserve(
int size);
405 return *values.front();
409 return *values.back();
418 void write(
int i,
int value);
421 bool read(
int i,
bool &out_value)
const;
422 bool read(
int i,
int &out_value)
const;
423 bool read(
int i,
double &out_value)
const;
432 values.push_back(node);
435 void append(
int value);
442 inline void append(
int value,
int maxColumns,
int numValues = 0) {
443 insertLF(maxColumns, numValues);
447 void append(
size_t value);
454 inline void append(
size_t value,
int maxColumns,
int numValues = 0){
455 insertLF(maxColumns, numValues);
459 void append(
double value);
466 inline void append(
double value,
int maxColumns,
int numValues = 0) {
467 insertLF(maxColumns, numValues);
479 insertLF(maxColumns, numValues);
480 append(value, stringStyle);
498 void insertLF(
int maxColumns,
int numValues);
503 friend class YamlReaderImpl;
519 if(obj->refCounter == 0){