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
00040
00041
00042
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
00097
00098
00099
00100
00101
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
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
00114 #endif
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
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
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
00149
00150 name = newname;
00151
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
00174
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
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