feisty meow concerns codebase  2.140
configuration_list.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : configuration_list *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 2001-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
15 #include "configlet.h"
16 #include "configuration_list.h"
17 
18 #include <basis/astring.h>
19 #include <structures/amorph.h>
20 
21 #include <typeinfo>
22 
23 using namespace basis;
24 using namespace structures;
25 
26 namespace configuration {
27 
28 class cl_figlet_list : public amorph<configlet> {};
29 
31 
32 configuration_list::configuration_list()
33 : _figs(new cl_figlet_list)
34 {
35 }
36 
38 {
39  WHACK(_figs);
40 }
41 
42 void configuration_list::reset() { _figs->reset(); }
43 
44 void configuration_list::add(const configlet &new_item)
45 {
46  zap(new_item);
47  _figs->append(new_item.duplicate());
48 }
49 
50 const configlet *configuration_list::find(const configlet &to_find) const
51 {
52  for (int i = 0; i < _figs->elements(); i++) {
53  configlet &curr = *_figs->borrow(i);
54  if ( (to_find.section() == curr.section())
55  && (to_find.entry() == curr.entry())
56  && (typeid(curr) == typeid(to_find)) ) {
57  return &curr;
58  }
59  }
60  return NULL_POINTER;
61 }
62 
63 bool configuration_list::zap(const configlet &dead_item)
64 {
65  for (int i = 0; i < _figs->elements(); i++) {
66  configlet &curr = *_figs->borrow(i);
67  if ( (dead_item.section() == curr.section())
68  && (dead_item.entry() == curr.entry()) ) {
69  _figs->zap(i, i);
70  return true;
71  }
72  }
73  return false;
74 }
75 
77 {
78  bool to_return = true;
79  for (int i = 0; i < _figs->elements(); i++) {
80  configlet &curr = *_figs->borrow(i);
81  if (!curr.load(config)) to_return = false; // any failure is bad.
82  }
83  return to_return;
84 }
85 
87 {
88  bool to_return = true;
89  for (int i = 0; i < _figs->elements(); i++) {
90  configlet &curr = *_figs->borrow(i);
91  if (!curr.store(config)) to_return = false; // any failure is bad.
92  }
93  return to_return;
94 }
95 
96 } //namespace.
97 
Represents an atom of configuration info.
Definition: configlet.h:33
const basis::astring & section() const
observes the section of this configlet.
Definition: configlet.cpp:53
virtual configlet * duplicate() const =0
a virtual copy constructor for configlets.
virtual bool store(configurator &config) const =0
writes the configlet's information out to the "config".
const basis::astring & entry() const
observes the entry name of this configlet.
Definition: configlet.cpp:55
virtual bool load(configurator &config)=0
retrieves the configlet's information from the "config".
bool store(configurator &config) const
writes the current values of all the configlets in "this" into "config".
const configlet * find(const configlet &to_find) const
locates the actual configlet with the section and entry of "to_find".
bool zap(const configlet &dead_item)
removes a previously added configuration item.
void add(const configlet &new_item)
adds another configuration atom into the list.
bool load(configurator &config)
reads the values of all the configlets stored in "config" into this.
void reset()
removes all items from the list.
Provides a base class for configuration repositories.
Definition: configurator.h:34
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition: functions.h:121
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55