专门用于日志记录的Functor
Specializing a Functor for logging
我一直潜伏在这里,试图弄清楚Functor是否能做我需要它做的事情。
我想做的是包装对类方法的调用,并以某种方式捕获函数返回的值。考虑到我的Functor类,我需要做什么才能将我的评论转化为代码:
template < typename Func >
class MySpecializedFunctor
{
Func t;
MyObject& p;
public:
MyFunctor( MyObject &obj, Func f )
{
p = obj;
t = f;
}
void runFunc( ... )
{
// Can I use an ellipsis '...' to pass values into t->xxxx() ???
// Assume the first param is always time, the others are never the same
bool b = p->t( time( NULL ), /* first value of ... */, /* second value of ... */ );
if ( !b )
{
// log error here
}
}
}
因为这是一个函数,所以被包装的函数可能有n个参数。
这可能吗?
编辑:我不能使用C++0X。
使用可变模板:
template <typename... Args>
void runFunc(Args&&... args)
{
bool b = p->t(time(NULL), std::forward<Args>(args)...);
if ( !b )
{
// log error here
}
}
或者重载runFunc,如果您的编译器不支持可变模板或完美转发:
// this one support 3 const arguments, you will need to overload
// all variations of arguments count and constness
template <typename Arg1, typename Arg2, typename Arg3>
void runFunc(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
{
bool b = p->t(time(NULL), arg1, arg2, arg3);
if ( !b )
{
// log error here
}
}
相关文章:
- 将两个数组中的差异记录在第三个数组中
- 禁止在控制台上记录谷神星
- 创建 Spdlog 异步文件记录器时遇到困难
- fastrtps:如何在发布/订阅级别使用 DDS 历史记录?
- 如何将自定义记录器与websocketpp一起使用?
- C++结构到德尔福记录dll调用
- 如何判断SSL_read是否已经接收并处理了来自单个消息的所有记录
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- 我是否可以使用 win32 句柄以编程方式记录发送到/接收到 USB/COM 的内容
- 使用 Doxygen 记录枚举类值,而不启用EXTRACT_ALL
- 在C++中隐藏键盘记录器的控制台窗口
- 堆栈上的最大激活记录数
- 记录Doxygen中的命名
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 在SQLITE数据库中写入记录需要花费大量时间.如何提高刀片操作效率?
- Qt 错误:QSqlQuery::value:尝试从表中检索统计信息时未定位在有效记录上 (QComboBox)
- 麻烦得到提升::记录器编译
- C++ 中混合二进制/文本日志记录的最佳做法
- 修改链表中的记录C++
- 专门用于日志记录的Functor