AmpGen 2.1
Loading...
Searching...
No Matches
ArgumentPack.h
Go to the documentation of this file.
1#ifndef AMPGEN_ARGUMENTPACK_H
2#define AMPGEN_ARGUMENTPACK_H
3
4#include "AmpGen/MetaUtils.h"
5#include <iostream>
6#include <memory>
7#include <string>
8#include <vector>
9
10namespace AmpGen
11{
12 #define DECLARE_ARGUMENT(X, Y) \
13 struct X : public AmpGen::Argument<Y> { \
14 template<class Z> \
15 explicit X(Z val = Z()) : AmpGen::Argument<Y>(val){} \
16 X() : AmpGen::Argument<Y>(){} \
17 }
18
24 struct IArgument {
25 virtual ~IArgument() = default;
26 };
27
54 template <typename TYPE>
55 struct Argument : public IArgument
56 {
57 template <typename T>
58 explicit Argument( T x ) : val(x) {}
59 Argument() = default;
60 operator TYPE() const { return val; }
61 TYPE val = { TYPE() };
62 };
63
71 {
72 public:
73 template <typename... ARGS>
74 explicit ArgumentPack( const ARGS&... args )
75 {
76 std::tuple<ARGS...> argTuple( args... );
77 for_each(argTuple, [this](const auto& f){ this->addArgument(f) ; } );
78 }
79 template <typename arg_type> arg_type* get() const
80 {
81 for( const auto& param : m_parameters )
82 {
83 auto ptr = dynamic_cast<arg_type*>(param.get());
84 if( ptr != nullptr ) return ptr;
85 }
86 return nullptr;
87 }
88 template <typename arg_type, typename default_arg_type=arg_type>
89 arg_type getArg( const default_arg_type& default_argument = default_arg_type() ) const
90 {
91 auto p = get<arg_type>();
92 return p == nullptr ? arg_type(default_argument) : *p;
93 }
94 private:
95 std::vector<std::shared_ptr<IArgument>> m_parameters;
96 template <typename T> void addArgument( const T& f ){ m_parameters.emplace_back( std::make_shared<T>(f) ) ; }
97 };
98} // namespace AmpGen
99
100#endif
arg_type getArg(const default_arg_type &default_argument=default_arg_type()) const
arg_type * get() const
ArgumentPack(const ARGS &... args)
std::enable_if_t< I==sizeof...(Tp), void > for_each(std::tuple< Tp... > &, FuncT)
Definition MetaUtils.h:39
Argument()=default
Virtual base class for arguments Named arguments to functions (python-style) are given a virtual base...
virtual ~IArgument()=default