Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Environ Class Reference

Inheritance diagram for Environ:

Inheritance graph
[legend]
Collaboration diagram for Environ:

Collaboration graph
[legend]

Detailed Description

Params.

Definition at line 15 of file Environ.h.

Public Member Functions

virtual ~Environ ()
 Free all remain messages for watchers.
void store (const Path &file)
 Save params.
void setParam (const std::string &name, const std::string &value)
 Set param.
void setParam (const std::string &name, long value)
 Store this integer value like string param.
std::string getParam (const std::string &name, const std::string &implicit="") const
 Return value.
int getAsInt (const std::string &name, int implicit=0) const
 Returns number value.
bool getAsBool (const std::string &name, bool implicit=false) const
 Returns boolean value.
void addWatcher (const std::string &name, BaseMsg *msg)
 Multiple watcher can watch param change.
void removeWatchers (const std::string &listenerName)
 Removes all registered watchers for given listener.
std::string toString () const
std::string getHelpInfo () const


Constructor & Destructor Documentation

Environ::~Environ  )  [virtual]
 

Free all remain messages for watchers.

Definition at line 23 of file Environ.cpp.

00024 {
00025     t_watchers::iterator end = m_watchers.end();
00026     for (t_watchers::iterator i = m_watchers.begin(); i != end; ++i) {
00027         delete i->second;
00028     }
00029 }


Member Function Documentation

void Environ::addWatcher const std::string &  name,
BaseMsg msg
 

Multiple watcher can watch param change.

Parameters:
name param name
msg message to raise

Definition at line 192 of file Environ.cpp.

00193 {
00194     m_watchers.insert(std::pair<std::string,BaseMsg*>(name, msg));
00195     LOG_DEBUG(ExInfo("add watcher")
00196             .addInfo("param", name)
00197             .addInfo("msg", msg->toString()));
00198 }

bool Environ::getAsBool const std::string &  name,
bool  implicit = false
const
 

Returns boolean value.

Recognizes 1/0, true/false, on/off, yes/no.

Parameters:
name param name
implicit default value = false
Returns:
stored value or implicit value when value is not recognized

Definition at line 159 of file Environ.cpp.

00161 {
00162     bool result = false;
00163     std::string value = getParam(name);
00164     if (value == "1" || value == "true" || value == "on" || value == "yes") {
00165         result = true;
00166     }
00167     else if (value == "0" || value == "false" || value == "off" ||
00168             value == "no")
00169     {
00170         result = false;
00171     }
00172     else {
00173         if (value != "") {
00174             //TODO: don't print this every time
00175             LOG_WARNING(ExInfo("cannot recognize boolean value")
00176                     .addInfo("property", name)
00177                     .addInfo("value", value)
00178                     .addInfo("default", implicit)
00179                     .addInfo("hint", "use 1/0, true/false, on/off, yes/no"));
00180         }
00181         result = implicit;
00182     }
00183     return result;
00184 }

int Environ::getAsInt const std::string &  name,
int  implicit = 0
const
 

Returns number value.

Implicit value is zero.

Parameters:
name param name
implicit default value = 0
Returns:
number or implicit

Definition at line 132 of file Environ.cpp.

00134 {
00135     std::string value = getParam(name);
00136     bool ok;
00137     int result = StringTool::readInt(value.c_str(), &ok);
00138     if (!ok) {
00139         if (value != "") {
00140             LOG_WARNING(ExInfo("cannot recognize numeric value")
00141                     .addInfo("property", name)
00142                     .addInfo("value", value)
00143                     .addInfo("default", implicit));
00144         }
00145         result = implicit;
00146     }
00147     return result;
00148 }

std::string Environ::getHelpInfo  )  const
 

Definition at line 228 of file Environ.cpp.

00229 {
00230     std::string help;
00231     t_values::const_iterator end = m_values.end();
00232     for (t_values::const_iterator i = m_values.begin(); i != end; ++i) {
00233         help += i->first + "='" + i->second + "'\n";
00234     }
00235     return help;
00236 }

std::string Environ::getParam const std::string &  name,
const std::string &  implicit = ""
const
 

Return value.

Implicit value is "".

Parameters:
name param name
implicit default value = ""
Returns:
value or implicit value

Definition at line 111 of file Environ.cpp.

00113 {
00114     std::string result = implicit;
00115 
00116     t_values::const_iterator it = m_values.find(name);
00117     if (m_values.end() != it) {
00118         result = it->second;
00119     }
00120     return result;
00121 }

void Environ::removeWatchers const std::string &  listenerName  ) 
 

Removes all registered watchers for given listener.

Definition at line 204 of file Environ.cpp.

00205 {
00206     t_watchers::iterator end = m_watchers.end();
00207     for (t_watchers::iterator i = m_watchers.begin(); i != end; /*empty*/) {
00208         t_watchers::iterator cur = i++;
00209         if (cur->second->getListenerName() == listenerName) {
00210             delete cur->second;
00211             m_watchers.erase(cur);
00212         }
00213     }
00214 }

void Environ::setParam const std::string &  name,
long  value
 

Store this integer value like string param.

Parameters:
name param name
value param value

Definition at line 97 of file Environ.cpp.

00098 {
00099     setParam(name, StringTool::toString(value));
00100 }

void Environ::setParam const std::string &  name,
const std::string &  value
 

Set param.

Notice watchers. When watcher is not available, it will be removed.

Parameters:
name param name
value param value

Definition at line 65 of file Environ.cpp.

00066 {
00067     if (m_values[name] != value) {
00068         m_values[name] = value;
00069         LOG_DEBUG(ExInfo("setParam")
00070                 .addInfo("param", name)
00071                 .addInfo("value", value));
00072 
00073         t_watchers::iterator it = m_watchers.find(name);
00074         if (m_watchers.end() != it) {
00075             t_watchers::size_type count = m_watchers.count(name);
00076             for (t_watchers::size_type i = 0; i < count; ++i) {
00077                 t_watchers::iterator cur_it = it++;
00078                 try {
00079                     cur_it->second->sendClone();
00080                 }
00081                 catch (NameException &e) {
00082                     LOG_WARNING(e.info());
00083                     delete cur_it->second;
00084                     m_watchers.erase(cur_it);
00085                 }
00086             }
00087         }
00088     }
00089 }

void Environ::store const Path file  ) 
 

Save params.

Parameters:
file where to store params, this file will be overwritten

Definition at line 36 of file Environ.cpp.

00037 {
00038     FILE *config = fopen(file.getNative().c_str(), "w");
00039     if (config) {
00040         fputs("-- this file is automatically generated\n", config);
00041 
00042         t_values::iterator end = m_values.end();
00043         for (t_values::iterator i = m_values.begin(); i != end; ++i) {
00044             fprintf(config, "setParam(\"%s\", \"%s\")\n",
00045                     i->first.c_str(), i->second.c_str());
00046         }
00047 
00048         fclose(config);
00049     }
00050     else {
00051         LOG_WARNING(ExInfo("cannot save config")
00052                 .addInfo("file", file.getNative()));
00053     }
00054 }

std::string Environ::toString  )  const
 

Definition at line 217 of file Environ.cpp.

00218 {
00219     ExInfo info("environ");
00220     t_values::const_iterator end = m_values.end();
00221     for (t_values::const_iterator i = m_values.begin(); i != end; ++i) {
00222         info.addInfo(i->first, i->second);
00223     }
00224     return info.info();
00225 }


The documentation for this class was generated from the following files:
Generated on Wed Jun 1 09:55:02 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2