Choreonoid  1.8
YAMLWriter.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_UTIL_YAML_WRITER_H
6 #define CNOID_UTIL_YAML_WRITER_H
7 
8 #include "ValueTree.h"
9 #include <string>
10 #include "exportdecl.h"
11 
12 namespace cnoid {
13 
14 class YAMLWriterImpl;
15 
16 class CNOID_EXPORT YAMLWriter
17 {
18 public:
19  YAMLWriter();
20  YAMLWriter(const std::string filename);
21  YAMLWriter(std::ostream& os);
22  ~YAMLWriter();
23 
24  void setOutput(std::ostream& os);
25  void flush();
26  bool openFile(const std::string& filename);
27  bool isFileOpen();
28  void closeFile();
29 
30  void setMessageSink(std::ostream& os);
31  void putMessage(const std::string& message);
32  std::ostream& messageSink();
33 
34  void putNode(const ValueNode* node);
35 
36  void setIndentWidth(int n);
37  int indentWidth() const;
38  void setKeyOrderPreservationMode(bool on);
39 
40  void startDocument();
41 
42  void putComment(const std::string& comment, bool doNewLine = true);
43 
44  void putString(const char* value);
45  void putString(const std::string& value);
46  void putSingleQuotedString(const char* value);
47  void putSingleQuotedString(const std::string& value);
48  void putDoubleQuotedString(const char* value);
49  void putDoubleQuotedString(const std::string& value);
50  void putBlockStyleString(const char* value, bool isLiteral);
51  void putBlockStyleString(const std::string& value, bool isLiteral);
52  void putLiteralString(const char* value) { putBlockStyleString(value, true); }
53  void putLiteralString(const std::string& value) { putBlockStyleString(value, true); }
54  void putFoldedString(const char* value) { putBlockStyleString(value, false); }
55  void putFoldedString(const std::string& value) { putBlockStyleString(value, false); }
56  void putScalar(bool value);
57  void putScalar(int value);
58  void putScalar(double value);
59  void putScalar(const char* value) { putString(value); }
60  void putScalar(const std::string& value){ putString(value); }
61  void setDoubleFormat(const char* format);
62 
63  void startMapping();
64  void startFlowStyleMapping();
65  void putKey(const char* key, StringStyle style = PLAIN_STRING);
66  void putKey(const std::string& key, StringStyle style = PLAIN_STRING);
67 
68  template <class DataType> void putKeyValue(const char* key, const DataType& value){
69  putKey(key);
70  putScalar(value);
71  }
72 
73  template <class DataType> void putKeyValue(const std::string& key, const DataType& value){
74  putKey(key);
75  putScalar(value);
76  }
77 
78  void endMapping();
79 
80  void startListing();
81  void startFlowStyleListing();
82  void endListing();
83 
84  const Mapping* info() const;
85  Mapping* info();
86 
87  template<typename T> T info(const std::string& key) const;
88  template<typename T> T info(const std::string& key, const T& defaultValue) const;
89  template<typename T> T getOrCreateInfo(const std::string& key, const T& defaultValue);
90  template<typename T> void setInfo(const std::string& key, const T& value);
91 
92  void resetInfo(Mapping* info);
93 
94 #ifdef CNOID_BACKWARD_COMPATIBILITY
95  void startSequence() { startListing(); }
96  void startFlowStyleSequence() { startFlowStyleListing(); }
97  void endSequence() { endListing(); }
98 #endif
99 
100 private:
101  YAMLWriterImpl* impl;
102 };
103 
104 template<> CNOID_EXPORT double YAMLWriter::info(const std::string& key) const;
105 template<> CNOID_EXPORT double YAMLWriter::info(const std::string& key, const double& defaultValue) const;
106 template<> CNOID_EXPORT bool YAMLWriter::info(const std::string& key, const bool& defaultValue) const;
107 template<> CNOID_EXPORT double YAMLWriter::getOrCreateInfo(const std::string& key, const double& defaultValue);
108 template<> CNOID_EXPORT bool YAMLWriter::getOrCreateInfo(const std::string& key, const bool& defaultValue);
109 template<> CNOID_EXPORT void YAMLWriter::setInfo(const std::string& key, const double& value);
110 template<> CNOID_EXPORT void YAMLWriter::setInfo(const std::string& key, const bool& value);
111 
112 #ifdef CNOID_BACKWARD_COMPATIBILITY
113 typedef YAMLWriter YamlWriter;
114 #endif
115 
116 }
117 
118 #endif
cnoid::YAMLWriter::setInfo
void setInfo(const std::string &key, const T &value)
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::YAMLWriter
Definition: YAMLWriter.h:16
cnoid::YAMLWriter::putKeyValue
void putKeyValue(const char *key, const DataType &value)
Definition: YAMLWriter.h:68
cnoid::ValueNode
Definition: ValueTree.h:34
cnoid::YAMLWriter::putFoldedString
void putFoldedString(const char *value)
Definition: YAMLWriter.h:54
ValueTree.h
cnoid::YAMLWriter::putFoldedString
void putFoldedString(const std::string &value)
Definition: YAMLWriter.h:55
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::StringStyle
StringStyle
Definition: ValueTree.h:24
cnoid::YAMLWriter::putScalar
void putScalar(const std::string &value)
Definition: YAMLWriter.h:60
cnoid::YAMLWriter::getOrCreateInfo
T getOrCreateInfo(const std::string &key, const T &defaultValue)
cnoid::YAMLWriter::putLiteralString
void putLiteralString(const std::string &value)
Definition: YAMLWriter.h:53
cnoid::YAMLWriter::putScalar
void putScalar(const char *value)
Definition: YAMLWriter.h:59
cnoid::PLAIN_STRING
@ PLAIN_STRING
Definition: ValueTree.h:24
cnoid::YAMLWriter::info
const Mapping * info() const
Definition: YAMLWriter.cpp:794
cnoid::YAMLWriter::putKeyValue
void putKeyValue(const std::string &key, const DataType &value)
Definition: YAMLWriter.h:73
cnoid::YAMLWriter::putLiteralString
void putLiteralString(const char *value)
Definition: YAMLWriter.h:52