/usr/include/choreonoid-1.1/cnoid/src/Util/YamlNodes.h is in libcnoid-dev 1.1.0+dfsg-6.1+b4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | /**
@author Shin'ichiro Nakaoka
*/
#ifndef CNOID_UTIL_YAML_NODES_H_INCLUDED
#define CNOID_UTIL_YAML_NODES_H_INCLUDED
#include "Utf8.h"
#include <map>
#include <vector>
#include <cstring>
#include <iosfwd>
#include <boost/intrusive_ptr.hpp>
#include "exportdecl.h"
namespace cnoid {
class YamlNode;
}
namespace cnoid {
void intrusive_ptr_add_ref(cnoid::YamlNode* obj);
void intrusive_ptr_release(cnoid::YamlNode* obj);
}
namespace cnoid {
enum YamlNodeType { YAML_NONE = 0, YAML_MAPPING, YAML_SEQUENCE, YAML_SCALAR, YAML_LF };
enum YamlStringStyle { YAML_PLAIN_STRING, YAML_SINGLE_QUOTED, YAML_DOUBLE_QUOTED, YAML_LITERAL, YAML_FOLDED };
class YamlScalar;
class YamlMapping;
class YamlSequence;
class YamlReaderImpl;
class YamlWriter;
class CNOID_EXPORT YamlNode
{
public:
static void initialize();
inline bool isValid() const { return type_ != YAML_NONE; }
inline YamlNodeType type() const { return type_; }
int toInt() const;
double toDouble() const;
bool toBool() const;
inline bool isString() const { return type_ == YAML_SCALAR; }
#ifdef _WIN32
const std::string toString() const;
const std::string toUtf8String() const;
inline operator std::string () const {
return toString();
}
#else
const std::string& toString() const;
const std::string& toUtf8String() const;
inline operator const std::string& () const {
return toString();
}
#endif
inline bool isMapping() const { return type_ == YAML_MAPPING; }
const YamlMapping* toMapping() const;
YamlMapping* toMapping();
inline bool isSequence() const { return type_ == YAML_SEQUENCE; }
const YamlSequence* toSequence() const;
YamlSequence* toSequence();
//bool read(std::string &out_value) const;
//bool read(bool &out_value) const;
bool read(int &out_value) const;
//bool read(double &out_value) const;
inline bool hasLineInfo() const { return (line_ >= 0); }
inline int line() const { return line_ + 1; }
inline int column() const { return column_ + 1; }
class CNOID_EXPORT Exception {
public:
virtual ~Exception();
int line() const { return line_; }
int column() const { return column_; }
const std::string& message() const { return message_; }
void setPosition(int line, int column) {
line_ = line;
column_ = column;
}
void setMessage(const std::string& m){
message_ = m;
}
private:
int line_;
int column_;
std::string message_;
};
class KeyNotFoundException : public Exception {
public:
const std::string& key() { return key_; }
void setKey(const std::string& key) { key_ = key; }
private:
std::string key_;
};
class NotScalarException : public Exception {
};
class ScalarTypeMismatchException : public Exception {
};
class NotMappingException : public Exception {
};
class NotSequenceException : public Exception {
};
class SyntaxException : public Exception {
};
class DocumentNotFoundException : public Exception {
};
private:
int refCounter;
protected:
YamlNode() : refCounter(0) { }
YamlNode(YamlNodeType type) : refCounter(0), type_(type), line_(-1), column_(-1) { }
virtual ~YamlNode() { }
void throwNotScalrException() const;
void throwNotMappingException() const;
void throwNotSequenceException() const;
YamlNodeType type_;
private:
// disabled copy operations
YamlNode(const YamlNode&);
YamlNode& operator=(const YamlNode&);
int line_;
int column_;
int indexInMapping; // used for YamlWriter
friend class YamlReaderImpl;
friend class YamlWriter;
friend class YamlScalar;
friend class YamlMapping;
friend class YamlSequence;
friend void intrusive_ptr_add_ref(YamlNode* obj);
friend void intrusive_ptr_release(YamlNode* obj);
};
typedef boost::intrusive_ptr<YamlNode> YamlNodePtr;
class CNOID_EXPORT YamlScalar : public YamlNode
{
private:
YamlScalar(const char* text, size_t length);
YamlScalar(const char* text, size_t length, YamlStringStyle stringStyle);
YamlScalar(const std::string& value, YamlStringStyle stringStyle);
std::string stringValue;
YamlStringStyle stringStyle;
friend class YamlReaderImpl;
friend class YamlWriter;
friend class YamlNode;
friend class YamlMapping;
friend class YamlSequence;
};
class CNOID_EXPORT YamlCollection : public YamlNode
{
public:
virtual ~YamlCollection();
inline void setFlowStyle(bool isFlowStyle = true) { isFlowStyle_ = isFlowStyle; }
inline bool isFlowStyle() const { return isFlowStyle_; }
void setDoubleFormat(const char* format);
inline const char* doubleFormat() { return doubleFormat_; }
protected:
YamlCollection();
const char* doubleFormat_;
private:
YamlCollection(const YamlCollection&);
YamlCollection& operator=(const YamlCollection&);
bool isFlowStyle_;
};
class CNOID_EXPORT YamlMapping : public YamlCollection
{
typedef std::map<std::string, YamlNodePtr> Container;
public:
typedef Container::iterator iterator;
typedef Container::const_iterator const_iterator;
YamlMapping();
YamlMapping(int line, int column);
virtual ~YamlMapping();
inline bool empty() const { return values.empty(); }
inline size_t size() const { return values.size(); }
void clear();
void setKeyQuoteStyle(YamlStringStyle style);
YamlNode* find(const std::string& key) const;
YamlMapping* findMapping(const std::string& key) const;
YamlSequence* findSequence(const std::string& key) const;
YamlNode& get(const std::string& key) const;
inline YamlNode& operator[](const std::string& key) const {
return get(key);
}
void insert(const std::string& key, YamlNodePtr node);
inline YamlMapping* openMapping(const std::string& key) {
return openMapping(key, false);
}
inline YamlMapping* openFlowStyleMapping(const std::string& key) {
return openFlowStyleMapping(key, false);
}
inline YamlMapping* createMapping(const std::string& key) {
return openMapping(key, true);
}
inline YamlMapping* createFlowStyleMapping(const std::string& key) {
return openFlowStyleMapping(key, true);
}
inline YamlSequence* openSequence(const std::string& key) {
return openSequence(key, false);
}
inline YamlSequence* openFlowStyleSequence(const std::string& key){
return openFlowStyleSequence(key, false);
}
inline YamlSequence* createSequence(const std::string& key){
return openSequence(key, true);
}
inline YamlSequence* createFlowStyleSequence(const std::string& key){
return openFlowStyleSequence(key, true);
}
bool read(const std::string &key, std::string &out_value) const;
bool readUtf8(const std::string &key, std::string &out_value) const;
bool read(const std::string &key, bool &out_value) const;
bool read(const std::string &key, int &out_value) const;
bool read(const std::string &key, double &out_value) const;
template <class T>
inline T read(const std::string& key) const {
T value;
if(read(key, value)){
return value;
} else {
throwKeyNotFoundException(key);
}
}
template <class T>
inline T get(const std::string& key, const T& defaultValue) const {
T value;
if(read(key, value)){
return value;
} else {
return defaultValue;
}
}
inline std::string get(const std::string& key, const char* defaultValue) const {
std::string value;
if(read(key, value)){
return value;
} else {
return defaultValue;
}
}
void writeUtf8(const std::string &key, const std::string& value, YamlStringStyle stringStyle = YAML_PLAIN_STRING);
void write(const std::string &key, const std::string& value, YamlStringStyle stringStyle = YAML_PLAIN_STRING) {
writeUtf8(key, toUtf8(value), stringStyle);
}
void writeUtf8(const std::string &key, const char* value, YamlStringStyle stringStyle = YAML_PLAIN_STRING){
writeUtf8(key, std::string(value), stringStyle);
}
void write(const std::string &key, const char* value, YamlStringStyle stringStyle = YAML_PLAIN_STRING){
write(key, std::string(value), stringStyle);
}
void write(const std::string &key, bool value);
void write(const std::string &key, int value);
void write(const std::string &key, double value);
void writePath(const std::string &key, const std::string& value);
typedef enum { READ_MODE, WRITE_MODE } AssignMode;
inline void setAssignMode(AssignMode mode) { this->mode = mode; }
template <class T>
void assign(const std::string& key, T& io_value, const T& defaultValue){
switch(mode){
case READ_MODE:
if(!read(key, io_value)){
io_value = defaultValue;
}
break;
case WRITE_MODE:
write(key, io_value);
break;
}
}
inline iterator begin() { return values.begin(); }
inline iterator end() { return values.end(); }
inline const_iterator begin() const { return values.begin(); }
inline const_iterator end() const { return values.end(); }
void throwKeyNotFoundException(const std::string& key) const;
private:
YamlMapping(const YamlMapping&);
YamlMapping& operator=(const YamlMapping&);
YamlMapping* openMapping(const std::string& key, bool doOverwrite);
YamlMapping* openFlowStyleMapping(const std::string& key, bool doOverwrite);
YamlSequence* openSequence(const std::string& key, bool doOverwrite);
YamlSequence* openFlowStyleSequence(const std::string& key, bool doOverwrite);
inline void insertSub(const std::string& key, YamlNode* node);
void writeSub(const std::string &key, const char* text, size_t length, YamlStringStyle stringStyle);
static bool compareIters(const YamlMapping::const_iterator& it1, const YamlMapping::const_iterator& it2);
Container values;
AssignMode mode;
int indexCounter;
YamlStringStyle keyQuoteStyle;
friend class YamlSequence;
friend class YamlReaderImpl;
friend class YamlWriter;
};
typedef boost::intrusive_ptr<YamlMapping> YamlMappingPtr;
/**
@todo add 'openMapping' and 'openSequence' methods
*/
class CNOID_EXPORT YamlSequence : public YamlCollection
{
typedef std::vector<YamlNodePtr> Container;
public:
YamlSequence();
YamlSequence(int size);
~YamlSequence();
typedef Container::iterator iterator;
typedef Container::const_iterator const_iterator;
inline bool empty() const { return values.empty(); }
inline int size() const { return values.size(); }
void clear();
void reserve(int size);
inline YamlNode& front() const {
return *values.front();
}
inline YamlNode& back() const {
return *values.back();
}
YamlNode* at(int i) const;
inline YamlNode& get(int i) const {
return *values[i];
}
void write(int i, int value);
void write(int i, const std::string& value, YamlStringStyle stringStyle = YAML_PLAIN_STRING);
bool read(int i, bool &out_value) const;
bool read(int i, int &out_value) const;
bool read(int i, double &out_value) const;
inline YamlNode& operator[](int i) const {
return *values[i];
}
YamlMapping* newMapping();
inline void append(YamlNodePtr node) {
values.push_back(node);
}
void append(int value);
/**
@param maxColumns LF is automatically inserted when the column pos is over maxColumsn
@param numValues If numValues is not greater than maxColumns, the initial LF is skipped.
This feature is disabled if numValues = 0.
*/
inline void append(int value, int maxColumns, int numValues = 0) {
insertLF(maxColumns, numValues);
append(value);
}
void append(size_t value);
/**
@param maxColumns LF is automatically inserted when the column pos is over maxColumsn
@param numValues If numValues is not greater than maxColumns, the initial LF is skipped.
This feature is disabled if numValues = 0.
*/
inline void append(size_t value, int maxColumns, int numValues = 0){
insertLF(maxColumns, numValues);
append(value);
}
void append(double value);
/**
@param maxColumns LF is automatically inserted when the column pos is over maxColumsn
@param numValues If numValues is not greater than maxColumns, the initial LF is skipped.
This feature is disabled if numValues = 0.
*/
inline void append(double value, int maxColumns, int numValues = 0) {
insertLF(maxColumns, numValues);
append(value);
}
void append(const std::string& value, YamlStringStyle stringStyle = YAML_PLAIN_STRING);
/**
@param maxColumns LF is automatically inserted when the column pos is over maxColumsn
@param numValues If numValues is not greater than maxColumns, the initial LF is skipped.
This feature is disabled if numValues = 0.
*/
inline void append(const std::string& value, int maxColumns, int numValues = 0, YamlStringStyle stringStyle = YAML_PLAIN_STRING){
insertLF(maxColumns, numValues);
append(value, stringStyle);
}
void appendLF();
inline iterator begin() { return values.begin(); }
inline iterator end() { return values.end(); }
inline const_iterator begin() const { return values.begin(); }
inline const_iterator end() const { return values.end(); };
private:
YamlSequence(int line, int column);
YamlSequence(int line, int column, int reservedSize);
YamlSequence(const YamlSequence&);
YamlSequence& operator=(const YamlSequence&);
void insertLF(int maxColumns, int numValues);
Container values;
friend class YamlMapping;
friend class YamlReaderImpl;
friend class YamlWriter;
};
typedef boost::intrusive_ptr<YamlSequence> YamlSequencePtr;
}
namespace cnoid
{
inline void intrusive_ptr_add_ref(cnoid::YamlNode* obj){
obj->refCounter++;
}
inline void intrusive_ptr_release(cnoid::YamlNode* obj){
obj->refCounter--;
if(obj->refCounter == 0){
delete obj;
}
}
};
#endif
|