-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNamed_values_container_factory.hpp
50 lines (42 loc) · 1.35 KB
/
Named_values_container_factory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
#include "Named_values_container.hpp"
class Named_values_containers_factory
{
public:
Named_values_containers_factory(const std::vector<std::string>& elements_names) :
_elements_names(elements_names)
{
for (size_t i = 0; i < _elements_names.size(); ++i)
{
_elements_ids.insert(std::make_pair(_elements_names[i], i));
}
};
~Named_values_containers_factory()
{
};
template <class T>
Named_values_container<T> make_container(const std::vector<T>& values) const
{
// TODO: add assert or something else
return Named_values_container<T>(make_elements<T>(values), _elements_ids);
};
template <class T>
Named_values_container<T> make_container() const
{
return make_container(std::vector<T>(_elements_names.size(), T()));
};
protected:
template <class T>
static std::vector <std::pair <T, std::string> > make_elements(const std::vector<T>& values
, const std::vector<std::string>& names)
{
std::vector <std::pair <T, std::string> > result(names.size());
for (size_t i = 0; i < names.size() && i < values.size(); ++i)
{
result.push_back(std::make_pair(values[i], names[i]));
}
return result;
}
std::vector<std::string> _elements_names;
std::unordered_map<std::string, size_t> _elements_ids;
};