Merge lp:~smspillaz/compiz-core/compiz-core.option-variant-type-fixes into lp:compiz-core/0.9.5

Proposed by Sam Spilsbury
Status: Merged
Merged at revision: 2965
Proposed branch: lp:~smspillaz/compiz-core/compiz-core.option-variant-type-fixes
Merge into: lp:compiz-core/0.9.5
Diff against target: 372 lines (+243/-38)
4 files modified
include/core/abiversion.h (+1/-1)
include/core/option.h (+115/-34)
src/option.cpp (+1/-1)
src/option/tests/option.cpp (+126/-2)
To merge this branch: bzr merge lp:~smspillaz/compiz-core/compiz-core.option-variant-type-fixes
Reviewer Review Type Date Requested Status
Daniel van Vugt Needs Fixing
Alan Griffiths Approve
Thomas Voß Approve
Review via email: mp+90242@code.launchpad.net

Description of the change

Fixes up a bunch of boost::variant edge cases

1. Fixes a case where const char * would be converted to bool, add a wrapper to ensure it gets converted to CompString
2. Fixes a case where unsigned short [] and unsigned short * would get converted to bool also, add a wrapper to ensure they are stored correctly
3. Adds some template specializations for the above, especially the reading part.

To post a comment you must log in.
Revision history for this message
Thomas Voß (thomas-voss) wrote :

Looks OK to me. Unit tests are passing and have been extended to check for the edge cases together with boost::variant. Nevertheless, the modelling of a color should be adjusted to an (unambiguous) struct in the future.

review: Approve
Revision history for this message
Sam Spilsbury (smspillaz) wrote :

+1

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

OK

review: Approve
Revision history for this message
Daniel van Vugt (vanvugt) wrote :

Conflict encountered while I was trying to merge this...

include/core/option.h:
<<<<<<< TREE
                template<typename T>
                void set (const T & t)
                {
                    mValue = t;
                }

                template<typename T>
                const T & get () const
                {
                    try
                    {
                        return boost::get<T> (mValue);
                    }
                    catch (...)
                    {
                        static T inst;
                        return inst;
                    }
                }
=======

                template<typename T>
                void set (const T & t);

                void set( unsigned short const (&color)[4] )
                {
                    mValue = ColorVector (color, color + 4);
                }

                void set (const char *c)
                {
                    mValue = CompString (c);
                }

                template<typename T>
                const T & get () const;
>>>>>>> MERGE-SOURCE

