Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

NamedO.C

Go to the documentation of this file.
00001 
00028 #include "jbltools/sfh/NamedO.h"
00029 
00030 #include <iostream>
00031 #include <sstream>
00032 
00033 #include <cassert>
00034 #include <vector>
00035 #include <map>
00036 #include <algorithm>
00037 #include <string>
00038 
00039 // #ifdef DEBUG
00040 // #undef DEBUG
00041 // #endif
00042 // #define DEBUG 2
00043 
00044 static const char *ident="@(#)$Id: NamedO.C,v 1.11 2006/06/30 11:05:00 blist Exp $";
00045 
00046 typedef std::map<std::string, NamedO *>  NamedObjectsType;
00047 static NamedObjectsType                 *namedObjects = 0;
00048 class MapValueEquals {
00049   public:
00050     MapValueEquals (NamedO *p_) : p(p_) {};
00051     bool operator() (std::pair<std::string, NamedO *> elem) {return elem.second == p;}
00052   private:
00053     NamedO *p;
00054 };
00055 
00056 int NamedO::Init::count = 0;
00057 
00058 void NamedO::initMap() {
00059   namedObjects = new NamedObjectsType;
00060 #if DEBUG>0
00061   std::cout << "NamedO::Init::count: Initialization finished." << std::endl;
00062 #endif
00063 }
00064 
00065 NamedO::NamedO(const char * name_)
00066 : name (name_ ? name_ : ""), 
00067   definition (name)
00068 {
00069   reg(); 
00070 }
00071 
00072 NamedO::NamedO(const std::string& name_)
00073 : name (name_), 
00074   definition (name)
00075 {
00076   reg(); 
00077 }
00078 
00079 NamedO::NamedO(const std::string& name_, const std::string& definition_)
00080 : name (name_), 
00081   definition (definition_)
00082 {
00083   reg(); 
00084 }
00085 
00086 NamedO::~NamedO() { 
00087   unreg(); 
00088 }
00089 
00090 void NamedO::destroy() { 
00091   delete this; 
00092 } 
00093 
00094 void NamedO::reg() 
00095 {
00096   // Because reg() is called within the constructor of 
00097   // NamedO, we cannot yet call "getName()", because 
00098   // getName will only work after the whole constructor has been completed.
00099   // So for the time being we just register the object in the list.
00100   // All objects such registered will be transferred to the namedObjects map
00101   // with the updateMap function before the map is used to search for an object.
00102   if (!namedObjects) {
00103     std::cerr << "NamedO::reg(): namedObjects don't exist yet!" << std::endl;
00104     initMap();
00105   }
00106   if (name != "" && name != "?" && name != " ") {
00107 #if DEBUG>1 
00108     std::cout << "NamedO::reg(): registering " << getName() << std::endl;
00109 #endif /* #if DEBUG>1 */
00110     if (namedObjects->find(name) != namedObjects->end()) {
00111 #if DEBUG>0 
00112       std::cerr << "NamedO::reg(): Trying to overwrite named object " << name << std::endl;
00113       // assert (namedObjects->find(name) == namedObjects->end());
00114 #endif /* #if DEBUG>1 */
00115     }
00116     else {
00117       (*namedObjects)[name]=this; 
00118     }
00119   }
00120   else {
00121 #if DEBUG>1 
00122     std::cout << "NamedO::reg(): will not register " << getName() << std::endl;
00123 #endif /* #if DEBUG>1 */
00124   
00125   }
00126 }
00127 
00128 void NamedO::unreg() {
00129   NamedObjectsType::iterator i = find_if (namedObjects->begin(), namedObjects->end(), MapValueEquals(this));
00130   if (i != namedObjects->end()) {
00131 #if DEBUG>1
00132       std::cout << "NamedO::unreg: removing " << (i->second ? i->second->getName() : "?") 
00133                 << " from namedObjects " << std::endl;
00134 #endif /* #if DEBUG>1 */
00135     namedObjects->erase(i);
00136   }                                                   
00137 }
00138 
00139 const char *NamedO::getName() const {
00140   return name.c_str();
00141 }
00142 
00143 const char *NamedO::getDefinition() const {
00144   return definition.c_str();
00145 }
00146 
00147 NamedO& NamedO::setName(const std::string& newname) {
00148   // don't remove object from map
00149   // unreg();
00150   name = newname;
00151   // prepare registration under new name
00152   reg();
00153   return *this;
00154 }
00155 
00156 NamedO& NamedO::setName(const char *newname) {
00157   return setName (std::string(newname ? newname : ""));
00158 }
00159 
00160 NamedO& NamedO::setDefinition(const std::string& newdef) {
00161   definition = newdef;
00162   return *this;
00163 }
00164 
00165 std::ostream& NamedO::print (std::ostream& os) const {
00166   os << getName() << ": " << getDefinition();
00167   return os;
00168 }                       
00169 
00170 
00171 NamedO *NamedO::getObject (const std::string& name) {
00172   NamedObjectsType::iterator it = namedObjects->find(name);
00173   //if (it == namedObjects->end()) return 0;
00174   //return it-second;
00175   NamedO *result = (it == namedObjects->end() ? 0 : it->second);
00176 #if DEBUG>1
00177     std::cout << "NamedO::getObject: returning " << (result ? result->getName() : "NONE") 
00178               << " for " << name << std::endl;
00179 #endif /* #if DEBUG>1 */
00180   return result;
00181 }
00182 
00183 NamedO *NamedO::getObject (const char *name) {
00184   if (!name) return 0;
00185   return getObject (std::string (name));
00186 }
00187 
00188 std::string NamedO::str (char c) {
00189   std::ostringstream os;
00190   os << c;
00191   return os.str();
00192 }
00193 
00194 std::string NamedO::str (const char *s) {
00195   return std::string(s);
00196 }
00197 
00198 std::string NamedO::str (int i) {
00199   std::ostringstream os;
00200   os << i;
00201   return os.str();
00202 }
00203 
00204 std::string NamedO::str (int i, int wide) {
00205   std::ostringstream os;
00206   os.width(wide);
00207   os.fill('0');
00208   os << i;
00209   return os.str();
00210 }
00211 
00212 std::string NamedO::str (float f) {
00213   std::ostringstream os;
00214   os << f;
00215   return os.str();
00216 }
00217 
00218 std::string NamedO::str (double d) {
00219   std::ostringstream os;
00220   os << d;
00221   return os.str();
00222 }
00223 
00224 std::string NamedO::str (bool b) {
00225   return b ? "true" : "false";
00226 }
00227 
00228 std::ostream& NamedO::listObjects(std::ostream& os) {
00229   if (!namedObjects) {
00230     std::cerr << "NamedO::listObjects: newObjects don't exist!\n";
00231     return os;
00232   }
00233   os << "List of NamedO objects:\n";
00234   for (NamedObjectsType::const_iterator it = namedObjects->begin(); it!=namedObjects->end(); ++it) {
00235     os << it->first << ": " << it->second->getName() << " = " << it->second->getDefinition() << '\n';
00236   }
00237   return os;
00238 }
00239 
00240 std::ostream& operator<< (std::ostream& os, const NamedO& rhs) {
00241   rhs.print(os);
00242   return os;
00243 }
00244 
00245 

Generated on Thu Oct 26 12:52:58 2006 for SFH by doxygen 1.3.2