aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-07-12 12:52:18 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-11-01 09:57:22 (GMT)
commitaec059644893c1314203d501b1d80cfd439f4d2f (patch)
tree8c7eab1e3e316bf7fe0ee7a98c32fdc12d773c43 /src/FbTk
parent592d306e702cac55245b8485db9a769f47584137 (diff)
downloadfluxbox_paul-aec059644893c1314203d501b1d80cfd439f4d2f.zip
fluxbox_paul-aec059644893c1314203d501b1d80cfd439f4d2f.tar.bz2
Add RangeTraits class which automatically clamps the value of resources
and start using it.
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/ResTraits.hh36
-rw-r--r--src/FbTk/Resource.hh3
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
32namespace FbTk { 33namespace FbTk {
33 34
@@ -226,6 +227,41 @@ private:
226 std::string m_delim; 227 std::string m_delim;
227}; 228};
228 229
230template<typename Traits>
231class RangeTraits: private Traits {
232public:
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
261private:
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;
281typedef Resource<unsigned int, IntTraits<unsigned int> > UIntResource; 281typedef Resource<unsigned int, IntTraits<unsigned int> > UIntResource;
282typedef Resource<std::string, StringTraits> StringResource; 282typedef Resource<std::string, StringTraits> StringResource;
283 283
284typedef RangeTraits<IntTraits<int> > RangedIntTraits;
285typedef 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