review: Needs Fixing

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'include/core/abiversion.h'
--- include/core/abiversion.h 2012-01-24 08:24:18 +0000
+++ include/core/abiversion.h 2012-01-26 08:18:25 +0000
@@ -5,6 +5,6 @@
5# error Conflicting definitions of CORE_ABIVERSION5# error Conflicting definitions of CORE_ABIVERSION
6#endif6#endif
77
8#define CORE_ABIVERSION 201201238#define CORE_ABIVERSION 20120125
99
10#endif // COMPIZ_ABIVERSION_H10#endif // COMPIZ_ABIVERSION_H
1111
=== modified file 'include/core/option.h'
--- include/core/option.h 2012-01-24 15:22:49 +0000
+++ include/core/option.h 2012-01-26 08:18:25 +0000
@@ -42,6 +42,7 @@
42class CompMatch;42class CompMatch;
43class CompScreen;43class CompScreen;
4444
45
45/**46/**
46 * A configuration option with boolean, int, float, String, Color, Key, Button,47 * A configuration option with boolean, int, float, String, Color, Key, Button,
47 * Edge, Bell, or List.48 * Edge, Bell, or List.
@@ -74,16 +75,18 @@
74 class Value {75 class Value {
75 public:76 public:
7677
77 typedef boost::variant<78 typedef std::vector <unsigned short> ColorVector;
78 bool,79
79 int,80 typedef boost::variant<
80 float,81 bool,
81 CompString,82 int,
82 unsigned short*,83 float,
83 boost::recursive_wrapper<CompAction>,84 CompString,
84 boost::recursive_wrapper<CompMatch>,85 boost::recursive_wrapper<ColorVector>,
85 boost::recursive_wrapper<std::vector<Value> >86 boost::recursive_wrapper<CompAction>,
86 > variant_type;87 boost::recursive_wrapper<CompMatch>,
88 boost::recursive_wrapper<std::vector<Value> >
89 > variant_type;
8790
88 typedef std::vector<Value> Vector;91 typedef std::vector<Value> Vector;
8992
@@ -93,8 +96,15 @@
93 }96 }
9497
95 template<typename T>98 template<typename T>
96 Value( const T & t ) : mListType(TypeUnset),99 Value( const T & t );
97 mValue(t)100
101 Value( unsigned short const (&color)[4] ) : mListType(TypeUnset),
102 mValue (ColorVector (color, color + 4))
103 {
104 }
105
106 Value( const char *c ) : mListType (TypeUnset),
107 mValue (CompString (c))
98 {108 {
99 }109 }
100110
@@ -112,28 +122,22 @@
112 return mListType;122 return mListType;
113 }123 }
114124
115 template<typename T>125
116 void set (const T & t)126 template<typename T>
117 {127 void set (const T & t);
118 mValue = t;128
119 }129 void set( unsigned short const (&color)[4] )
120130 {
121 /* In order to be exception safe, this MUST131 mValue = ColorVector (color, color + 4);
122 * return a copy. Prefer to use a specific132 }
123 * member instead */133
124 template<typename T>134 void set (const char *c)
125 const T & get (const T & defaultValue = T ()) const135 {
126 {136 mValue = CompString (c);
127 try137 }
128 {138
129 return boost::get<T> (mValue);139 template<typename T>
130 }140 const T & get () const;
131 catch (...)
132 {
133 static T inst;
134 return inst;
135 }
136 }
137141
138 void142 void
139 set (Type t, const Vector & v);143 set (Type t, const Vector & v);
@@ -296,6 +300,83 @@
296 PrivateOption *priv;300 PrivateOption *priv;
297};301};
298302
303namespace compiz {
304namespace detail {
305
306template<typename Type>
307inline
308Type const& CompOption_Value_get(CompOption::Value::variant_type const& mValue)
309{
310 try
311 {
312 return boost::get<Type> (mValue);
313 }
314 catch (...)
315 {
316 static Type inst;
317 return inst;
318 }
319}
320
321template<>
322inline
323short unsigned int * const& CompOption_Value_get<short unsigned int *>(CompOption::Value::variant_type const& mValue)
324{
325 try
326 {
327 static short unsigned int * some = 0;
328 CompOption::Value::ColorVector const& tmp(boost::get<CompOption::Value::ColorVector>(mValue));
329
330 some = const_cast<unsigned short *> (&(tmp[0]));
331 return some;
332 }
333 catch (...)
334 {
335 static short unsigned int * none = 0;
336 return none;
337 }
338}
339
340template<typename Type>
341inline
342void CompOption_Value_set (CompOption::Value::variant_type & mValue, Type &t)
343{
344 mValue = t;
345}
346
347template <>
348inline
349void CompOption_Value_set<unsigned short *> (CompOption::Value::variant_type & mValue, unsigned short * &t)
350{
351 mValue = CompOption::Value::ColorVector (t, t + 4);
352}
353
354}
355}
356
357template<typename T>
358inline
359const T & CompOption::Value::get () const
360{
361 return compiz::detail::CompOption_Value_get<T>(mValue);
362}
363
364template<typename T>
365inline
366void CompOption::Value::set (const T & t)
367{
368 return compiz::detail::CompOption_Value_set<T>(mValue, const_cast <T &> (t));
369}
370
371template<typename T>
372inline
373CompOption::Value::Value (const T & t) :
374 mListType (CompOption::TypeUnset)
375{
376 set (t);
377}
378
379
299extern CompOption::Vector noOptions;380extern CompOption::Vector noOptions;
300381
301#endif382#endif
302383
=== modified file 'src/option.cpp'
--- src/option.cpp 2012-01-24 14:16:08 +0000
+++ src/option.cpp 2012-01-26 08:18:25 +0000
@@ -138,7 +138,7 @@
138{138{
139 try139 try
140 {140 {
141 return boost::get<unsigned short*>(mValue);141 return const_cast <unsigned short *> (&((boost::get<ColorVector>(mValue))[0]));
142 }142 }
143 catch (...)143 catch (...)
144 {144 {
145145
=== modified file 'src/option/tests/option.cpp'
--- src/option/tests/option.cpp 2012-01-18 16:56:31 +0000
+++ src/option/tests/option.cpp 2012-01-26 08:18:25 +0000
@@ -6,6 +6,7 @@
6#include "core/option.h"6#include "core/option.h"
77
8namespace {8namespace {
9
9 template<typename T>10 template<typename T>
10 void11 void
11 check_type_value(CompOption::Type type, const T & value)12 check_type_value(CompOption::Type type, const T & value)
@@ -15,6 +16,44 @@
15 ASSERT_EQ(v.type(),type);16 ASSERT_EQ(v.type(),type);
16 ASSERT_EQ (v.get<T>(),value);17 ASSERT_EQ (v.get<T>(),value);
17 }18 }
19
20 template<>
21 void
22 check_type_value(CompOption::Type type, const unsigned short (& value)[4])
23 {
24 CompOption::Value v;
25 v.set(value);
26 ASSERT_EQ(v.type(),type);
27 unsigned short * color = v.get<unsigned short*>();
28 ASSERT_NE((void*)0, color);
29 for (int i = 0; i != 4; ++i) ASSERT_EQ(value[i], color[i]);
30 }
31
32 unsigned short testColor[4] = {255, 0, 255, 0};
33 unsigned short testColor2[4] = {0, 255, 0, 255};
34
35 template<typename T>
36 void
37 check_list_type(CompOption::Type listType, CompOption::Value::Vector &list)
38 {
39 CompOption::Value vl;
40 vl.set (list);
41
42 ASSERT_EQ (vl.type (), CompOption::TypeList);
43 ASSERT_EQ (vl.get <CompOption::Value::Vector> (), list);
44
45 for (CompOption::Value::Vector::const_iterator it = vl.get <CompOption::Value::Vector> ().begin ();
46 it != vl.get <CompOption::Value::Vector> ().end ();
47 it++)
48 {
49 T inst;
50 CompOption::Value value (inst);
51
52 const CompOption::Value &v (*it);
53
54 ASSERT_EQ (v.type (), value.type ());
55 }
56 }
18}57}
1958
20TEST(CompOption,Value)59TEST(CompOption,Value)
@@ -25,15 +64,100 @@
2564
26 check_type_value<int> (CompOption::TypeInt, 1);65 check_type_value<int> (CompOption::TypeInt, 1);
27 check_type_value<float> (CompOption::TypeFloat, 1.f);66 check_type_value<float> (CompOption::TypeFloat, 1.f);
28 check_type_value<CompString> (CompOption::TypeString, CompString("Check"));67 check_type_value<CompString> (CompOption::TypeString, CompString ("Check"));
2968 check_type_value<CompString> (CompOption::TypeString, "core");
69
30 check_type_value<CompAction> (CompOption::TypeAction, CompAction());70 check_type_value<CompAction> (CompOption::TypeAction, CompAction());
31 check_type_value<CompMatch> (CompOption::TypeMatch, CompMatch());71 check_type_value<CompMatch> (CompOption::TypeMatch, CompMatch());
3272
73 check_type_value<unsigned short[4]> (CompOption::TypeColor, testColor);
74
33 check_type_value<CompOption::Value::Vector> (CompOption::TypeList, CompOption::Value::Vector(5));75 check_type_value<CompOption::Value::Vector> (CompOption::TypeList, CompOption::Value::Vector(5));
3476
35 CompOption::Value v1, v2;77 CompOption::Value v1, v2;
36 ASSERT_EQ (v1,v2);78 ASSERT_EQ (v1,v2);
37 v1.set (CompString("SomeString"));79 v1.set (CompString("SomeString"));
38 ASSERT_TRUE(v1 != v2);80 ASSERT_TRUE(v1 != v2);
81
82 CompOption::Value::Vector vec;
83 CompOption::Value v;
84
85 v.set (true);
86 vec.push_back (v);
87 vec.push_back (v);
88
89 check_list_type<bool> (CompOption::TypeBool, vec);
90
91 vec.clear ();
92 v.set (CompString ("foo"));
93 vec.push_back (v);
94 vec.push_back (v);
95
96 check_list_type<CompString> (CompOption::TypeString, vec);
97}
98
99TEST(CompOption,Color)
100{
101
102 CompOption::Value value(testColor);
103
104 unsigned short * color = value.c();
105 ASSERT_NE((void*)0, color);
106 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
107
108 color = value.get<unsigned short*>();
109 ASSERT_NE((void*)0, color);
110 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
111
112 value.set(testColor2);
113
114 color = value.c();
115 ASSERT_NE((void*)0, color);
116 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
117
118 color = value.get<unsigned short*>();
119 ASSERT_NE((void*)0, color);
120 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
121
122 CompOption::Value v;
123
124 v.set (testColor);
125
126 color = v.c();
127 ASSERT_NE((void*)0, color);
128 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
129
130 color = v.get<unsigned short*>();
131 ASSERT_NE((void*)0, color);
132 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
133
134 v.set(testColor2);
135
136 color = v.c();
137 ASSERT_NE((void*)0, color);
138 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
139
140 color = v.get<unsigned short*>();
141 ASSERT_NE((void*)0, color);
142 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
143
144 v.set (static_cast <short unsigned int *> (testColor));
145
146 color = v.c();
147 ASSERT_NE((void*)0, color);
148 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
149
150 color = v.get<unsigned short*>();
151 ASSERT_NE((void*)0, color);
152 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
153
154 v.set(testColor2);
155
156 color = v.c();
157 ASSERT_NE((void*)0, color);
158 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
159
160 color = v.get<unsigned short*>();
161 ASSERT_NE((void*)0, color);
162 for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
39}163}

Subscribers

People subscribed via source and target branches