TransformConfig.h 3.1 KB

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