Merge lp:~smspillaz/compiz-core/compiz-core.vector-color-variant into lp:compiz-core/0.9.5

Proposed by Sam Spilsbury
Status: Rejected
Rejected by: Sam Spilsbury
Proposed branch: lp:~smspillaz/compiz-core/compiz-core.vector-color-variant
Merge into: lp:compiz-core/0.9.5
Diff against target: 173 lines (+87/-9)
4 files modified
include/core/abiversion.h (+1/-1)
include/core/option.h (+22/-5)
src/option.cpp (+1/-1)
src/option/tests/option.cpp (+63/-2)
To merge this branch: bzr merge lp:~smspillaz/compiz-core/compiz-core.vector-color-variant
Reviewer Review Type Date Requested Status
Alan Griffiths Needs Fixing
Thomas Voß Pending
Review via email: mp+90112@code.launchpad.net

This proposal supersedes a proposal from 2012-01-25.

Description of the change

Makes color wrap around std::vector

To post a comment you must log in.
Revision history for this message
Alan Griffiths (alan-griffiths) wrote : Posted in a previous version of this proposal

1. Don't #include <iostream> where it isn't needed. (I don't think you even need #include <iosfwd>.)

2. As you're specifying the length of the array as 4 here:

    Value( unsigned short c[4] ) : mListType(TypeUnset),
    mValue (ColorVector (c, c + sizeof (c) / sizeof (unsigned short)))

it seems overly complex to compute it by "sizeof (c) / sizeof (unsigned short))" - (even a "length_of" template would make the intention clearer).

review: Needs Fixing
2967. By Sam Spilsbury

Remove #include <iostream>

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

Also as written c is an "unsigned short*" so the calculation doesn't work as intended.

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

I suspect that:
58 + void set( unsigned short *s ) {
59 + mValue = ColorVector (s, s + sizeof (s) / sizeof (unsigned short));
60 + }

should be:

template<size_t N>
void set( unsigned short (const &s)[N] ) {
  mValue = ColorVector (s, s + N);
}

review: Needs Fixing
2968. By Sam Spilsbury

Explicitly require an array of unsigned short[4]

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

Rather than "+ std::cout << "color: " << color[0] << color[1] << color[2] << color[3] << std::endl;" ASSERT the expected values.

review: Needs Fixing
2969. By Sam Spilsbury

const correctness

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

Running the test:
$ src/option/tests/compiz_option_test
Running main() from gtest_main.cc
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from CompOption
[ RUN ] CompOption.Value
/home/alan/Documents/programming/c++/compiz/compiz-core.vector-color-variant/src/option/tests/option.cpp:15: Failure
Value of: type
  Actual: 4
Expected: v.type()
Which is: 0
[ FAILED ] CompOption.Value (1 ms)
[ RUN ] CompOption.Color
color: 25502550
Segmentation fault (core dumped)

review: Needs Fixing

Unmerged revisions

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-25 14:36:23 +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-25 14:36:23 +0000
16@@ -74,12 +74,14 @@
17 class Value {
18 public:
19
20+ typedef std::vector <unsigned short> ColorVector;
21+
22 typedef boost::variant<
23 bool,
24 int,
25 float,
26 CompString,
27- unsigned short*,
28+ boost::recursive_wrapper<ColorVector>,
29 boost::recursive_wrapper<CompAction>,
30 boost::recursive_wrapper<CompMatch>,
31 boost::recursive_wrapper<std::vector<Value> >
32@@ -97,6 +99,16 @@
33 mValue(t)
34 {
35 }
36+
37+ Value( unsigned short const (& color)[4] ) : mListType(TypeUnset),
38+ mValue (ColorVector (color, color + 4))
39+ {
40+ }
41+
42+ Value( const char *s ) : mListType(TypeUnset),
43+ mValue( CompString (s) )
44+ {
45+ }
46
47 ~Value();
48
49@@ -117,12 +129,17 @@
50 {
51 mValue = t;
52 }
53+
54+ void set( const char* c ) {
55+ mValue = CompString(c);
56+ }
57+
58+ void set( unsigned short const (& color)[4]) {
59+ mValue = ColorVector (color, color + 4);
60+ }
61
62- /* In order to be exception safe, this MUST
63- * return a copy. Prefer to use a specific
64- * member instead */
65 template<typename T>
66- const T & get (const T & defaultValue = T ()) const
67+ const T & get () const
68 {
69 try
70 {
71
72=== modified file 'src/option.cpp'
73--- src/option.cpp 2012-01-24 14:16:08 +0000
74+++ src/option.cpp 2012-01-25 14:36:23 +0000
75@@ -138,7 +138,7 @@
76 {
77 try
78 {
79- return boost::get<unsigned short*>(mValue);
80+ return const_cast <unsigned short *> (&((boost::get<ColorVector>(mValue))[0]));
81 }
82 catch (...)
83 {
84
85=== modified file 'src/option/tests/option.cpp'
86--- src/option/tests/option.cpp 2012-01-18 16:56:31 +0000
87+++ src/option/tests/option.cpp 2012-01-25 14:36:23 +0000
88@@ -15,6 +15,32 @@
89 ASSERT_EQ(v.type(),type);
90 ASSERT_EQ (v.get<T>(),value);
91 }
92+
93+ unsigned short testColor[4] = {255, 0, 255, 0};
94+ unsigned short testColor2[4] = {0, 255, 0, 255};
95+
96+ template<typename T>
97+ void
98+ check_list_type(CompOption::Type listType, CompOption::Value::Vector &list)
99+ {
100+ CompOption::Value vl;
101+ vl.set (list);
102+
103+ ASSERT_EQ (vl.type (), CompOption::TypeList);
104+ ASSERT_EQ (vl.get <CompOption::Value::Vector> (), list);
105+
106+ for (CompOption::Value::Vector::const_iterator it = vl.get <CompOption::Value::Vector> ().begin ();
107+ it != vl.get <CompOption::Value::Vector> ().end ();
108+ it++)
109+ {
110+ T inst;
111+ CompOption::Value value (inst);
112+
113+ const CompOption::Value &v (*it);
114+
115+ ASSERT_EQ (v.type (), value.type ());
116+ }
117+ }
118 }
119
120 TEST(CompOption,Value)
121@@ -25,15 +51,50 @@
122
123 check_type_value<int> (CompOption::TypeInt, 1);
124 check_type_value<float> (CompOption::TypeFloat, 1.f);
125- check_type_value<CompString> (CompOption::TypeString, CompString("Check"));
126-
127+ check_type_value<CompString> (CompOption::TypeString, CompString ("Check"));
128+ check_type_value<CompString> (CompOption::TypeString, "core");
129+
130 check_type_value<CompAction> (CompOption::TypeAction, CompAction());
131 check_type_value<CompMatch> (CompOption::TypeMatch, CompMatch());
132
133+ check_type_value<unsigned short*> (CompOption::TypeColor, testColor);
134+
135 check_type_value<CompOption::Value::Vector> (CompOption::TypeList, CompOption::Value::Vector(5));
136
137 CompOption::Value v1, v2;
138 ASSERT_EQ (v1,v2);
139 v1.set (CompString("SomeString"));
140 ASSERT_TRUE(v1 != v2);
141+
142+ CompOption::Value::Vector vec;
143+ CompOption::Value v;
144+
145+ v.set (true);
146+ vec.push_back (v);
147+ vec.push_back (v);
148+
149+ check_list_type<bool> (CompOption::TypeBool, vec);
150+
151+ vec.clear ();
152+ v.set (CompString ("foo"));
153+ vec.push_back (v);
154+ vec.push_back (v);
155+
156+ check_list_type<CompString> (CompOption::TypeString, vec);
157+}
158+
159+TEST(CompOption,Color)
160+{
161+
162+ CompOption::Value value(testColor);
163+ unsigned short * color = value.c();
164+ std::cout << "color: " << color[0] << color[1] << color[2] << color[3] << std::endl;
165+ color = value.get<unsigned short*>();
166+ std::cout << "color: " << color[0] << color[1] << color[2] << color[3] << std::endl;
167+ value.set(testColor2);
168+ color = value.c();
169+ std::cout << "color: " << color[0] << color[1] << color[2] << color[3] << std::endl;
170+ color = value.get<unsigned short*>();
171+ std::cout << "color: " << color[0] << color[1] << color[2] << color[3] << std::endl;
172+
173 }

Subscribers

People subscribed via source and target branches