Fix issue in Rocket.lua script.
[Cafu-Engine.git] / Libs / Variables.cpp
blob839aff34e0eb8752e452efe76c409d24adcb2aad
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
7 #include "Variables.hpp"
8 #include "Network/State.hpp"
11 /****************/
12 /*** VarBaseT ***/
13 /****************/
15 bool cf::TypeSys::VarBaseT::HasFlag(const char* Flag) const
17 if (!m_Flags) return false;
19 for (unsigned int i = 0; m_Flags[i]; i++)
20 if (strcmp(Flag, m_Flags[i]) == 0)
21 return true;
23 return false;
27 const char* cf::TypeSys::VarBaseT::GetFlag(const char* Flag, unsigned int Nr, const char* Default) const
29 if (!m_Flags) return Default;
31 unsigned int i;
33 for (i = 0; m_Flags[i]; i++)
34 if (strcmp(Flag, m_Flags[i]) == 0)
35 break;
37 for (unsigned int j = i; m_Flags[j]; j++)
38 if (j == i + Nr)
39 return m_Flags[j];
41 return Default;
45 /***************/
46 /*** VarT<T> ***/
47 /***************/
49 template<class T>
50 void cf::TypeSys::VarT<T>::Serialize(cf::Network::OutStreamT& Stream) const
52 Stream << Get();
56 // With g++, we cannot write cf::TypeSys::VarT<Vector3fT>::Serialize(...),
57 // see search results for "g++ error specialization in different namespace" for details.
58 namespace cf
60 namespace TypeSys
62 template<> // Must specialize, because an OutStreamT cannot take a Vector2fT directly.
63 void VarT<Vector2fT>::Serialize(cf::Network::OutStreamT& Stream) const
65 Stream << Get().x;
66 Stream << Get().y;
69 template<> // Must specialize, because an OutStreamT cannot take a Vector3fT directly.
70 void VarT<Vector3fT>::Serialize(cf::Network::OutStreamT& Stream) const
72 Stream << Get().x;
73 Stream << Get().y;
74 Stream << Get().z;
77 template<> // Must specialize, because an OutStreamT cannot take a Vector3dT directly.
78 void VarT<Vector3dT>::Serialize(cf::Network::OutStreamT& Stream) const
80 Stream << Get().x;
81 Stream << Get().y;
82 Stream << Get().z;
85 template<> // Must specialize, because an OutStreamT cannot take a BoundingBox3dT directly.
86 void VarT<BoundingBox3dT>::Serialize(cf::Network::OutStreamT& Stream) const
88 Stream << Get().Min.x;
89 Stream << Get().Min.y;
90 Stream << Get().Min.z;
92 Stream << Get().Max.x;
93 Stream << Get().Max.y;
94 Stream << Get().Max.z;
100 template<class T>
101 void cf::TypeSys::VarT<T>::Deserialize(cf::Network::InStreamT& Stream)
103 T v;
105 Stream >> v;
107 // Derived classes may have overridden Set() to add "side-effects", such as updating graphical resources.
108 // Therefore, we cannot write `m_Value = v;` in place of `Set(v);` here.
109 Set(v);
113 // With g++, we cannot write cf::TypeSys::VarT<Vector3fT>::Deserialize(...),
114 // see search results for "g++ error specialization in different namespace" for details.
115 namespace cf
117 namespace TypeSys
119 template<> // Must specialize, because an InStreamT cannot take a Vector2fT directly.
120 void VarT<Vector2fT>::Deserialize(cf::Network::InStreamT& Stream)
122 Vector2fT v;
124 Stream >> v.x;
125 Stream >> v.y;
127 // Derived classes may have overridden Set() to add "side-effects", such as updating graphical resources.
128 // Therefore, we cannot write `m_Value = v;` in place of `Set(v);` here.
129 Set(v);
132 template<> // Must specialize, because an InStreamT cannot take a Vector3fT directly.
133 void VarT<Vector3fT>::Deserialize(cf::Network::InStreamT& Stream)
135 Vector3fT v;
137 Stream >> v.x;
138 Stream >> v.y;
139 Stream >> v.z;
141 // Derived classes may have overridden Set() to add "side-effects", such as updating graphical resources.
142 // Therefore, we cannot write `m_Value = v;` in place of `Set(v);` here.
143 Set(v);
146 template<> // Must specialize, because an InStreamT cannot take a Vector3dT directly.
147 void VarT<Vector3dT>::Deserialize(cf::Network::InStreamT& Stream)
149 Vector3dT v;
151 Stream >> v.x;
152 Stream >> v.y;
153 Stream >> v.z;
155 // Derived classes may have overridden Set() to add "side-effects", such as updating graphical resources.
156 // Therefore, we cannot write `m_Value = v;` in place of `Set(v);` here.
157 Set(v);
160 template<> // Must specialize, because an InStreamT cannot take a BoundingBox3dT directly.
161 void VarT<BoundingBox3dT>::Deserialize(cf::Network::InStreamT& Stream)
163 BoundingBox3dT BB;
165 Stream >> BB.Min.x;
166 Stream >> BB.Min.y;
167 Stream >> BB.Min.z;
169 Stream >> BB.Max.x;
170 Stream >> BB.Max.y;
171 Stream >> BB.Max.z;
173 // Derived classes may have overridden Set() to add "side-effects", such as updating graphical resources.
174 // Therefore, we cannot write `m_Value = BB;` in place of `Set(BB);` here.
175 Set(BB);
181 template<class T>
182 void cf::TypeSys::VarT<T>::accept(VisitorT& Visitor)
184 Visitor.visit(*this);
188 template<class T>
189 void cf::TypeSys::VarT<T>::accept(VisitorConstT& Visitor) const
191 Visitor.visit(*this);
195 /********************/
196 /*** VarArrayT<T> ***/
197 /********************/
199 template<class T>
200 cf::TypeSys::VarArrayT<T>::VarArrayT(const char* Name, unsigned int InitSize, const T& InitValue, const char* Flags[])
201 : VarBaseT(Name, Flags)
203 m_Array.PushBackEmptyExact(InitSize);
205 for (unsigned int i = 0; i < InitSize; i++)
206 m_Array[i] = InitValue;
210 template<class T>
211 void cf::TypeSys::VarArrayT<T>::Serialize(cf::Network::OutStreamT& Stream) const
213 Stream << m_Array;
217 template<class T>
218 void cf::TypeSys::VarArrayT<T>::Deserialize(cf::Network::InStreamT& Stream)
220 // Contrary to `VarT::Deserialize()`, which calls `VarT::Set()` in order to account for
221 // side-effects resulting from derived classes that have provided an override for `VarT::Set()`,
222 // any such side-effects are not supported and not accounted for here.
223 Stream >> m_Array;
227 template<class T>
228 void cf::TypeSys::VarArrayT<T>::accept(VisitorT& Visitor)
230 Visitor.visit(*this);
234 template<class T>
235 void cf::TypeSys::VarArrayT<T>::accept(VisitorConstT& Visitor) const
237 Visitor.visit(*this);
241 /***************/
242 /*** VarManT ***/
243 /***************/
245 void cf::TypeSys::VarManT::Add(VarBaseT* Var)
247 m_VarsArray.PushBack(Var);
248 m_VarsMap[Var->GetName()] = Var;
252 void cf::TypeSys::VarManT::AddAlias(const char* Alias, VarBaseT* Var)
254 m_VarsMap[Alias] = Var;
258 /*******************************/
259 /*** Template Instantiations ***/
260 /*******************************/
262 template class cf::TypeSys::VarT<float>;
263 template class cf::TypeSys::VarT<double>;
264 template class cf::TypeSys::VarT<int>;
265 template class cf::TypeSys::VarT<unsigned int>;
266 template class cf::TypeSys::VarT<uint16_t>;
267 template class cf::TypeSys::VarT<uint8_t>;
268 template class cf::TypeSys::VarT<bool>;
269 template class cf::TypeSys::VarT<std::string>;
270 template class cf::TypeSys::VarT<Vector2fT>;
271 template class cf::TypeSys::VarT<Vector3fT>;
272 template class cf::TypeSys::VarT<Vector3dT>;
273 template class cf::TypeSys::VarT<BoundingBox3dT>;
274 template class cf::TypeSys::VarArrayT<uint32_t>;
275 template class cf::TypeSys::VarArrayT<uint16_t>;
276 template class cf::TypeSys::VarArrayT<uint8_t>;
277 template class cf::TypeSys::VarArrayT<std::string>;