diff options
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/ResTraits.hh | 36 | ||||
-rw-r--r-- | src/FbTk/Resource.hh | 3 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/FbTk/ResTraits.hh b/src/FbTk/ResTraits.hh index 19decb5..e1c3871 100644 --- a/src/FbTk/ResTraits.hh +++ b/src/FbTk/ResTraits.hh | |||
@@ -28,6 +28,7 @@ | |||
28 | #include "FbString.hh" | 28 | #include "FbString.hh" |
29 | #include "Luamm.hh" | 29 | #include "Luamm.hh" |
30 | #include "StringUtil.hh" | 30 | #include "StringUtil.hh" |
31 | #include "Util.hh" | ||
31 | 32 | ||
32 | namespace FbTk { | 33 | namespace FbTk { |
33 | 34 | ||
@@ -226,6 +227,41 @@ private: | |||
226 | std::string m_delim; | 227 | std::string m_delim; |
227 | }; | 228 | }; |
228 | 229 | ||
230 | template<typename Traits> | ||
231 | class RangeTraits: private Traits { | ||
232 | public: | ||
233 | typedef typename Traits::Type Type; | ||
234 | |||
235 | RangeTraits(const Type &min, const Type &max, const Traits &traits = Traits()) | ||
236 | : Traits(traits), m_min(min), m_max(max) | ||
237 | { assert(m_min <= m_max); } | ||
238 | |||
239 | std::string toString(const Type &x) const { | ||
240 | assert(x >= m_min); | ||
241 | assert(x <= m_max); | ||
242 | |||
243 | return Traits::toString(x); | ||
244 | } | ||
245 | |||
246 | void toLua(const Type &x, lua::state &l) const { | ||
247 | assert(x >= m_min); | ||
248 | assert(x <= m_max); | ||
249 | |||
250 | Traits::toLua(x, l); | ||
251 | } | ||
252 | |||
253 | Type fromString(const std::string &x) const { | ||
254 | return FbTk::Util::clamp(Traits::fromString(x), m_min, m_max); | ||
255 | } | ||
256 | |||
257 | Type fromLua(lua::state &l) const { | ||
258 | return FbTk::Util::clamp(Traits::fromLua(l), m_min, m_max); | ||
259 | } | ||
260 | |||
261 | private: | ||
262 | Type m_min, m_max; | ||
263 | }; | ||
264 | |||
229 | } // end namespace FbTk | 265 | } // end namespace FbTk |
230 | 266 | ||
231 | #endif // FBTK_RESTRAITS_HH | 267 | #endif // FBTK_RESTRAITS_HH |
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh index c6f12d6..37a7ed3 100644 --- a/src/FbTk/Resource.hh +++ b/src/FbTk/Resource.hh | |||
@@ -281,6 +281,9 @@ typedef Resource<int, IntTraits<int> > IntResource; | |||
281 | typedef Resource<unsigned int, IntTraits<unsigned int> > UIntResource; | 281 | typedef Resource<unsigned int, IntTraits<unsigned int> > UIntResource; |
282 | typedef Resource<std::string, StringTraits> StringResource; | 282 | typedef Resource<std::string, StringTraits> StringResource; |
283 | 283 | ||
284 | typedef RangeTraits<IntTraits<int> > RangedIntTraits; | ||
285 | typedef Resource<int, RangedIntTraits> RangedIntResource; | ||
286 | |||
284 | } // end namespace FbTk | 287 | } // end namespace FbTk |
285 | 288 | ||
286 | #endif // FBTK_RESOURCE_HH | 289 | #endif // FBTK_RESOURCE_HH |