diff options
Diffstat (limited to 'src/FbTk/ResTraits.hh')
-rw-r--r-- | src/FbTk/ResTraits.hh | 36 |
1 files changed, 36 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 |