root/branches/streaming-rework/src/libutil/OptionContainer.cpp

Revision 419, 12.3 kB (checked in by pieterpalmers, 17 years ago)

namespace simplification

Line 
1 /* $Id$ */
2
3 /*
4  *   FreeBob Streaming API
5  *   FreeBob = Firewire (pro-)audio for linux
6  *
7  *   http://freebob.sf.net
8  *
9  *   Copyright (C) 2007 Pieter Palmers <pieterpalmers@users.sourceforge.net>
10  *
11  *   This program is free software {} you can redistribute it and/or modify
12  *   it under the terms of the GNU General Public License as published by
13  *   the Free Software Foundation {} either version 2 of the License, or
14  *   (at your option) any later version.
15  *
16  *   This program is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY {} without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with this program {} if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  *
26  *
27  */
28
29 #include "OptionContainer.h"
30
31 #include <iostream>
32 #include <sstream>
33
34 namespace Util {
35
36 IMPL_DEBUG_MODULE( OptionContainer, OptionContainer, DEBUG_LEVEL_NORMAL );
37
38 OptionContainer::Option::Option()
39     : m_Name(""),
40     m_stringValue(""),
41     m_boolValue(false),
42     m_doubleValue(0.0),
43     m_intValue(0),
44     m_uintValue(0),
45     m_Type(EInvalid)
46 {}
47
48 OptionContainer::Option::Option(std::string n)
49     : m_Name(n),
50     m_stringValue(""),
51     m_boolValue(false),
52     m_doubleValue(0.0),
53     m_intValue(0),
54     m_uintValue(0),
55     m_Type(EInvalid)
56 {}
57
58 OptionContainer::Option::Option(std::string n, std::string v)
59     : m_Name(n),
60     m_stringValue(v),
61     m_boolValue(false),
62     m_doubleValue(0.0),
63     m_intValue(0),
64     m_uintValue(0),
65     m_Type(EString)
66 {}
67
68 OptionContainer::Option::Option(std::string n, bool v)
69     : m_Name(n),
70     m_stringValue(""),
71     m_boolValue(v),
72     m_doubleValue(0.0),
73     m_intValue(0),
74     m_uintValue(0),
75     m_Type(EBool)
76 {}
77
78 OptionContainer::Option::Option(std::string n, double v)
79     : m_Name(n),
80     m_stringValue(""),
81     m_boolValue(false),
82     m_doubleValue(v),
83     m_intValue(0),
84     m_uintValue(0),
85     m_Type(EDouble)
86 {}
87
88 OptionContainer::Option::Option(std::string n, int64_t v)
89     : m_Name(n),
90     m_stringValue(""),
91     m_boolValue(false),
92     m_doubleValue(0.0),
93     m_intValue(v),
94     m_uintValue(0),
95     m_Type(EInt)
96 {}
97
98 OptionContainer::Option::Option(std::string n, uint64_t v)
99     : m_Name(n),
100     m_stringValue(""),
101     m_boolValue(false),
102     m_doubleValue(0.0),
103     m_intValue(0),
104     m_uintValue(v),
105     m_Type(EUInt)
106 {}
107
108 void OptionContainer::Option::set(std::string v) { m_stringValue = v; m_Type=EString;}
109 void OptionContainer::Option::set(bool v)        { m_boolValue = v; m_Type=EBool;}
110 void OptionContainer::Option::set(double v)      { m_doubleValue = v; m_Type=EDouble;}
111 void OptionContainer::Option::set(int64_t v)     { m_intValue = v; m_Type=EInt;}
112 void OptionContainer::Option::set(uint64_t v)    { m_uintValue = v; m_Type=EUInt;}
113
114 bool
115 OptionContainer::Option::serialize( Glib::ustring basePath, Util::IOSerialize& ser ) const
116 {
117     bool result;
118     result  = ser.write( basePath + "m_Name", Glib::ustring(m_Name) );
119     result &= ser.write( basePath + "m_stringValue", Glib::ustring(m_stringValue) );
120     result &= ser.write( basePath + "m_boolValue", m_boolValue );
121     result &= ser.write( basePath + "m_doubleValue", m_doubleValue );
122     result &= ser.write( basePath + "m_intValue", m_intValue );
123     result &= ser.write( basePath + "m_uintValue", m_uintValue );
124     result &= ser.write( basePath + "m_Type", m_Type );
125
126     return result;
127 }
128
129
130 OptionContainer::Option
131 OptionContainer::Option::deserialize( Glib::ustring basePath,
132                      Util::IODeserialize& deser )
133 {
134     bool result;
135     Option op=Option();
136     Glib::ustring tmpstr;
137    
138     result  = deser.read( basePath + "m_Name", tmpstr );
139     op.m_Name = tmpstr;
140     result &= deser.read( basePath + "m_stringValue", tmpstr );
141     op.m_stringValue = tmpstr;
142     result &= deser.read( basePath + "m_boolValue", op.m_boolValue );
143     result &= deser.read( basePath + "m_doubleValue", op.m_doubleValue );
144     result &= deser.read( basePath + "m_intValue", op.m_intValue );
145     result &= deser.read( basePath + "m_uintValue", op.m_uintValue );
146     result &= deser.read( basePath + "m_Type", op.m_Type );
147
148     if(result) {
149         return op;
150     } else {
151         return Option();
152     }
153 }
154
155 // ------------------------
156 OptionContainer::OptionContainer() {
157
158 }
159
160 OptionContainer::~OptionContainer() {
161
162 }
163
164 // -------------- SETTERS --------------------
165 bool OptionContainer::setOption(std::string name, std::string v) {
166     Option o=getOption(name);
167     if (o.getType()==OptionContainer::Option::EInvalid) return false;
168     o.set(v);
169     return setOption(o);
170 }
171
172 bool OptionContainer::setOption(std::string name, bool v) {
173     Option o=getOption(name);
174     if (o.getType()==OptionContainer::Option::EInvalid) return false;
175     o.set(v);
176     return setOption(o);
177 }
178
179 bool OptionContainer::setOption(std::string name, double v) {
180     Option o=getOption(name);
181     if (o.getType()==OptionContainer::Option::EInvalid) return false;
182     o.set(v);
183     return setOption(o);
184 }
185
186 bool OptionContainer::setOption(std::string name, int64_t v) {
187     Option o=getOption(name);
188     if (o.getType()==OptionContainer::Option::EInvalid) return false;
189     o.set((int64_t)v);
190     return setOption(o);
191 }
192
193 bool OptionContainer::setOption(std::string name, uint64_t v) {
194     Option o=getOption(name);
195     if (o.getType()==OptionContainer::Option::EInvalid) return false;
196     o.set((uint64_t)v);
197     return setOption(o);
198 }
199
200 bool OptionContainer::setOption(std::string name, int32_t v) {
201     Option o=getOption(name);
202     if (o.getType()==OptionContainer::Option::EInvalid) return false;
203     o.set((int64_t)v);
204     return setOption(o);
205 }
206
207 bool OptionContainer::setOption(std::string name, uint32_t v) {
208     Option o=getOption(name);
209     if (o.getType()==OptionContainer::Option::EInvalid) return false;
210     o.set((uint64_t)v);
211     return setOption(o);
212 }
213
214 bool OptionContainer::setOption(std::string name, int16_t v) {
215     Option o=getOption(name);
216     if (o.getType()==OptionContainer::Option::EInvalid) return false;
217     o.set((int64_t)v);
218     return setOption(o);
219 }
220
221 bool OptionContainer::setOption(std::string name, uint16_t v) {
222     Option o=getOption(name);
223     if (o.getType()==OptionContainer::Option::EInvalid) return false;
224     o.set((uint64_t)v);
225     return setOption(o);
226 }
227
228 bool OptionContainer::setOption(std::string name, int8_t v) {
229     Option o=getOption(name);
230     if (o.getType()==OptionContainer::Option::EInvalid) return false;
231     o.set((int64_t)v);
232     return setOption(o);
233 }
234
235 bool OptionContainer::setOption(std::string name, uint8_t v) {
236     Option o=getOption(name);
237     if (o.getType()==OptionContainer::Option::EInvalid) return false;
238     o.set((uint64_t)v);
239     return setOption(o);
240 }
241
242 // -------------- GETTERS --------------------
243
244 bool OptionContainer::getOption(std::string name, std::string &v) {
245     Option o=getOption(name);
246     if (o.getType()!=OptionContainer::Option::EString) return false;
247     v=o.getString();
248     return true;
249 }
250
251 bool OptionContainer::getOption(std::string name, bool &v) {
252     Option o=getOption(name);
253     if (o.getType()!=OptionContainer::Option::EBool) return false;
254     v=o.getBool();
255     return true;
256 }
257
258 bool OptionContainer::getOption(std::string name, double &v) {
259     Option o=getOption(name);
260     if (o.getType()!=OptionContainer::Option::EDouble) return false;
261     v=o.getDouble();
262     return true;
263 }
264 bool OptionContainer::getOption(std::string name, float &v) {
265     Option o=getOption(name);
266     if (o.getType()!=OptionContainer::Option::EDouble) return false;
267     v=o.getDouble();
268     return true;
269 }
270
271 bool OptionContainer::getOption(std::string name, int64_t &v) {
272     Option o=getOption(name);
273     if (o.getType()!=OptionContainer::Option::EInt) return false;
274     v=o.getInt();
275     return true;
276 }
277 bool OptionContainer::getOption(std::string name, int32_t &v) {
278     Option o=getOption(name);
279     if (o.getType()!=OptionContainer::Option::EInt) return false;
280     v=o.getInt();
281     return true;
282 }
283 bool OptionContainer::getOption(std::string name, int16_t &v) {
284     Option o=getOption(name);
285     if (o.getType()!=OptionContainer::Option::EInt) return false;
286     v=o.getInt();
287     return true;
288 }
289 bool OptionContainer::getOption(std::string name, int8_t &v) {
290     Option o=getOption(name);
291     if (o.getType()!=OptionContainer::Option::EInt) return false;
292     v=o.getInt();
293     return true;
294 }
295
296 bool OptionContainer::getOption(std::string name, uint64_t &v) {
297     Option o=getOption(name);
298     if (o.getType()!=OptionContainer::Option::EUInt) return false;
299     v=o.getUInt();
300     return true;
301 }
302 bool OptionContainer::getOption(std::string name, uint32_t &v) {
303     Option o=getOption(name);
304     if (o.getType()!=OptionContainer::Option::EUInt) return false;
305     v=o.getUInt();
306     return true;
307 }
308 bool OptionContainer::getOption(std::string name, uint16_t &v) {
309     Option o=getOption(name);
310     if (o.getType()!=OptionContainer::Option::EUInt) return false;
311     v=o.getUInt();
312     return true;
313 }
314 bool OptionContainer::getOption(std::string name, uint8_t &v) {
315     Option o=getOption(name);
316     if (o.getType()!=OptionContainer::Option::EUInt) return false;
317     v=o.getUInt();
318     return true;
319 }
320
321 OptionContainer::Option::EType OptionContainer::getOptionType(std::string name) {
322     Option o=getOption(name);
323     return o.getType();
324
325 }
326
327 OptionContainer::Option OptionContainer::getOption(std::string name) {
328     int i=findOption(name);
329     if (i<0) {
330         return Option();
331     } else {
332         return m_Options.at(i);
333     }
334 }
335
336 bool OptionContainer::addOption(Option o) {
337     if (o.getType()==OptionContainer::Option::EInvalid) {
338         return false;
339     }
340     if (hasOption(o)){
341         return false;
342     }
343    
344     m_Options.push_back(o);
345    
346     return true;
347 }
348
349 bool OptionContainer::setOption(Option o) {
350     int i=findOption(o);
351     if (i<0) {
352         return false;
353     } else {
354         m_Options.erase(m_Options.begin()+i);
355         m_Options.push_back(o);
356         return true;
357     }
358 }
359
360 bool OptionContainer::removeOption(Option o) {
361     int i=findOption(o);
362     if (i<0) {
363         return false;
364     } else {
365         m_Options.erase(m_Options.begin()+i);
366         return true;
367     }
368 }
369
370 bool OptionContainer::removeOption(std::string name) {
371     int i=findOption(name);
372     if (i<0) {
373         return false;
374     } else {
375         m_Options.erase(m_Options.begin()+i);
376         return true;
377     }
378 }
379
380 bool OptionContainer::hasOption(std::string name) {
381     return (findOption(name) >= 0);
382 }
383
384 bool OptionContainer::hasOption(Option o) {
385     return (findOption(o) >= 0);
386 }
387
388 int OptionContainer::findOption(Option o) {
389     int i=0;
390     for ( OptionVectorIterator it = m_Options.begin();
391       it != m_Options.end();
392       ++it )
393     {
394         if((*it).getName() == o.getName()) {
395             return i;
396         }
397         i++;
398     }
399     return -1;
400 }
401
402 int OptionContainer::findOption(std::string name) {
403     int i=0;
404     for ( OptionVectorIterator it = m_Options.begin();
405       it != m_Options.end();
406       ++it )
407     {
408         if((*it).getName() == name) {
409             return i;
410         }
411         i++;
412     }
413     return -1;
414 }
415
416 // serialization support
417
418 bool
419 OptionContainer::serializeOptions( Glib::ustring basePath,
420                                    Util::IOSerialize& ser) const
421 {
422     bool result = true;
423     int i = 0;
424
425     for ( OptionVector::const_iterator it = m_Options.begin();
426           it != m_Options.end();
427           ++it )
428     {
429         const Option& pOption = *it;
430
431         std::ostringstream strstrm;
432         strstrm << basePath << "/" << "Option" << i;
433         result &= pOption.serialize( strstrm.str() + "/", ser );
434         i++;
435     }
436
437     return result;
438 }
439
440 bool
441 OptionContainer::deserializeOptions( Glib::ustring basePath,
442                                      Util::IODeserialize& deser,
443                                      OptionContainer& container)
444 {
445     int i = 0;
446     bool bFinished = false;
447     bool result=true;
448     do {
449         std::ostringstream strstrm;
450         strstrm << basePath << "/" << "Option" << i;
451        
452         Option pOption = Option::deserialize( strstrm.str() + "/",
453                                               deser );
454         if ( pOption.getType() != Option::EInvalid ) {
455             result &= container.addOption(pOption);
456             i++;
457         } else {
458             bFinished = true;
459         }
460     } while ( !bFinished );
461
462     return result;
463 }
464
465
466 } // end of namespace Util
Note: See TracBrowser for help on using the browser.