Mercurial > thymian
annotate templates/template_struct.h @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
| rev | line source |
|---|---|
| ferencd@0 | 1 #ifndef TEMPLATE_STRUCT_H |
| ferencd@0 | 2 #define TEMPLATE_STRUCT_H |
| ferencd@0 | 3 |
| ferencd@0 | 4 #include <string> |
| ferencd@0 | 5 #include <vector> |
| ferencd@0 | 6 #include <map> |
| ferencd@0 | 7 |
| ferencd@0 | 8 /** |
| ferencd@0 | 9 * @brief The template_struct class represents a logical structure that can be inserted into the |
| ferencd@0 | 10 * template's content in order to have a logical grouping of various data. |
| ferencd@0 | 11 */ |
| ferencd@0 | 12 class template_struct |
| ferencd@0 | 13 { |
| ferencd@0 | 14 public: |
| ferencd@0 | 15 |
| ferencd@0 | 16 template_struct() = default; |
| ferencd@0 | 17 explicit template_struct(const std::string& t) : name("name"), type(t) {} |
| ferencd@0 | 18 template_struct(const std::string& n, const std::string& t) : name(n), type(t) {} |
| ferencd@0 | 19 |
| ferencd@0 | 20 virtual ~template_struct() = default; |
| ferencd@0 | 21 |
| ferencd@0 | 22 std::string& operator[] (const char* p) |
| ferencd@0 | 23 { |
| ferencd@0 | 24 return struct_members[std::string(p)]; |
| ferencd@0 | 25 } |
| ferencd@0 | 26 |
| ferencd@0 | 27 const std::string& operator[] (const char* p) const |
| ferencd@0 | 28 { |
| ferencd@0 | 29 return struct_members.at(std::string(p)); |
| ferencd@0 | 30 } |
| ferencd@0 | 31 |
| ferencd@0 | 32 std::string& operator[] (const std::string& p) |
| ferencd@0 | 33 { |
| ferencd@0 | 34 return struct_members[std::string(p)]; |
| ferencd@0 | 35 } |
| ferencd@0 | 36 |
| ferencd@0 | 37 const std::string& operator[] (const std::string& p) const |
| ferencd@0 | 38 { |
| ferencd@0 | 39 return struct_members.at(std::string(p)); |
| ferencd@0 | 40 } |
| ferencd@0 | 41 |
| ferencd@0 | 42 std::vector<std::string> keys() const |
| ferencd@0 | 43 { |
| ferencd@0 | 44 std::vector<std::string> v; |
| ferencd@0 | 45 for(const auto& it : struct_members) |
| ferencd@0 | 46 { |
| ferencd@0 | 47 v.push_back(it.first); |
| ferencd@0 | 48 } |
| ferencd@0 | 49 return v; |
| ferencd@0 | 50 } |
| ferencd@0 | 51 |
| ferencd@0 | 52 bool has_key(const std::string& k) |
| ferencd@0 | 53 { |
| ferencd@0 | 54 for(const auto& it : struct_members) |
| ferencd@0 | 55 { |
| ferencd@0 | 56 if(it.first == k) return true; |
| ferencd@0 | 57 } |
| ferencd@0 | 58 return false; |
| ferencd@0 | 59 } |
| ferencd@0 | 60 |
| ferencd@0 | 61 std::string name; |
| ferencd@0 | 62 std::string type; |
| ferencd@0 | 63 std::map<std::string, std::string> struct_members; |
| ferencd@0 | 64 }; |
| ferencd@0 | 65 |
| ferencd@0 | 66 #endif // TEMPLATE_STRUCT_H |
