value-traits 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // file : xml/value-traits -*- C++ -*-
  2. // copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
  3. // license : MIT; see accompanying LICENSE file
  4. #ifndef XML_VALUE_TRAITS
  5. #define XML_VALUE_TRAITS
  6. #include <xml/details/pre.hxx>
  7. #include <string>
  8. #include <cstddef> // std::size_t
  9. #include <xml/forward>
  10. #include <xml/details/export.hxx>
  11. namespace xml
  12. {
  13. template <typename T>
  14. struct default_value_traits
  15. {
  16. static T
  17. parse (std::string, const parser&);
  18. static std::string
  19. serialize (const T&, const serializer&);
  20. };
  21. template <>
  22. struct LIBSTUDXML_EXPORT default_value_traits<bool>
  23. {
  24. static bool
  25. parse (std::string, const parser&);
  26. static std::string
  27. serialize (bool v, const serializer&)
  28. {
  29. return v ? "true" : "false";
  30. }
  31. };
  32. template <>
  33. struct LIBSTUDXML_EXPORT default_value_traits<std::string>
  34. {
  35. static std::string
  36. parse (std::string s, const parser&)
  37. {
  38. return s;
  39. }
  40. static std::string
  41. serialize (const std::string& v, const serializer&)
  42. {
  43. return v;
  44. }
  45. };
  46. template <typename T>
  47. struct value_traits: default_value_traits<T> {};
  48. template <typename T, std::size_t N>
  49. struct value_traits<T[N]>: default_value_traits<const T*> {};
  50. }
  51. #include <xml/value-traits.txx>
  52. #include <xml/details/post.hxx>
  53. #endif // XML_VALUE_TRAITS