From 047382865bde1a899412747a2081637cfb965d71 Mon Sep 17 00:00:00 2001 From: paulc Date: Fri, 8 Apr 2005 12:45:19 +0000 Subject: [PATCH] More yateclass related cleanups. git-svn-id: http://yate.null.ro/svn/yate/trunk@280 acf43c95-373e-0410-b603-e72c3f656dc1 --- contrib/ysip/Makefile.in | 3 +- engine/Channel.cpp | 7 ++-- engine/Configuration.cpp | 6 ++-- engine/DataFormat.cpp | 71 ++++++++++++++++++---------------------- engine/Engine.cpp | 31 ++++++++---------- engine/NamedList.cpp | 6 ++-- engine/ObjList.cpp | 25 ++++++++++++-- yateclass.h | 12 +++++++ 8 files changed, 88 insertions(+), 73 deletions(-) diff --git a/contrib/ysip/Makefile.in b/contrib/ysip/Makefile.in index 3b46c580..006822ac 100644 --- a/contrib/ysip/Makefile.in +++ b/contrib/ysip/Makefile.in @@ -9,8 +9,7 @@ DEFS := INCLUDES := -I@top_srcdir@ -I../.. -I@srcdir@ CFLAGS := -O2 @MODULE_CPPFLAGS@ @INLINE_FLAGS@ LDFLAGS:= -L.. -lyate -INCFILES := @top_srcdir@/yatengine.h @top_srcdir@/yatephone.h \ - @srcdir@/ysip.h @srcdir@/util.h +INCFILES := @top_srcdir@/yateclass.h @srcdir@/ysip.h @srcdir@/util.h PROGS= LIBS = libysip.a diff --git a/engine/Channel.cpp b/engine/Channel.cpp index f8ca617a..6e475cf3 100644 --- a/engine/Channel.cpp +++ b/engine/Channel.cpp @@ -562,11 +562,10 @@ void Driver::statusParams(String& str) void Driver::statusChannels(String& str) { - ObjList* l = &m_chans; - for (; l; l=l->next()) { + ObjList* l = m_chans.skipNull(); + for (; l; l=l->skipNext()) { Channel* c = static_cast(l->get()); - if (c) - str.append(c->id(),",") << "=" << c->status() << "|" << c->address(); + str.append(c->id(),",") << "=" << c->status() << "|" << c->address(); } } diff --git a/engine/Configuration.cpp b/engine/Configuration.cpp index 290ac149..ae8d151f 100644 --- a/engine/Configuration.cpp +++ b/engine/Configuration.cpp @@ -200,11 +200,9 @@ bool Configuration::save() const return false; FILE *f = ::fopen(c_str(),"w"); if (f) { - ObjList *ol = const_cast(&m_sections); - for (;ol;ol=ol->next()) { + ObjList *ol = m_sections.skipNull(); + for (;ol;ol=ol->skipNext()) { NamedList *nl = static_cast(ol->get()); - if (!nl) - continue; ::fprintf(f,"[%s]\n",nl->c_str()); unsigned int n = nl->length(); for (unsigned int i = 0; i < n; i++) { diff --git a/engine/DataFormat.cpp b/engine/DataFormat.cpp index 5ad7c61c..12c3d6ef 100644 --- a/engine/DataFormat.cpp +++ b/engine/DataFormat.cpp @@ -159,11 +159,10 @@ void DataSource::Forward(const DataBlock& data, unsigned long timeDelta) } Lock lock(m_mutex); ref(); - ObjList *l = &m_consumers; - for (; l; l=l->next()) { + ObjList *l = m_consumers.skipNull(); + for (; l; l=l->skipNext()) { DataConsumer *c = static_cast(l->get()); - if (c) - c->Consume(data,timeDelta); + c->Consume(data,timeDelta); } m_timestamp += timeDelta; deref(); @@ -414,17 +413,15 @@ String DataTranslator::srcFormats(const String& dFormat) { String s; s_mutex.lock(); - ObjList *l = &s_factories; - for (; l; l=l->next()) { + ObjList *l = s_factories.skipNull(); + for (; l; l=l->skipNext()) { TranslatorFactory *f = static_cast(l->get()); - if (f) { - const TranslatorCaps *caps = f->getCapabilities(); - for (; caps && caps->src && caps->dest; caps++) { - if (dFormat == caps->dest->name) { - if (!s.null()) - s << " "; - s << caps->src->name << "@" << caps->cost; - } + const TranslatorCaps *caps = f->getCapabilities(); + for (; caps && caps->src && caps->dest; caps++) { + if (dFormat == caps->dest->name) { + if (!s.null()) + s << " "; + s << caps->src->name << "@" << caps->cost; } } } @@ -436,17 +433,15 @@ String DataTranslator::destFormats(const String& sFormat) { String s; s_mutex.lock(); - ObjList *l = &s_factories; - for (; l; l=l->next()) { + ObjList *l = s_factories.skipNull(); + for (; l; l=l->skipNext()) { TranslatorFactory *f = static_cast(l->get()); - if (f) { - const TranslatorCaps *caps = f->getCapabilities(); - for (; caps && caps->src && caps->dest; caps++) { - if (sFormat == caps->src->name) { - if (!s.null()) - s << " "; - s << caps->dest->name << "@" << caps->cost; - } + const TranslatorCaps *caps = f->getCapabilities(); + for (; caps && caps->src && caps->dest; caps++) { + if (sFormat == caps->src->name) { + if (!s.null()) + s << " "; + s << caps->dest->name << "@" << caps->cost; } } } @@ -458,16 +453,14 @@ int DataTranslator::cost(const String& sFormat, const String& dFormat) { int c = -1; s_mutex.lock(); - ObjList *l = &s_factories; - for (; l; l=l->next()) { + ObjList *l = s_factories.skipNull(); + for (; l; l=l->skipNext()) { TranslatorFactory *f = static_cast(l->get()); - if (f) { - const TranslatorCaps *caps = f->getCapabilities(); - for (; caps && caps->src && caps->dest; caps++) { - if ((c == -1) || (c > caps->cost)) { - if ((sFormat == caps->src->name) && (dFormat == caps->dest->name)) - c = caps->cost; - } + const TranslatorCaps *caps = f->getCapabilities(); + for (; caps && caps->src && caps->dest; caps++) { + if ((c == -1) || (c > caps->cost)) { + if ((sFormat == caps->src->name) && (dFormat == caps->dest->name)) + c = caps->cost; } } } @@ -485,14 +478,12 @@ DataTranslator* DataTranslator::create(const String& sFormat, const String& dFor DataTranslator *trans = 0; s_mutex.lock(); - ObjList *l = &s_factories; - for (; l; l=l->next()) { + ObjList *l = s_factories.skipNull(); + for (; l; l=l->skipNext()) { TranslatorFactory *f = static_cast(l->get()); - if (f) { - trans = f->create(sFormat,dFormat); - if (trans) - break; - } + trans = f->create(sFormat,dFormat); + if (trans) + break; } s_mutex.unlock(); diff --git a/engine/Engine.cpp b/engine/Engine.cpp index 08887366..6abddefb 100644 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -268,18 +268,16 @@ int Engine::run() while (s_haltcode == -1) { if (s_cmds) { Output("Executing initial commands"); - for (ObjList* c = s_cmds; c; c=c->next()) { + for (ObjList* c = s_cmds->skipNull(); c; c=c->skipNext()) { String* s = static_cast(c->get()); - if (s) { - Message m("engine.command"); - m.addParam("line",*s); - if (dispatch(m)) { - if (m.retValue()) - Output("%s",m.retValue().c_str()); - } - else - Debug(DebugWarn,"Unrecognized command '%s'",s->c_str()); + Message m("engine.command"); + m.addParam("line",*s); + if (dispatch(m)) { + if (m.retValue()) + Output("%s",m.retValue().c_str()); } + else + Debug(DebugWarn,"Unrecognized command '%s'",s->c_str()); } s_cmds->destruct(); s_cmds = 0; @@ -451,21 +449,20 @@ void Engine::initPlugins() Debug(DebugInfo,"Engine::initPlugins()"); #endif dispatch("engine.init"); - ObjList *l = &plugins; - for (; l; l = l->next()) { + ObjList *l = plugins.skipNull(); + for (; l; l = l->skipNext()) { Plugin *p = static_cast(l->get()); - if (p) - p->initialize(); + p->initialize(); } } int Engine::usedPlugins() { int used = 0; - ObjList *l = &plugins; - for (; l; l = l->next()) { + ObjList *l = plugins.skipNull(); + for (; l; l = l->skipNext()) { Plugin *p = static_cast(l->get()); - if (p && p->isBusy()) + if (p->isBusy()) used++; } return used; diff --git a/engine/NamedList.cpp b/engine/NamedList.cpp index 2ce0aceb..0fd0cce7 100644 --- a/engine/NamedList.cpp +++ b/engine/NamedList.cpp @@ -86,10 +86,10 @@ NamedList& NamedList::clearParam(const String& name) NamedString* NamedList::getParam(const String& name) const { XDebug(DebugInfo,"NamedList::getParam(\"%s\")",name.c_str()); - const ObjList *p = &m_params; - for (;p;p=p->next()) { + const ObjList *p = m_params.skipNull(); + for (;p;p=p->skipNext()) { NamedString *s = static_cast(p->get()); - if (s && (s->name() == name)) + if (s->name() == name) return s; } return 0; diff --git a/engine/ObjList.cpp b/engine/ObjList.cpp index fcae7e31..ce86c19b 100644 --- a/engine/ObjList.cpp +++ b/engine/ObjList.cpp @@ -78,6 +78,25 @@ ObjList* ObjList::last() const return const_cast(n); } +ObjList* ObjList::skipNull() const +{ + const ObjList *n = this; + while (n && !n->get()) + n = n->next(); + return const_cast(n); +} + +ObjList* ObjList::skipNext() const +{ + const ObjList *n = this; + while (n) { + n = n->next(); + if (n && n->get()) + break; + } + return const_cast(n); +} + ObjList* ObjList::operator+(int index) const { if (index < 0) @@ -107,11 +126,11 @@ ObjList* ObjList::find(const GenObject* obj) const ObjList* ObjList::find(const String& str) const { XDebug(DebugAll,"ObjList::find(\"%s\") [%p]",str.c_str(),this); - const ObjList *n = this; + const ObjList *n = skipNull(); while (n) { - if (n->get() && str.matches(n->get()->toString())) + if (str.matches(n->get()->toString())) break; - n = n->next(); + n = n->skipNext(); } XDebug(DebugInfo,"ObjList::find returning %p",n); return const_cast(n); diff --git a/yateclass.h b/yateclass.h index 3dae2df5..02ff87a5 100644 --- a/yateclass.h +++ b/yateclass.h @@ -607,6 +607,18 @@ public: */ ObjList* last() const; + /** + * Skip over NULL holding items in the list + * @return Pointer to the first non NULL holding item in list or NULL + */ + ObjList* skipNull() const; + + /** + * Advance in the list skipping over NULL holding items + * @return Pointer to the next non NULL holding item in list or NULL + */ + ObjList* skipNext() const; + /** * Pointer-like indexing operator * @param index Index of the list item to retrive