dir structure changed
This commit is contained in:
68
src/system/syselmts.hpp
Normal file
68
src/system/syselmts.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef _SYSELEMENTS_HPP_
|
||||
#define _SYSELEMENTS_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <stdexcept>
|
||||
|
||||
class SystemElement
|
||||
{
|
||||
public:
|
||||
std::string name;
|
||||
SystemElement(std::string name) : name(name) {};
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class SystemElementContainer : public SystemElement
|
||||
{
|
||||
private:
|
||||
static_assert(std::is_base_of<SystemElement, T>::value, "T shall be a system element descendant !");
|
||||
unsigned int iter_count;
|
||||
std::unordered_map<std::string, T> content;
|
||||
|
||||
bool name_exists(std::string name)
|
||||
{
|
||||
return get(name) != nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
SystemElementContainer(std::string name) : SystemElement(name) {};
|
||||
SystemElementContainer(std::string name, std::vector<T> elements) : SystemElement(name)
|
||||
{
|
||||
add(elements);
|
||||
}
|
||||
void add(T element)
|
||||
{
|
||||
if ("" == element.name)
|
||||
{
|
||||
throw std::runtime_error("System elements with empty names are forbidden");
|
||||
}
|
||||
if (name_exists(element.name))
|
||||
{
|
||||
throw std::runtime_error("System elements of same names are forbidden");
|
||||
}
|
||||
content.insert({element.name, element});
|
||||
}
|
||||
void add(std::vector<T> elements)
|
||||
{
|
||||
for (auto &element : elements)
|
||||
{
|
||||
add(element);
|
||||
}
|
||||
}
|
||||
T *get(std::string name)
|
||||
{
|
||||
auto it = content.find(name);
|
||||
if (it != content.end())
|
||||
{
|
||||
return &it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user