JsonExpression.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #pragma once
  2. #include <Array.h>
  3. #include <Critical.h>
  4. #include <functional>
  5. #include <JSON.h>
  6. #include <Text.h>
  7. #include <Trie.h>
  8. #include "Noise.h"
  9. #include "TypeRegistry.h"
  10. class Chunk;
  11. class JExpressionMemory;
  12. class JExpressionMemory : public virtual Framework::ReferenceCounter
  13. {
  14. private:
  15. Framework::RCTrie<Noise> noises;
  16. Framework::Trie<float> floatVariables;
  17. Framework::Trie<bool> boolVariables;
  18. Chunk* currentChunk;
  19. Framework::Critical cs;
  20. public:
  21. JExpressionMemory();
  22. ~JExpressionMemory();
  23. void lock();
  24. void unlock();
  25. float getNoise(Framework::Text name, float x, float y, float z) const;
  26. void setNoise(Framework::Text name, Noise* noise);
  27. void setCurrentChunk(Chunk* chunk);
  28. float getFloatVariable(Framework::Text name) const;
  29. void setFloatVariable(Framework::Text name, float value);
  30. bool getBoolVariable(Framework::Text name) const;
  31. void setBoolVariable(Framework::Text name, bool value);
  32. Chunk* zCurrentChunk();
  33. };
  34. class JFloatExpression : public virtual Framework::ReferenceCounter
  35. {
  36. private:
  37. public:
  38. JFloatExpression();
  39. virtual float getValue(JExpressionMemory* zMemory) = 0;
  40. };
  41. class JBoolExpression : public virtual Framework::ReferenceCounter
  42. {
  43. private:
  44. public:
  45. JBoolExpression();
  46. virtual bool getValue(JExpressionMemory* zMemory) = 0;
  47. };
  48. class JVariableFloatExpression : public JFloatExpression
  49. {
  50. private:
  51. Framework::Text name;
  52. public:
  53. JVariableFloatExpression();
  54. float getValue(JExpressionMemory* zMemory) override;
  55. void setName(Framework::Text name);
  56. Framework::Text getName() const;
  57. };
  58. class JVariableFloatExpressionFactory
  59. : public SubTypeFactory<JFloatExpression, JVariableFloatExpression>
  60. {
  61. public:
  62. JVariableFloatExpressionFactory();
  63. JVariableFloatExpression* fromJson(
  64. Framework::JSON::JSONObject* zJson) const override;
  65. Framework::JSON::JSONObject* toJsonObject(
  66. JVariableFloatExpression* zObject) const override;
  67. JSONObjectValidationBuilder* addToValidator(
  68. JSONObjectValidationBuilder* builder) const override;
  69. const char* getTypeToken() const override;
  70. };
  71. class JVariableBoolExpression : public JBoolExpression
  72. {
  73. private:
  74. Framework::Text name;
  75. public:
  76. JVariableBoolExpression();
  77. bool getValue(JExpressionMemory* zMemory) override;
  78. void setName(Framework::Text name);
  79. Framework::Text getName() const;
  80. };
  81. class JVariableBoolExpressionFactory
  82. : public SubTypeFactory<JBoolExpression, JVariableBoolExpression>
  83. {
  84. public:
  85. JVariableBoolExpressionFactory();
  86. JVariableBoolExpression* fromJson(
  87. Framework::JSON::JSONObject* zJson) const override;
  88. Framework::JSON::JSONObject* toJsonObject(
  89. JVariableBoolExpression* zObject) const override;
  90. JSONObjectValidationBuilder* addToValidator(
  91. JSONObjectValidationBuilder* builder) const override;
  92. const char* getTypeToken() const override;
  93. };
  94. class JConstantFloatExpression : public JFloatExpression
  95. {
  96. private:
  97. float value;
  98. public:
  99. JConstantFloatExpression();
  100. float getValue(JExpressionMemory* zMemory) override;
  101. void setValue(float value);
  102. float getValue() const;
  103. };
  104. class JConstantFloatExpressionFactory
  105. : public SubTypeFactory<JFloatExpression, JConstantFloatExpression>
  106. {
  107. public:
  108. JConstantFloatExpressionFactory();
  109. JConstantFloatExpression* fromJson(
  110. Framework::JSON::JSONObject* zJson) const override;
  111. Framework::JSON::JSONObject* toJsonObject(
  112. JConstantFloatExpression* zObject) const override;
  113. JSONObjectValidationBuilder* addToValidator(
  114. JSONObjectValidationBuilder* builder) const override;
  115. const char* getTypeToken() const override;
  116. };
  117. class JConstantBoolExpression : public JBoolExpression
  118. {
  119. private:
  120. bool value;
  121. public:
  122. JConstantBoolExpression();
  123. bool getValue(JExpressionMemory* zMemory) override;
  124. void setValue(bool value);
  125. bool getValue() const;
  126. };
  127. class JConstantBoolExpressionFactory
  128. : public SubTypeFactory<JBoolExpression, JConstantBoolExpression>
  129. {
  130. public:
  131. JConstantBoolExpressionFactory();
  132. JConstantBoolExpression* fromJson(
  133. Framework::JSON::JSONObject* zJson) const override;
  134. Framework::JSON::JSONObject* toJsonObject(
  135. JConstantBoolExpression* zObject) const override;
  136. JSONObjectValidationBuilder* addToValidator(
  137. JSONObjectValidationBuilder* builder) const override;
  138. const char* getTypeToken() const override;
  139. };
  140. class JNoiseFloatExpression : public JFloatExpression
  141. {
  142. private:
  143. Framework::Text name;
  144. JFloatExpression* x;
  145. JFloatExpression* y;
  146. JFloatExpression* z;
  147. public:
  148. JNoiseFloatExpression();
  149. ~JNoiseFloatExpression();
  150. float getValue(JExpressionMemory* zMemory) override;
  151. void setName(Framework::Text name);
  152. Framework::Text getName() const;
  153. void setX(JFloatExpression* x);
  154. JFloatExpression* zX() const;
  155. void setY(JFloatExpression* y);
  156. JFloatExpression* zY() const;
  157. void setZ(JFloatExpression* z);
  158. JFloatExpression* zZ() const;
  159. };
  160. class JNoiseFloatExpressionFactory
  161. : public SubTypeFactory<JFloatExpression, JNoiseFloatExpression>
  162. {
  163. public:
  164. JNoiseFloatExpressionFactory();
  165. JNoiseFloatExpression* fromJson(
  166. Framework::JSON::JSONObject* zJson) const override;
  167. Framework::JSON::JSONObject* toJsonObject(
  168. JNoiseFloatExpression* zObject) const override;
  169. JSONObjectValidationBuilder* addToValidator(
  170. JSONObjectValidationBuilder* builder) const override;
  171. const char* getTypeToken() const override;
  172. };
  173. class JOperatorFloatExpression : public JFloatExpression
  174. {
  175. private:
  176. Framework::Text op;
  177. std::function<float(float a, float b)> accumulator;
  178. Framework::RCArray<JFloatExpression> values;
  179. public:
  180. JOperatorFloatExpression();
  181. float getValue(JExpressionMemory* zMemory) override;
  182. void setOperator(
  183. Framework::Text op, std::function<float(float a, float b)> accumulator);
  184. Framework::Text getOperator();
  185. void addValue(JFloatExpression* value);
  186. const Framework::RCArray<JFloatExpression>& getValues() const;
  187. };
  188. class JOperatorFloatExpressionFactory
  189. : public SubTypeFactory<JFloatExpression, JOperatorFloatExpression>
  190. {
  191. public:
  192. JOperatorFloatExpressionFactory();
  193. JOperatorFloatExpression* fromJson(
  194. Framework::JSON::JSONObject* zJson) const override;
  195. Framework::JSON::JSONObject* toJsonObject(
  196. JOperatorFloatExpression* zObject) const override;
  197. JSONObjectValidationBuilder* addToValidator(
  198. JSONObjectValidationBuilder* builder) const override;
  199. const char* getTypeToken() const override;
  200. };
  201. class JBoolOperatorBoolExpression : public JBoolExpression
  202. {
  203. private:
  204. Framework::Text op;
  205. std::function<bool(bool a, bool b)> accumulator;
  206. Framework::RCArray<JBoolExpression> values;
  207. public:
  208. JBoolOperatorBoolExpression();
  209. bool getValue(JExpressionMemory* zMemory) override;
  210. void setOperator(
  211. Framework::Text op, std::function<float(float a, float b)> accumulator);
  212. Framework::Text getOperator();
  213. void addValue(JBoolExpression* value);
  214. const Framework::RCArray<JBoolExpression>& getValues() const;
  215. };
  216. class JBoolOperatorBoolExpressionFactory
  217. : public SubTypeFactory<JBoolExpression, JBoolOperatorBoolExpression>
  218. {
  219. public:
  220. JBoolOperatorBoolExpressionFactory();
  221. JBoolOperatorBoolExpression* fromJson(
  222. Framework::JSON::JSONObject* zJson) const override;
  223. Framework::JSON::JSONObject* toJsonObject(
  224. JBoolOperatorBoolExpression* zObject) const override;
  225. JSONObjectValidationBuilder* addToValidator(
  226. JSONObjectValidationBuilder* builder) const override;
  227. const char* getTypeToken() const override;
  228. };
  229. class JFloatOperatorBoolExpression : public JBoolExpression
  230. {
  231. private:
  232. Framework::Text op;
  233. std::function<bool(float a, float b)> accumulator;
  234. Framework::RCArray<JFloatExpression> values;
  235. public:
  236. JFloatOperatorBoolExpression();
  237. bool getValue(JExpressionMemory* zMemory) override;
  238. void setOperator(
  239. Framework::Text op, std::function<bool(float a, float b)> accumulator);
  240. Framework::Text getOperator();
  241. void addValue(JFloatExpression* value);
  242. const Framework::RCArray<JFloatExpression>& getValues() const;
  243. };
  244. class JFloatOperatorBoolExpressionFactory
  245. : public SubTypeFactory<JBoolExpression, JFloatOperatorBoolExpression>
  246. {
  247. public:
  248. JFloatOperatorBoolExpressionFactory();
  249. JFloatOperatorBoolExpression* fromJson(
  250. Framework::JSON::JSONObject* zJson) const override;
  251. Framework::JSON::JSONObject* toJsonObject(
  252. JFloatOperatorBoolExpression* zObject) const override;
  253. JSONObjectValidationBuilder* addToValidator(
  254. JSONObjectValidationBuilder* builder) const override;
  255. const char* getTypeToken() const override;
  256. };
  257. class JBlockTypeBoolExpression : public JBoolExpression
  258. {
  259. private:
  260. int typeId;
  261. JFloatExpression* x;
  262. JFloatExpression* y;
  263. JFloatExpression* z;
  264. public:
  265. JBlockTypeBoolExpression();
  266. ~JBlockTypeBoolExpression();
  267. bool getValue(JExpressionMemory* zMemory) override;
  268. void setTypeId(int typeId);
  269. int getTypeId() const;
  270. void setX(JFloatExpression* x);
  271. JFloatExpression* zX() const;
  272. void setY(JFloatExpression* y);
  273. JFloatExpression* zY() const;
  274. void setZ(JFloatExpression* z);
  275. JFloatExpression* zZ() const;
  276. };
  277. class JBlockTypeBoolExpressionFactory
  278. : public SubTypeFactory<JBoolExpression, JBlockTypeBoolExpression>
  279. {
  280. public:
  281. JBlockTypeBoolExpressionFactory();
  282. JBlockTypeBoolExpression* fromJson(
  283. Framework::JSON::JSONObject* zJson) const override;
  284. Framework::JSON::JSONObject* toJsonObject(
  285. JBlockTypeBoolExpression* zObject) const override;
  286. JSONObjectValidationBuilder* addToValidator(
  287. JSONObjectValidationBuilder* builder) const override;
  288. const char* getTypeToken() const override;
  289. };