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 
243  {
244  if( ! _si )
245  return Entry();
246 
247  SerializationInfo& si = _si->addElement();
248  return Entry(&si);
249  }
250 
253  void removeEntry(const std::string& name)
254  {
255  if( _si )
256  _si->removeMember(name);
257  }
258 
261  void removeEntry(const char* name)
262  {
263  if( _si )
264  _si->removeMember(name);
265  }
266 
269  void removeEntry(const Entry& e)
270  {
271  if( ! _si || ! e._si )
272  return;
273 
274  _si->removeMember( *e._si );
275  }
276 
279  Entry begin() const
280  {
281  if( ! _si )
282  return this->end();
283 
285  if( it == _si->end() )
286  return this->end();
287 
288  SerializationInfo& si = *it;
289  return Entry(&si);
290  }
291 
294  Entry end() const
295  {
296  return Entry();
297  }
298 
301  Entry entry(const std::string& name) const
302  {
303  if( ! _si )
304  return this->end();
305 
306  SerializationInfo* si = _si->findMember(name);
307  return Entry(si);
308  }
309 
312  Entry entry(const char* name) const
313  {
314  if( ! _si )
315  return this->end();
316 
317  SerializationInfo* si = _si->findMember(name);
318  return Entry(si);
319  }
320 
323  Entry makeEntry(const char* name)
324  {
325  if( ! _si )
326  return this->end();
327 
328  SerializationInfo* si = _si->findMember(name);
329  if( ! si )
330  si = &_si->addMember(name);
331 
332  return Entry(si);
333  }
334 
337  Entry makeEntry(const std::string& name)
338  {
339  if( ! _si )
340  return this->end();
341 
342  SerializationInfo* si = _si->findMember(name);
343  if( ! si )
344  si = &_si->addMember(name);
345 
346  return Entry(si);
347  }
348 
351  Entry operator[] (const std::string& name) const
352  {
353  return this->entry(name);
354  }
355 
358  Entry operator[] (const char* name) const
359  {
360  return this->entry(name);
361  }
362 
365  const char* name() const
366  { return _si->name(); }
367 
371  { return *this; }
372 
376  { return this; }
377 
381  {
382  _si = _si->sibling();
383  return *this;
384  }
385 
388  bool operator!=(const Entry& other) const
389  { return _si != other._si; }
390 
393  bool operator==(const Entry& other) const
394  { return _si == other._si; }
395 
398  bool operator!() const
399  { return _si == 0; }
400 
401  private:
402  SerializationInfo* _si;
403  };
404 
408  {
409  public:
410  explicit ConstEntry(const SerializationInfo* si = 0)
411  : _si(si)
412  {}
413 
416  template <typename T>
417  bool get(T& value) const
418  {
419  if( ! _si )
420  return false;
421 
422  *_si >>= value;
423  return true;
424  }
425 
429  {
430  if( ! _si )
431  return this->end();
432 
434  if(it == _si->end())
435  return this->end();
436 
437  const SerializationInfo& si = *it;
438  return ConstEntry(&si);
439  }
440 
443  ConstEntry end() const
444  {
445  return ConstEntry();
446  }
447 
450  ConstEntry entry(const std::string& name) const
451  {
452  if( ! _si )
453  return end();
454 
455  const SerializationInfo* si = _si->findMember(name);
456  return ConstEntry(si);
457  }
458 
461  ConstEntry entry(const char* name) const
462  {
463  if( ! _si )
464  return end();
465 
466  const SerializationInfo* si = _si->findMember(name);
467  return ConstEntry(si);
468  }
469 
472  ConstEntry operator[] (const std::string& name) const
473  {
474  return this->entry(name);
475  }
476 
479  ConstEntry operator[] (const char* name) const
480  {
481  return this->entry(name);
482  }
483 
486  const char* name() const
487  { return _si->name(); }
488 
491  const ConstEntry& operator*() const
492  { return *this; }
493 
496  const ConstEntry* operator->() const
497  { return this; }
498 
502  {
503  _si = _si->sibling();
504  return *this;
505  }
506 
509  bool operator!=(const ConstEntry& other) const
510  { return _si != other._si; }
511 
514  bool operator==(const ConstEntry& other) const
515  { return _si == other._si; }
516 
519  bool operator!() const
520  { return _si == 0; }
521 
522  private:
523  const SerializationInfo* _si;
524  };
525 
526  public:
530 
533  void clear();
534 
537  bool isEmpty() const;
538 
541  void setName(const char* name);
542 
546  { return root().begin(); }
547 
550  ConstEntry end() const
551  { return root().end(); }
552 
555  ConstEntry root() const
556  { return ConstEntry(this); }
557 
561  { return root().begin(); }
562 
566  { return root().end(); }
567 
571  { return Entry(this); }
572 
575  void load(std::basic_istream<Pt::Char>& is);
576 
579  void load(Pt::Formatter& formatter);
580 
583  void save(std::basic_ostream<Pt::Char>& os) const;
584 
587  void save(Pt::Formatter& formatter) const;
588 
591  ConstEntry entry(const std::string& name) const
592  {
593  return root().entry(name);
594  }
595 
598  ConstEntry entry(const char* name) const
599  {
600  return root().entry(name);
601  }
602 
605  ConstEntry operator[] (const std::string& name) const
606  {
607  return this->entry(name);
608  }
609 
612  ConstEntry operator[] (const char* name) const
613  {
614  return this->entry(name);
615  }
616 
619  Entry entry(const std::string& name)
620  {
621  SerializationInfo* si = this->findMember(name);
622  return Entry(si);
623  }
624 
627  Entry entry(const char* name)
628  {
629  SerializationInfo* si = this->findMember(name);
630  return Entry(si);
631  }
632 
635  Entry addEntry(const char* name)
636  {
637  return root().addEntry(name);
638  }
639 
642  Entry addEntry(const std::string& name)
643  {
644  return root().addEntry(name);
645  }
646 
649  Entry makeEntry(const char* name)
650  {
651  return root().makeEntry(name);
652  }
653 
656  Entry makeEntry(const std::string& name)
657  {
658  return root().makeEntry(name);
659  }
660 
663  void removeEntry(const char* name)
664  {
665  root().removeEntry(name);
666  }
667 
670  void removeEntry(const std::string& name)
671  {
672  root().removeEntry(name);
673  }
674 
677  Entry operator[] (const std::string& name)
678  {
679  return this->entry(name);
680  }
681 
684  Entry operator[] (const char* name)
685  {
686  return this->entry(name);
687  }
688 };
689 
690 } // namespace Pt
691 
692 #endif
ConstEntry end() const
End of sub entries.
Definition: Settings.h:443
ConstEntry entry(const std::string &name) const
Returns a sub entry.
Definition: Settings.h:450
Core module.
Definition: Allocator.h:33
Entry addEntry()
Adds a sub entry.
Definition: Settings.h:242
Iterator end()
Returns an iterator to the end of child elements.
Definition: SerializationInfo.h:826
ConstEntry begin() const
Begin of entries.
Definition: Settings.h:545
void load(std::basic_istream< Pt::Char > &is)
Loads settings from a input stream.
bool operator!=(const ConstEntry &other) const
Allows using the entry like an iterator.
Definition: Settings.h:509
bool operator==(const ConstEntry &other) const
Allows using the entry like an iterator.
Definition: Settings.h:514
ConstEntry end() const
End of entries.
Definition: Settings.h:550
Entry addEntry(const char *name)
Adds a top level entry.
Definition: Settings.h:635
Error during serialization of a type.
Definition: SerializationError.h:46
bool operator!=(const Entry &other) const
Allows using the entry like an iterator.
Definition: Settings.h:388
Entry & operator++()
Allows using the entry like an iterator.
Definition: Settings.h:380
void removeEntry(const std::string &name)
Removes a top level entry.
Definition: Settings.h:670
Entry addEntry(const std::string &name)
Adds a top level entry.
Definition: Settings.h:642
bool operator==(const Entry &other) const
Allows using the entry like an iterator.
Definition: Settings.h:393
Entry entry(const char *name) const
Returns a sub entry.
Definition: Settings.h:312
Settings Format Error.
Definition: Settings.h:43
void save(std::basic_ostream< Pt::Char > &os) const
Saves settings to a output stream.
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition: SerializationInfo.h:449
Entry begin() const
Begin of sub entries.
Definition: Settings.h:279
Entry makeEntry(const std::string &name)
Returns a sub entry.
Definition: Settings.h:337
void save(Pt::Formatter &formatter) const
Saves settings using a formatter.
Entry * operator->()
Allows using the entry like an iterator.
Definition: Settings.h:375
void set(const T &value)
Sets the value.
Definition: Settings.h:209
bool operator!() const
Returns true if entry is invalid.
Definition: Settings.h:519
Constant settings entry.
Definition: Settings.h:408
ConstEntry root() const
Returns the root entry.
Definition: Settings.h:555
SerializationInfo & addElement()
Add a sequence element.
void removeEntry(const Entry &e)
Removes a sub entry.
Definition: Settings.h:269
const char * name() const
Returns the entry name.
Definition: Settings.h:365
void removeEntry(const char *name)
Removes a top level entry.
Definition: Settings.h:663
Entry end() const
End of sub entries.
Definition: Settings.h:294
Entry makeEntry(const char *name)
Returns a sub entry.
Definition: Settings.h:323
std::size_t line() const
Returns the line number where the error occured.
Definition: Settings.h:54
bool get(T &value) const
Gets the value.
Definition: Settings.h:417
ConstEntry begin() const
Begin of sub entries.
Definition: Settings.h:428
void setName(const char *name)
Sets the name of the settings root.
bool operator!() const
Returns true if entry is invalid.
Definition: Settings.h:398
ConstEntry entry(const char *name) const
Returns a top level entry.
Definition: Settings.h:598
void removeEntry(const char *name)
Removes a sub entry.
Definition: Settings.h:261
Entry entry(const char *name)
Returns a top level entry.
Definition: Settings.h:627
Store application settings.
Definition: Settings.h:173
Entry end()
End of entries.
Definition: Settings.h:565
Const forward iterator for child elements.
Definition: SerializationInfo.h:783
Entry begin()
Begin of entries.
Definition: Settings.h:560
bool get(T &value) const
Gets the value.
Definition: Settings.h:197
Entry addEntry(const char *name)
Adds a sub entry.
Definition: Settings.h:231
~SettingsError()
Destructor.
Definition: Settings.h:49
ConstEntry & operator++()
Allows using the entry like an iterator.
Definition: Settings.h:501
const SerializationInfo * findMember(const std::string &name) const
Find a struct member.
Definition: SerializationInfo.h:508
ConstEntry entry(const char *name) const
Returns a sub entry.
Definition: Settings.h:461
bool isEmpty() const
Returns true if settings are empty.
void removeEntry(const std::string &name)
Removes a sub entry.
Definition: Settings.h:253
Forward Iterator for child elements.
Definition: SerializationInfo.h:738
const ConstEntry & operator*() const
Allows using the entry like an iterator.
Definition: Settings.h:491
Entry entry(const std::string &name) const
Returns a sub entry.
Definition: Settings.h:301
const ConstEntry * operator->() const
Allows using the entry like an iterator.
Definition: Settings.h:496
Entry addEntry(const std::string &name)
Adds a sub entry.
Definition: Settings.h:220
ConstEntry entry(const std::string &name) const
Returns a top level entry.
Definition: Settings.h:591
void load(Pt::Formatter &formatter)
Loads settings using a formatter.
Settings()
Default constructor.
SettingsError(const char *what, std::size_t line)
Constructor.
void clear()
Clears the settings.
Entry entry(const std::string &name)
Returns a top level entry.
Definition: Settings.h:619
Support for serialization to different formats.
Definition: Formatter.h:46
Entry root()
Returns the root entry.
Definition: Settings.h:570
Modifiable settings entry.
Definition: Settings.h:178
Entry & operator*()
Allows using the entry like an iterator.
Definition: Settings.h:370
Entry makeEntry(const std::string &name)
Makes a top level entry.
Definition: Settings.h:656
const char * name() const
Returns the entry name.
Definition: Settings.h:486
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:59
Iterator begin()
Returns an iterator to the begin of child elements.
Entry makeEntry(const char *name)
Makes a top level entry.
Definition: Settings.h:649