PropertyTree.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <boost/property_tree/ptree.hpp>
  2. #include "anki/misc/PropertyTree.h"
  3. #include "anki/util/Exception.h"
  4. namespace anki {
  5. namespace PropertyTree {
  6. //==============================================================================
  7. // getBool =
  8. //==============================================================================
  9. bool getBool(const boost::property_tree::ptree& pt, const char* tag)
  10. {
  11. std::string str = pt.get<std::string>(tag);
  12. if(str == "true")
  13. {
  14. return true;
  15. }
  16. else if(str == "false")
  17. {
  18. return false;
  19. }
  20. else
  21. {
  22. throw ANKI_EXCEPTION("Expected true or false for tag " + tag +
  23. " and not " + str);
  24. }
  25. }
  26. //==============================================================================
  27. // getBoolOptional =
  28. //==============================================================================
  29. extern boost::optional<bool> getBoolOptional(
  30. const boost::property_tree::ptree& pt, const char* tag)
  31. {
  32. boost::optional<std::string> str = pt.get_optional<std::string>(tag);
  33. if(str)
  34. {
  35. if(str.get() == "true")
  36. {
  37. return boost::optional<bool>(true);
  38. }
  39. else if(str.get() == "false")
  40. {
  41. return boost::optional<bool>(true);
  42. }
  43. else
  44. {
  45. throw ANKI_EXCEPTION("Expected true or false for tag " + tag +
  46. " and not " + str.get());
  47. }
  48. }
  49. return boost::optional<bool>();
  50. }
  51. //==============================================================================
  52. // getFloat =
  53. //==============================================================================
  54. float getFloat(const boost::property_tree::ptree& pt)
  55. {
  56. return pt.get<float>("float");
  57. }
  58. //==============================================================================
  59. // getVec2 =
  60. //==============================================================================
  61. Vec2 getVec2(const boost::property_tree::ptree& pt)
  62. {
  63. const boost::property_tree::ptree& tree = pt.get_child("vec2");
  64. return Vec2(tree.get<float>("x"), tree.get<float>("y"));
  65. }
  66. //==============================================================================
  67. // getVec3 =
  68. //==============================================================================
  69. Vec3 getVec3(const boost::property_tree::ptree& pt)
  70. {
  71. const boost::property_tree::ptree& tree = pt.get_child("vec3");
  72. return Vec3(
  73. tree.get<float>("x"),
  74. tree.get<float>("y"),
  75. tree.get<float>("z"));
  76. }
  77. //==============================================================================
  78. // getVec4 =
  79. //==============================================================================
  80. Vec4 getVec4(const boost::property_tree::ptree& pt)
  81. {
  82. const boost::property_tree::ptree& tree = pt.get_child("vec4");
  83. return Vec4(
  84. tree.get<float>("x"),
  85. tree.get<float>("y"),
  86. tree.get<float>("z"),
  87. tree.get<float>("w"));
  88. }
  89. } // end namespace
  90. } // end namespace