| 31 | |
| 32 | namespace TEngine { |
| 33 | class Attribute |
| 34 | { |
| 35 | using attribute_map_t = std::unordered_map<std::string, any>; |
| 36 | using value_t = attribute_map_t::value_type; |
| 37 | |
| 38 | public: |
| 39 | Attribute() = default; |
| 40 | Attribute(const Attribute&) = default; |
| 41 | |
| 42 | Attribute(std::initializer_list<value_t> __l) |
| 43 | { |
| 44 | dict_map_ = __l; |
| 45 | } |
| 46 | |
| 47 | any& operator[](const std::string& name) |
| 48 | { |
| 49 | if(ExistAttr(name)) |
| 50 | return GetAttr(name); |
| 51 | |
| 52 | SetAttr(name, any()); |
| 53 | |
| 54 | return dict_map_.at(name); |
| 55 | } |
| 56 | |
| 57 | virtual bool ExistAttr(const std::string& name) const |
| 58 | { |
| 59 | if(dict_map_.count(name)) |
| 60 | return true; |
| 61 | else |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | template <typename T> void GetAttr(const std::string& name, T* v, T default_v) |
| 66 | { |
| 67 | if(ExistAttr(name)) |
| 68 | { |
| 69 | *v = any_cast<T>(GetAttr(name)); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | *v = default_v; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | template <typename T> void GetAttr(const std::string& name, T* v) |
| 78 | { |
| 79 | if(ExistAttr(name)) |
| 80 | { |
| 81 | *v = any_cast<T>(GetAttr(name)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | virtual any& GetAttr(const std::string& name) |
| 86 | { |
| 87 | static any dummy; |
| 88 | |
| 89 | if(dict_map_.count(name)) |
| 90 | return dict_map_.at(name); |