root/trunk/libffado/src/libutil/OptionContainer.cpp

Revision 864, 12.3 kB (checked in by ppalmers, 15 years ago)

update license to GPLv2 or GPLv3 instead of GPLv2 or any later version. Update copyrights to reflect the new year

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