Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JColorFacet.hh
Go to the documentation of this file.
1#ifndef __JLANG__JCOLORFACET__
2#define __JLANG__JCOLORFACET__
3
4#include <locale>
5#include <ostream>
6#include <string>
7#include <vector>
8#include <map>
9
10#include "JLang/JType.hh"
11#include "JLang/JTypeList.hh"
13#include "JLang/JException.hh"
14
15
16/**
17 * \author mdejong
18 */
19
20namespace JLANG {}
21namespace JPP { using namespace JLANG; }
22
23
24namespace JLANG {
25
26 /**
27 * Enumeration of text colors.
28 */
29 enum JColor_t {
30 RED, //!< red
31 GREEN, //!< green
32 BLUE, //!< blue
33 WHITE, //!< white
34 CYAN, //!< cyan
35 PURPLE, //!< purple
36 YELLOW, //!< yellow
37 RESET, //!< reset
38 BOLD //!< bold
39 };
40
41
42 /**
43 * Facet interface to specify text color.
44 * This class extends the std::locale::facet class.
45 */
46 struct JColorFacet :
47 public std::locale::facet
48 {
49 static std::locale::id id;
50
51
52 /**
53 * Constructor.
54 *
55 * \param refs reference count
56 */
57 JColorFacet(std::size_t refs = 0) :
58 std::locale::facet(refs)
59 {}
60
61
62 /**
63 * Print color.
64 *
65 * \param color code
66 * \return text
67 */
68 virtual const char* c_str(const JColor_t color) const = 0;
69
70
71 /**
72 * Clone this facet.
73 *
74 * \return pointer to newly created facet
75 */
76 virtual JColorFacet* clone() const = 0;
77
78
79 /**
80 * Check color.
81 *
82 * \param color code
83 * \return true if color; else false
84 */
85 static inline bool is_color(const int color)
86 {
87 return !is_bold(color) && !is_reset(color);
88 }
89
90
91 /**
92 * Check bold.
93 *
94 * \param color code
95 * \return true if bold; else false
96 */
97 static inline bool is_bold(const int color)
98 {
99 return color == BOLD;
100 }
101
102
103 /**
104 * Check reset.
105 *
106 * \param color code
107 * \return true if reset; else false
108 */
109 static inline bool is_reset(const int color)
110 {
111 return color == RESET;
112 }
113 private:
114
115 JColorFacet(const JColorFacet&); // not defined
116 void operator=(const JColorFacet&); // not defined
117 };
118
119
120 /**
121 * Facet class to specify text color for ASCII.
122 */
124 public JColorFacet
125 {
126 /**
127 * Get name of facet.
128 *
129 * \return name
130 */
131 static inline const char* getName()
132 {
133 return "ASCII";
134 }
135
136
137 /**
138 * Constructor.
139 *
140 * \param refs reference count
141 */
142 JColorFacetASCII(std::size_t refs = 0) :
143 JColorFacet(refs)
144 {}
145
146
147 /**
148 * Print color.
149 *
150 * \param color code
151 * \return text
152 */
153 virtual const char* c_str(const JColor_t color) const override
154 {
155 switch (color) {
156 case RED: return "\033[91m";
157 case GREEN: return "\033[92m";
158 case BLUE: return "\033[94m";
159 case WHITE: return "\033[97m";
160 case CYAN: return "\033[96m";
161 case PURPLE: return "\033[95m";
162 case YELLOW: return "\033[93m";
163 case BOLD: return "\033[1m";
164 case RESET: return "\033[0m";
165 default: return "";
166 }
167 }
168
169
170 /**
171 * Clone this facet.
172 *
173 * \return pointer to newly created facet
174 */
175 virtual JColorFacetASCII* clone() const override
176 {
177 return new JColorFacetASCII();
178 }
179 };
180
181
182 /**
183 * Facet class to specify text color for ELcode.
184 */
186 public JColorFacet
187 {
188 /**
189 * Get name of facet.
190 *
191 * \return name
192 */
193 static inline const char* getName()
194 {
195 return "ELcode";
196 }
197
198
199 /**
200 * Constructor.
201 *
202 * \param refs reference count
203 */
204 JColorFacetELcode(std::size_t refs = 0) :
205 JColorFacet(refs)
206 {}
207
208
209 /**
210 * Print color.
211 *
212 * \param color code
213 * \return text
214 */
215 virtual const char* c_str(const JColor_t color) const override
216 {
217 static std::string buffer;
218
219 history.push_back(color);
220
221 switch (color) {
222
223 case RED: return "[color=red]";
224 case GREEN: return "[color=green]";
225 case BLUE: return "[color=blue]";
226 case WHITE: return "[color=white]";
227 case CYAN: return "[color=cyan]";
228 case PURPLE: return "[color=purple]";
229 case YELLOW: return "[color=yellow]";
230 case BOLD: return "[bold]";
231
232 case RESET:
233
234 buffer.clear();
235
236 for (std::vector<int>::const_reverse_iterator i = history.rbegin(); i != history.rend(); ++i) {
237 if (is_color(*i))
238 buffer += "[/color]";
239 else if (is_bold (*i))
240 buffer += "[/bold]";
241 else
242 ;
243 }
244
245 history.clear();
246
247 return buffer.c_str();
248
249 default: return "";
250 }
251 }
252
253
254 /**
255 * Clone this facet.
256 *
257 * \return pointer to newly created facet
258 */
259 virtual JColorFacetELcode* clone() const override
260 {
261 return new JColorFacetELcode();
262 }
263
264 private:
266 };
267
268
269 /**
270 * Typelist of color facets.
271 */
273
274
275 /**
276 * Auxiliary map for color facets.
277 */
279 public std::map<std::string, JSinglePointer<JColorFacet> >
280 {
282 typedef typename map_type::key_type key_type;
283 typedef typename map_type::mapped_type mapped_type;
284 typedef typename map_type::value_type value_type;
285
286
287 /**
288 * Default constructor.
289 */
294
295
296 /**
297 * Get value for given key.
298 *
299 * Note that this method will throw an error if given key is absent.
300 *
301 * \param key key
302 * \return value
303 */
304 const mapped_type& operator[](const key_type& key) const
305 {
306 const_iterator p = find(key);
307
308 if (p != this->end()) {
309 return p->second;
310 }
311
312 THROW(JNullPointerException, "Invalid key " << key);
313 }
314
315
316 /**
317 * Insert data type.
318 *
319 * \param type data type
320 */
321 template<class T>
322 void operator()(const JType<T>& type)
323 {
324 insert(value_type(T::getName(), new T()));
325 }
326 };
327
328
329 /**
330 * Color facets.
331 */
333}
334
335
336/**
337 * Print color.
338 *
339 * \param out output stream
340 * \param color color
341 * \return output stream
342 */
343inline std::ostream& operator<<(std::ostream& out, const JLANG::JColor_t color)
344{
345 using namespace std;
346 using namespace JPP;
347
348 const locale& loc = out.getloc();
349
350 if (has_facet<JColorFacetASCII>(loc))
351 return out << use_facet<JColorFacetASCII> (loc).c_str(color);
352 else if (has_facet<JColorFacetELcode>(loc))
353 return out << use_facet<JColorFacetELcode>(loc).c_str(color);
354 else
355 return out << JColorFacetASCII().c_str(color);
356}
357
358#endif
std::ostream & operator<<(std::ostream &out, const JLANG::JColor_t color)
Print color.
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Exception for null pointer operation.
char * loc(char *orig)
Auxiliary classes and methods for language specific functionality.
void for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Definition JTypeList.hh:414
static const JColorFacetMap_t color_facets
Color facets.
JTYPELIST< JColorFacetASCII, JColorFacetELcode >::typelist JColorFacetTypes_t
Typelist of color facets.
JColor_t
Enumeration of text colors.
@ RED
red
@ PURPLE
purple
@ CYAN
cyan
@ RESET
reset
@ YELLOW
yellow
@ WHITE
white
@ GREEN
green
@ BOLD
bold
@ BLUE
blue
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Facet class to specify text color for ASCII.
virtual JColorFacetASCII * clone() const override
Clone this facet.
JColorFacetASCII(std::size_t refs=0)
Constructor.
virtual const char * c_str(const JColor_t color) const override
Print color.
static const char * getName()
Get name of facet.
Facet class to specify text color for ELcode.
virtual const char * c_str(const JColor_t color) const override
Print color.
virtual JColorFacetELcode * clone() const override
Clone this facet.
std::vector< int > history
static const char * getName()
Get name of facet.
JColorFacetELcode(std::size_t refs=0)
Constructor.
Auxiliary map for color facets.
std::map< std::string, JSinglePointer< JColorFacet > > map_type
map_type::mapped_type mapped_type
map_type::value_type value_type
void operator()(const JType< T > &type)
Insert data type.
const mapped_type & operator[](const key_type &key) const
Get value for given key.
JColorFacetMap_t()
Default constructor.
map_type::key_type key_type
Facet interface to specify text color.
static std::locale::id id
static bool is_reset(const int color)
Check reset.
static bool is_color(const int color)
Check color.
void operator=(const JColorFacet &)
static bool is_bold(const int color)
Check bold.
JColorFacet(std::size_t refs=0)
Constructor.
virtual const char * c_str(const JColor_t color) const =0
Print color.
virtual JColorFacet * clone() const =0
Clone this facet.
JColorFacet(const JColorFacet &)
Type list.
Definition JTypeList.hh:23
Auxiliary class for a type holder.
Definition JType.hh:19