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
1=== modified file 'include/core/abiversion.h'
2--- include/core/abiversion.h 2012-01-24 08:24:18 +0000
3+++ include/core/abiversion.h 2012-01-26 08:18:25 +0000
4@@ -5,6 +5,6 @@
5 # error Conflicting definitions of CORE_ABIVERSION
6 #endif
7
8-#define CORE_ABIVERSION 20120123
9+#define CORE_ABIVERSION 20120125
10
11 #endif // COMPIZ_ABIVERSION_H
12
13=== modified file 'include/core/option.h'
14--- include/core/option.h 2012-01-24 15:22:49 +0000
15+++ include/core/option.h 2012-01-26 08:18:25 +0000
16@@ -42,6 +42,7 @@
17 class CompMatch;
18 class CompScreen;
19
20+
21 /**
22 * A configuration option with boolean, int, float, String, Color, Key, Button,
23 * Edge, Bell, or List.
24@@ -74,16 +75,18 @@
25 class Value {
26 public:
27
28- typedef boost::variant<
29- bool,
30- int,
31- float,
32- CompString,
33- unsigned short*,
34- boost::recursive_wrapper<CompAction>,
35- boost::recursive_wrapper<CompMatch>,
36- boost::recursive_wrapper<std::vector<Value> >
37- > variant_type;
38+ typedef std::vector <unsigned short> ColorVector;
39+
40+ typedef boost::variant<
41+ bool,
42+ int,
43+ float,
44+ CompString,
45+ boost::recursive_wrapper<ColorVector>,
46+ boost::recursive_wrapper<CompAction>,
47+ boost::recursive_wrapper<CompMatch>,
48+ boost::recursive_wrapper<std::vector<Value> >
49+ > variant_type;
50
51 typedef std::vector<Value> Vector;
52
53@@ -93,8 +96,15 @@
54 }
55
56 template<typename T>
57- Value( const T & t ) : mListType(TypeUnset),
58- mValue(t)
59+ Value( const T & t );
60+
61+ Value( unsigned short const (&color)[4] ) : mListType(TypeUnset),
62+ mValue (ColorVector (color, color + 4))
63+ {
64+ }
65+
66+ Value( const char *c ) : mListType (TypeUnset),
67+ mValue (CompString (c))
68 {
69 }
70
71@@ -112,28 +122,22 @@
72 return mListType;
73 }
74
75- template<typename T>
76- void set (const T & t)
77- {
78- mValue = t;
79- }
80-
81- /* In order to be exception safe, this MUST
82- * return a copy. Prefer to use a specific
83- * member instead */
84- template<typename T>
85- const T & get (const T & defaultValue = T ()) const
86- {
87- try
88- {
89- return boost::get<T> (mValue);
90- }
91- catch (...)
92- {
93- static T inst;
94- return inst;
95- }
96- }
97+
98+ template<typename T>
99+ void set (const T & t);
100+
101+ void set( unsigned short const (&color)[4] )
102+ {
103+ mValue = ColorVector (color, color + 4);
104+ }
105+
106+ void set (const char *c)
107+ {
108+ mValue = CompString (c);
109+ }
110+
111+ template<typename T>
112+ const T & get () const;
113
114 void
115 set (Type t, const Vector & v);
116@@ -296,6 +300,83 @@
117 PrivateOption *priv;
118 };
119
120+namespace compiz {
121+namespace detail {
122+
123+template<typename Type>
124+inline
125+Type const& CompOption_Value_get(CompOption::Value::variant_type const& mValue)
126+{
127+ try
128+ {
129+ return boost::get<Type> (mValue);
130+ }
131+ catch (...)
132+ {
133+ static Type inst;
134+ return inst;
135+ }
136+}
137+
138+template<>
139+inline
140+short unsigned int * const& CompOption_Value_get<short unsigned int *>(CompOption::Value::variant_type const& mValue)
141+{
142+ try
143+ {
144+ static short unsigned int * some = 0;
145+ CompOption::Value::ColorVector const& tmp(boost::get<CompOption::Value::ColorVector>(mValue));
146+
147+ some = const_cast<unsigned short *> (&(tmp[0]));
148+ return some;
149+ }
150+ catch (...)
151+ {
152+ static short unsigned int * none = 0;
153+ return none;
154+ }
155+}
156+
157+template<typename Type>
158+inline
159+void CompOption_Value_set (CompOption::Value::variant_type & mValue, Type &t)
160+{
161+ mValue = t;
162+}
163+
164+template <>
165+inline
166+void CompOption_Value_set<unsigned short *> (CompOption::Value::variant_type & mValue, unsigned short * &t)
167+{
168+ mValue = CompOption::Value::ColorVector (t, t + 4);
169+}
170+
171+}
172+}
173+
174+template<typename T>
175+inline
176+const T & CompOption::Value::get () const
177+{
178+ return compiz::detail::CompOption_Value_get<T>(mValue);
179+}
180+
181+template<typename T>
182+inline
183+void CompOption::Value::set (const T & t)
184+{
185+ return compiz::detail::CompOption_Value_set<T>(mValue, const_cast <T &> (t));
186+}
187+
188+template<typename T>
189+inline
190+CompOption::Value::Value (const T & t) :
191+ mListType (CompOption::TypeUnset)
192+{
193+ set (t);
194+}
195+
196+
197 extern CompOption::Vector noOptions;
198
199 #endif
200
201=== modified file 'src/option.cpp'
202--- src/option.cpp 2012-01-24 14:16:08 +0000
203+++ src/option.cpp 2012-01-26 08:18:25 +0000
204@@ -138,7 +138,7 @@
205 {
206 try
207 {
208- return boost::get<unsigned short*>(mValue);
209+ return const_cast <unsigned short *> (&((boost::get<ColorVector>(mValue))[0]));
210 }
211 catch (...)
212 {
213
214=== modified file 'src/option/tests/option.cpp'
215--- src/option/tests/option.cpp 2012-01-18 16:56:31 +0000
216+++ src/option/tests/option.cpp 2012-01-26 08:18:25 +0000
217@@ -6,6 +6,7 @@
218 #include "core/option.h"
219
220 namespace {
221+
222 template<typename T>
223 void
224 check_type_value(CompOption::Type type, const T & value)
225@@ -15,6 +16,44 @@
226 ASSERT_EQ(v.type(),type);
227 ASSERT_EQ (v.get<T>(),value);
228 }
229+
230+ template<>
231+ void
232+ check_type_value(CompOption::Type type, const unsigned short (& value)[4])
233+ {
234+ CompOption::Value v;
235+ v.set(value);
236+ ASSERT_EQ(v.type(),type);
237+ unsigned short * color = v.get<unsigned short*>();
238+ ASSERT_NE((void*)0, color);
239+ for (int i = 0; i != 4; ++i) ASSERT_EQ(value[i], color[i]);
240+ }
241+
242+ unsigned short testColor[4] = {255, 0, 255, 0};
243+ unsigned short testColor2[4] = {0, 255, 0, 255};
244+
245+ template<typename T>
246+ void
247+ check_list_type(CompOption::Type listType, CompOption::Value::Vector &list)
248+ {
249+ CompOption::Value vl;
250+ vl.set (list);
251+
252+ ASSERT_EQ (vl.type (), CompOption::TypeList);
253+ ASSERT_EQ (vl.get <CompOption::Value::Vector> (), list);
254+
255+ for (CompOption::Value::Vector::const_iterator it = vl.get <CompOption::Value::Vector> ().begin ();
256+ it != vl.get <CompOption::Value::Vector> ().end ();
257+ it++)
258+ {
259+ T inst;
260+ CompOption::Value value (inst);
261+
262+ const CompOption::Value &v (*it);
263+
264+ ASSERT_EQ (v.type (), value.type ());
265+ }
266+ }
267 }
268
269 TEST(CompOption,Value)
270@@ -25,15 +64,100 @@
271
272 check_type_value<int> (CompOption::TypeInt, 1);
273 check_type_value<float> (CompOption::TypeFloat, 1.f);
274- check_type_value<CompString> (CompOption::TypeString, CompString("Check"));
275-
276+ check_type_value<CompString> (CompOption::TypeString, CompString ("Check"));
277+ check_type_value<CompString> (CompOption::TypeString, "core");
278+
279 check_type_value<CompAction> (CompOption::TypeAction, CompAction());
280 check_type_value<CompMatch> (CompOption::TypeMatch, CompMatch());
281
282+ check_type_value<unsigned short[4]> (CompOption::TypeColor, testColor);
283+
284 check_type_value<CompOption::Value::Vector> (CompOption::TypeList, CompOption::Value::Vector(5));
285
286 CompOption::Value v1, v2;
287 ASSERT_EQ (v1,v2);
288 v1.set (CompString("SomeString"));
289 ASSERT_TRUE(v1 != v2);
290+
291+ CompOption::Value::Vector vec;
292+ CompOption::Value v;
293+
294+ v.set (true);
295+ vec.push_back (v);
296+ vec.push_back (v);
297+
298+ check_list_type<bool> (CompOption::TypeBool, vec);
299+
300+ vec.clear ();
301+ v.set (CompString ("foo"));
302+ vec.push_back (v);
303+ vec.push_back (v);
304+
305+ check_list_type<CompString> (CompOption::TypeString, vec);
306+}
307+
308+TEST(CompOption,Color)
309+{
310+
311+ CompOption::Value value(testColor);
312+
313+ unsigned short * color = value.c();
314+ ASSERT_NE((void*)0, color);
315+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
316+
317+ color = value.get<unsigned short*>();
318+ ASSERT_NE((void*)0, color);
319+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
320+
321+ value.set(testColor2);
322+
323+ color = value.c();
324+ ASSERT_NE((void*)0, color);
325+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
326+
327+ color = value.get<unsigned short*>();
328+ ASSERT_NE((void*)0, color);
329+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
330+
331+ CompOption::Value v;
332+
333+ v.set (testColor);
334+
335+ color = v.c();
336+ ASSERT_NE((void*)0, color);
337+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
338+
339+ color = v.get<unsigned short*>();
340+ ASSERT_NE((void*)0, color);
341+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
342+
343+ v.set(testColor2);
344+
345+ color = v.c();
346+ ASSERT_NE((void*)0, color);
347+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
348+
349+ color = v.get<unsigned short*>();
350+ ASSERT_NE((void*)0, color);
351+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
352+
353+ v.set (static_cast <short unsigned int *> (testColor));
354+
355+ color = v.c();
356+ ASSERT_NE((void*)0, color);
357+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
358+
359+ color = v.get<unsigned short*>();
360+ ASSERT_NE((void*)0, color);
361+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor[i], color[i]);
362+
363+ v.set(testColor2);
364+
365+ color = v.c();
366+ ASSERT_NE((void*)0, color);
367+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
368+
369+ color = v.get<unsigned short*>();
370+ ASSERT_NE((void*)0, color);
371+ for (int i = 0; i != 4; ++i) ASSERT_EQ(testColor2[i], color[i]);
372 }

Subscribers

People subscribed via source and target branches