You.i Engine
YiBundle.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_BUNDLE_H_
3 #define _YI_BUNDLE_H_
4 
5 #include "framework/YiAny.h"
6 #include "framework/YiPredef.h"
7 #include "utility/YiError.h"
8 #include "utility/YiString.h"
9 
44 class CYIBundle
45 {
46 public:
47  CYIBundle();
48 
52  CYIBundle(const CYIBundle &bundle);
53 
57  CYIBundle(CYIBundle &&bundle) noexcept;
58 
59  virtual ~CYIBundle();
60 
64  CYIBundle &operator=(const CYIBundle &bundle);
65 
69  CYIBundle &operator=(CYIBundle &&bundle);
70 
74  template<typename T>
75  void Put(const CYIString &key, T &&value);
76 
80  template<typename T>
81  bool Get(const CYIString &key, T *pValue) const;
82 
86  template<typename T>
87  T GetWithDefault(const CYIString &key, const T &defaultValue) const;
88 
92  template<typename T>
93  T Get(const CYIString &key) const;
94 
98  const CYIAny &Get(const CYIString &key) const;
99 
103  std::vector<CYIString> GetKeys() const;
104 
108  bool IsKeyPresent(const CYIString &key) const;
109 
113  void Clear();
114 
115 protected:
116  virtual void OnDataModified(const CYIString &key);
117 
118 private:
119  std::map<CYIString, CYIAny> m_data;
120 };
121 
126 template<typename T>
127 void CYIBundle::Put(const CYIString &key, T &&value)
128 {
129  m_data[key] = CYIAny(std::forward<T>(value));
130  OnDataModified(key);
131 }
132 
133 template<typename T>
134 bool CYIBundle::Get(const CYIString &key, T *pValue) const
135 {
136  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
137 
138  if (element != m_data.end())
139  {
140  *pValue = AnyCast<T>(element->second);
141  return true;
142  }
143 
144  return false;
145 }
146 
147 template<typename T>
148 T CYIBundle::GetWithDefault(const CYIString &key, const T &defaultValue) const
149 {
150  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
151 
152  if (element != m_data.end())
153  {
154  return AnyCast<T>(element->second);
155  }
156 
157  // Key not found!
158  return defaultValue;
159 }
160 
161 template<typename T>
162 T CYIBundle::Get(const CYIString &key) const
163 {
164  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
165 
166  if (element != m_data.end())
167  {
168  return AnyCast<T>(element->second);
169  }
170 
171  // Key not found!
172  YI_LOGI("CYIBundle", "Key '%s' not found in bundle while using direct access. The default constructor of the template type will be used.", key.GetData());
173  return T();
174 }
175 
176 #endif // _YI_BUNDLE_H_
virtual ~CYIBundle()
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:36
CYIBundle & operator=(const CYIBundle &bundle)
CYIBundle represents a collection of key-value pairs.
Definition: YiBundle.h:44
A class that holds instances of any type that is copy-constructible.
Definition: YiAny.h:77
void Clear()
void Put(const CYIString &key, T &&value)
Definition: YiBundle.h:127
bool IsKeyPresent(const CYIString &key) const
virtual void OnDataModified(const CYIString &key)
#define YI_LOGI(tag, message,...)
Definition: YiLogger.h:124
const char * GetData() const
T GetWithDefault(const CYIString &key, const T &defaultValue) const
Definition: YiBundle.h:148
std::vector< CYIString > GetKeys() const
bool Get(const CYIString &key, T *pValue) const
Definition: YiBundle.h:134