【发布时间】:2019-07-08 09:43:05
【问题描述】:
上下文:
不使用堆的嵌入式 c++。
我想掌握我的代码(包括它的大小),所以我不想使用标准库,例如 std::function。
第一种方法:
让我们使用CRTP的修改版本来举这个例子(这是我的代码的简化版本):
注意:我的回调方法可能有这两个签名:bool (ChildCrtp::*)(void); 和 void (ChildCrtp::*)(int)(一个用于操作,一个用于条件)。
#include <iostream>
#include <stdint.h>
using namespace std;
void* operator new(size_t size)
{
cout << "ERROR HEAP USED" << endl;
}
template <typename FunctionType = void, typename... ArgumentType>
class GenericCallback
{
public:
virtual ~GenericCallback(){}
virtual FunctionType Execute(ArgumentType... arg) = 0; //!< execute callback
virtual bool IsValid() const = 0; //!< check if callback is valid
};
template <typename ObjectType, typename FunctionType = void, typename... ArgumentType>
class Callback : public GenericCallback<FunctionType, ArgumentType...>
{
public:
Callback() ://!< Default constructor
pObject_m(0),
pFunction_m(0)
{
}
Callback(ObjectType* pObject_m, FunctionType(ObjectType::*pFunction_m)(ArgumentType...))//!< Constructor
{
this->pObject_m = pObject_m;
this->pFunction_m = pFunction_m;
}
virtual FunctionType Execute(ArgumentType... arg)//!< execute callback implementation
{
return (pObject_m->*pFunction_m)(arg...);
}
virtual bool IsValid(void) const//!< callback validity check implementation
{
return (pObject_m != 0) && (pFunction_m != 0);
}
private:
ObjectType* pObject_m; //!< pointer to object where the callback is defined
FunctionType(ObjectType::* pFunction_m)(ArgumentType...); //!< pointer to the callback (function-member) of the object
};
template<typename ChildCrtp>
class Interface
{
public:
using FooSpecificCallback = Callback<ChildCrtp, bool>;
virtual int getValue(void) = 0;
bool IsPositive() { return (getValue() > 0); };
bool IsNegative(void) { return (getValue() < 0); };
bool IsEven(void) { return ((getValue() % 2) == 0); };
bool IsOdd(void) { return ((getValue() % 2) == 1); };
FooSpecificCallback isPositive_ = FooSpecificCallback(static_cast<ChildCrtp*>(this), &Interface::IsPositive);//line to be removed
FooSpecificCallback isNegative_ = FooSpecificCallback(static_cast<ChildCrtp*>(this), &Interface::IsNegative);//line to be removed
FooSpecificCallback isEven_ = FooSpecificCallback(static_cast<ChildCrtp*>(this), &Interface::IsEven);//line to be removed
FooSpecificCallback isOdd_ = FooSpecificCallback(static_cast<ChildCrtp*>(this), &Interface::IsOdd);//line to be removed
};
class Mother
{
public:
using FooGenericCallback = GenericCallback<bool>* ;
int getValue(){return x_;};
void storeCallback(FooGenericCallback pCallback){pCallback_ = pCallback;};
bool callCallback(){return (pCallback_->IsValid() == false)?:pCallback_->Execute();};
private:
int x_ = 3;
FooGenericCallback pCallback_;
};
class Child : public Mother, public Interface<Child>
{
public:
int getValue(){return Mother::getValue();}
void setup(void){storeCallback(&isPositive_);}
};
int main()
{
Child c;
c.setup();
cout << std::boolalpha << "Is " << c.getValue() << " positive? " << c.callCallback() << endl;
return 0;
}
这种设计有几个问题:
- 回调对象被存储两次
- 接口具有非函数成员属性:回调。
- 写一个lib很痛苦,因为你需要写方法和回调,而且你必须在所有使用你的回调的类中定义它!
- 可能不适合使用 CRTP。为什么我使用 CRTP?见[这里]。(How to define a template specific type that can be inherited?)
解决方案?
这可能吗?
我在正确的轨道上吗?如果不是,什么是正确的工具?
我在谷歌上搜索了几首曲目,但仍然不知道该怎么做:
1) 使用模板类型定义
不知道怎么做
2) 用作模板参数
我知道将函数作为模板参数传递是possible/valid
但是我的尝试没有成功:
#include <iostream>
#include <stdint.h>
using namespace std;
void* operator new(size_t size)
{
cout << "ERROR HEAP USED" << endl;
}
template <typename FunctionType = void, typename... ArgumentType>
class GenericCallback
{
public:
virtual ~GenericCallback(){}
virtual FunctionType Execute(ArgumentType... arg) = 0; //!< execute callback
virtual bool IsValid() const = 0; //!< check if callback is valid
};
template <typename ObjectType, typename FunctionType = void, typename... ArgumentType>
class Callback : public GenericCallback<FunctionType, ArgumentType...>
{
public:
Callback() ://!< Default constructor
pObject_m(0),
pFunction_m(0)
{
}
Callback(ObjectType* pObject_m, FunctionType(ObjectType::*pFunction_m)(ArgumentType...))//!< Constructor
{
this->pObject_m = pObject_m;
this->pFunction_m = pFunction_m;
}
virtual FunctionType Execute(ArgumentType... arg)//!< execute callback implementation
{
return (pObject_m->*pFunction_m)(arg...);
}
virtual bool IsValid(void) const//!< callback validity check implementation
{
return (pObject_m != 0) && (pFunction_m != 0);
}
private:
ObjectType* pObject_m; //!< pointer to object where the callback is defined
FunctionType(ObjectType::* pFunction_m)(ArgumentType...); //!< pointer to the callback (function-member) of the object
};
template<typename ChildCrtp>
class Interface
{
public:
using FooSpecificCallback = Callback<ChildCrtp, bool>;
using FooPrototype = bool(Interface::*)();
template<FooPrototype op>
FooSpecificCallback* checkIf(void)
{
//I'm trying to take the address of this temporary object, which is not legal in C++.
return &FooSpecificCallback(static_cast<ChildCrtp*>(this), op);
}
virtual int getValue(void) = 0;
bool IsNegative() { return (getValue() < 0); };
};
class Mother
{
public:
using FooGenericCallback = GenericCallback<bool>*;
int getValue(){return x_;};
void storeCallback(FooGenericCallback pCallback){pCallback_ = pCallback;};
bool callCallback(){return (pCallback_->IsValid() == false)?:pCallback_->Execute();};
private:
int x_ = 3;
FooGenericCallback pCallback_;
};
class Child : public Mother, public Interface<Child>
{
public:
int getValue(){return Mother::getValue();}
void setup(void){storeCallback(checkIf<&Child::IsNegative>());}
};
int main()
{
Child c;
c.setup();
cout << std::boolalpha << "expectFalse: " << c.callCallback() << endl;
return 0;
}
我收到以下错误
error: taking address of temporary [-fpermissive]
由于不能获取临时对象的地址,这在 C++ 中是不合法的。
这个回调接口的问题是它需要一个指针来存储对象“FooGenericCallback”,它不能是“FooSpecificCallback”,因为对象类型在母类中是未知的。
3) 将回调实现为接口的其他方式
how to implement callback as an interface
但解决方案仍然使用对象将函数成员存储在接口(或接口的子接口)中。
4) Lambda...
我知道 lambdas 会简化我的生活,事实上我首先使用 lambdas 并且代码大小从 60kB 翻倍到 120kB(!),因为 lambdas 的存储方式:在 std::function 中。答案应该不是“lambda”:)
【问题讨论】:
-
PS 很好,PSS 可以接受,但是 PSSS 和 PSSSS 吓到我了。将附加信息合并到文本中而不是单独提供它们可能会更好。
-
同意,进行编辑。
-
哎呀,显然我不清楚,你把我的句子按字面意思理解了......我的意思是编辑不必在最后进行,而不是两个 PS 应该被保留......但无论如何它并不那么重要:)
-
我不明白你想达到什么目的。什么是
Callback模板,为什么不能使用std::mem_fn之类的模板? -
我不想使用std库有两个主要原因:1/我想掌握我正在编写的代码,所以制作我自己的lib,2/不想依赖std:function 因为那是我的代码在使用 lambdas 时加倍的原因。
标签: c++ templates interface callback embedded