You.i Engine
YiBundle.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. All rights reserved.
2 #ifndef _YI_BUNDLE_H_
3 #define _YI_BUNDLE_H_
4 
6 #include "framework/YiAny.h"
7 #include "framework/YiPredef.h"
8 #include "utility/YiError.h"
9 #include "utility/YiString.h"
10 
46 {
47 public:
48  CYIBundle();
49 
53  CYIBundle(const CYIBundle &bundle);
54 
55  virtual ~CYIBundle();
56 
60  CYIBundle & operator=(const CYIBundle &bundle);
61 
65  template <typename T>
66  void Put(const CYIString &key, T value);
67 
71  template <typename T>
72  bool Get(const CYIString &key, T *pValue) const;
73 
77  template <typename T>
78  T GetWithDefault(const CYIString &key, const T &defaultValue) const;
79 
83  template <typename T>
84  T Get(const CYIString &key) const;
85 
89  bool IsKeyPresent(const CYIString &key) const;
90 
94  void Clear();
95 
96 protected:
97  virtual void OnDataModified(const CYIString &key);
98 
99 private:
100  std::map<CYIString, CYIAny> m_data;
101 };
102 
107 template <typename T>
108 void CYIBundle::Put(const CYIString &key, T value)
109 {
110  m_data[key] = CYIAny(value);
111  OnDataModified(key);
112 }
113 
114 template <typename T>
115 bool CYIBundle::Get(const CYIString &key, T *pValue) const
116 {
117  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
118 
119  if (element != m_data.end())
120  {
121  *pValue = AnyCast<T>(element->second);
122  return true;
123  }
124 
125  return false;
126 }
127 
128 template <typename T>
129 T CYIBundle::GetWithDefault(const CYIString &key, const T &defaultValue) const
130 {
131  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
132 
133  if (element != m_data.end())
134  {
135  return AnyCast<T>(element->second);
136  }
137 
138  // Key not found!
139  return defaultValue;
140 }
141 
142 template <typename T>
143 T CYIBundle::Get(const CYIString &key) const
144 {
145  std::map<CYIString, CYIAny>::const_iterator element = m_data.find(key);
146 
147  if (element != m_data.end())
148  {
149  return AnyCast<T>(element->second);
150  }
151 
152  // Key not found!
153  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());
154  return T();
155 }
156 
157 #endif // _YI_BUNDLE_H_
virtual ~CYIBundle()
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:35
CYIBundle & operator=(const CYIBundle &bundle)
CYIBundle represents a collection of key-value pairs.
Definition: YiBundle.h:45
A class that holds instances of any type that is copy-constructible.
Definition: YiAny.h:77
void Clear()
The base class for an object accessible from script source code.
Definition: YiScriptableObject.h:28
T GetWithDefault(const CYIString &key, const T &defaultValue) const
Definition: YiBundle.h:129
virtual void OnDataModified(const CYIString &key)
#define YI_LOGI(tag, msg,...)
Definition: YiLoggerHelper.h:76
bool IsKeyPresent(const CYIString &key) const
void Put(const CYIString &key, T value)
Definition: YiBundle.h:108
bool Get(const CYIString &key, T *pValue) const
Definition: YiBundle.h:115
const char * GetData() const