tudocomp
– The TU Dortmund Compression Framework
CreateAlgorithm.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/Env.hpp>
4 #include <tudocomp/Registry.hpp>
5 #include <tudocomp/Algorithm.hpp>
7 
8 namespace tdc {
9 
10 template<typename T, typename registry_root_t = T>
11 class Builder {
12  Registry<registry_root_t> m_registry;
13  std::string m_options;
14 public:
15  inline Builder() {}
16 
18  inline Builder& options(const std::string& options) {
19  m_options = options;
20  return *this;
21  }
22 
23 
26  m_registry = reg;
27  return *this;
28  }
29 
31  inline Env env() {
32  Meta meta = T::meta();
33  auto fixed_static_args = std::move(meta).build_static_args_ast_value();
34 
35  auto padded_options = meta.name() + "(" + m_options + ")";
36  auto meta_type = meta.type();
37 
38  eval::AlgorithmTypes types = m_registry.algorithm_map();
39  gather_types(types, {
40  std::move(meta)
41  });
42 
43  ast::Parser p { padded_options };
44 
45  auto evald_algo = eval::cl_eval(
46  p.parse_value(),
47  meta_type,
48  types,
49  std::move(fixed_static_args)
50  );
51 
52  auto evaluated_options = evald_algo.as_algorithm();
53 
54  auto env_root = std::make_shared<EnvRoot>(std::move(evaluated_options));
55  Env env(env_root, env_root->algo_value());
56 
57  return env;
58  }
59 
65  template<typename... Args>
66  inline T instance(Args&&... args) {
67  return T(env(), std::forward<Args>(args)...);
68  }
69 };
70 
82 template<typename T, typename registry_root_t = T>
85 }
86 
87 
89 inline std::unique_ptr<Compressor> create_algo_with_registry_dynamic(
91  const AlgorithmValue& algorithm_value) {
92  return registry.select_algorithm(algorithm_value);
93 }
94 
95 template<typename T, typename registry_root_t, typename... Args>
96 T create_algo_with_registry(const std::string& options,
97  const Registry<registry_root_t>& registry,
98  Args&&... args) {
99  return builder<T, registry_root_t>()
100  .registry(registry)
101  .options(options)
102  .instance(std::forward<Args>(args)...);
103 }
105 
117 template<class T, class... Args>
118 T create_algo(const std::string& options, Args&&... args) {
119  return create_algo_with_registry<T>(options, Registry<T>(), std::forward<Args>(args)...);
120 }
121 
130 template<class T>
132  return create_algo<T>("");
133 }
134 
144 inline Env create_env(Meta&& meta, const std::string& options = "") {
145  auto fixed_static_args = std::move(meta).build_static_args_ast_value();
146 
147  auto padded_options = meta.name() + "(" + options + ")";
148  auto meta_type = meta.type();
149 
150  eval::AlgorithmTypes types;
151  gather_types(types, {
152  std::move(meta)
153  });
154 
155  ast::Parser p { padded_options };
156 
157  auto evald_algo = eval::cl_eval(
158  p.parse_value(),
159  meta_type,
160  types,
161  std::move(fixed_static_args)
162  );
163 
164  auto evaluated_options = evald_algo.as_algorithm();
165  auto env_root = std::make_shared<EnvRoot>(std::move(evaluated_options));
166  Env env(env_root, env_root->algo_value());
167 
168  return env;
169 }
170 
171 }
172 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
const std::string & type() const
Returns the algorithm&#39;s type.
Definition: Meta.hpp:257
Env create_env(Meta &&meta, const std::string &options="")
Creates an environment.
T create_algo(const std::string &options, Args &&... args)
Template for easy algorithm instantiation.
const std::string & name() const
Returns the algorithm&#39;s name.
Definition: Meta.hpp:251
Builder & registry(const Registry< registry_root_t > &reg)
Sets a registry to be used for dynamic type lookups during instantiation.
T instance(Args &&... args)
Instances T by creating the right Env instance, and passing any extra arguments to the constructor of...
eval::AlgorithmTypes & algorithm_map()
Env env()
Creates the Env instance needed for instantiating T.
Builder & options(const std::string &options)
Sets an options string for the created environment.
Builder< T, registry_root_t > builder()
Builder pattern template for easy algorithm instantiation.
Local environment for a compression/encoding/decompression call.