TransformConfig.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include <string>
  5. #include <variant>
  6. #include <iostream>
  7. #include <map>
  8. namespace bell
  9. {
  10. class TransformConfig
  11. {
  12. protected:
  13. int invalidInt = -0x7C;
  14. std::string invalidString = "_invalid";
  15. public:
  16. TransformConfig() = default;
  17. virtual ~TransformConfig() = default;
  18. int currentVolume = 60;
  19. virtual std::string rawGetString(const std::string &field) = 0;
  20. virtual int rawGetInt(const std::string &field) = 0;
  21. virtual bool isArray(const std::string &field) = 0;
  22. virtual float rawGetFloat(const std::string &field) = 0;
  23. virtual std::vector<float> rawGetFloatArray(const std::string &field) = 0;
  24. virtual std::vector<int> rawGetIntArray(const std::string &field) = 0;
  25. typedef std::variant<int, float, std::string> Value;
  26. std::map<std::string, std::vector<Value>> rawValues;
  27. Value getRawValue(const std::string &field)
  28. {
  29. int index = this->currentVolume * (rawValues[field].size()) / 100;
  30. if (index >= rawValues[field].size())
  31. index = rawValues[field].size() - 1;
  32. return rawValues[field][index];
  33. }
  34. std::string getString(const std::string &field, bool isRequired = false, std::string defaultValue = "")
  35. {
  36. if (rawValues.count(field) == 0)
  37. {
  38. rawValues[field] = std::vector<Value>({Value(rawGetString(field))});
  39. }
  40. auto val = std::get<std::string>(getRawValue(field));
  41. if (val == invalidString)
  42. {
  43. if (isRequired)
  44. throw std::invalid_argument("Field " + field + " is required");
  45. else
  46. return defaultValue;
  47. }
  48. else
  49. return val;
  50. }
  51. int getInt(const std::string &field, bool isRequired = false, int defaultValue = 0)
  52. {
  53. if (rawValues.count(field) == 0)
  54. {
  55. if (isArray(field))
  56. {
  57. rawValues[field] = std::vector<Value>();
  58. for (auto f : rawGetIntArray(field))
  59. {
  60. rawValues[field].push_back(f);
  61. }
  62. }
  63. else
  64. {
  65. rawValues[field] = std::vector<Value>({Value(rawGetInt(field))});
  66. }
  67. }
  68. auto val = std::get<int>(getRawValue(field));
  69. if (val == invalidInt)
  70. {
  71. if (isRequired)
  72. throw std::invalid_argument("Field " + field + " is required");
  73. else
  74. return defaultValue;
  75. }
  76. else
  77. return val;
  78. }
  79. float getFloat(const std::string &field, bool isRequired = false, float defaultValue = 0)
  80. {
  81. if (rawValues.count(field) == 0)
  82. {
  83. if (isArray(field))
  84. {
  85. rawValues[field] = std::vector<Value>();
  86. for (auto f : rawGetFloatArray(field))
  87. {
  88. rawValues[field].push_back(f);
  89. }
  90. }
  91. else
  92. {
  93. rawValues[field] = std::vector<Value>({ Value(rawGetFloat(field)) });
  94. }
  95. }
  96. auto val = std::get<float>(getRawValue(field));
  97. if (val == invalidInt)
  98. {
  99. if (isRequired)
  100. throw std::invalid_argument("Field " + field + " is required");
  101. else
  102. return defaultValue;
  103. }
  104. else
  105. return val;
  106. }
  107. std::vector<int> getChannels()
  108. {
  109. auto channel = getInt("channel", false, invalidInt);
  110. if (channel != invalidInt)
  111. {
  112. return std::vector<int>({channel});
  113. }
  114. return rawGetIntArray("channels");
  115. }
  116. };
  117. }