presage  0.9.2~beta
configuration.cpp
Go to the documentation of this file.
1 
2 /******************************************************
3  * Presage, an extensible predictive text entry system
4  * ---------------------------------------------------
5  *
6  * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License along
19  with this program; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  **********(*)*/
23 
24 
25 #include "configuration.h"
26 
27 #include <iostream>
28 
30 {
31  configuration = new std::map<std::string, Variable*>();
32 }
33 
35 {
36  for (std::map<std::string, Variable*>::iterator it = configuration->begin();
37  it != configuration->end();
38  it++) {
39  //std::cerr << "[Configuration] Deleting variable: " << it->first << '\t' << it->second << std::endl;
40  delete it->second;
41  //std::cerr << "[Configuration] Deleted variable: " << it->first << std::endl;
42  }
43  delete configuration;
44 }
45 
46 Variable* Configuration::find(const std::string& variable) const
47 {
48  std::map<std::string, Variable*>::const_iterator it = configuration->find (variable);
49  if (it == configuration->end()) {
50  // variable not found, create exception message
51  std::string message = "[Configuration] Cannot find variable " + variable;
52 
53  // if we get here, variable was not found in the configuration,
54  // hence we have a right to complain
56  }
57 
58  return it->second;
59 }
60 
61 Variable* Configuration::operator[](const std::string& variable) const
62 {
63  return find(variable);
64 }
65 
66 void Configuration::insert(const std::string& variable,
67  const std::string& value)
68 {
69  std::map<std::string, Variable*>::const_iterator it = configuration->find (variable);
70  if (it != configuration->end ()) {
71  it->second->set_value (value);
72  //std::cerr << "[Configuration] Modifying existing variable: " << variable << std::endl;
73 
74  } else {
75  Variable* var = new Variable (variable);
76  var->set_value (value);
77  configuration->insert (std::pair<std::string, Variable*> (variable, var));
78 
79  //std::cerr << "[Configuration] Adding new variable: " << variable << '\t' << var << std::endl;
80  }
81 
82  //std::cerr << "[Configuration] Inserted variable: " << variable << std::endl;
83 }
84 
85 void Configuration::remove(const std::string& variable)
86 {
87  std::map<std::string, Variable*>::iterator it = configuration->find (variable);
88  if (it != configuration->end()) {
89  delete it->second;
90  configuration->erase (it);
91  }
92 }
93 
95 {
96  // iterate map
97  for (std::map<std::string, Variable*>::const_iterator map_it = configuration->begin ();
98  map_it != configuration->end ();
99  map_it++) {
100 
101  // variable
102  std::cout << map_it->first;
103 
104  // value
105  std::cout << " = " << map_it->second->get_value () << std::endl;
106  }
107 }
108 
109 std::map<std::string, Variable*>::const_iterator Configuration::begin () const
110 {
111  return configuration->begin();
112 }
113 
114 std::map<std::string, Variable*>::const_iterator Configuration::end () const
115 {
116  return configuration->end();
117 }
void insert(const std::string &variable, const std::string &value)
Variable * operator[](const std::string &variable) const
std::map< std::string, Variable * >::const_iterator end() const
std::map< std::string, Variable * > * configuration
Definition: configuration.h:63
Variable * find(const std::string &variable) const
void print() const
void set_value(std::string value)
Definition: variable.cpp:67
void remove(const std::string &variable)
std::map< std::string, Variable * >::const_iterator begin() const
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278