JsonExpression.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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* createValue(
  64. Framework::JSON::JSONObject* zJson) const override;
  65. void fromJson(JVariableFloatExpression* zResult,
  66. Framework::JSON::JSONObject* zJson) const override;
  67. void toJson(JVariableFloatExpression* zObject,
  68. Framework::JSON::JSONObject* zResult) const override;
  69. JSONObjectValidationBuilder* addToValidator(
  70. JSONObjectValidationBuilder* builder) const override;
  71. Framework::Text getTypeToken() const override;
  72. };
  73. class JVariableBoolExpression : public JBoolExpression
  74. {
  75. private:
  76. Framework::Text name;
  77. public:
  78. JVariableBoolExpression();
  79. bool getValue(JExpressionMemory* zMemory) override;
  80. void setName(Framework::Text name);
  81. Framework::Text getName() const;
  82. };
  83. class JVariableBoolExpressionFactory
  84. : public SubTypeFactory<JBoolExpression, JVariableBoolExpression>
  85. {
  86. public:
  87. JVariableBoolExpressionFactory();
  88. JVariableBoolExpression* createValue(
  89. Framework::JSON::JSONObject* zJson) const override;
  90. void fromJson(JVariableBoolExpression* zResult,
  91. Framework::JSON::JSONObject* zJson) const override;
  92. void toJson(JVariableBoolExpression* zObject,
  93. Framework::JSON::JSONObject* zResult) const override;
  94. JSONObjectValidationBuilder* addToValidator(
  95. JSONObjectValidationBuilder* builder) const override;
  96. Framework::Text getTypeToken() const override;
  97. };
  98. class JConstantFloatExpression : public JFloatExpression
  99. {
  100. private:
  101. float value;
  102. public:
  103. JConstantFloatExpression();
  104. float getValue(JExpressionMemory* zMemory) override;
  105. void setValue(float value);
  106. float getValue() const;
  107. };
  108. class JConstantFloatExpressionFactory
  109. : public SubTypeFactory<JFloatExpression, JConstantFloatExpression>
  110. {
  111. public:
  112. JConstantFloatExpressionFactory();
  113. JConstantFloatExpression* createValue(
  114. Framework::JSON::JSONObject* zJson) const override;
  115. void fromJson(JConstantFloatExpression* zResult,
  116. Framework::JSON::JSONObject* zJson) const override;
  117. void toJson(JConstantFloatExpression* zObject,
  118. Framework::JSON::JSONObject* zResult) const override;
  119. JSONObjectValidationBuilder* addToValidator(
  120. JSONObjectValidationBuilder* builder) const override;
  121. Framework::Text getTypeToken() const override;
  122. };
  123. class JConstantBoolExpression : public JBoolExpression
  124. {
  125. private:
  126. bool value;
  127. public:
  128. JConstantBoolExpression();
  129. bool getValue(JExpressionMemory* zMemory) override;
  130. void setValue(bool value);
  131. bool getValue() const;
  132. };
  133. class JConstantBoolExpressionFactory
  134. : public SubTypeFactory<JBoolExpression, JConstantBoolExpression>
  135. {
  136. public:
  137. JConstantBoolExpressionFactory();
  138. JConstantBoolExpression* createValue(
  139. Framework::JSON::JSONObject* zJson) const override;
  140. void fromJson(JConstantBoolExpression* zResult,
  141. Framework::JSON::JSONObject* zJson) const override;
  142. void toJson(JConstantBoolExpression* zObject,
  143. Framework::JSON::JSONObject* zResult) const override;
  144. JSONObjectValidationBuilder* addToValidator(
  145. JSONObjectValidationBuilder* builder) const override;
  146. Framework::Text getTypeToken() const override;
  147. };
  148. class JNoiseFloatExpression : public JFloatExpression
  149. {
  150. private:
  151. Framework::Text name;
  152. JFloatExpression* x;
  153. JFloatExpression* y;
  154. JFloatExpression* z;
  155. public:
  156. JNoiseFloatExpression();
  157. ~JNoiseFloatExpression();
  158. float getValue(JExpressionMemory* zMemory) override;
  159. void setName(Framework::Text name);
  160. Framework::Text getName() const;
  161. void setX(JFloatExpression* x);
  162. JFloatExpression* zX() const;
  163. void setY(JFloatExpression* y);
  164. JFloatExpression* zY() const;
  165. void setZ(JFloatExpression* z);
  166. JFloatExpression* zZ() const;
  167. };
  168. class JNoiseFloatExpressionFactory
  169. : public SubTypeFactory<JFloatExpression, JNoiseFloatExpression>
  170. {
  171. public:
  172. JNoiseFloatExpressionFactory();
  173. JNoiseFloatExpression* createValue(
  174. Framework::JSON::JSONObject* zJson) const override;
  175. void fromJson(JNoiseFloatExpression* zResult,
  176. Framework::JSON::JSONObject* zJson) const override;
  177. void toJson(JNoiseFloatExpression* zObject,
  178. Framework::JSON::JSONObject* zResult) const override;
  179. JSONObjectValidationBuilder* addToValidator(
  180. JSONObjectValidationBuilder* builder) const override;
  181. Framework::Text getTypeToken() const override;
  182. };
  183. class JOperatorFloatExpression : public JFloatExpression
  184. {
  185. private:
  186. Framework::Text op;
  187. std::function<float(float a, float b)> accumulator;
  188. Framework::RCArray<JFloatExpression> values;
  189. public:
  190. JOperatorFloatExpression();
  191. float getValue(JExpressionMemory* zMemory) override;
  192. void setOperator(
  193. Framework::Text op, std::function<float(float a, float b)> accumulator);
  194. Framework::Text getOperator();
  195. void addValue(JFloatExpression* value);
  196. const Framework::RCArray<JFloatExpression>& getValues() const;
  197. };
  198. class JOperatorFloatExpressionFactory
  199. : public SubTypeFactory<JFloatExpression, JOperatorFloatExpression>
  200. {
  201. public:
  202. JOperatorFloatExpressionFactory();
  203. JOperatorFloatExpression* createValue(
  204. Framework::JSON::JSONObject* zJson) const override;
  205. void fromJson(JOperatorFloatExpression* zResult,
  206. Framework::JSON::JSONObject* zJson) const override;
  207. void toJson(JOperatorFloatExpression* zObject,
  208. Framework::JSON::JSONObject* zResult) const override;
  209. JSONObjectValidationBuilder* addToValidator(
  210. JSONObjectValidationBuilder* builder) const override;
  211. Framework::Text getTypeToken() const override;
  212. };
  213. class JBoolOperatorBoolExpression : public JBoolExpression
  214. {
  215. private:
  216. Framework::Text op;
  217. std::function<bool(bool a, bool b)> accumulator;
  218. Framework::RCArray<JBoolExpression> values;
  219. public:
  220. JBoolOperatorBoolExpression();
  221. bool getValue(JExpressionMemory* zMemory) override;
  222. void setOperator(
  223. Framework::Text op, std::function<float(float a, float b)> accumulator);
  224. Framework::Text getOperator();
  225. void addValue(JBoolExpression* value);
  226. const Framework::RCArray<JBoolExpression>& getValues() const;
  227. };
  228. class JBoolOperatorBoolExpressionFactory
  229. : public SubTypeFactory<JBoolExpression, JBoolOperatorBoolExpression>
  230. {
  231. public:
  232. JBoolOperatorBoolExpressionFactory();
  233. JBoolOperatorBoolExpression* createValue(
  234. Framework::JSON::JSONObject* zJson) const override;
  235. void fromJson(JBoolOperatorBoolExpression* zResult,
  236. Framework::JSON::JSONObject* zJson) const override;
  237. void toJson(JBoolOperatorBoolExpression* zObject,
  238. Framework::JSON::JSONObject* zResult) const override;
  239. JSONObjectValidationBuilder* addToValidator(
  240. JSONObjectValidationBuilder* builder) const override;
  241. Framework::Text getTypeToken() const override;
  242. };
  243. class JFloatOperatorBoolExpression : public JBoolExpression
  244. {
  245. private:
  246. Framework::Text op;
  247. std::function<bool(float a, float b)> accumulator;
  248. Framework::RCArray<JFloatExpression> values;
  249. public:
  250. JFloatOperatorBoolExpression();
  251. bool getValue(JExpressionMemory* zMemory) override;
  252. void setOperator(
  253. Framework::Text op, std::function<bool(float a, float b)> accumulator);
  254. Framework::Text getOperator();
  255. void addValue(JFloatExpression* value);
  256. const Framework::RCArray<JFloatExpression>& getValues() const;
  257. };
  258. class JFloatOperatorBoolExpressionFactory
  259. : public SubTypeFactory<JBoolExpression, JFloatOperatorBoolExpression>
  260. {
  261. public:
  262. JFloatOperatorBoolExpressionFactory();
  263. JFloatOperatorBoolExpression* createValue(
  264. Framework::JSON::JSONObject* zJson) const override;
  265. void fromJson(JFloatOperatorBoolExpression* zResult,
  266. Framework::JSON::JSONObject* zJson) const override;
  267. void toJson(JFloatOperatorBoolExpression* zObject,
  268. Framework::JSON::JSONObject* zResult) const override;
  269. JSONObjectValidationBuilder* addToValidator(
  270. JSONObjectValidationBuilder* builder) const override;
  271. Framework::Text getTypeToken() const override;
  272. };
  273. class JBlockTypeBoolExpression : public JBoolExpression
  274. {
  275. private:
  276. int typeId;
  277. JFloatExpression* x;
  278. JFloatExpression* y;
  279. JFloatExpression* z;
  280. public:
  281. JBlockTypeBoolExpression();
  282. ~JBlockTypeBoolExpression();
  283. bool getValue(JExpressionMemory* zMemory) override;
  284. void setTypeId(int typeId);
  285. int getTypeId() const;
  286. void setX(JFloatExpression* x);
  287. JFloatExpression* zX() const;
  288. void setY(JFloatExpression* y);
  289. JFloatExpression* zY() const;
  290. void setZ(JFloatExpression* z);
  291. JFloatExpression* zZ() const;
  292. };
  293. class JBlockTypeBoolExpressionFactory
  294. : public SubTypeFactory<JBoolExpression, JBlockTypeBoolExpression>
  295. {
  296. public:
  297. JBlockTypeBoolExpressionFactory();
  298. JBlockTypeBoolExpression* createValue(
  299. Framework::JSON::JSONObject* zJson) const override;
  300. void fromJson(JBlockTypeBoolExpression* zResult,
  301. Framework::JSON::JSONObject* zJson) const override;
  302. void toJson(JBlockTypeBoolExpression* zObject,
  303. Framework::JSON::JSONObject* zResult) const override;
  304. JSONObjectValidationBuilder* addToValidator(
  305. JSONObjectValidationBuilder* builder) const override;
  306. Framework::Text getTypeToken() const override;
  307. };