Skip to content

Commit cebd3a2

Browse files
committed
Add "of" method to xobject
1 parent bdaed06 commit cebd3a2

4 files changed

Lines changed: 417 additions & 2 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ Support for dynamic typing in C++.
6262

6363
return(out.str());
6464
}
65+
66+
// Example using the traditional approach:
67+
xobject font;
68+
font.put("color", xnode::value_of(0xff0000));
69+
font.put("font_name", xnode::value_of(std::string("arial")));
70+
font.put("size", xnode::value_of(12));
71+
printInFont(xnode::value_of(font), "Hello World!");
72+
73+
// Example using the static 'of' method for cleaner code:
74+
printInFont(
75+
xnode::value_of(xobject::of(
76+
"color", xnode::value_of(0xff0000),
77+
"font_name", xnode::value_of(std::string("arial")),
78+
"size", xnode::value_of(12),
79+
"bold", xnode::value_of(true)
80+
)),
81+
"Hello World!"
82+
);
83+
84+
// The static 'of' method allows for more concise and readable code
85+
// by creating an xobject with properties in a single expression.
6586

6687
* acquire ownership of & auto-delete objects
6788

include/details/property_list.h

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,157 @@ class property_list {
3535
typedef typename value_container_type::iterator value_iterator;
3636
typedef typename value_container_type::const_iterator value_const_iterator;
3737

38+
// Static factory methods
39+
static this_type of(const KeyType& k1, const ValueType& v1) {
40+
this_type result;
41+
result.put(k1, v1);
42+
return result;
43+
}
44+
45+
static this_type of(const KeyType& k1, const ValueType& v1,
46+
const KeyType& k2, const ValueType& v2) {
47+
this_type result;
48+
result.put(k1, v1);
49+
result.put(k2, v2);
50+
return result;
51+
}
52+
53+
static this_type of(const KeyType& k1, const ValueType& v1,
54+
const KeyType& k2, const ValueType& v2,
55+
const KeyType& k3, const ValueType& v3) {
56+
this_type result;
57+
result.put(k1, v1);
58+
result.put(k2, v2);
59+
result.put(k3, v3);
60+
return result;
61+
}
62+
63+
static this_type of(const KeyType& k1, const ValueType& v1,
64+
const KeyType& k2, const ValueType& v2,
65+
const KeyType& k3, const ValueType& v3,
66+
const KeyType& k4, const ValueType& v4) {
67+
this_type result;
68+
result.put(k1, v1);
69+
result.put(k2, v2);
70+
result.put(k3, v3);
71+
result.put(k4, v4);
72+
return result;
73+
}
74+
75+
static this_type of(const KeyType& k1, const ValueType& v1,
76+
const KeyType& k2, const ValueType& v2,
77+
const KeyType& k3, const ValueType& v3,
78+
const KeyType& k4, const ValueType& v4,
79+
const KeyType& k5, const ValueType& v5) {
80+
this_type result;
81+
result.put(k1, v1);
82+
result.put(k2, v2);
83+
result.put(k3, v3);
84+
result.put(k4, v4);
85+
result.put(k5, v5);
86+
return result;
87+
}
88+
89+
static this_type of(const KeyType& k1, const ValueType& v1,
90+
const KeyType& k2, const ValueType& v2,
91+
const KeyType& k3, const ValueType& v3,
92+
const KeyType& k4, const ValueType& v4,
93+
const KeyType& k5, const ValueType& v5,
94+
const KeyType& k6, const ValueType& v6) {
95+
this_type result;
96+
result.put(k1, v1);
97+
result.put(k2, v2);
98+
result.put(k3, v3);
99+
result.put(k4, v4);
100+
result.put(k5, v5);
101+
result.put(k6, v6);
102+
return result;
103+
}
104+
105+
static this_type of(const KeyType& k1, const ValueType& v1,
106+
const KeyType& k2, const ValueType& v2,
107+
const KeyType& k3, const ValueType& v3,
108+
const KeyType& k4, const ValueType& v4,
109+
const KeyType& k5, const ValueType& v5,
110+
const KeyType& k6, const ValueType& v6,
111+
const KeyType& k7, const ValueType& v7) {
112+
this_type result;
113+
result.put(k1, v1);
114+
result.put(k2, v2);
115+
result.put(k3, v3);
116+
result.put(k4, v4);
117+
result.put(k5, v5);
118+
result.put(k6, v6);
119+
result.put(k7, v7);
120+
return result;
121+
}
122+
123+
static this_type of(const KeyType& k1, const ValueType& v1,
124+
const KeyType& k2, const ValueType& v2,
125+
const KeyType& k3, const ValueType& v3,
126+
const KeyType& k4, const ValueType& v4,
127+
const KeyType& k5, const ValueType& v5,
128+
const KeyType& k6, const ValueType& v6,
129+
const KeyType& k7, const ValueType& v7,
130+
const KeyType& k8, const ValueType& v8) {
131+
this_type result;
132+
result.put(k1, v1);
133+
result.put(k2, v2);
134+
result.put(k3, v3);
135+
result.put(k4, v4);
136+
result.put(k5, v5);
137+
result.put(k6, v6);
138+
result.put(k7, v7);
139+
result.put(k8, v8);
140+
return result;
141+
}
142+
143+
static this_type of(const KeyType& k1, const ValueType& v1,
144+
const KeyType& k2, const ValueType& v2,
145+
const KeyType& k3, const ValueType& v3,
146+
const KeyType& k4, const ValueType& v4,
147+
const KeyType& k5, const ValueType& v5,
148+
const KeyType& k6, const ValueType& v6,
149+
const KeyType& k7, const ValueType& v7,
150+
const KeyType& k8, const ValueType& v8,
151+
const KeyType& k9, const ValueType& v9) {
152+
this_type result;
153+
result.put(k1, v1);
154+
result.put(k2, v2);
155+
result.put(k3, v3);
156+
result.put(k4, v4);
157+
result.put(k5, v5);
158+
result.put(k6, v6);
159+
result.put(k7, v7);
160+
result.put(k8, v8);
161+
result.put(k9, v9);
162+
return result;
163+
}
164+
165+
static this_type of(const KeyType& k1, const ValueType& v1,
166+
const KeyType& k2, const ValueType& v2,
167+
const KeyType& k3, const ValueType& v3,
168+
const KeyType& k4, const ValueType& v4,
169+
const KeyType& k5, const ValueType& v5,
170+
const KeyType& k6, const ValueType& v6,
171+
const KeyType& k7, const ValueType& v7,
172+
const KeyType& k8, const ValueType& v8,
173+
const KeyType& k9, const ValueType& v9,
174+
const KeyType& k10, const ValueType& v10) {
175+
this_type result;
176+
result.put(k1, v1);
177+
result.put(k2, v2);
178+
result.put(k3, v3);
179+
result.put(k4, v4);
180+
result.put(k5, v5);
181+
result.put(k6, v6);
182+
result.put(k7, v7);
183+
result.put(k8, v8);
184+
result.put(k9, v9);
185+
result.put(k10, v10);
186+
return result;
187+
}
188+
38189
property_list() : dirty_keys_(false) {}
39190

40191
property_list(const property_list &src):

test/property_list_test.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,84 @@ void TestKeyIteratorModification() {
182182
Assert(keys.size() == 2, "Should copy 2 keys to vector");
183183
}
184184

185+
// Test static of() methods
186+
void TestStaticOfMethods() {
187+
// Test of() with 1 pair
188+
{
189+
auto props = property_list<string, int>::of("one", 1);
190+
Assert(props.size() == 1, "Should create list with 1 item");
191+
Assert(props.get("one") == 1, "Should have correct value for key 'one'");
192+
}
193+
194+
// Test of() with 2 pairs
195+
{
196+
auto props = property_list<string, int>::of("one", 1, "two", 2);
197+
Assert(props.size() == 2, "Should create list with 2 items");
198+
Assert(props.get("one") == 1, "Should have correct value for key 'one'");
199+
Assert(props.get("two") == 2, "Should have correct value for key 'two'");
200+
201+
// Check insertion order
202+
auto keys = props.get_keys();
203+
Assert(keys[0] == "one", "First key should be 'one'");
204+
Assert(keys[1] == "two", "Second key should be 'two'");
205+
}
206+
207+
// Test of() with 3 pairs
208+
{
209+
auto props = property_list<string, int>::of("one", 1, "two", 2, "three", 3);
210+
Assert(props.size() == 3, "Should create list with 3 items");
211+
Assert(props.get("one") == 1, "Should have correct value for key 'one'");
212+
Assert(props.get("two") == 2, "Should have correct value for key 'two'");
213+
Assert(props.get("three") == 3, "Should have correct value for key 'three'");
214+
215+
// Check insertion order
216+
auto keys = props.get_keys();
217+
Assert(keys[0] == "one", "First key should be 'one'");
218+
Assert(keys[1] == "two", "Second key should be 'two'");
219+
Assert(keys[2] == "three", "Third key should be 'three'");
220+
}
221+
222+
// Test of() with 5 pairs
223+
{
224+
auto props = property_list<string, int>::of(
225+
"one", 1, "two", 2, "three", 3, "four", 4, "five", 5
226+
);
227+
Assert(props.size() == 5, "Should create list with 5 items");
228+
Assert(props.get("one") == 1, "Should have correct value for key 'one'");
229+
Assert(props.get("five") == 5, "Should have correct value for key 'five'");
230+
231+
// Check insertion order preserved
232+
auto keys = props.get_keys();
233+
Assert(keys[0] == "one", "First key should be 'one'");
234+
Assert(keys[4] == "five", "Fifth key should be 'five'");
235+
}
236+
237+
// Test of() with 10 pairs
238+
{
239+
auto props = property_list<string, int>::of(
240+
"one", 1, "two", 2, "three", 3, "four", 4, "five", 5,
241+
"six", 6, "seven", 7, "eight", 8, "nine", 9, "ten", 10
242+
);
243+
Assert(props.size() == 10, "Should create list with 10 items");
244+
Assert(props.get("one") == 1, "Should have correct value for key 'one'");
245+
Assert(props.get("ten") == 10, "Should have correct value for key 'ten'");
246+
247+
// Check insertion order preserved
248+
auto keys = props.get_keys();
249+
Assert(keys[0] == "one", "First key should be 'one'");
250+
Assert(keys[9] == "ten", "Tenth key should be 'ten'");
251+
}
252+
253+
// Test overwriting keys
254+
{
255+
auto props = property_list<string, int>::of(
256+
"one", 1, "two", 2, "one", 3
257+
);
258+
Assert(props.size() == 2, "Should have 2 items when key is repeated");
259+
Assert(props.get("one") == 3, "Should have last value for repeated key");
260+
}
261+
}
262+
185263
// Main test runner
186264
int main() {
187265
try {
@@ -193,6 +271,7 @@ int main() {
193271
TestConstValuesIterators();
194272
TestKeysIteratorsWithReorg();
195273
TestKeyIteratorModification();
274+
TestStaticOfMethods();
196275

197276
std::cout << "All tests passed!" << std::endl;
198277
return 0;

0 commit comments

Comments
 (0)