类Python的C ++装饰器 [英] Python-like C++ decorators

查看:177
本文介绍了类Python的C ++装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @decorator 
def装饰

有没有方法用C ++来装饰函数或方法? (self,* args,** kwargs):
pass

使用宏例如:

  DECORATE(decorator_method)
int修饰(int a,float b = 0)
{
返回0;
}

  DECORATOR_MACRO 
void decorated(mytype& a,mytype2 * b)
{
}

是否有可能?

解决方案

std :: function 为我提出的解决方案提供了大部分构建模块。

这是我提出的解决方案。

  #include<的iostream> 
#include< functional>

// -------------------------------
// BEGIN装饰器实现
// -------------------------------

template< class> struct Decorator;

模板< class R,class ... Args>
struct Decorator< R(Args ...)>
{
Decorator(std :: function< R(Args ...)> f):f_(f){}

R operator()(Args .. 。args)
{
std :: cout<< 调用装饰的功能。
返回f_(args ...);
}
std :: function< R(Args ...)> F_;
};

模板< class R,class ... Args>
Decorator< R(Args ...)> makeDecorator(R(* f)(Args ...))
{
返回装饰器< R(Args ...)>(std :: function< R(Args ...)> (F));
}

// -------------------------------
// END装饰器实现
// -------------------------------

// -------------------------------
//示例函数来装饰。
// -------------------------------

//建议的解决方案doesn没有使用默认值。
// int decorated1(int a,float b = 0)
int decorated1(int a,float b)
{
std :: cout<< a =<< a<< ,b =<< b<的std :: ENDL;
返回0;
}

void decorated2(int a)
{
std :: cout<< a =<< a<<的std :: ENDL;
}

int main()
{
auto method1 = makeDecorator(decorated1);
method1(10,30.3);
auto method2 = makeDecorator(decorated2);
method2(10);
}

输出:

 调用装饰函数。 
a = 10,b = 30.3
调用装饰函数。
a = 10

PS $ b

Decorator 提供了一个可以添加除函数调用之外的功能的地方。如果你想简单地传递 std :: function ,你可以使用:

  template< class R,class ... Args> 
std :: function< R(Args ...)> makeDecorator(R(* f)(Args ...))
{
return std :: function< R(Args ...)>(f);
}


Are there ways to decorate functions or methods in C++ like in python style?

@decorator
def decorated(self, *args, **kwargs):
     pass

Using macros for example:

DECORATE(decorator_method)
int decorated(int a, float b = 0)
{
    return 0;
}

or

DECORATOR_MACRO
void decorated(mytype& a, mytype2* b)
{
}

Is it possible?

解决方案

std::function provides most of the building blocks for my proposed solution.

Here is my proposed solution.

#include <iostream>
#include <functional>

//-------------------------------
// BEGIN decorator implementation
//-------------------------------

template <class> struct Decorator;

template <class R, class... Args>
struct Decorator<R(Args ...)>
{
   Decorator(std::function<R(Args ...)> f) : f_(f) {}

   R operator()(Args ... args)
   {
      std::cout << "Calling the decorated function.\n";
      return f_(args...);
   }
   std::function<R(Args ...)> f_;
};

template<class R, class... Args>
Decorator<R(Args...)> makeDecorator(R (*f)(Args ...))
{
   return Decorator<R(Args...)>(std::function<R(Args...)>(f));
}

//-------------------------------
// END decorator implementation
//-------------------------------

//-------------------------------
// Sample functions to decorate.
//-------------------------------

// Proposed solution doesn't work with default values.
// int decorated1(int a, float b = 0)
int decorated1(int a, float b)
{
   std::cout << "a = " << a << ", b = " << b << std::endl;
   return 0;
}

void decorated2(int a)
{
   std::cout << "a = " << a << std::endl;
}

int main()
{
   auto method1 = makeDecorator(decorated1);
   method1(10, 30.3);
   auto method2 = makeDecorator(decorated2);
   method2(10);
}

Output:

Calling the decorated function.
a = 10, b = 30.3
Calling the decorated function.
a = 10

PS

Decorator provides a place where you can add functionality beyond making the function call. If you want a simple pass through to std::function, you can use:

template<class R, class... Args >
std::function<R(Args...)> makeDecorator(R (*f)(Args ...))
{
   return std::function<R(Args...)>(f);
}

这篇关于类Python的C ++装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