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

ztigrdn8  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(62)

系统一瞥

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

以前的方法

  • 像往常一样,现在宏被定义,对象的创建类/函数之外。它工作得很好。
  • 设置宏值的方式是从一个ini文件,从我的Angular 来看是不可访问的。CMake的设计是到目前为止不可修改的。一切正常
  • 本方法的一个例子:
  • 文件名为math.cpp
#ifndef MATH
#define MATH
#endif
LOG<MATH> math("file_name.txt"); // Constructor of LOG<MATH> requires a file name explicitly
#undef  MATH

class Do_Math
{
  public:
    Do_Math(){}
    virtual ~Do_Math(){}

    void Process()
    {
      math.Write(2); // Write is a function of LOG<MATH> class
    }
};

字符串

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

所需方法/我的目标:

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

  • 现在,在math.cpp文件
#include "log_dbg.h"
class Do_Math : public LOG_DBG
{
  public:
    Do_Math(){}
    virtual ~Do_Math(){}

    void Process()
    {
      math.Write(2); // Write is a function of LOG<MATH> class
    }
};

中包含并继承它

  • 现在,我仍然使用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#

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

#include <iostream>

// do not declare a generic log interface
// you will not know what the log infrastructure will
// need and what its capabilities are/will be (in the future)
class math_reporting_itf
{
public:
    virtual ~math_reporting_itf() = default;
    virtual void report_process(int value) const noexcept = 0; // reporting should never throw!
};

// An implementation of a logger (this one logs to std::cout)
class math_dbg_logger_t :
    public math_reporting_itf
{
public:
    math_dbg_logger_t() // todo open file etc.
    {
    }

    void report_process(int value) const noexcept override
    {
        // write to file, a database, a network logger whatever
        std::cout << "Math Process (" << value << ")\n";
    }
};

class Do_Math 
{
public:

    Do_Math(const math_reporting_itf& reporting) :
        m_reporting{ reporting }
    {
    }

    void Process()
    {
        // math.Write(2); // Write is a function of LOG<MATH> class
        m_reporting.report_process(2);
    }

private:
    const math_reporting_itf& m_reporting;
};

int main()
{
    math_dbg_logger_t logger;
    Do_Math math{logger};
    math.Process();
    return 1;
}

字符串

相关问题