convert.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #ifndef NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include <array>
  9. #include <limits>
  10. #include <list>
  11. #include <map>
  12. #include <sstream>
  13. #include <vector>
  14. #include "yaml-cpp/binary.h"
  15. #include "yaml-cpp/node/impl.h"
  16. #include "yaml-cpp/node/iterator.h"
  17. #include "yaml-cpp/node/node.h"
  18. #include "yaml-cpp/node/type.h"
  19. #include "yaml-cpp/null.h"
  20. namespace YAML {
  21. class Binary;
  22. struct _Null;
  23. template <typename T>
  24. struct convert;
  25. } // namespace YAML
  26. namespace YAML {
  27. namespace conversion {
  28. inline bool IsInfinity(const std::string& input) {
  29. return input == ".inf" || input == ".Inf" || input == ".INF" ||
  30. input == "+.inf" || input == "+.Inf" || input == "+.INF";
  31. }
  32. inline bool IsNegativeInfinity(const std::string& input) {
  33. return input == "-.inf" || input == "-.Inf" || input == "-.INF";
  34. }
  35. inline bool IsNaN(const std::string& input) {
  36. return input == ".nan" || input == ".NaN" || input == ".NAN";
  37. }
  38. }
  39. // Node
  40. template <>
  41. struct convert<Node> {
  42. static Node encode(const Node& rhs) { return rhs; }
  43. static bool decode(const Node& node, Node& rhs) {
  44. rhs.reset(node);
  45. return true;
  46. }
  47. };
  48. // std::string
  49. template <>
  50. struct convert<std::string> {
  51. static Node encode(const std::string& rhs) { return Node(rhs); }
  52. static bool decode(const Node& node, std::string& rhs) {
  53. if (!node.IsScalar())
  54. return false;
  55. rhs = node.Scalar();
  56. return true;
  57. }
  58. };
  59. // C-strings can only be encoded
  60. template <>
  61. struct convert<const char*> {
  62. static Node encode(const char*& rhs) { return Node(rhs); }
  63. };
  64. template <std::size_t N>
  65. struct convert<const char[N]> {
  66. static Node encode(const char(&rhs)[N]) { return Node(rhs); }
  67. };
  68. template <>
  69. struct convert<_Null> {
  70. static Node encode(const _Null& /* rhs */) { return Node(); }
  71. static bool decode(const Node& node, _Null& /* rhs */) {
  72. return node.IsNull();
  73. }
  74. };
  75. #define YAML_DEFINE_CONVERT_STREAMABLE(type, negative_op) \
  76. template <> \
  77. struct convert<type> { \
  78. static Node encode(const type& rhs) { \
  79. std::stringstream stream; \
  80. stream.precision(std::numeric_limits<type>::digits10 + 1); \
  81. stream << rhs; \
  82. return Node(stream.str()); \
  83. } \
  84. \
  85. static bool decode(const Node& node, type& rhs) { \
  86. if (node.Type() != NodeType::Scalar) \
  87. return false; \
  88. const std::string& input = node.Scalar(); \
  89. std::stringstream stream(input); \
  90. stream.unsetf(std::ios::dec); \
  91. if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) \
  92. return true; \
  93. if (std::numeric_limits<type>::has_infinity) { \
  94. if (conversion::IsInfinity(input)) { \
  95. rhs = std::numeric_limits<type>::infinity(); \
  96. return true; \
  97. } else if (conversion::IsNegativeInfinity(input)) { \
  98. rhs = negative_op std::numeric_limits<type>::infinity(); \
  99. return true; \
  100. } \
  101. } \
  102. \
  103. if (std::numeric_limits<type>::has_quiet_NaN && \
  104. conversion::IsNaN(input)) { \
  105. rhs = std::numeric_limits<type>::quiet_NaN(); \
  106. return true; \
  107. } \
  108. \
  109. return false; \
  110. } \
  111. }
  112. #define YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(type) \
  113. YAML_DEFINE_CONVERT_STREAMABLE(type, -)
  114. #define YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(type) \
  115. YAML_DEFINE_CONVERT_STREAMABLE(type, +)
  116. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(int);
  117. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(short);
  118. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long);
  119. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long long);
  120. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned);
  121. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned short);
  122. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned long);
  123. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned long long);
  124. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(char);
  125. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(signed char);
  126. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned char);
  127. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(float);
  128. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(double);
  129. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long double);
  130. #undef YAML_DEFINE_CONVERT_STREAMABLE_SIGNED
  131. #undef YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED
  132. #undef YAML_DEFINE_CONVERT_STREAMABLE
  133. // bool
  134. template <>
  135. struct convert<bool> {
  136. static Node encode(bool rhs) { return rhs ? Node("true") : Node("false"); }
  137. YAML_CPP_API static bool decode(const Node& node, bool& rhs);
  138. };
  139. // std::map
  140. template <typename K, typename V>
  141. struct convert<std::map<K, V>> {
  142. static Node encode(const std::map<K, V>& rhs) {
  143. Node node(NodeType::Map);
  144. for (typename std::map<K, V>::const_iterator it = rhs.begin();
  145. it != rhs.end(); ++it)
  146. node.force_insert(it->first, it->second);
  147. return node;
  148. }
  149. static bool decode(const Node& node, std::map<K, V>& rhs) {
  150. if (!node.IsMap())
  151. return false;
  152. rhs.clear();
  153. for (const_iterator it = node.begin(); it != node.end(); ++it)
  154. #if defined(__GNUC__) && __GNUC__ < 4
  155. // workaround for GCC 3:
  156. rhs[it->first.template as<K>()] = it->second.template as<V>();
  157. #else
  158. rhs[it->first.as<K>()] = it->second.as<V>();
  159. #endif
  160. return true;
  161. }
  162. };
  163. // std::vector
  164. template <typename T>
  165. struct convert<std::vector<T>> {
  166. static Node encode(const std::vector<T>& rhs) {
  167. Node node(NodeType::Sequence);
  168. for (typename std::vector<T>::const_iterator it = rhs.begin();
  169. it != rhs.end(); ++it)
  170. node.push_back(*it);
  171. return node;
  172. }
  173. static bool decode(const Node& node, std::vector<T>& rhs) {
  174. if (!node.IsSequence())
  175. return false;
  176. rhs.clear();
  177. for (const_iterator it = node.begin(); it != node.end(); ++it)
  178. #if defined(__GNUC__) && __GNUC__ < 4
  179. // workaround for GCC 3:
  180. rhs.push_back(it->template as<T>());
  181. #else
  182. rhs.push_back(it->as<T>());
  183. #endif
  184. return true;
  185. }
  186. };
  187. // std::list
  188. template <typename T>
  189. struct convert<std::list<T>> {
  190. static Node encode(const std::list<T>& rhs) {
  191. Node node(NodeType::Sequence);
  192. for (typename std::list<T>::const_iterator it = rhs.begin();
  193. it != rhs.end(); ++it)
  194. node.push_back(*it);
  195. return node;
  196. }
  197. static bool decode(const Node& node, std::list<T>& rhs) {
  198. if (!node.IsSequence())
  199. return false;
  200. rhs.clear();
  201. for (const_iterator it = node.begin(); it != node.end(); ++it)
  202. #if defined(__GNUC__) && __GNUC__ < 4
  203. // workaround for GCC 3:
  204. rhs.push_back(it->template as<T>());
  205. #else
  206. rhs.push_back(it->as<T>());
  207. #endif
  208. return true;
  209. }
  210. };
  211. // std::array
  212. template <typename T, std::size_t N>
  213. struct convert<std::array<T, N>> {
  214. static Node encode(const std::array<T, N>& rhs) {
  215. Node node(NodeType::Sequence);
  216. for (const auto& element : rhs) {
  217. node.push_back(element);
  218. }
  219. return node;
  220. }
  221. static bool decode(const Node& node, std::array<T, N>& rhs) {
  222. if (!isNodeValid(node)) {
  223. return false;
  224. }
  225. for (auto i = 0u; i < node.size(); ++i) {
  226. #if defined(__GNUC__) && __GNUC__ < 4
  227. // workaround for GCC 3:
  228. rhs[i] = node[i].template as<T>();
  229. #else
  230. rhs[i] = node[i].as<T>();
  231. #endif
  232. }
  233. return true;
  234. }
  235. private:
  236. static bool isNodeValid(const Node& node) {
  237. return node.IsSequence() && node.size() == N;
  238. }
  239. };
  240. // std::pair
  241. template <typename T, typename U>
  242. struct convert<std::pair<T, U>> {
  243. static Node encode(const std::pair<T, U>& rhs) {
  244. Node node(NodeType::Sequence);
  245. node.push_back(rhs.first);
  246. node.push_back(rhs.second);
  247. return node;
  248. }
  249. static bool decode(const Node& node, std::pair<T, U>& rhs) {
  250. if (!node.IsSequence())
  251. return false;
  252. if (node.size() != 2)
  253. return false;
  254. #if defined(__GNUC__) && __GNUC__ < 4
  255. // workaround for GCC 3:
  256. rhs.first = node[0].template as<T>();
  257. #else
  258. rhs.first = node[0].as<T>();
  259. #endif
  260. #if defined(__GNUC__) && __GNUC__ < 4
  261. // workaround for GCC 3:
  262. rhs.second = node[1].template as<U>();
  263. #else
  264. rhs.second = node[1].as<U>();
  265. #endif
  266. return true;
  267. }
  268. };
  269. // binary
  270. template <>
  271. struct convert<Binary> {
  272. static Node encode(const Binary& rhs) {
  273. return Node(EncodeBase64(rhs.data(), rhs.size()));
  274. }
  275. static bool decode(const Node& node, Binary& rhs) {
  276. if (!node.IsScalar())
  277. return false;
  278. std::vector<unsigned char> data = DecodeBase64(node.Scalar());
  279. if (data.empty() && !node.Scalar().empty())
  280. return false;
  281. rhs.swap(data);
  282. return true;
  283. }
  284. };
  285. }
  286. #endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66