Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Configuration.cc

Go to the documentation of this file.
00001 /*
00002  * Configuration.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Application configuration holder.
00006  *
00007  * Copyright (c) 2004 Milan Cermak, Petr Stepanek
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 #include <stlib/Dictionary.h>
00026 #include <stlib/KAVIterator.h>
00027 #include <stlib/Set.h>
00028 #include <stlib/String.h>
00029 
00030 #include <stlib/tools/ConfigurationReader.h>
00031 #include <stlib/tools/Configuration.h>
00032 
00033 namespace Tools {
00034 
00035 Dictionary *Configuration::_instances = nil;
00036 ConfigurationReader *Configuration::_reader = nil;
00037 
00038 Configuration::Configuration(void)
00039 {
00040     bindings = new Dictionary(2);
00041 }
00042 
00043 Configuration::Configuration(String *name)
00044 {
00045     bindings = new Dictionary(2);
00046     instances()->put(name, this);
00047 }
00048 
00049 Configuration::~Configuration(void)
00050 {
00051     Object *key = instances()->keyAtValue(this, nil);
00052     if (key != nil) instances()->removeKey(key);
00053     delete bindings;
00054 }
00055 
00056 /* Instance creation protocol */
00057 Object *Configuration::namedAt(String *name, String *item)
00058 {
00059     Configuration *config = named(name);
00060     return config->at(item);
00061 }
00062 
00063 Configuration *Configuration::named(String *name)
00064 {
00065     Configuration *configuration;
00066     configuration = dynamic_cast<Configuration *>(instances()->at(name, nil));
00067     if (configuration == nil) {
00068         configuration = Configuration::fromFile(name);
00069         instances()->put(name, configuration);
00070     }
00071     return configuration;
00072 }
00073 
00074 Configuration *Configuration::fromFile(String *filename)
00075 {
00076     configurationReader()->onFilename(filename);
00077     return configurationReader()->readConfiguration();
00078 }
00079 
00080 /* Class-accessing protocol */
00081 
00082 String *Configuration::className(void) const
00083 {
00084     return new String("Configuration");
00085 }
00086 
00087 ConfigurationReader *Configuration::configurationReader(void)
00088 {
00089     if (_reader == nil)
00090         _reader = new ConfigurationReader;
00091     return _reader;
00092 }
00093 
00094 void Configuration::configurationReader(ConfigurationReader *reader)
00095 {
00096     _reader = reader;
00097 }
00098 
00099 Dictionary *Configuration::instances(void)
00100 {
00101     if (_instances == nil)
00102         _instances = new Dictionary(2);
00103     return _instances;
00104 }
00105 
00106 /* Accessing protocol */
00107 Object *Configuration::at(String *key)
00108 {
00109     return bindings->at(key);
00110 }
00111 
00112 Object *Configuration::at(const char *key)
00113 {
00114     return bindings->at(key);
00115 }
00116 
00117 Object *Configuration::at(String *key, Object *ifAbsentValue)
00118 {
00119     return bindings->at(key, ifAbsentValue);
00120 }
00121 
00122 Object *Configuration::at(const char *key, const char *ifAbsentValue)
00123 {
00124     return bindings->at(key, ifAbsentValue);
00125 }
00126 
00127 void Configuration::put(String *key, Object *value)
00128 {
00129     bindings->put(key, value);
00130 }
00131 
00132 Set *Configuration::keys(void)
00133 {
00134     KeysAndValuesIterator *i;
00135     Set *coll = new Set(bindings->size());
00136     for (i = bindings->keysAndValuesIterator(); !i->finished(); i->next()) {
00137         coll->add(i->key());
00138     }
00139     return coll;
00140 }
00141 
00142 /* Printing protocol */
00143 void Configuration::printOn(Stream *stream) const
00144 {
00145     Object::printOn(stream);
00146     stream->nextPut('{');
00147     stream->print(bindings);
00148     stream->nextPut('}');
00149 }
00150 
00151 /* Removing protocol */
00152 Object *Configuration::removeKey(String *key)
00153 {
00154     return bindings->removeKey(key);
00155 }
00156 
00157 /* Testing protocol */
00158 bool Configuration::includesKey(String *key)
00159 {
00160     return bindings->includesKey(key);
00161 }
00162 
00163 bool Configuration::isEmpty(void)
00164 {
00165     return bindings->isEmpty();
00166 }
00167 
00168 /* Utilities protocol */
00169 void Configuration::reloadAll(void)
00170 {
00171     KeysAndValuesIterator *i;
00172 
00173     for (i = instances()->keysAndValuesIterator(); !i->finished(); i->next()) {
00174         configurationReader()->onFilename(dynamic_cast<String *>(i->key()));
00175         configurationReader()->readConfiguration(dynamic_cast<Configuration *>(i->value()));
00176     }
00177 }
00178 
00179 }; /* namespace Tools */

Generated on Mon Nov 27 09:47:54 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2