code.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef AFUN_CODE_H
  2. #define AFUN_CODE_H
  3. #include "aFuntool.h"
  4. #include "aFunCoreExport.h"
  5. namespace aFuncore {
  6. class AFUN_CORE_EXPORT Code {
  7. public:
  8. typedef enum CodeType {
  9. code_start = 0,
  10. code_element = 1,
  11. code_block = 2,
  12. } CodeType;
  13. typedef enum BlockType {
  14. block_p = '(',
  15. block_b = '[',
  16. block_c = '{',
  17. } BlockType;
  18. Code(const Code &)=delete;
  19. Code &operator=(const Code &)=delete;
  20. static Code *create(aFuntool::FileLine line, aFuntool::ConstFilePath file="");
  21. static Code *create(const std::string &element,
  22. aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=aFuntool::NUL);
  23. static Code *create(BlockType block_type, Code *son,
  24. aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=aFuntool::NUL);
  25. static void destruct(Code *code);
  26. Code *connect(Code *code);
  27. void display() const;
  28. void displayAll() const;
  29. bool write_v1(FILE *f, bool debug=false) const;
  30. bool writeAll_v1(FILE *f, bool debug=false) const;
  31. Code *read_v1(FILE *f, bool debug=false, int8_t read_type=code_element, bool to_son=false);
  32. bool readAll_v1(FILE *f, bool debug=false);
  33. [[nodiscard]] std::string getMD5_v1() const;
  34. [[nodiscard]] std::string getMD5All_v1() const;
  35. bool writeByteCode(aFuntool::ConstFilePath file_path, bool debug=false) const; // NOLINT 允许忽略返回值
  36. bool readByteCode(aFuntool::ConstFilePath file_path);
  37. [[nodiscard]] CodeType getType() const;
  38. [[nodiscard]] char getPrefix() const;
  39. [[nodiscard]] const char *getElement() const;
  40. [[nodiscard]] BlockType getBlockType() const;
  41. [[nodiscard]] Code *getSon() const;
  42. [[nodiscard]] Code *toNext() const;
  43. [[nodiscard]] Code *toPrev() const;
  44. [[nodiscard]] Code *toFather() const;
  45. [[nodiscard]] aFuntool::FileLine getFileLine() const;
  46. [[nodiscard]] aFuntool::FilePath getFilePath() const;
  47. protected:
  48. explicit Code(aFuntool::FileLine line, aFuntool::ConstFilePath file="");
  49. Code(const std::string &element, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=aFuntool::NUL);
  50. Code(BlockType block_type, Code *son, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=aFuntool::NUL);
  51. ~Code();
  52. private:
  53. CodeType type;
  54. char prefix=aFuntool::NUL;
  55. union {
  56. char *element = nullptr; // union 内不使用 std::string
  57. struct { // NOLINT 不需要初始化
  58. BlockType block_type;
  59. Code *son;
  60. };
  61. };
  62. Code *father = nullptr;;
  63. Code *next = nullptr;;
  64. Code *prev = nullptr;;
  65. aFuntool::FileLine line;
  66. aFuntool::FilePath file;
  67. };
  68. }
  69. #include "code.inline.h"
  70. #endif //AFUN_CODE_H