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#define DECLARE_ARGUMENT(X, Y) \
12 struct X : public AmpGen::Argument<Y> { \
13 template <class Z> explicit X(Z val = Z()) : AmpGen::Argument<Y>(val) {} \
14 X() : AmpGen::Argument<Y>() {} \
15 }
16
22 struct IArgument {
23 virtual ~IArgument() = default;
24 };
25
52 template <typename TYPE> struct Argument : public IArgument {
53 template <typename T> explicit Argument(T x) : val(x) {}
54 Argument() = default;
55 operator TYPE() const { return val; }
56 TYPE val = {TYPE()};
57 };
58
66 public:
67 template <typename... ARGS> explicit ArgumentPack(const ARGS &...args) {
68 std::tuple<ARGS...> argTuple(args...);
69 for_each(argTuple, [this](const auto &f) { this->addArgument(f); });
70 }
71 template <typename arg_type> arg_type *get() const {
72 for(const auto &param : m_parameters) {
73 auto ptr = dynamic_cast<arg_type *>(param.get());
74 if(ptr != nullptr) return ptr;
75 }
76 return nullptr;
77 }
78 template <typename arg_type, typename default_arg_type = arg_type> arg_type getArg(const default_arg_type &default_argument = default_arg_type()) const {
79 auto p = get<arg_type>();
80 return p == nullptr ? arg_type(default_argument) : *p;
81 }
82
83 private:
84 std::vector<std::shared_ptr<IArgument>> m_parameters;
85 template <typename T> void addArgument(const T &f) { m_parameters.emplace_back(std::make_shared<T>(f)); }
86 };
87} // namespace AmpGen
88
89#endif
arg_type getArg(const default_arg_type &default_argument=default_arg_type()) const
ArgumentPack(const ARGS &...args)
arg_type * get() const
std::enable_if_t< I==sizeof...(Tp), void > for_each(std::tuple< Tp... > &, FuncT)
Definition MetaUtils.h:36
Argument()=default
Virtual base class for arguments Named arguments to functions (python-style) are given a virtual base...
virtual ~IArgument()=default