code.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef AFUN_CODE_H
  2. #define AFUN_CODE_H
  3. #include "aFuntool.h"
  4. #include "aFunCoreExport.h"
  5. #include "core.h"
  6. namespace aFuncore {
  7. AFUN_CORE_EXPORT 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. protected:
  23. explicit Code(FileLine line, ConstFilePath file="");
  24. Code (const std::string &element, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
  25. Code (BlockType block_type, Code *son, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
  26. ~Code();
  27. public:
  28. Code(const Code &)=delete;
  29. Code &operator=(const Code &)=delete;
  30. static Code *create(FileLine line, ConstFilePath file="") {
  31. return new Code(line, file);
  32. }
  33. static Code *create(const std::string &element,
  34. aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL) {
  35. return new Code(element, line, file, prefix);
  36. }
  37. static Code *create(BlockType block_type, Code *son,
  38. aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL) {
  39. return new Code(block_type, son, line, file);
  40. }
  41. static void destruct(Code *code);
  42. Code *connect(Code *code);
  43. void display() const;
  44. void displayAll() const;
  45. bool write_v1(FILE *f, bool debug=false) const;
  46. bool writeAll_v1(FILE *f, bool debug=false) const;
  47. Code *read_v1(FILE *f, bool debug=false, int8_t read_type=code_element, bool to_son=false);
  48. bool readAll_v1(FILE *f, bool debug=false);
  49. [[nodiscard]] std::string getMD5_v1() const;
  50. [[nodiscard]] std::string getMD5All_v1() const;
  51. bool writeByteCode(ConstFilePath file_path, bool debug=false) const; // NOLINT 允许忽略返回值
  52. bool readByteCode(ConstFilePath file_path);
  53. [[nodiscard]] CodeType getType() const {return type;}
  54. [[nodiscard]] char getPrefix() const {return prefix;}
  55. [[nodiscard]] const char *getElement() const {if (type != code_element) return ""; return element;}
  56. [[nodiscard]] BlockType getBlockType() const {if (type != code_block) return block_p; return block_type;}
  57. [[nodiscard]] Code *getSon() const {if (type != code_block) return nullptr; return son;}
  58. [[nodiscard]] Code *toNext() const {return next;}
  59. [[nodiscard]] Code *toPrev() const {return prev;}
  60. [[nodiscard]] Code *toFather() const {return father;}
  61. [[nodiscard]] aFuntool::FileLine getFileLine() const {return line;}
  62. [[nodiscard]] aFuntool::FilePath getFilePath() const {return file;}
  63. };
  64. }
  65. #endif //AFUN_CODE_H