code.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef AFUN_CODE_HPP
  2. #define AFUN_CODE_HPP
  3. #include "tool.hpp"
  4. #include "aFunCoreExport.h"
  5. #include "core.hpp"
  6. namespace aFuncore {
  7. class Code {
  8. CodeType type;
  9. char prefix=NUL;
  10. union {
  11. char *element = nullptr; // union 内不使用 std::string
  12. struct { // NOLINT 不需要初始化
  13. BlockType block_type;
  14. Code *son;
  15. };
  16. };
  17. Code *father = nullptr;;
  18. Code *next = nullptr;;
  19. Code *prev = nullptr;;
  20. aFuntool::FileLine line;
  21. aFuntool::FilePath file;
  22. public:
  23. AFUN_CORE_EXPORT explicit Code(FileLine line, ConstFilePath file="");
  24. AFUN_CORE_EXPORT Code (const std::string &element, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
  25. AFUN_CORE_EXPORT Code (BlockType block_type, Code *son, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
  26. AFUN_CORE_EXPORT ~Code();
  27. AFUN_CORE_EXPORT Code *connect(Code *code);
  28. AFUN_CORE_EXPORT void destructAll();
  29. AFUN_CORE_EXPORT void display() const;
  30. AFUN_CORE_EXPORT void displayAll() const;
  31. AFUN_CORE_EXPORT bool write_v1(FILE *f, bool debug=false) const;
  32. AFUN_CORE_EXPORT bool writeAll_v1(FILE *f, bool debug=false) const;
  33. AFUN_CORE_EXPORT Code *read_v1(FILE *f, bool debug=false, int8_t read_type=code_element, bool to_son=false);
  34. AFUN_CORE_EXPORT bool readAll_v1(FILE *f, bool debug=false);
  35. [[nodiscard]] AFUN_CORE_EXPORT std::string getMD5_v1() const;
  36. [[nodiscard]] AFUN_CORE_EXPORT std::string getMD5All_v1() const;
  37. AFUN_CORE_EXPORT bool writeByteCode(ConstFilePath file_path, bool debug=false) const; // NOLINT 允许忽略返回值
  38. AFUN_CORE_EXPORT bool readByteCode(ConstFilePath file_path);
  39. [[nodiscard]] CodeType getType() const {return type;}
  40. [[nodiscard]] char getPrefix() const {return prefix;}
  41. [[nodiscard]] const char *getElement() const {if (type != code_element) return ""; return element;}
  42. [[nodiscard]] BlockType getBlockType() const {if (type != code_block) return block_p; return block_type;}
  43. [[nodiscard]] Code *getSon() const {if (type != code_block) return nullptr; return son;}
  44. [[nodiscard]] Code *toNext() const {return next;}
  45. [[nodiscard]] Code *toPrev() const {return prev;}
  46. [[nodiscard]] Code *toFather() const {return father;}
  47. [[nodiscard]] aFuntool::FileLine getFileLine() const {return line;}
  48. [[nodiscard]] aFuntool::FilePath getFilePath() const {return file;}
  49. };
  50. }
  51. #endif //AFUN_CODE_HPP