Подтвердить что ты не робот

Python-подобные декодеры С++

Существуют ли способы декодирования функций или методов в С++, например, в стиле python?

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

Использование макросов, например:

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

или

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

Возможно ли это?

4b9b3361

Ответ 1

std::function предоставляет большинство строительных блоков для моего предлагаемого решения.

Вот мое предлагаемое решение.

#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);
}

Вывод:

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

PS

Decorator предоставляет место, где вы можете добавить функциональность, помимо вызова функции. Если вам нужен простой проход до std::function, вы можете использовать:

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

Ответ 2

Вот моя попытка. Работает под С++ 14 (общий вывод lambdas и вывод типа возврата).

#include <iostream>
#include <functional>

/* Decorator function example,
   returns negative (! operator) of given function
*/
template <typename T>
auto reverse_func(T func)
{
    auto r_func =
    [=](auto ...args)
    { 
        return !func(args...); 
    };

    return r_func; 
}

/* Decorator function example,
   prints result of given function before it returned
*/
template <typename T>
auto print_result_func(T func)
{
    auto r_func = 
    [=](auto ...args)
    {
        auto result = func(args...);
        std::cout << "Result: " << result << std::endl;
        return result;
    };

    return r_func;
}

/* Function to be decorated example,
   checks whether two given arguments are equal
*/
bool cmp(int x, int y)
{
    return x == y;
}

/* Decorator macro */
#define DECORATE(function, decorator) \
    decorator<decltype(function)>(function)

int main()
{
    auto reversed = DECORATE(cmp, reverse_func);
    auto print_normal = DECORATE(cmp, print_result_func);
    auto print_reversed = DECORATE(reversed, print_result_func);
    auto print_double_normal = DECORATE(print_normal, print_result_func);
    auto print_double_reversed = DECORATE(print_reversed, print_result_func);

    std::cout << cmp(1,2) << reversed(1,2) << std::endl;
    print_double_normal(1,2);
    print_reversed(1,2);
    print_double_reversed(1,2);
}

Ответ 3

Вы можете получить некоторые ограниченные функциональные возможности этого типа, используя оператор предварительной обработки переднего конца ##. См. https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html. Трудность состоит в том, что в C каждое имя функции должно быть определено во время ссылки, поэтому функции не являются объектами, которые могут быть преобразованы, как это делает Python. Так что в декораторах Python полезный и хороший стиль, но в C такие трюки должны использоваться экономно, если вообще.

Ответ 4

Все приведенные выше ответы сложны и используют библиотеки. Мой ответ здесь самый простой и не нуждается в заголовке библиотеки.

    // "DECORATOR.h"
    #pragma once
    #ifndef DECORATOR_H
    #define DECORATOR_H

    template<typename T>
    class deco
    {
        T* m_func;
     public:
        explicit deco(T func);

        template<typename ...args>
        auto operator()(args... Args);
    }
    #endif // DECORATOR_H

Теперь в файле реализации сделайте следующее

   // "DECORATOR.cpp"
   template<typename T>
   inline deco<T>::deco(T func)
   :m_func(func)
   {
   };

   // implementing the function call operator
   template <typename T>
   template <typename ...args>
   auto deco<T>::operator()(args ...Args)
   {
       //Do some stuff defore the decorated function call
       // ....
       // Call the decorated function.
       auto rv = m_func(Args...);

       //Do some stuff after the function call
       // ....
       return rv;
   }

Конец истории. Теперь вот как это использовать в вашем коде.

    // "main.cpp"
    #include "DECORATOR.h"
    #include <stdio.h>  // just for printf()

    // functions to decorate
    int add(int a, int b)
    {
        return a+b;
    };

    int sub(int a, int b)
    {
        return a-b;
    };

    // Main function
    int main()
    {
        // decorate the functions "add", "sub"
        deco<decltype(add)> add_Deco(add);
        deco<decltype(sub)> sub_Deco(sub);

        // call your decorated functions
        printf("result of decorated Add =%d\n", add_Deco(5,2));
        printf("result of decorated Sub =%d\n", sub_Deco(4,3));
        return 0;
    }

Это он Folks!

Плюсы:

  • КЛАСС "deco" имеет только один элемент данных = > малый палец памяти

  • оператор() принимает любое количество аргументов, поэтому вы можете украсить любую функцию независимо от ее количества аргументов.

  • Простая реализация = > простая отладка и тестирование.

Минусы:

  • Неизвестно!