var.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef AFUN_VAR_H
  2. #define AFUN_VAR_H
  3. #include "aFuntool.h"
  4. #include "aFunCoreExport.h"
  5. #include "core.h"
  6. #include "gc.h"
  7. #include <list>
  8. namespace aFuncore {
  9. class AFUN_CORE_EXPORT Var : public GcObject<class Var> {
  10. Object *data;
  11. public:
  12. Inter *const inter;
  13. Var(Object *data_, Inter *inter_);
  14. ~Var() override = default;
  15. [[nodiscard]] virtual Object *getData();
  16. virtual void setData(Object *data_);
  17. };
  18. class AFUN_CORE_EXPORT VarSpace : public GcObject<class VarSpace> {
  19. public:
  20. static const size_t VAR_HASH_SIZE = 100; // 环境变量哈希表大小
  21. private:
  22. struct VarCup {
  23. std::string name;
  24. Var *var;
  25. VarCup *next=nullptr;
  26. };
  27. size_t count;
  28. VarCup *var[VAR_HASH_SIZE];
  29. public:
  30. Inter *const inter;
  31. explicit VarSpace(Inter *inter_);
  32. ~VarSpace() override;
  33. template <typename Callable,typename...T>
  34. void forEach(Callable func, T...arg);
  35. [[nodiscard]] size_t getCount() const;
  36. [[nodiscard]] virtual Var *findVar(const std::string &name);
  37. virtual VarOperationFlat defineVar(const std::string &name, Object *data);
  38. virtual VarOperationFlat defineVar(const std::string &name, Var *data);
  39. virtual VarOperationFlat setVar(const std::string &name, Object *data);
  40. virtual VarOperationFlat delVar(const std::string &name);
  41. [[nodiscard]] Object *findObject(const std::string &name);
  42. };
  43. class AFUN_CORE_EXPORT ProtectVarSpace : public VarSpace {
  44. bool is_protect;
  45. public:
  46. explicit ProtectVarSpace(Inter *inter_);
  47. [[nodiscard]] bool getProtect() const;
  48. bool setProtect(bool protect);
  49. VarOperationFlat defineVar(const std::string &name, Object *data) override;
  50. VarOperationFlat defineVar(const std::string &name, Var *data) override;
  51. VarOperationFlat setVar(const std::string &name, Object *data) override;
  52. VarOperationFlat delVar(const std::string &name) override;
  53. };
  54. class AFUN_CORE_EXPORT VarList {
  55. std::list<VarSpace *> varspace;
  56. public:
  57. explicit VarList() = default;
  58. explicit VarList(VarList *varlist);
  59. explicit VarList(VarSpace *varspace);
  60. ~VarList() = default;
  61. VarList(const VarList &) = delete;
  62. VarList &operator=(const VarList &) = delete;
  63. void connect(VarList *varlist);
  64. void push(VarSpace *varspace_);
  65. template <typename Callable,typename...T>
  66. void forEach(Callable func, T...arg);
  67. [[nodiscard]] virtual Var *findVar(const std::string &name);
  68. virtual bool defineVar(const std::string &name, Object *data);
  69. virtual bool defineVar(const std::string &name, Var *data);
  70. virtual bool setVar(const std::string &name, Object *data);
  71. virtual bool delVar(const std::string &name);
  72. [[nodiscard]] Object *findObject(const std::string &name);
  73. };
  74. }
  75. #include "var.inline.h"
  76. #include "var.template.h"
  77. #endif //AFUN_VAR_H