node_visitor_base.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef NODE_VISITOR_BASE_H
  2. #define NODE_VISITOR_BASE_H
  3. #include "../variables/variable.h"
  4. #include "../utils/path.h"
  5. #include "../utils/query.h"
  6. #include <memory>
  7. #include <vector>
  8. #include <functional>
  9. #include <optional>
  10. namespace binom {
  11. enum class VisitorType : ui8 {
  12. ram_storage_visitor = 0,
  13. file_storage_visitor = 1
  14. };
  15. class NodeVisitorBase {
  16. public:
  17. static constexpr ui64 find_all = ui64(-1);
  18. virtual ~NodeVisitorBase() = default;
  19. virtual VarType getType() const = 0;
  20. virtual VisitorType getVisitorType() const = 0;
  21. VarTypeClass getTypeClass() const {return toTypeClass(getType());}
  22. virtual bool isNull() const = 0;
  23. bool isInvalid() const {return getTypeClass() == VarTypeClass::invalid_type;}
  24. bool isPrimitive() const {return getTypeClass() == VarTypeClass::primitive;}
  25. bool isBufferArray() const {return getTypeClass() == VarTypeClass::buffer_array;}
  26. bool isArray() const {return getTypeClass() == VarTypeClass::array;}
  27. bool isObject() const {return getTypeClass() == VarTypeClass::object;}
  28. bool isIterable() const {return !isPrimitive();}
  29. virtual bool isValueRef() const = 0;
  30. virtual ui64 getElementCount() const = 0;
  31. virtual NodeVisitorBase& stepInside(ui64 index) = 0;
  32. virtual NodeVisitorBase& stepInside(BufferArray name) = 0;
  33. virtual NodeVisitorBase& stepInside(Path path) = 0;
  34. virtual bool contains(ui64 index) = 0;
  35. virtual bool contains(BufferArray name) = 0;
  36. virtual bool contains(Path path) = 0;
  37. virtual void setVariable(Variable var) = 0;
  38. virtual void setVariable(ui64 index, Variable var) = 0;
  39. virtual void setVariable(BufferArray name, Variable var) = 0;
  40. virtual void setVariable(Path path, Variable var) = 0;
  41. virtual void pushBack(Variable var) = 0;
  42. virtual void pushFront(Variable var) = 0;
  43. virtual void insert(ui64 index, Variable var) = 0;
  44. virtual void insert(BufferArray name, Variable var) = 0;
  45. virtual void remove(ui64 index, ui64 count = 1) = 0;
  46. virtual void remove(BufferArray name) = 0;
  47. virtual void remove(Path path) = 0;
  48. virtual NodeVisitorBase& operator()(ui64 index) = 0;
  49. virtual NodeVisitorBase& operator()(BufferArray name) = 0;
  50. virtual NodeVisitorBase& operator()(Path path) = 0;
  51. Variable getVariableClone();
  52. NodeVisitor& toRAMVisitor() {
  53. if(getVisitorType() != VisitorType::ram_storage_visitor)
  54. throw Exception(ErrCode::binom_invalid_visitor);
  55. return *reinterpret_cast<NodeVisitor*>(this);
  56. }
  57. FileNodeVisitor& toFileVisitor() {
  58. if(getVisitorType() != VisitorType::file_storage_visitor)
  59. throw Exception(ErrCode::binom_invalid_visitor);
  60. return *reinterpret_cast<FileNodeVisitor*>(this);
  61. }
  62. virtual operator Variable() = 0;
  63. NodeVisitorBase& upCast() {return *reinterpret_cast<NodeVisitorBase*>(this);}
  64. };
  65. typedef std::vector<std::unique_ptr<NodeVisitorBase>> NodeVector;
  66. class TestExpressionFrame {
  67. QRel last_rel = QRel::AND;
  68. bool last_value = true;
  69. void invalidTest();
  70. void testUNumber(ui64 node_number, Query::QueryEpression& field);
  71. void testNumber(i64 node_number, Query::QueryEpression& field);;
  72. void testString(BufferArray node_string, Query::QueryEpression& field);;
  73. bool getRelationResult(bool last, QRel rel, bool current);
  74. public:
  75. void operator()(Query::QueryEpression expr, FileNodeVisitor node, const ui64 index);
  76. void operator()(Query::QueryEpression expr, NodeVisitor node, const ui64 index);
  77. bool getValue() const {return last_value;}
  78. };
  79. }
  80. #endif // NODE_VISITOR_BASE_H