Settings.h
1 /*
2  * Copyright (C) 2005-2010 by Dr. Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef Pt_Settings_h
29 #define Pt_Settings_h
30 
31 #include <Pt/Api.h>
32 #include <Pt/SerializationInfo.h>
33 #include <string>
34 #include <cstddef>
35 
36 namespace Pt {
37 
42 class PT_API SettingsError : public SerializationError
43 {
44  public:
46  SettingsError(const char* what, std::size_t line);
47 
49  ~SettingsError() throw()
50  {}
51 
54  std::size_t line() const
55  { return _line; }
56 
57  private:
59  std::size_t _line;
60 };
61 
172 class PT_API Settings : private SerializationInfo
173 {
174  public:
177  class Entry
178  {
179  public:
180  explicit Entry(SerializationInfo* si = 0)
181  : _si(si)
182  {}
183 
184  Entry(const Entry& entry)
185  : _si(entry._si)
186  {}
187 
188  Entry& operator=(const Entry& entry)
189  {
190  _si = entry._si;
191  return *this;
192  }
193 
196  template <typename T>
197  bool get(T& value) const
198  {
199  if( ! _si )
200  return false;
201 
202  *_si >>= value;
203  return true;
204  }
205 
208  template <typename T>
209  void set(const T& value)
210  {
211  if( _si )
212  {
213  _si->setVoid();
214  *_si <<= value;
215  }
216  }
217 
220  Entry addEntry(const std::string& name)
221  {
222  if( ! _si )
223  return Entry();
224 
225  SerializationInfo& si = _si->addMember(name);
226  return Entry(&si);
227  }
228 
231  Entry addEntry(const char* name)
232  {
233  if( ! _si )
234  return Entry();
235 
236  SerializationInfo& si = _si->addMember(name);
237  return Entry(&si);
238  }
239 
242  void removeEntry(const std::string& name)
243  {
244  if( _si )
245  _si->removeMember(name);
246  }
247 
250  void removeEntry(const char* name)
251  {
252  if( _si )
253  _si->removeMember(name);
254  }
255 
258  Entry begin() const
259  {
260  if( ! _si )
261  return this->end();
262 
264  if( it == _si->end() )
265  return this->end();
266 
267  SerializationInfo& si = *it;
268  return Entry(&si);
269  }
270 
273  Entry end() const
274  {
275  return Entry();
276  }
277 
280  Entry entry(const std::string& name) const
281  {
282  if( ! _si )
283  return this->end();
284 
285  SerializationInfo* si = _si->findMember(name);
286  return Entry(si);
287  }
288 
291  Entry entry(const char* name) const
292  {
293  if( ! _si )
294  return this->end();
295 
296  SerializationInfo* si = _si->findMember(name);
297  return Entry(si);
298  }
299 
302  Entry makeEntry(const char* name)
303  {
304  if( ! _si )
305  return this->end();
306 
307  SerializationInfo* si = _si->findMember(name);
308  if( ! si )
309  si = &_si->addMember(name);
310 
311  return Entry(si);
312  }
313 
316  Entry makeEntry(const std::string& name)
317  {
318  if( ! _si )
319  return this->end();
320 
321  SerializationInfo* si = _si->findMember(name);
322  if( ! si )
323  si = &_si->addMember(name);
324 
325  return Entry(si);
326  }
327 
330  Entry operator[] (const std::string& name) const
331  {
332  return this->entry(name);
333  }
334 
337  Entry operator[] (const char* name) const
338  {
339  return this->entry(name);
340  }
341 
344  const char* name() const
345  { return _si->name(); }
346 
350  { return *this; }
351 
355  { return this; }
356 
360  {
361  _si = _si->sibling();
362  return *this;
363  }
364 
367  bool operator!=(const Entry& other) const
368  { return _si != other._si; }
369 
372  bool operator==(const Entry& other) const
373  { return _si == other._si; }
374 
377  bool operator!() const
378  { return _si == 0; }
379 
380  private:
381  SerializationInfo* _si;
382  };
383 
387  {
388  public:
389  explicit ConstEntry(const SerializationInfo* si = 0)
390  : _si(si)
391  {}
392 
395  template <typename T>
396  bool get(T& value) const
397  {
398  if( ! _si )
399  return false;
400 
401  *_si >>= value;
402  return true;
403  }
404 
408  {
409  if( ! _si )
410  return this->end();
411 
413  if(it == _si->end())
414  return this->end();
415 
416  const SerializationInfo& si = *it;
417  return ConstEntry(&si);
418  }
419 
422  ConstEntry end() const
423  {
424  return ConstEntry();
425  }
426 
429  ConstEntry entry(const std::string& name) const
430  {
431  if( ! _si )
432  return end();
433 
434  const SerializationInfo* si = _si->findMember(name);
435  return ConstEntry(si);
436  }
437 
440  ConstEntry entry(const char* name) const
441  {
442  if( ! _si )
443  return end();
444 
445  const SerializationInfo* si = _si->findMember(name);
446  return ConstEntry(si);
447  }
448 
451  ConstEntry operator[] (const std::string& name) const
452  {
453  return this->entry(name);
454  }
455 
458  ConstEntry operator[] (const char* name) const
459  {
460  return this->entry(name);
461  }
462 
465  const char* name() const
466  { return _si->name(); }
467 
470  const ConstEntry& operator*() const
471  { return *this; }
472 
475  const ConstEntry* operator->() const
476  { return this; }
477 
481  {
482  _si = _si->sibling();
483  return *this;
484  }
485 
488  bool operator!=(const ConstEntry& other) const
489  { return _si != other._si; }
490 
493  bool operator==(const ConstEntry& other) const
494  { return _si == other._si; }
495 
498  bool operator!() const
499  { return _si == 0; }
500 
501  private:
502  const SerializationInfo* _si;
503  };
504 
505  public:
508  Settings();
509 
510  void clear();
511 
515  { return root().begin(); }
516 
519  ConstEntry end() const
520  { return root().end(); }
521 
524  ConstEntry root() const
525  { return ConstEntry(this); }
526 
530  { return root().begin(); }
531 
535  { return root().end(); }
536 
540  { return Entry(this); }
541 
544  void load( std::basic_istream<Pt::Char>& is );
545 
548  void save( std::basic_ostream<Pt::Char>& os ) const;
549 
552  ConstEntry entry(const std::string& name) const
553  {
554  return root().entry(name);
555  }
556 
559  ConstEntry entry(const char* name) const
560  {
561  return root().entry(name);
562  }
563 
566  ConstEntry operator[] (const std::string& name) const
567  {
568  return this->entry(name);
569  }
570 
573  ConstEntry operator[] (const char* name) const
574  {
575  return this->entry(name);
576  }
577 
580  Entry entry(const std::string& name)
581  {
582  SerializationInfo* si = this->findMember(name);
583  return Entry(si);
584  }
585 
588  Entry entry(const char* name)
589  {
590  SerializationInfo* si = this->findMember(name);
591  return Entry(si);
592  }
593 
596  Entry addEntry(const char* name)
597  {
598  return root().addEntry(name);
599  }
600 
603  Entry addEntry(const std::string& name)
604  {
605  return root().addEntry(name);
606  }
607 
610  Entry makeEntry(const char* name)
611  {
612  return root().makeEntry(name);
613  }
614 
617  Entry makeEntry(const std::string& name)
618  {
619  return root().makeEntry(name);
620  }
621 
624  void removeEntry(const char* name)
625  {
626  root().removeEntry(name);
627  }
628 
631  void removeEntry(const std::string& name)
632  {
633  root().removeEntry(name);
634  }
635 
638  Entry operator[] (const std::string& name)
639  {
640  return this->entry(name);
641  }
642 
645  Entry operator[] (const char* name)
646  {
647  return this->entry(name);
648  }
649 };
650 
651 } // namespace Pt
652 
653 #endif
bool operator==(const Entry &other) const
Allows using the entry like an iterator.
Definition: Settings.h:372
ConstEntry entry(const char *name) const
Returns a sub entry.
Definition: Settings.h:440
const SerializationInfo * findMember(const std::string &name) const
Find a struct member.
Definition: SerializationInfo.h:504
Const forward iterator for child elements.
Definition: SerializationInfo.h:778
Entry end() const
End of sub entries.
Definition: Settings.h:273
Entry & operator++()
Allows using the entry like an iterator.
Definition: Settings.h:359
Settings Format Error.
Definition: Settings.h:42
ConstEntry & operator++()
Allows using the entry like an iterator.
Definition: Settings.h:480
ConstEntry entry(const std::string &name) const
Returns a top level entry.
Definition: Settings.h:552
ConstEntry begin() const
Begin of entries.
Definition: Settings.h:514
const char * name() const
Returns the entry name.
Definition: Settings.h:465
Iterator end()
Returns an iterator to the end of child elements.
Definition: SerializationInfo.h:822
Entry begin()
Begin of entries.
Definition: Settings.h:529
Entry makeEntry(const char *name)
Makes a top level entry.
Definition: Settings.h:610
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition: SerializationInfo.h:449
bool operator!() const
Returns true if entry is invalid.
Definition: Settings.h:498
Modifiable settings entry.
Definition: Settings.h:177
Error during serialization of a type.
Definition: SerializationError.h:45
ConstEntry end() const
End of sub entries.
Definition: Settings.h:422
~SettingsError()
Destructor.
Definition: Settings.h:49
void removeEntry(const char *name)
Removes a sub entry.
Definition: Settings.h:250
Entry entry(const std::string &name) const
Returns a sub entry.
Definition: Settings.h:280
Entry makeEntry(const char *name)
Returns a sub entry.
Definition: Settings.h:302
Entry makeEntry(const std::string &name)
Returns a sub entry.
Definition: Settings.h:316
void set(const T &value)
Sets the value.
Definition: Settings.h:209
Entry * operator->()
Allows using the entry like an iterator.
Definition: Settings.h:354
Constant settings entry.
Definition: Settings.h:386
const ConstEntry * operator->() const
Allows using the entry like an iterator.
Definition: Settings.h:475
bool operator!=(const ConstEntry &other) const
Allows using the entry like an iterator.
Definition: Settings.h:488
const ConstEntry & operator*() const
Allows using the entry like an iterator.
Definition: Settings.h:470
ConstEntry entry(const char *name) const
Returns a top level entry.
Definition: Settings.h:559
bool operator!() const
Returns true if entry is invalid.
Definition: Settings.h:377
std::size_t line() const
Returns the line number where the error occured.
Definition: Settings.h:54
Entry addEntry(const char *name)
Adds a sub entry.
Definition: Settings.h:231
ConstEntry end() const
End of entries.
Definition: Settings.h:519
void removeEntry(const std::string &name)
Removes a top level entry.
Definition: Settings.h:631
ConstEntry root() const
Returns the root entry.
Definition: Settings.h:524
Entry & operator*()
Allows using the entry like an iterator.
Definition: Settings.h:349
Entry entry(const char *name) const
Returns a sub entry.
Definition: Settings.h:291
Entry addEntry(const std::string &name)
Adds a sub entry.
Definition: Settings.h:220
Entry addEntry(const char *name)
Adds a top level entry.
Definition: Settings.h:596
void removeEntry(const std::string &name)
Removes a sub entry.
Definition: Settings.h:242
ConstEntry begin() const
Begin of sub entries.
Definition: Settings.h:407
Entry makeEntry(const std::string &name)
Makes a top level entry.
Definition: Settings.h:617
ConstEntry entry(const std::string &name) const
Returns a sub entry.
Definition: Settings.h:429
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:58
Iterator begin()
Returns an iterator to the begin of child elements.
Entry addEntry(const std::string &name)
Adds a top level entry.
Definition: Settings.h:603
Entry entry(const std::string &name)
Returns a top level entry.
Definition: Settings.h:580
bool operator!=(const Entry &other) const
Allows using the entry like an iterator.
Definition: Settings.h:367
Entry begin() const
Begin of sub entries.
Definition: Settings.h:258
bool operator==(const ConstEntry &other) const
Allows using the entry like an iterator.
Definition: Settings.h:493
Entry root()
Returns the root entry.
Definition: Settings.h:539
Forward Iterator for child elements.
Definition: SerializationInfo.h:733
Store application settings.
Definition: Settings.h:172
Entry end()
End of entries.
Definition: Settings.h:534
const char * name() const
Returns the entry name.
Definition: Settings.h:344
void removeEntry(const char *name)
Removes a top level entry.
Definition: Settings.h:624
Entry entry(const char *name)
Returns a top level entry.
Definition: Settings.h:588