Merge lp:~thomas-voss/compiz-core/fix-option into lp:compiz-core/0.9.5
- fix-option
- Merge into 0.9.5
Status: | Merged |
---|---|
Merged at revision: | 2912 |
Proposed branch: | lp:~thomas-voss/compiz-core/fix-option |
Merge into: | lp:compiz-core/0.9.5 |
Diff against target: |
1047 lines (+331/-527) 9 files modified
include/core/action.h (+1/-1) include/core/option.h (+100/-52) src/CMakeLists.txt (+35/-14) src/action.cpp (+1/-1) src/option.cpp (+105/-433) src/privateoption.h (+0/-26) src/tests/CMakeLists.txt (+20/-0) src/tests/globals.h (+24/-0) src/tests/option.cpp (+45/-0) |
To merge this branch: | bzr merge lp:~thomas-voss/compiz-core/fix-option |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Sam Spilsbury | Approve | ||
Alan Griffiths | Approve | ||
Review via email:
|
Commit message
Description of the change
Replaced custom implementation of a discriminated union type in CompOption::Value with boost::variant, introduced a set of unit tests and cleaned up CompOption::Value.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Sam Spilsbury (smspillaz) wrote : | # |
Approving for now so we can get this merged, there are a few small things and some big things that we should beautify
21 +
22 +#include <boost/variant.hpp>
23 +
Whitespace
24 #include <vector>
25
26 class PrivateOption;
27 class PrivateValue;
28 class PrivateRestriction;
29 +
30 class CompAction;
31 class CompMatch;
32 class CompScreen;
33 @@ -54,12 +58,12 @@
34 TypeString,
35 TypeColor,
36 TypeAction,
37 + TypeMatch,
38 + TypeList,
39 TypeKey,
40 TypeButton,
41 TypeEdge,
42 TypeBell,
43 - TypeMatch,
44 - TypeList,
Seems redundant
212 -add_executable (compiz
213 - ${CMAKE_
214 +add_library (compiz_core
215 + ${CMAKE_
216 ${CMAKE_
217 - ${CMAKE_
218 ${CMAKE_
219 ${CMAKE_
220 ${CMAKE_
221 @@ -86,6 +85,10 @@
222 ${_bcop_sources}
223 )
224
225 +add_executable (compiz
226 + ${CMAKE_
227 +)
228 +
In general I agree with the idea of having a libcompiz, since it will be useful for other things later on, but I think for the tests we should strive to have each independently compiled module link to independent tests, rather than statically linking in compiz_core each time (executable bloat, increases link time, static initialization and destruction etc).
Of course in this case, CompOption has a hard dependency on CompScreen to dispatch option changes to plugin classes, so for now the refactoring required to abstract that out would be too large for this merge.
281 +add_subdirecto
282 +
Ideally follow the same directory layout practises
970 === added file 'src/tests/
971 --- src/tests/globals.h 1970-01-01 00:00:00 +0000
972 +++ src/tests/globals.h 2012-01-16 14:53:32 +0000
973 @@ -0,0 +1,24 @@
This reminds me that globals.h really really needs to die.
There are a few parts where there need to be whitespace between the caller and the brackets, eg
foo () , foo <bar> () not
foo<b>()
Preview Diff
1 | === modified file 'include/core/action.h' |
2 | --- include/core/action.h 2010-11-09 14:13:19 +0000 |
3 | +++ include/core/action.h 2012-01-16 14:53:32 +0000 |
4 | @@ -154,7 +154,7 @@ |
5 | |
6 | void copyState (const CompAction &action); |
7 | |
8 | - bool operator== (const CompAction& val); |
9 | + bool operator== (const CompAction& val) const; |
10 | CompAction & operator= (const CompAction &action); |
11 | |
12 | bool keyFromString (const CompString &str); |
13 | |
14 | === modified file 'include/core/option.h' |
15 | --- include/core/option.h 2011-10-31 13:51:00 +0000 |
16 | +++ include/core/option.h 2012-01-16 14:53:32 +0000 |
17 | @@ -29,11 +29,15 @@ |
18 | #define _COMPOPTION_H |
19 | |
20 | #include <core/string.h> |
21 | + |
22 | +#include <boost/variant.hpp> |
23 | + |
24 | #include <vector> |
25 | |
26 | class PrivateOption; |
27 | class PrivateValue; |
28 | class PrivateRestriction; |
29 | + |
30 | class CompAction; |
31 | class CompMatch; |
32 | class CompScreen; |
33 | @@ -54,12 +58,12 @@ |
34 | TypeString, |
35 | TypeColor, |
36 | TypeAction, |
37 | + TypeMatch, |
38 | + TypeList, |
39 | TypeKey, |
40 | TypeButton, |
41 | TypeEdge, |
42 | TypeBell, |
43 | - TypeMatch, |
44 | - TypeList, |
45 | /* internal use only */ |
46 | TypeUnset |
47 | } Type; |
48 | @@ -69,61 +73,105 @@ |
49 | */ |
50 | class Value { |
51 | public: |
52 | + |
53 | + typedef boost::variant< |
54 | + bool, |
55 | + int, |
56 | + float, |
57 | + CompString, |
58 | + unsigned short*, |
59 | + boost::recursive_wrapper<CompAction>, |
60 | + boost::recursive_wrapper<CompMatch>, |
61 | + boost::recursive_wrapper<std::vector<Value> > |
62 | + > variant_type; |
63 | + |
64 | typedef std::vector<Value> Vector; |
65 | |
66 | public: |
67 | - Value (); |
68 | - Value (const Value &); |
69 | - Value (const bool b); |
70 | - Value (const int i); |
71 | - Value (const float f); |
72 | - Value (const unsigned short *color); |
73 | - Value (const CompString& s); |
74 | - Value (const char *s); |
75 | - Value (const CompMatch& m); |
76 | - Value (const CompAction& a); |
77 | - Value (Type type, const Vector& l); |
78 | - ~Value (); |
79 | - |
80 | - Type type () const; |
81 | - |
82 | - void set (const bool b); |
83 | - void set (const int i); |
84 | - void set (const float f); |
85 | - void set (const unsigned short *color); |
86 | - void set (const CompString& s); |
87 | - void set (const char *s); |
88 | - void set (const CompMatch& m); |
89 | - void set (const CompAction& a); |
90 | - void set (Type type, const Vector& l); |
91 | - |
92 | - bool b (); |
93 | - int i (); |
94 | - float f (); |
95 | - unsigned short* c (); |
96 | - CompString s (); |
97 | - CompMatch & match (); |
98 | - CompAction & action (); |
99 | - Type listType (); |
100 | - Vector & list (); |
101 | - |
102 | - bool operator== (const Value& val); |
103 | - bool operator!= (const Value& val); |
104 | - Value & operator= (const Value &val); |
105 | - |
106 | - operator bool (); |
107 | - operator int (); |
108 | - operator float (); |
109 | - operator unsigned short * (); |
110 | - operator CompString (); |
111 | - operator CompMatch & (); |
112 | - operator CompAction & (); |
113 | - operator CompAction * (); |
114 | - operator Type (); |
115 | - operator Vector & (); |
116 | + Value () : mListType(TypeUnset) |
117 | + { |
118 | + } |
119 | + |
120 | + template<typename T> |
121 | + Value( const T & t ) : mListType(TypeUnset), |
122 | + mValue(t) |
123 | + { |
124 | + } |
125 | + |
126 | + ~Value(); |
127 | + |
128 | + Type |
129 | + type () const |
130 | + { |
131 | + return static_cast<Type>(mValue.which()); |
132 | + } |
133 | + |
134 | + Type |
135 | + listType () const |
136 | + { |
137 | + return mListType; |
138 | + } |
139 | + |
140 | + template<typename T> |
141 | + void set (const T & t) |
142 | + { |
143 | + mValue = t; |
144 | + } |
145 | + |
146 | + template<typename T> |
147 | + const T & get () const |
148 | + { |
149 | + return boost::get<T> (mValue); |
150 | + } |
151 | + |
152 | + void |
153 | + set (Type t, const Vector & v); |
154 | + |
155 | + bool |
156 | + b () const; |
157 | + |
158 | + int |
159 | + i () const; |
160 | + |
161 | + float |
162 | + f () const; |
163 | + |
164 | + unsigned short* |
165 | + c () const; |
166 | + |
167 | + const CompString & |
168 | + s () const; |
169 | + |
170 | + CompString & |
171 | + s (); |
172 | + |
173 | + const CompMatch & |
174 | + match () const; |
175 | + |
176 | + CompMatch & |
177 | + match (); |
178 | + |
179 | + const CompAction & |
180 | + action () const; |
181 | + |
182 | + CompAction & |
183 | + action (); |
184 | + |
185 | + const Vector & |
186 | + list () const; |
187 | + |
188 | + Vector & |
189 | + list (); |
190 | + |
191 | + bool |
192 | + operator== (const Value & rhs) const; |
193 | + |
194 | + bool |
195 | + operator!= (const Value & rhs) const; |
196 | |
197 | private: |
198 | - PrivateValue *priv; |
199 | + Type mListType; |
200 | + variant_type mValue; |
201 | }; |
202 | |
203 | /** |
204 | |
205 | === modified file 'src/CMakeLists.txt' |
206 | --- src/CMakeLists.txt 2012-01-12 13:44:07 +0000 |
207 | +++ src/CMakeLists.txt 2012-01-16 14:53:32 +0000 |
208 | @@ -59,10 +59,9 @@ |
209 | ${CORE_MOD_LIBRARY_DIRS} |
210 | ) |
211 | |
212 | -add_executable (compiz |
213 | - ${CMAKE_CURRENT_SOURCE_DIR}/region.cpp |
214 | +add_library (compiz_core |
215 | + ${CMAKE_CURRENT_SOURCE_DIR}/region.cpp |
216 | ${CMAKE_CURRENT_SOURCE_DIR}/atoms.cpp |
217 | - ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp |
218 | ${CMAKE_CURRENT_SOURCE_DIR}/actions.cpp |
219 | ${CMAKE_CURRENT_SOURCE_DIR}/screen.cpp |
220 | ${CMAKE_CURRENT_SOURCE_DIR}/window.cpp |
221 | @@ -86,6 +85,10 @@ |
222 | ${_bcop_sources} |
223 | ) |
224 | |
225 | +add_executable (compiz |
226 | + ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp |
227 | +) |
228 | + |
229 | # workaround for build race |
230 | add_dependencies (compiz core-xml-file) |
231 | |
232 | @@ -94,17 +97,33 @@ |
233 | PROPERTY CORE_MOD_LIBRARIES) |
234 | |
235 | target_link_libraries ( |
236 | - compiz ${COMPIZ_LIBRARIES} |
237 | - |
238 | - m |
239 | - pthread |
240 | - dl |
241 | - |
242 | - compiz_string |
243 | - compiz_timer |
244 | - compiz_logmessage |
245 | - compiz_pluginclasshandler |
246 | -# ${CORE_MOD_LIBRARIES} |
247 | + compiz_core |
248 | + |
249 | + ${COMPIZ_LIBRARIES} |
250 | + |
251 | + m |
252 | + pthread |
253 | + dl |
254 | + |
255 | + compiz_string |
256 | + compiz_timer |
257 | + compiz_logmessage |
258 | + compiz_pluginclasshandler |
259 | +) |
260 | + |
261 | +target_link_libraries ( |
262 | + compiz |
263 | + compiz_core |
264 | + ${COMPIZ_LIBRARIES} |
265 | + |
266 | + m |
267 | + pthread |
268 | + dl |
269 | + |
270 | + compiz_string |
271 | + compiz_timer |
272 | + compiz_logmessage |
273 | + compiz_pluginclasshandler |
274 | ) |
275 | |
276 | install ( |
277 | @@ -112,4 +131,6 @@ |
278 | DESTINATION ${COMPIZ_DESTDIR}${exec_prefix} |
279 | ) |
280 | |
281 | +add_subdirectory(tests) |
282 | + |
283 | enable_coverage_report( TARGETS compiz ) |
284 | |
285 | === modified file 'src/action.cpp' |
286 | --- src/action.cpp 2011-10-31 13:51:00 +0000 |
287 | +++ src/action.cpp 2012-01-16 14:53:32 +0000 |
288 | @@ -455,7 +455,7 @@ |
289 | } |
290 | |
291 | bool |
292 | -CompAction::operator== (const CompAction& val) |
293 | +CompAction::operator== (const CompAction& val) const |
294 | { |
295 | if (priv->state != val.priv->state) |
296 | return false; |
297 | |
298 | === modified file 'src/option.cpp' |
299 | --- src/option.cpp 2010-11-11 03:14:20 +0000 |
300 | +++ src/option.cpp 2012-01-16 14:53:32 +0000 |
301 | @@ -32,164 +32,14 @@ |
302 | #include <boost/foreach.hpp> |
303 | #define foreach BOOST_FOREACH |
304 | |
305 | +#include <core/action.h> |
306 | #include <core/core.h> |
307 | +#include <core/match.h> |
308 | #include <core/option.h> |
309 | #include "privateoption.h" |
310 | |
311 | CompOption::Vector noOptions (0); |
312 | |
313 | -CompOption::Value::Value () : |
314 | - priv (new PrivateValue ()) |
315 | -{ |
316 | -} |
317 | - |
318 | -CompOption::Value::Value (const Value &v) : |
319 | - priv (new PrivateValue (*v.priv)) |
320 | -{ |
321 | -} |
322 | - |
323 | -CompOption::Value::~Value () |
324 | -{ |
325 | - delete priv; |
326 | -} |
327 | - |
328 | -CompOption::Value::Value (const bool b) : |
329 | - priv (new PrivateValue ()) |
330 | -{ |
331 | - set (b); |
332 | -} |
333 | - |
334 | -CompOption::Value::Value (const int i) : |
335 | - priv (new PrivateValue ()) |
336 | -{ |
337 | - set (i); |
338 | -} |
339 | - |
340 | -CompOption::Value::Value (const float f) : |
341 | - priv (new PrivateValue ()) |
342 | -{ |
343 | - set (f); |
344 | -} |
345 | - |
346 | -CompOption::Value::Value (const unsigned short *color) : |
347 | - priv (new PrivateValue ()) |
348 | -{ |
349 | - set (color); |
350 | -} |
351 | - |
352 | -CompOption::Value::Value (const CompString& s) : |
353 | - priv (new PrivateValue ()) |
354 | -{ |
355 | - set (s); |
356 | -} |
357 | - |
358 | -CompOption::Value::Value (const char *s) : |
359 | - priv (new PrivateValue ()) |
360 | -{ |
361 | - set (s); |
362 | -} |
363 | - |
364 | - |
365 | -CompOption::Value::Value (const CompMatch& m) : |
366 | - priv (new PrivateValue ()) |
367 | -{ |
368 | - set (m); |
369 | -} |
370 | - |
371 | -CompOption::Value::Value (const CompAction& a) : |
372 | - priv (new PrivateValue ()) |
373 | -{ |
374 | - set (a); |
375 | -} |
376 | - |
377 | -CompOption::Value::Value (CompOption::Type type, const Vector& l) : |
378 | - priv (new PrivateValue ()) |
379 | -{ |
380 | - set (type, l); |
381 | -} |
382 | - |
383 | -CompOption::Type |
384 | -CompOption::Value::type () const |
385 | -{ |
386 | - return priv->type; |
387 | -} |
388 | - |
389 | -void |
390 | -CompOption::Value::set (const bool b) |
391 | -{ |
392 | - priv->reset (); |
393 | - priv->type = CompOption::TypeBool; |
394 | - priv->value.b = b; |
395 | -} |
396 | - |
397 | -void |
398 | -CompOption::Value::set (const int i) |
399 | -{ |
400 | - priv->reset (); |
401 | - priv->type = CompOption::TypeInt; |
402 | - priv->value.i = i; |
403 | -} |
404 | - |
405 | -void |
406 | -CompOption::Value::set (const float f) |
407 | -{ |
408 | - priv->reset (); |
409 | - priv->type = CompOption::TypeFloat; |
410 | - priv->value.f = f; |
411 | -} |
412 | - |
413 | -void |
414 | -CompOption::Value::set (const unsigned short *color) |
415 | -{ |
416 | - priv->reset (); |
417 | - priv->type = CompOption::TypeColor; |
418 | - priv->value.c[0] = color[0]; |
419 | - priv->value.c[1] = color[1]; |
420 | - priv->value.c[2] = color[2]; |
421 | - priv->value.c[3] = color[3]; |
422 | -} |
423 | - |
424 | -void |
425 | -CompOption::Value::set (const CompString& s) |
426 | -{ |
427 | - priv->reset (); |
428 | - priv->type = CompOption::TypeString; |
429 | - priv->string = s; |
430 | -} |
431 | - |
432 | -void |
433 | -CompOption::Value::set (const char *s) |
434 | -{ |
435 | - priv->reset (); |
436 | - priv->type = CompOption::TypeString; |
437 | - priv->string = CompString (s); |
438 | -} |
439 | - |
440 | -void |
441 | -CompOption::Value::set (const CompMatch& m) |
442 | -{ |
443 | - priv->reset (); |
444 | - priv->type = CompOption::TypeMatch; |
445 | - priv->match = m; |
446 | -} |
447 | - |
448 | -void |
449 | -CompOption::Value::set (const CompAction& a) |
450 | -{ |
451 | - priv->reset (); |
452 | - priv->type = CompOption::TypeAction; |
453 | - priv->action = a; |
454 | -} |
455 | - |
456 | -void |
457 | -CompOption::Value::set (CompOption::Type type, const Vector& l) |
458 | -{ |
459 | - priv->reset (); |
460 | - priv->type = CompOption::TypeList; |
461 | - priv->list = l; |
462 | - priv->listType = type; |
463 | -} |
464 | - |
465 | static bool |
466 | checkIsAction (CompOption::Type type) |
467 | { |
468 | @@ -207,307 +57,130 @@ |
469 | return false; |
470 | } |
471 | |
472 | -bool |
473 | -CompOption::Value::b () |
474 | -{ |
475 | - if (!priv->checkType (CompOption::TypeBool)) |
476 | - return false; |
477 | - |
478 | - return priv->value.b; |
479 | -} |
480 | - |
481 | -int |
482 | -CompOption::Value::i () |
483 | -{ |
484 | - if (!priv->checkType (CompOption::TypeInt)) |
485 | - return 0; |
486 | - |
487 | - return priv->value.i; |
488 | -} |
489 | - |
490 | -float |
491 | -CompOption::Value::f () |
492 | -{ |
493 | - if (!priv->checkType (CompOption::TypeFloat)) |
494 | - return 0.0; |
495 | - |
496 | - return priv->value.f; |
497 | -} |
498 | + |
499 | |
500 | static unsigned short defaultColor[4] = { 0x0, 0x0, 0x0, 0xffff}; |
501 | |
502 | -unsigned short * |
503 | -CompOption::Value::c () |
504 | -{ |
505 | - if (!priv->checkType (CompOption::TypeColor)) |
506 | - return reinterpret_cast<unsigned short *> (defaultColor); |
507 | - |
508 | - return priv->value.c; |
509 | -} |
510 | - |
511 | -CompString |
512 | + |
513 | +static void |
514 | +finiOptionValue (CompOption::Value &v) |
515 | +{ |
516 | + switch (v.type()) { |
517 | + case CompOption::TypeAction: |
518 | + case CompOption::TypeKey: |
519 | + case CompOption::TypeButton: |
520 | + case CompOption::TypeEdge: |
521 | + case CompOption::TypeBell: |
522 | + if (v.action ().state () & CompAction::StateAutoGrab && screen) |
523 | + screen->removeAction (&v.action ()); |
524 | + break; |
525 | + |
526 | + case CompOption::TypeList: |
527 | + foreach (CompOption::Value &val, v.list ()) |
528 | + finiOptionValue (val); |
529 | + break; |
530 | + |
531 | + default: |
532 | + break; |
533 | + } |
534 | +} |
535 | + |
536 | +CompOption::Value::~Value() |
537 | +{ |
538 | + finiOptionValue(*this); |
539 | +} |
540 | + |
541 | +void |
542 | +CompOption::Value::set (Type t, const CompOption::Value::Vector & v) |
543 | +{ |
544 | + mListType = t; |
545 | + mValue = v; |
546 | +} |
547 | + |
548 | +bool |
549 | +CompOption::Value::b () const |
550 | +{ |
551 | + return boost::get<bool>(mValue); |
552 | +} |
553 | + |
554 | +int |
555 | +CompOption::Value::i () const |
556 | +{ |
557 | + return boost::get<int>(mValue); |
558 | +} |
559 | + |
560 | +float |
561 | +CompOption::Value::f () const |
562 | +{ |
563 | + return boost::get<float>(mValue); |
564 | +} |
565 | + |
566 | +unsigned short* |
567 | +CompOption::Value::c () const |
568 | +{ |
569 | + return boost::get<unsigned short*>(mValue); |
570 | +} |
571 | + |
572 | +const CompString & |
573 | +CompOption::Value::s () const |
574 | +{ |
575 | + return boost::get<CompString>(mValue); |
576 | +} |
577 | + |
578 | +CompString & |
579 | CompOption::Value::s () |
580 | { |
581 | - if (!priv->checkType (CompOption::TypeString)) |
582 | - return ""; |
583 | + return boost::get < CompString > (mValue); |
584 | +} |
585 | |
586 | - return priv->string; |
587 | +const CompMatch & |
588 | +CompOption::Value::match () const |
589 | +{ |
590 | + return boost::get<CompMatch>(mValue); |
591 | } |
592 | |
593 | CompMatch & |
594 | CompOption::Value::match () |
595 | { |
596 | - priv->checkType (CompOption::TypeMatch); |
597 | + return boost::get<CompMatch>(mValue); |
598 | +} |
599 | |
600 | - return priv->match; |
601 | +const CompAction & |
602 | +CompOption::Value::action () const |
603 | +{ |
604 | + return boost::get<CompAction>(mValue); |
605 | } |
606 | |
607 | CompAction & |
608 | CompOption::Value::action () |
609 | { |
610 | - priv->checkType (priv->type); |
611 | - |
612 | - if (!checkIsAction (priv->type)) |
613 | - compLogMessage ("core", CompLogLevelWarn, |
614 | - "CompOption::Value not an action"); |
615 | - |
616 | - return priv->action; |
617 | + return boost::get<CompAction>(mValue); |
618 | } |
619 | |
620 | -CompOption::Type |
621 | -CompOption::Value::listType () |
622 | +// Type listType () const; |
623 | + |
624 | +const CompOption::Value::Vector & |
625 | +CompOption::Value::list () const |
626 | { |
627 | - priv->checkType (CompOption::TypeList); |
628 | - |
629 | - return priv->listType; |
630 | + return boost::get< std::vector<Value> >(mValue); |
631 | } |
632 | |
633 | CompOption::Value::Vector & |
634 | CompOption::Value::list () |
635 | { |
636 | - priv->checkType (CompOption::TypeList); |
637 | - |
638 | - return priv->list; |
639 | -} |
640 | - |
641 | -CompOption::Value::operator bool () |
642 | -{ |
643 | - return b (); |
644 | -} |
645 | - |
646 | -CompOption::Value::operator int () |
647 | -{ |
648 | - return i (); |
649 | -} |
650 | - |
651 | -CompOption::Value::operator float () |
652 | -{ |
653 | - return f (); |
654 | -} |
655 | - |
656 | -CompOption::Value::operator unsigned short * () |
657 | -{ |
658 | - return c (); |
659 | -} |
660 | - |
661 | -CompOption::Value::operator CompString () |
662 | -{ |
663 | - return s (); |
664 | -} |
665 | - |
666 | -CompOption::Value::operator CompMatch & () |
667 | -{ |
668 | - return match (); |
669 | -} |
670 | - |
671 | -CompOption::Value::operator CompAction & () |
672 | -{ |
673 | - return action (); |
674 | -} |
675 | - |
676 | -CompOption::Value::operator CompAction * () |
677 | -{ |
678 | - return &action (); |
679 | -} |
680 | - |
681 | -CompOption::Value::operator Type () |
682 | -{ |
683 | - return listType (); |
684 | -} |
685 | - |
686 | -CompOption::Value::operator Vector & () |
687 | -{ |
688 | - return list (); |
689 | -} |
690 | - |
691 | -bool |
692 | -CompOption::Value::operator== (const CompOption::Value &val) |
693 | -{ |
694 | - if (priv->type != val.priv->type) |
695 | - return false; |
696 | - |
697 | - switch (priv->type) |
698 | - { |
699 | - case CompOption::TypeBool: |
700 | - return priv->value.b == val.priv->value.b; |
701 | - break; |
702 | - |
703 | - case CompOption::TypeInt: |
704 | - return priv->value.i == val.priv->value.i; |
705 | - break; |
706 | - |
707 | - case CompOption::TypeFloat: |
708 | - return priv->value.f == val.priv->value.f; |
709 | - break; |
710 | - |
711 | - case CompOption::TypeColor: |
712 | - return (priv->value.c[0] == val.priv->value.c[0]) && |
713 | - (priv->value.c[1] == val.priv->value.c[1]) && |
714 | - (priv->value.c[2] == val.priv->value.c[2]) && |
715 | - (priv->value.c[3] == val.priv->value.c[3]); |
716 | - break; |
717 | - |
718 | - case CompOption::TypeString: |
719 | - return priv->string.compare (val.priv->string) == 0; |
720 | - break; |
721 | - |
722 | - case CompOption::TypeMatch: |
723 | - return priv->match == val.priv->match; |
724 | - break; |
725 | - |
726 | - case CompOption::TypeAction: |
727 | - return priv->action == val.priv->action; |
728 | - break; |
729 | - |
730 | - case CompOption::TypeList: |
731 | - if (priv->listType != val.priv->listType) |
732 | - return false; |
733 | - |
734 | - if (priv->list.size () != val.priv->list.size ()) |
735 | - return false; |
736 | - |
737 | - for (unsigned int i = 0; i < priv->list.size (); i++) |
738 | - if (priv->list[i] != val.priv->list[i]) |
739 | - return false; |
740 | - |
741 | - return true; |
742 | - break; |
743 | - |
744 | - default: |
745 | - break; |
746 | - } |
747 | - |
748 | - return true; |
749 | -} |
750 | - |
751 | -bool |
752 | -CompOption::Value::operator!= (const CompOption::Value &val) |
753 | -{ |
754 | - return !(*this == val); |
755 | -} |
756 | - |
757 | -static void |
758 | -finiOptionValue (CompOption::Value &v, |
759 | - CompOption::Type type) |
760 | -{ |
761 | - switch (type) { |
762 | - case CompOption::TypeAction: |
763 | - case CompOption::TypeKey: |
764 | - case CompOption::TypeButton: |
765 | - case CompOption::TypeEdge: |
766 | - case CompOption::TypeBell: |
767 | - if (v.action ().state () & CompAction::StateAutoGrab && screen) |
768 | - screen->removeAction (&v.action ()); |
769 | - break; |
770 | - |
771 | - case CompOption::TypeList: |
772 | - foreach (CompOption::Value &val, v.list ()) |
773 | - finiOptionValue (val, v.listType ()); |
774 | - break; |
775 | - |
776 | - default: |
777 | - break; |
778 | - } |
779 | -} |
780 | - |
781 | -CompOption::Value & |
782 | -CompOption::Value::operator= (const CompOption::Value &val) |
783 | -{ |
784 | - if (this == &val) |
785 | - return *this; |
786 | - |
787 | - finiOptionValue (*this, priv->type); |
788 | - |
789 | - delete priv; |
790 | - priv = new PrivateValue (*val.priv); |
791 | - |
792 | - return *this; |
793 | -} |
794 | - |
795 | -PrivateValue::PrivateValue () : |
796 | - type (CompOption::TypeUnset), |
797 | - string (""), |
798 | - action (), |
799 | - match (), |
800 | - listType (CompOption::TypeUnset), |
801 | - list () |
802 | -{ |
803 | - memset (&value, 0, sizeof (ValueUnion)); |
804 | -} |
805 | - |
806 | -PrivateValue::PrivateValue (const PrivateValue& p) : |
807 | - type (p.type), |
808 | - string (p.string), |
809 | - action (p.action), |
810 | - match (p.match), |
811 | - listType (p.listType), |
812 | - list (p.list) |
813 | -{ |
814 | - memcpy (&value, &p.value, sizeof (ValueUnion)); |
815 | -} |
816 | - |
817 | -bool |
818 | -PrivateValue::checkType (CompOption::Type refType) |
819 | -{ |
820 | - if (type == CompOption::TypeUnset) |
821 | - { |
822 | - compLogMessage ("core", CompLogLevelWarn, |
823 | - "Value type is not yet set"); |
824 | - return false; |
825 | - } |
826 | - |
827 | - if (type != refType) |
828 | - { |
829 | - compLogMessage ("core", CompLogLevelWarn, |
830 | - "Value type does not match (is %d, expected %d)", |
831 | - type, refType); |
832 | - return false; |
833 | - } |
834 | - |
835 | - return true; |
836 | -} |
837 | - |
838 | -void |
839 | -PrivateValue::reset () |
840 | -{ |
841 | - switch (type) { |
842 | - case CompOption::TypeString: |
843 | - string = ""; |
844 | - break; |
845 | - case CompOption::TypeMatch: |
846 | - match = CompMatch (); |
847 | - break; |
848 | - case CompOption::TypeAction: |
849 | - action = CompAction (); |
850 | - break; |
851 | - case CompOption::TypeList: |
852 | - list.clear (); |
853 | - listType = CompOption::TypeBool; |
854 | - break; |
855 | - default: |
856 | - break; |
857 | - } |
858 | - type = CompOption::TypeBool; |
859 | + return boost::get< std::vector<Value> >(mValue); |
860 | +} |
861 | + |
862 | +bool |
863 | +CompOption::Value::operator== (const Value & rhs) const |
864 | +{ |
865 | + return mValue == rhs.mValue; |
866 | +} |
867 | + |
868 | +bool |
869 | +CompOption::Value::operator!= (const Value & rhs) const |
870 | +{ |
871 | + return !(mValue == rhs.mValue); |
872 | } |
873 | |
874 | CompOption::Restriction::Restriction () : |
875 | @@ -664,10 +337,9 @@ |
876 | |
877 | static void |
878 | finiScreenOptionValue (CompScreen *s, |
879 | - CompOption::Value &v, |
880 | - CompOption::Type type) |
881 | + CompOption::Value &v) |
882 | { |
883 | - switch (type) { |
884 | + switch (v.type()) { |
885 | case CompOption::TypeAction: |
886 | case CompOption::TypeKey: |
887 | case CompOption::TypeButton: |
888 | @@ -679,7 +351,7 @@ |
889 | |
890 | case CompOption::TypeList: |
891 | foreach (CompOption::Value &val, v.list ()) |
892 | - finiScreenOptionValue (s, val, v.listType ()); |
893 | + finiScreenOptionValue (s, val); |
894 | break; |
895 | |
896 | default: |
897 | @@ -689,7 +361,7 @@ |
898 | |
899 | CompOption::~CompOption () |
900 | { |
901 | - finiOptionValue (priv->value, priv->type); |
902 | + finiOptionValue (priv->value); |
903 | delete priv; |
904 | } |
905 | |
906 | |
907 | === modified file 'src/privateoption.h' |
908 | --- src/privateoption.h 2010-11-09 14:13:19 +0000 |
909 | +++ src/privateoption.h 2012-01-16 14:53:32 +0000 |
910 | @@ -56,32 +56,6 @@ |
911 | RestrictionUnion rest; |
912 | }; |
913 | |
914 | -typedef union { |
915 | - bool b; |
916 | - int i; |
917 | - float f; |
918 | - unsigned short c[4]; |
919 | -} ValueUnion; |
920 | - |
921 | -class PrivateValue { |
922 | - public: |
923 | - PrivateValue (); |
924 | - PrivateValue (const PrivateValue&); |
925 | - |
926 | - void reset (); |
927 | - bool checkType (CompOption::Type refType); |
928 | - |
929 | - CompOption::Type type; |
930 | - ValueUnion value; |
931 | - CompString string; |
932 | - CompAction action; |
933 | - CompMatch match; |
934 | - CompOption::Type listType; |
935 | - CompOption::Value::Vector list; |
936 | - |
937 | - bool active; |
938 | -}; |
939 | - |
940 | class PrivateOption |
941 | { |
942 | public: |
943 | |
944 | === added directory 'src/tests' |
945 | === added file 'src/tests/CMakeLists.txt' |
946 | --- src/tests/CMakeLists.txt 1970-01-01 00:00:00 +0000 |
947 | +++ src/tests/CMakeLists.txt 2012-01-16 14:53:32 +0000 |
948 | @@ -0,0 +1,20 @@ |
949 | +include_directories( |
950 | + ${CMAKE_CURRENT_SOURCE_DIR} |
951 | +) |
952 | + |
953 | +add_executable( |
954 | + compiz_option_test |
955 | + |
956 | + ${CMAKE_CURRENT_SOURCE_DIR}/option.cpp |
957 | +) |
958 | + |
959 | +target_link_libraries( |
960 | + compiz_option_test |
961 | + |
962 | + compiz_core |
963 | + |
964 | + ${GTEST_BOTH_LIBRARIES} |
965 | + ${CMAKE_THREAD_LIBS_INIT} # Link in pthread. |
966 | +) |
967 | + |
968 | +add_test( compiz_option_test compiz_option_test ) |
969 | |
970 | === added file 'src/tests/globals.h' |
971 | --- src/tests/globals.h 1970-01-01 00:00:00 +0000 |
972 | +++ src/tests/globals.h 2012-01-16 14:53:32 +0000 |
973 | @@ -0,0 +1,24 @@ |
974 | +#ifndef GLOBALS_H |
975 | +#define GLOBALS_H |
976 | + |
977 | +char *programName; |
978 | +char **programArgv; |
979 | +int programArgc; |
980 | + |
981 | +bool shutDown = false; |
982 | +bool restartSignal = false; |
983 | + |
984 | +CompWindow *lastFoundWindow = 0; |
985 | + |
986 | +bool replaceCurrentWm = false; |
987 | +bool indirectRendering = false; |
988 | +bool noDetection = false; |
989 | +bool useDesktopHints = false; |
990 | +bool debugOutput = false; |
991 | +bool useCow = true; |
992 | + |
993 | +std::list <CompString> initialPlugins; |
994 | + |
995 | +unsigned int pluginClassHandlerIndex = 0; |
996 | + |
997 | +#endif // GLOBALS_H |
998 | |
999 | === added file 'src/tests/option.cpp' |
1000 | --- src/tests/option.cpp 1970-01-01 00:00:00 +0000 |
1001 | +++ src/tests/option.cpp 2012-01-16 14:53:32 +0000 |
1002 | @@ -0,0 +1,45 @@ |
1003 | +#include <gtest/gtest.h> |
1004 | + |
1005 | +#include "core/core.h" |
1006 | +#include "core/action.h" |
1007 | +#include "core/match.h" |
1008 | +#include "core/option.h" |
1009 | + |
1010 | +#include "globals.h" |
1011 | + |
1012 | +namespace { |
1013 | + template<typename T> |
1014 | + void |
1015 | + check_type_value(CompOption::Type type, const T & value) |
1016 | + { |
1017 | + CompOption::Value v; |
1018 | + v.set(value); |
1019 | + ASSERT_EQ(v.type(),type); |
1020 | + ASSERT_EQ (v.get<T>(),value); |
1021 | + } |
1022 | +} |
1023 | + |
1024 | + |
1025 | + |
1026 | +static unsigned short defaultColor[4] = { 0x0, 0x0, 0x0, 0xffff}; |
1027 | + |
1028 | +TEST(CompOption,Value) |
1029 | +{ |
1030 | + |
1031 | + check_type_value<bool> (CompOption::TypeBool, true); |
1032 | + check_type_value<bool> (CompOption::TypeBool, false); |
1033 | + |
1034 | + check_type_value<int> (CompOption::TypeInt, 1); |
1035 | + check_type_value<float> (CompOption::TypeFloat, 1.f); |
1036 | + check_type_value<CompString> (CompOption::TypeString, CompString("Check")); |
1037 | + |
1038 | + check_type_value<CompAction> (CompOption::TypeAction, CompAction()); |
1039 | + check_type_value<CompMatch> (CompOption::TypeMatch, CompMatch()); |
1040 | + |
1041 | + check_type_value<CompOption::Value::Vector> (CompOption::TypeList, CompOption::Value::Vector(5)); |
1042 | + |
1043 | + CompOption::Value v1, v2; |
1044 | + ASSERT_EQ (v1,v2); |
1045 | + v1.set (CompString("SomeString")); |
1046 | + ASSERT_TRUE(v1 != v2); |
1047 | +} |
While I think symmetric operators (e.g. operator==) should be free functions this looks a lot cleaner.
(Member symmetric operator functions cannot support type conversions symmetrically, non-member ones can - in this case it doesn't matter, but it is preferable to follow the same style always.)