4 #include <epicsMutex.h> 5 #include <epicsGuard.h> 7 #include <epicsExport.h> 12 typedef std::map<const std::string, Object*>
objects_t;
14 typedef std::map<const std::string, Object::create_factory_t>
factories_t;
17 static epicsMutex *objectsLock=0;
20 void initObjects(
void* rmsg)
22 std::string *emsg=(std::string*)rmsg;
26 objectsLock =
new epicsMutex;
27 }
catch(std::exception& e) {
34 epicsThreadOnceId initOnce = EPICS_THREAD_ONCE_INIT;
37 void initObjectsOnce()
40 epicsThreadOnce(&initOnce, &initObjects, (
void*)&emsg);
42 throw std::runtime_error(emsg);
67 epicsGuard<epicsMutex> g(*objectsLock);
70 throw std::invalid_argument(
"Object name can not be empty string");
72 objects_t::const_iterator it=objects->find(n);
73 if(it!=objects->end()) {
74 std::ostringstream strm;
75 strm<<
"Object name '"<<n<<
"' already exists.";
76 throw std::runtime_error(strm.str());
81 m_obj_parent->m_obj_children.insert(
this);
87 epicsGuard<epicsMutex> g(*objectsLock);
90 m_obj_parent->m_obj_children.erase(
this);
92 objects_t::iterator it=objects->find(
name());
93 if(it==objects->end())
94 errlogPrintf(
"Can not remove object '%s' because it is not in global list.\n",
name().c_str());
110 epicsGuard<epicsMutex> g(*objectsLock);
111 objects_t::const_iterator it=objects->find(n);
112 if(it!=objects->end())
121 epicsGuard<epicsMutex> g(*objectsLock);
123 objects_t::const_iterator it=objects->find(name);
124 if(it!=objects->end())
128 throw std::runtime_error(SB()<<
"Object not found : "<<
name);
130 factories_t::const_iterator it=factories->find(klass);
131 if(it==factories->end())
132 throw std::runtime_error(SB()<<
"No such Object factory: "<<klass);
133 return (it->second)(
name, klass, args);
141 epicsGuard<epicsMutex> g(*objectsLock);
143 factories_t::const_iterator it=factories->find(klass);
144 if(it!=factories->end())
145 throw std::runtime_error(SB()<<
"Can't replace Object factory: "<<klass);
146 (*factories)[klass] = fn;
153 epicsGuard<epicsMutex> g(*objectsLock);
155 for(objects_t::const_iterator it=objects->begin();
156 it!=objects->end(); ++it)
158 if(!(*cb)(it->second, arg))
166 propArgs(std::ostream& s, std::string i) :strm(s), indent(i) {}
176 }
catch (std::exception& e) {
177 args->
strm <<
"<Error: "<<e.what()<<
">";
185 void showObject(std::ostream& strm,
Object& obj, std::string indent,
int depth,
int maxdepth,
bool props)
190 strm <<indent <<
"Object: " <<obj.
name() <<
"\n" 191 <<indent <<
"Type: "<<
typeid (obj).
name()<<
"\n";
195 showObject(strm, **it, args.indent, depth+1, maxdepth, props);
199 void dol(
int lvl,
const char* obj)
203 epicsGuard<epicsMutex> g(*objectsLock);
205 std::cout <<objects->size() <<
" Device Objects\n";
208 for(objects_t::const_iterator it=objects->begin();
209 it!=objects->end(); ++it)
211 if(it->second->parent())
213 showObject(std::cout, *it->second,
"", 0, lvl+1,
false);
217 objects_t::const_iterator it=objects->find(obj);
218 if(it==objects->end()) {
219 std::cout<<
"Object '"<<obj<<
"' does not exist\n";
222 showObject(std::cout, *it->second,
"", 0, lvl+1,
false);
224 }
catch(std::exception& e){
225 epicsPrintf(
"Error: %s\n", e.what());
230 void dor(
int lvl,
const char* obj)
234 epicsGuard<epicsMutex> g(*objectsLock);
236 std::cout <<objects->size() <<
" Device Objects\n";
239 for(objects_t::const_iterator it=objects->begin();
240 it!=objects->end(); ++it)
242 if(it->second->parent())
244 showObject(std::cout, *it->second,
"", 0, lvl+1,
true);
248 objects_t::const_iterator it=objects->find(obj);
249 if(it==objects->end()) {
250 std::cout<<
"Object '"<<obj<<
"' does not exist\n";
253 showObject(std::cout, *it->second,
"", 0, lvl+1,
true);
255 }
catch(std::exception& e){
256 epicsPrintf(
"Error: %s\n", e.what());
262 static const iocshArg dolArg0 = {
"level",iocshArgInt};
263 static const iocshArg dolArg1 = {
"object name",iocshArgString};
264 static const iocshArg *
const dolArgs[2] =
266 static const iocshFuncDef dolFuncDef =
268 static void dolCallFunc(
const iocshArgBuf *
args)
270 dol(args[0].ival, args[1].sval);
273 static const iocshArg dorArg0 = {
"level",iocshArgInt};
274 static const iocshArg dorArg1 = {
"object name",iocshArgString};
275 static const iocshArg *
const dorArgs[2] =
277 static const iocshFuncDef dorFuncDef =
279 static void dorCallFunc(
const iocshArgBuf *args)
281 dor(args[0].ival, args[1].sval);
287 iocshRegister(&dolFuncDef,dolCallFunc);
288 iocshRegister(&dorFuncDef,dorCallFunc);
291 #include <epicsExport.h> void dol(int lvl, const char *obj)
std::map< const std::string, Object::create_factory_t > factories_t
std::map< std::string, std::string > create_args_t
static Object * getCreateObject(const std::string &name, const std::string &klass, const create_args_t &args=create_args_t())
virtual ~propertyBase()=0
virtual const std::type_info & type() const =0
Object *(* create_factory_t)(const std::string &name, const std::string &klass, const create_args_t &args)
m_obj_children_t::const_iterator child_iterator
virtual const char * what()
Object(const std::string &n, const Object *par=0)
std::map< const std::string, Object * > objects_t
virtual void visitProperties(bool(*)(propertyBase *, void *), void *)=0
static Object * getObject(const std::string &name)
static void visitObjects(bool(*)(Object *, void *), void *)
virtual void show(std::ostream &) const
Print the value of the field w/o leading or trailing whitespace.
virtual const char * name() const =0
child_iterator beginChild() const
const std::string & name() const
static void addFactory(const std::string &klass, create_factory_t fn)
child_iterator endChild() const
virtual propertyBase * getPropertyBase(const char *, const std::type_info &)=0
void dor(int lvl, const char *obj)
epicsExportRegistrar(objectsreg)
propArgs(std::ostream &s, std::string i)