You.i Engine
YiBundle.h
Go to the documentation of this file.
1 // © You i Labs Inc. All rights reserved.
2 #ifndef _YI_BUNDLE_H_
3 #define _YI_BUNDLE_H_
4 
5 #include "framework/YiAny.h"
6 #include "framework/YiPredef.h"
8 #include "utility/YiString.h"
9 
10 #include <map>
11 
46 class CYIBundle
47 {
48 public:
49  CYIBundle();
50 
54  CYIBundle(const CYIBundle &bundle);
55 
59  CYIBundle(CYIBundle &&bundle) noexcept;
60 
61  virtual ~CYIBundle();
62 
66  CYIBundle &operator=(const CYIBundle &bundle);
67 
71  CYIBundle &operator=(CYIBundle &&bundle);
72 
76  template<typename T>
77  void Put(const CYIString &key, T &&value);
78 
82  template<typename T>
83  bool Get(const CYIString &key, T *pValue) const;
84 
88  template<typename T>
89  T GetWithDefault(const CYIString &key, const T &defaultValue) const;
90 
94  template<typename T>
95  T Get(const CYIString &key) const;
96 
100  const CYIAny &Get(const CYIString &key) const;
101 
105  std::vector<CYIString> GetKeys() const;
106 
110  bool IsKeyPresent(const CYIString &key) const;
111 
115  void Clear();
116 
117 protected:
118  virtual void OnDataModified(const CYIString &key);
119 
120 private:
121  std::map<CYIString, CYIAny> m_data;
122 };
123 
128 template<typename T>
129 void CYIBundle::Put(const CYIString &key, T &&value)
130 {
131  m_data[key] = CYIAny(std::forward<T>(value));
132  OnDataModified(key);
133 }
134 
135 template<typename T>
136 bool CYIBundle::Get(const CYIString &key, T *pValue) const
137 {
138  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
139 
140  if (element != m_data.end())
141  {
142  *pValue = AnyCast<T>(element->second);
143  return true;
144  }
145 
146  return false;
147 }
148 
149 template<typename T>
150 T CYIBundle::GetWithDefault(const CYIString &key, const T &defaultValue) const
151 {
152  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
153 
154  if (element != m_data.end())
155  {
156  return AnyCast<T>(element->second);
157  }
158 
159  // Key not found!
160  return defaultValue;
161 }
162 
163 template<typename T>
164 T CYIBundle::Get(const CYIString &key) const
165 {
166  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
167 
168  if (element != m_data.end())
169  {
170  return AnyCast<T>(element->second);
171  }
172 
173  // Key not found!
174  youi_private::LogInfo("CYIBundle", "Key '%s' not found in bundle while using direct access. The default constructor of the template type will be used.", key.GetData());
175  return T();
176 }
177 
178 #endif // _YI_BUNDLE_H_
virtual ~CYIBundle()
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:32
CYIBundle & operator=(const CYIBundle &bundle)
CYIBundle represents a collection of key-value pairs.
Definition: YiBundle.h:46
A class that holds instances of any type that is copy-constructible.
Definition: YiAny.h:80
void Clear()
void Put(const CYIString &key, T &&value)
Definition: YiBundle.h:129
bool IsKeyPresent(const CYIString &key) const
virtual void OnDataModified(const CYIString &key)
const char * GetData() const
T GetWithDefault(const CYIString &key, const T &defaultValue) const
Definition: YiBundle.h:150
std::vector< CYIString > GetKeys() const
bool Get(const CYIString &key, T *pValue) const
Definition: YiBundle.h:136