cmake 用作成员变量的模板参数时替换宏值

ztigrdn8  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(280)

系统一瞥

在宏系统声明之后,使用它作为模板参数来示例化一个类的对象。稍后,该模板参数将使用CMake操作转换为0或-1。

以前的方法

  • 像往常一样,现在宏被定义,对象的创建类/函数之外。它工作得很好。
  • 设置宏值的方式是从一个ini文件,从我的Angular 来看是不可访问的。CMake的设计是到目前为止不可修改的。一切正常
  • 本方法的一个例子:
  • 文件名为math.cpp
  1. #ifndef MATH
  2. #define MATH
  3. #endif
  4. LOG<MATH> math("file_name.txt"); // Constructor of LOG<MATH> requires a file name explicitly
  5. #undef MATH
  6. class Do_Math
  7. {
  8. public:
  9. Do_Math(){}
  10. virtual ~Do_Math(){}
  11. void Process()
  12. {
  13. math.Write(2); // Write is a function of LOG<MATH> class
  14. }
  15. };

字符串

  • 在这里,类模板LOG<MATH>只适用于LOG<0>LOG<-1>
  • 要在dbg.ini文件中转换MATH,请将以下内容写入
  1. [math.cpp]
  2. MATH = ON
  • dbg.ini文件的工作方法在专用的CMake文件中定义。

所需方法/我的目标:

  • 目前的方式有一个问题,它不是作为OOP处理,每次使用对象都必须写入单独的文件,它们将被使用
  • 所以,我们的想法是将这些对象作为类成员转移,然后这个新类将被其他需要继承的对象继承。
  • 我已经计划使用宏作为新类的受保护成员来转移对象声明
  • 到目前为止我所做的
  • 创建了一个新类,只声明LOG<MATH>的对象,如下所示
  • 考虑文件名log_dbg.h
  1. class LOG_DBG
  2. {
  3. public:
  4. LOG_DBG():math("file_name.txt"){}
  5. virtual ~LOG_DBG(){}
  6. protected:
  7. #ifndef MATH
  8. #define MATH
  9. #endif
  10. LOG<MATH> math;
  11. #undef MATH
  12. };

  • 现在,在math.cpp文件
  1. #include "log_dbg.h"
  2. class Do_Math : public LOG_DBG
  3. {
  4. public:
  5. Do_Math(){}
  6. virtual ~Do_Math(){}
  7. void Process()
  8. {
  9. math.Write(2); // Write is a function of LOG<MATH> class
  10. }
  11. };

中包含并继承它

  • 现在,我仍然使用dbg.ini文件只改变了文件路径,这是有[log_dbg.h]

问题

  • 现在,我可以编译代码,并且非常确定,main.cpp与对象math也没有问题。但是,现在dbg.ini文件不能将ON放置在宏名称上,这就是为什么math.Write(2);行没有执行。因为,默认的类模板是LOG<-1>,其中所有函数都是未定义的。所以,如果必须工作,类模板应该转换为LOG<0>。从MATH0转换是通过设置宏名称的dbg.ini文件完成的到ONON将使用CMake函数转换为0
  • 我相信,预处理器编译正确,但不相信值的替换是否来自dbg.ini文件。

有没有我做错的步骤?任何建议都是值得赞赏的。

1tu0hz3e

1tu0hz3e1#

这就是我在代码中的意思:

  1. #include <iostream>
  2. // do not declare a generic log interface
  3. // you will not know what the log infrastructure will
  4. // need and what its capabilities are/will be (in the future)
  5. class math_reporting_itf
  6. {
  7. public:
  8. virtual ~math_reporting_itf() = default;
  9. virtual void report_process(int value) const noexcept = 0; // reporting should never throw!
  10. };
  11. // An implementation of a logger (this one logs to std::cout)
  12. class math_dbg_logger_t :
  13. public math_reporting_itf
  14. {
  15. public:
  16. math_dbg_logger_t() // todo open file etc.
  17. {
  18. }
  19. void report_process(int value) const noexcept override
  20. {
  21. // write to file, a database, a network logger whatever
  22. std::cout << "Math Process (" << value << ")\n";
  23. }
  24. };
  25. class Do_Math
  26. {
  27. public:
  28. Do_Math(const math_reporting_itf& reporting) :
  29. m_reporting{ reporting }
  30. {
  31. }
  32. void Process()
  33. {
  34. // math.Write(2); // Write is a function of LOG<MATH> class
  35. m_reporting.report_process(2);
  36. }
  37. private:
  38. const math_reporting_itf& m_reporting;
  39. };
  40. int main()
  41. {
  42. math_dbg_logger_t logger;
  43. Do_Math math{logger};
  44. math.Process();
  45. return 1;
  46. }

字符串

展开查看全部

相关问题