Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
digxel.hpp
Go to the documentation of this file.
1/*
2 DIGXEL.hpp - grid aligned and fixed format time specifications
3
4 Copyright (C)
5 2010, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
56#ifndef LIB_TIME_DIGXEL_H
57#define LIB_TIME_DIGXEL_H
58
59#include "lib/error.hpp"
60#include "lib/symbol.hpp"
61#include "lib/util.hpp"
62
63#include <boost/lexical_cast.hpp>
64#include <functional>
65#include <string>
66#include <cstdio>
67#include <cmath>
68
69using std::string;
70
71
72namespace lib {
73namespace time {
74
75 namespace digxel {
76
77 using lib::Literal;
78 using boost::lexical_cast;
79
80 using CBuf = CStr;
81
82
88 template<typename NUM, size_t len>
90 {
91 enum{ bufsiz = len+1 };
92
95
96 public:
98 : printbuffer_()
99 , formatSpec_(fmt)
100 {
101 clear();
102 }
103
104 void clear() { printbuffer_[0] = '\0'; }
105 bool empty() { return '\0' == *printbuffer_; }
106
107 size_t
108 maxlen() const
109 {
110 return len;
111 }
112
113 CBuf
114 show (NUM val)
115 {
116 if (empty())
117 {
118 size_t space = std::snprintf (printbuffer_, bufsiz, formatSpec_, val);
119 REQUIRE (space < bufsiz, "Digxel value exceeded available buffer size. "
120 "For showing %s, %zu+1 chars instead of just %zu+1 would be required."
121 , cStr(lexical_cast<string>(val)), space, len);
122 }
123 ENSURE (!empty());
124 return printbuffer_;
125 }
126 };
127
128
133 template<typename NUM>
134 struct Formatter;
135
136 template<>
137 struct Formatter<int>
138 : PrintfFormatter<int, 9>
139 {
140 Formatter() : PrintfFormatter<int,9>("%3d") { }
141 };
142
143 template<>
144 struct Formatter<double>
145 : PrintfFormatter<double, 7>
146 {
147 Formatter() : PrintfFormatter<double,7>("%06.3f") { }
148 };
149
150 /* == other specialised Formatters == */
152 : PrintfFormatter<int, 4>
153 {
154 SexaFormatter() : PrintfFormatter<int,4>("%02d") { }
155 };
156
158 : PrintfFormatter<uint, 2>
159 {
161 };
162
164 : PrintfFormatter<long, 20>
165 {
166 CountFormatter() : PrintfFormatter<long,20>("%04ld") { }
167 };
168
170 : PrintfFormatter<int, 9>
171 {
172 HourFormatter() : PrintfFormatter<int,9>("%2d") { }
173 };
174
175
177 {
178 void clear() { }
179 size_t maxlen() const { return 1; }
180 CBuf show (int val) { return val<0? "-":" "; }
181 };
182
183 } //(End) digxel configuration namespace
184
185
186
187
188
189 using std::bind;
190 using std::function;
191 using std::placeholders::_1;
192
193
213 template< typename NUM
214 , class FMT = digxel::Formatter<NUM>
215 >
216 class Digxel
217 {
218 mutable
221
223 typedef function<void(NUM)> _Mutator;
224
225
227
228 public:
239 template<typename FUN, class THIS>
240 void
241 installMutator (FUN mutate, THIS& self)
242 {
243 mutator = bind (mutate, &self, _1 );
244 }
245
246
248 : buffer_()
249 , value_ ()
250 , mutator()
251 { }
252
253 // using the standard copy operations
254
255 operator NUM() const { return value_; }
256 operator string() const { return show(); }
257
258 size_t maxlen() const { return buffer_.maxlen(); }
259
260
262 show() const
263 {
264 return buffer_.show (value_);
265 }
266
267
268 void
270 {
271 if (n == value_) return;
272 if (mutator)
273 mutator (n);
274 else
275 setValueRaw (n);
276 }
277
278 void
279 setValueRaw (NUM newVal)
280 {
281 if (newVal != value_)
282 {
283 value_ = newVal;
284 buffer_.clear();
285 }
286 }
287
288
289
290 //---Supporting-increments--------------
291 Digxel& operator+= (NUM inc) { *this = value_ + inc; return *this; }
292 Digxel& operator-= (NUM dec) { *this = value_ - dec; return *this; }
293 Digxel& operator++ () { *this = value_ + 1; return *this; }
294 Digxel& operator-- () { *this = value_ - 1; return *this; }
295 NUM operator++ (int) { NUM p(value_); *this =p+1; return p;}
296 NUM operator-- (int) { NUM p(value_); *this =p-1; return p;}
297
298 //---Supporting-total-ordering----------
299 auto operator<=>(Digxel const& o) const { return value_ <=> NUM(o); }
300 bool operator== (Digxel const& o) const { return value_ == NUM(o); }
301 };
302
303
304
305 /* == predefined Digxel configurations == */
309
310 using FrameCnt = int64_t;
312
313
316 class Signum
317 : public Digxel<int,digxel::SignFormatter>
318 {
320
321 void
322 storeSign (int val)
323 {
324 setValueRaw (val<0? -1:+1);
325 }
326
327 public:
329 {
330 setValueRaw(1);
332 }
333
334 using _Par::operator=;
335
336 friend int operator*= (Signum& s, int c) { s = c*s; return s; }
337 };
338
339
340}} // lib::time
341#endif
Inline string literal.
Definition symbol.hpp:78
A number element for building structured numeric displays.
Definition digxel.hpp:217
void operator=(NUM n)
Definition digxel.hpp:269
Digxel & operator++()
Definition digxel.hpp:293
Digxel & operator--()
Definition digxel.hpp:294
Digxel & operator+=(NUM inc)
Definition digxel.hpp:291
auto operator<=>(Digxel const &o) const
Definition digxel.hpp:299
_Mutator mutator
Functor for setting a new digxel value.
Definition digxel.hpp:226
void setValueRaw(NUM newVal)
Definition digxel.hpp:279
digxel::CBuf show() const
Definition digxel.hpp:262
Digxel & operator-=(NUM dec)
Definition digxel.hpp:292
function< void(NUM)> _Mutator
Definition digxel.hpp:223
bool operator==(Digxel const &o) const
Definition digxel.hpp:300
size_t maxlen() const
Definition digxel.hpp:258
Digxel< NUM, FMT > _Digxel
Definition digxel.hpp:222
void installMutator(FUN mutate, THIS &self)
install an external functor to be applied on any new digxel value.
Definition digxel.hpp:241
special Digxel to show a sign.
Definition digxel.hpp:318
void storeSign(int val)
Definition digxel.hpp:322
friend int operator*=(Signum &s, int c)
Definition digxel.hpp:336
Digxel< int, digxel::SignFormatter > _Par
Definition digxel.hpp:319
Default / base implementation for Digxel formatting.
Definition digxel.hpp:90
Lumiera error handling (C++ interface).
const char * CStr
Definition error.hpp:42
unsigned int uint
Definition integral.hpp:29
default configured Formatter implementations for some of the basic numeric types
Definition digxel.hpp:134
Digxel< uint, digxel::HexaFormatter > HexaDigit
for displaying a hex byte
Definition digxel.hpp:307
int64_t FrameCnt
relative framecount or frame number.
Definition digxel.hpp:310
Implementation namespace for support and library code.
Marker types to indicate a literal string and a Symbol.
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition symbol.hpp:60
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...