convert.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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>::max_digits10); \
  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. } \
  89. const std::string& input = node.Scalar(); \
  90. std::stringstream stream(input); \
  91. stream.unsetf(std::ios::dec); \
  92. if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) { \
  93. return true; \
  94. } \
  95. if (std::numeric_limits<type>::has_infinity) { \
  96. if (conversion::IsInfinity(input)) { \
  97. rhs = std::numeric_limits<type>::infinity(); \
  98. return true; \
  99. } else if (conversion::IsNegativeInfinity(input)) { \
  100. rhs = negative_op std::numeric_limits<type>::infinity(); \
  101. return true; \
  102. } \
  103. } \
  104. \
  105. if (std::numeric_limits<type>::has_quiet_NaN) { \
  106. if (conversion::IsNaN(input)) { \
  107. rhs = std::numeric_limits<type>::quiet_NaN(); \
  108. return true; \
  109. } \
  110. } \
  111. \
  112. return false; \
  113. } \
  114. }
  115. #define YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(type) \
  116. YAML_DEFINE_CONVERT_STREAMABLE(type, -)
  117. #define YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(type) \
  118. YAML_DEFINE_CONVERT_STREAMABLE(type, +)
  119. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(int);
  120. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(short);
  121. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long);
  122. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long long);
  123. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned);
  124. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned short);
  125. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned long);
  126. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned long long);
  127. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(char);
  128. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(signed char);
  129. YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(unsigned char);
  130. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(float);
  131. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(double);
  132. YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(long double);
  133. #undef YAML_DEFINE_CONVERT_STREAMABLE_SIGNED
  134. #undef YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED
  135. #undef YAML_DEFINE_CONVERT_STREAMABLE
  136. // bool
  137. template <>
  138. struct convert<bool> {
  139. static Node encode(bool rhs) { return rhs ? Node("true") : Node("false"); }
  140. YAML_CPP_API static bool decode(const Node& node, bool& rhs);
  141. };
  142. // std::map
  143. template <typename K, typename V>
  144. struct convert<std::map<K, V>> {
  145. static Node encode(const std::map<K, V>& rhs) {
  146. Node node(NodeType::Map);
  147. for (typename std::map<K, V>::const_iterator it = rhs.begin();
  148. it != rhs.end(); ++it)
  149. node.force_insert(it->first, it->second);
  150. return node;
  151. }
  152. static bool decode(const Node& node, std::map<K, V>& rhs) {
  153. if (!node.IsMap())
  154. return false;
  155. rhs.clear();
  156. for (const_iterator it = node.begin(); it != node.end(); ++it)
  157. #if defined(__GNUC__) && __GNUC__ < 4
  158. // workaround for GCC 3:
  159. rhs[it->first.template as<K>()] = it->second.template as<V>();
  160. #else
  161. rhs[it->first.as<K>()] = it->second.as<V>();
  162. #endif
  163. return true;
  164. }
  165. };
  166. // std::vector
  167. template <typename T>
  168. struct convert<std::vector<T>> {
  169. static Node encode(const std::vector<T>& rhs) {
  170. Node node(NodeType::Sequence);
  171. for (typename std::vector<T>::const_iterator it = rhs.begin();
  172. it != rhs.end(); ++it)
  173. node.push_back(*it);
  174. return node;
  175. }
  176. static bool decode(const Node& node, std::vector<T>& rhs) {
  177. if (!node.IsSequence())
  178. return false;
  179. rhs.clear();
  180. for (const_iterator it = node.begin(); it != node.end(); ++it)
  181. #if defined(__GNUC__) && __GNUC__ < 4
  182. // workaround for GCC 3:
  183. rhs.push_back(it->template as<T>());
  184. #else
  185. rhs.push_back(it->as<T>());
  186. #endif
  187. return true;
  188. }
  189. };
  190. // std::list
  191. template <typename T>
  192. struct convert<std::list<T>> {
  193. static Node encode(const std::list<T>& rhs) {
  194. Node node(NodeType::Sequence);
  195. for (typename std::list<T>::const_iterator it = rhs.begin();
  196. it != rhs.end(); ++it)
  197. node.push_back(*it);
  198. return node;
  199. }
  200. static bool decode(const Node& node, std::list<T>& rhs) {
  201. if (!node.IsSequence())
  202. return false;
  203. rhs.clear();
  204. for (const_iterator it = node.begin(); it != node.end(); ++it)
  205. #if defined(__GNUC__) && __GNUC__ < 4
  206. // workaround for GCC 3:
  207. rhs.push_back(it->template as<T>());
  208. #else
  209. rhs.push_back(it->as<T>());
  210. #endif
  211. return true;
  212. }
  213. };
  214. // std::array
  215. template <typename T, std::size_t N>
  216. struct convert<std::array<T, N>> {
  217. static Node encode(const std::array<T, N>& rhs) {
  218. Node node(NodeType::Sequence);
  219. for (const auto& element : rhs) {
  220. node.push_back(element);
  221. }
  222. return node;
  223. }
  224. static bool decode(const Node& node, std::array<T, N>& rhs) {
  225. if (!isNodeValid(node)) {
  226. return false;
  227. }
  228. for (auto i = 0u; i < node.size(); ++i) {
  229. #if defined(__GNUC__) && __GNUC__ < 4
  230. // workaround for GCC 3:
  231. rhs[i] = node[i].template as<T>();
  232. #else
  233. rhs[i] = node[i].as<T>();
  234. #endif
  235. }
  236. return true;
  237. }
  238. private:
  239. static bool isNodeValid(const Node& node) {
  240. return node.IsSequence() && node.size() == N;
  241. }
  242. };
  243. // std::pair
  244. template <typename T, typename U>
  245. struct convert<std::pair<T, U>> {
  246. static Node encode(const std::pair<T, U>& rhs) {
  247. Node node(NodeType::Sequence);
  248. node.push_back(rhs.first);
  249. node.push_back(rhs.second);
  250. return node;
  251. }
  252. static bool decode(const Node& node, std::pair<T, U>& rhs) {
  253. if (!node.IsSequence())
  254. return false;
  255. if (node.size() != 2)
  256. return false;
  257. #if defined(__GNUC__) && __GNUC__ < 4
  258. // workaround for GCC 3:
  259. rhs.first = node[0].template as<T>();
  260. #else
  261. rhs.first = node[0].as<T>();
  262. #endif
  263. #if defined(__GNUC__) && __GNUC__ < 4
  264. // workaround for GCC 3:
  265. rhs.second = node[1].template as<U>();
  266. #else
  267. rhs.second = node[1].as<U>();
  268. #endif
  269. return true;
  270. }
  271. };
  272. // binary
  273. template <>
  274. struct convert<Binary> {
  275. static Node encode(const Binary& rhs) {
  276. return Node(EncodeBase64(rhs.data(), rhs.size()));
  277. }
  278. static bool decode(const Node& node, Binary& rhs) {
  279. if (!node.IsScalar())
  280. return false;
  281. std::vector<unsigned char> data = DecodeBase64(node.Scalar());
  282. if (data.empty() && !node.Scalar().empty())
  283. return false;
  284. rhs.swap(data);
  285. return true;
  286. }
  287. };
  288. }
  289. #endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66