From 29cc0cf236a897afc3ffad4a2582f8c16d91bfe2 Mon Sep 17 00:00:00 2001 From: Stefan Sayer Date: Wed, 4 Jul 2007 21:19:06 +0000 Subject: [PATCH] combined AmArg and AmArgArray into one class git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@383 8eb893ce-cfd4-0310-b710-fb5ebe64c474 --- apps/examples/di_dialer/DIDial.cpp | 2 +- apps/examples/di_dialer/DIDial.h | 2 +- apps/examples/di_log/DILog.cpp | 2 +- apps/examples/di_log/DILog.h | 2 +- apps/examples/tutorial/cc_acc/CCAcc.cpp | 2 +- apps/examples/tutorial/cc_acc/CCAcc.h | 2 +- apps/examples/tutorial/mycc/MyCC.cpp | 6 +- apps/examples/xmlrpc2di/XMLRPC2DI.cpp | 62 ++++------ apps/examples/xmlrpc2di/XMLRPC2DI.h | 8 +- apps/ivr/IvrDialogBase.cpp | 6 +- apps/registrar_client/SIPRegistrarClient.cpp | 6 +- apps/registrar_client/SIPRegistrarClient.h | 2 +- apps/voicemail/AnswerMachine.cpp | 2 +- apps/webconference/RoomInfo.cpp | 22 ++-- apps/webconference/RoomInfo.h | 4 +- apps/webconference/WebConference.cpp | 29 ++--- apps/webconference/WebConference.h | 18 +-- core/AmApi.cpp | 2 +- core/AmApi.h | 2 +- core/AmArg.cpp | 115 +++++++++++++++++++ core/AmArg.h | 108 ++++++++--------- core/AmEvent.h | 4 +- core/plug-in/session_timer/UserTimer.cpp | 2 +- core/plug-in/session_timer/UserTimer.h | 2 +- core/plug-in/stats/StatsUDPServer.cpp | 4 +- core/plug-in/uac_auth/UACAuth.cpp | 2 +- core/plug-in/uac_auth/UACAuth.h | 2 +- doc/Readme.registrar_client | 2 +- 28 files changed, 248 insertions(+), 174 deletions(-) create mode 100644 core/AmArg.cpp diff --git a/apps/examples/di_dialer/DIDial.cpp b/apps/examples/di_dialer/DIDial.cpp index 190c00f0..62583738 100644 --- a/apps/examples/di_dialer/DIDial.cpp +++ b/apps/examples/di_dialer/DIDial.cpp @@ -95,7 +95,7 @@ DIDial::DIDial() { DIDial::~DIDial() { } -void DIDial::invoke(const string& method, const AmArgArray& args, AmArgArray& ret) +void DIDial::invoke(const string& method, const AmArg& args, AmArg& ret) { if(method == "dial"){ ret.push(dialout(args.get(0).asCStr(), diff --git a/apps/examples/di_dialer/DIDial.h b/apps/examples/di_dialer/DIDial.h index 2f3e7f2a..0c0c0f3c 100644 --- a/apps/examples/di_dialer/DIDial.h +++ b/apps/examples/di_dialer/DIDial.h @@ -43,5 +43,5 @@ class DIDial : public AmDynInvoke DIDial(); ~DIDial(); static DIDial* instance(); - void invoke(const string& method, const AmArgArray& args, AmArgArray& ret); + void invoke(const string& method, const AmArg& args, AmArg& ret); }; diff --git a/apps/examples/di_log/DILog.cpp b/apps/examples/di_log/DILog.cpp index 7b512cc82..97a9a506 100644 --- a/apps/examples/di_log/DILog.cpp +++ b/apps/examples/di_log/DILog.cpp @@ -38,7 +38,7 @@ int DILog::onLoad() { DILog::~DILog() { } -void DILog::invoke(const string& method, const AmArgArray& args, AmArgArray& ret) { +void DILog::invoke(const string& method, const AmArg& args, AmArg& ret) { if(method == "dumplog") { ret.push(dumpLog().c_str()); } else if(method == "dumplogtodisk") { diff --git a/apps/examples/di_log/DILog.h b/apps/examples/di_log/DILog.h index 1c2e1216..0874014e 100644 --- a/apps/examples/di_log/DILog.h +++ b/apps/examples/di_log/DILog.h @@ -22,7 +22,7 @@ class DILog : public AmLoggingFacility, public AmDynInvoke, public AmDynInvokeFa AmDynInvoke* getInstance() { return instance(); } // DI API static DILog* instance(); - void invoke(const string& method, const AmArgArray& args, AmArgArray& ret); + void invoke(const string& method, const AmArg& args, AmArg& ret); int onLoad(); diff --git a/apps/examples/tutorial/cc_acc/CCAcc.cpp b/apps/examples/tutorial/cc_acc/CCAcc.cpp index 82865da6..3790539b 100644 --- a/apps/examples/tutorial/cc_acc/CCAcc.cpp +++ b/apps/examples/tutorial/cc_acc/CCAcc.cpp @@ -37,7 +37,7 @@ CCAcc::CCAcc() { CCAcc::~CCAcc() { } -void CCAcc::invoke(const string& method, const AmArgArray& args, AmArgArray& ret) +void CCAcc::invoke(const string& method, const AmArg& args, AmArg& ret) { if(method == "getCredit"){ ret.push(getCredit(args.get(0).asCStr())); diff --git a/apps/examples/tutorial/cc_acc/CCAcc.h b/apps/examples/tutorial/cc_acc/CCAcc.h index 289ef187..00769cf7 100644 --- a/apps/examples/tutorial/cc_acc/CCAcc.h +++ b/apps/examples/tutorial/cc_acc/CCAcc.h @@ -23,5 +23,5 @@ class CCAcc : public AmDynInvoke CCAcc(); ~CCAcc(); static CCAcc* instance(); - void invoke(const string& method, const AmArgArray& args, AmArgArray& ret); + void invoke(const string& method, const AmArg& args, AmArg& ret); }; diff --git a/apps/examples/tutorial/mycc/MyCC.cpp b/apps/examples/tutorial/mycc/MyCC.cpp index 15c7a874..ce4e1d24 100644 --- a/apps/examples/tutorial/mycc/MyCC.cpp +++ b/apps/examples/tutorial/mycc/MyCC.cpp @@ -123,7 +123,7 @@ void MyCCDialog::onDtmf(int event, int duration) { pin +=int2str(event); DBG("pin is now '%s'\n", pin.c_str()); } else { - AmArgArray di_args,ret; + AmArg di_args,ret; di_args.push(pin.c_str()); cc_acc->invoke("getCredit", di_args, ret); credit = ret.get(0).asInt(); @@ -195,7 +195,7 @@ void MyCCDialog::onOtherReply(const AmSipReply& reply) { startAccounting(); setInOut(NULL, NULL); // set the call timer - AmArgArray di_args,ret; + AmArg di_args,ret; di_args.push(TIMERID_CREDIT_TIMEOUT); di_args.push(credit); // in seconds di_args.push(dlg.local_tag.c_str()); @@ -244,7 +244,7 @@ void MyCCDialog::stopAccounting() { if (now.tv_usec>500000) now.tv_sec++; DBG("Call lasted %ld seconds\n", now.tv_sec); - AmArgArray di_args,ret; + AmArg di_args,ret; di_args.push(pin.c_str()); di_args.push((int)now.tv_sec); cc_acc->invoke("subtractCredit", di_args, ret); diff --git a/apps/examples/xmlrpc2di/XMLRPC2DI.cpp b/apps/examples/xmlrpc2di/XMLRPC2DI.cpp index f57b9b3e..0e9ed574 100644 --- a/apps/examples/xmlrpc2di/XMLRPC2DI.cpp +++ b/apps/examples/xmlrpc2di/XMLRPC2DI.cpp @@ -134,9 +134,9 @@ void XMLRPC2DIServer::registerMethods(const std::string& iface) { iface.c_str()); return; } - AmArgArray dummy, fct_list; + AmArg dummy, fct_list; di->invoke("_list", dummy, fct_list); - + for (unsigned int i=0;igetFactory4Di(fact_name); @@ -229,7 +229,7 @@ void XMLRPC2DIServerDIMethod::execute(XmlRpcValue& params, XmlRpcValue& result) if(!di){ throw XmlRpcException("could not get instance from factory", 500); } - AmArgArray ret; + AmArg ret; di->invoke(fct_name, args, ret); XMLRPC2DIServer::amarg2xmlrpcval(ret, result); @@ -240,8 +240,8 @@ void XMLRPC2DIServerDIMethod::execute(XmlRpcValue& params, XmlRpcValue& result) } catch (const AmDynInvoke::NotImplemented& e) { throw XmlRpcException("Exception: AmDynInvoke::NotImplemented: " + e.what, 504); - } catch (const AmArgArray::OutOfBoundsException& e) { - throw XmlRpcException("Exception: AmArgArray out of bounds - paramter number mismatch.", 300); + } catch (const AmArg::OutOfBoundsException& e) { + throw XmlRpcException("Exception: AmArg out of bounds - paramter number mismatch.", 300); } catch (const string& e) { throw XmlRpcException("Exception: "+e, 500); } catch (...) { @@ -249,7 +249,7 @@ void XMLRPC2DIServerDIMethod::execute(XmlRpcValue& params, XmlRpcValue& result) } } -void XMLRPC2DIServer::xmlrpcval2amarg(XmlRpcValue& v, AmArgArray& a, +void XMLRPC2DIServer::xmlrpcval2amarg(XmlRpcValue& v, AmArg& a, unsigned int start_index) { if (v.valid()) { for (int i=start_index; i(a.asObject()); - if (NULL != arr) { - result[pos].setSize(arr->size()); - - for (unsigned int i=0;isize();i++) { - const AmArg& r = arr->get(i); - amarg2xmlrpcval(r, result[pos], i); - } - delete arr; - } else { - WARN("unsupported return value type of parameter %d (not AmArgArray)\n", pos); + case AmArg::Array: + result.setSize(a.size()); + for (size_t i=0;ip_dlg->getLocalTag().c_str()); @@ -405,7 +405,7 @@ static PyObject* IvrDialogBase_removeTimer(IvrDialogBase* self, PyObject* args) return NULL; } - AmArgArray di_args,ret; + AmArg di_args,ret; di_args.push(id); di_args.push(self->p_dlg->getLocalTag().c_str()); @@ -421,7 +421,7 @@ static PyObject* IvrDialogBase_removeTimers(IvrDialogBase* self, PyObject* args) { assert(self->p_dlg); - AmArgArray di_args,ret; + AmArg di_args,ret; di_args.push(self->p_dlg->getLocalTag().c_str()); self->p_dlg->user_timer-> diff --git a/apps/registrar_client/SIPRegistrarClient.cpp b/apps/registrar_client/SIPRegistrarClient.cpp index 309cfb8c..725fe2ff 100644 --- a/apps/registrar_client/SIPRegistrarClient.cpp +++ b/apps/registrar_client/SIPRegistrarClient.cpp @@ -396,7 +396,7 @@ void SIPRegistrarClient::onNewRegistration(SIPNewRegistrationEvent* new_reg) { DBG("enabling UAC Auth for new registration.\n"); // get a sessionEventHandler from uac_auth - AmArgArray di_args,ret; + AmArg di_args,ret; AmArg a; a.setBorrowedPointer(reg); di_args.push(a); @@ -553,8 +553,8 @@ bool SIPRegistrarClient::getRegistrationState(const string& handle, return res; } -void SIPRegistrarClient::invoke(const string& method, const AmArgArray& args, - AmArgArray& ret) +void SIPRegistrarClient::invoke(const string& method, const AmArg& args, + AmArg& ret) { if(method == "createRegistration"){ ret.push(createRegistration(args.get(0).asCStr(), diff --git a/apps/registrar_client/SIPRegistrarClient.h b/apps/registrar_client/SIPRegistrarClient.h index 4dadc862..291e0d94 100644 --- a/apps/registrar_client/SIPRegistrarClient.h +++ b/apps/registrar_client/SIPRegistrarClient.h @@ -175,7 +175,7 @@ public: // DI API static SIPRegistrarClient* instance(); void invoke(const string& method, - const AmArgArray& args, AmArgArray& ret); + const AmArg& args, AmArg& ret); bool onSipReply(const AmSipReply& rep); int onLoad(); diff --git a/apps/voicemail/AnswerMachine.cpp b/apps/voicemail/AnswerMachine.cpp index 1ee7f757..0a8c6423 100644 --- a/apps/voicemail/AnswerMachine.cpp +++ b/apps/voicemail/AnswerMachine.cpp @@ -535,7 +535,7 @@ void AnswerMachineDialog::process(AmEvent* event) case 0: playlist.addToPlaylist(new AmPlaylistItem(NULL,&a_msg)); - {AmArgArray di_args,ret; + {AmArg di_args,ret; di_args.push(RECORD_TIMER); di_args.push(AnswerMachineFactory::MaxRecordTime); di_args.push(getLocalTag().c_str()); diff --git a/apps/webconference/RoomInfo.cpp b/apps/webconference/RoomInfo.cpp index 4e777acf..11f2b565 100644 --- a/apps/webconference/RoomInfo.cpp +++ b/apps/webconference/RoomInfo.cpp @@ -17,13 +17,13 @@ bool ConferenceRoomParticipant::expired(const struct timeval& now) { (unsigned int)diff.tv_sec > PARTICIPANT_EXPIRED_DELAY; } -AmArgArray* ConferenceRoomParticipant::asArgArray() { - AmArgArray* res = new AmArgArray(); - res->push(AmArg(localtag.c_str())); - res->push(AmArg(number.c_str())); - res->push(AmArg((int)status)); - res->push(AmArg(last_reason.c_str())); - res->push(AmArg((int)muted)); +AmArg ConferenceRoomParticipant::asArgArray() { + AmArg res; + res.push(AmArg(localtag.c_str())); + res.push(AmArg(number.c_str())); + res.push(AmArg((int)status)); + res.push(AmArg(last_reason.c_str())); + res.push(AmArg((int)muted)); return res; } @@ -55,14 +55,12 @@ void ConferenceRoom::cleanExpired() { } } -AmArgArray* ConferenceRoom::asArgArray() { +AmArg ConferenceRoom::asArgArray() { cleanExpired(); - AmArgArray* res = new AmArgArray(); + AmArg res; for (list::iterator it=participants.begin(); it != participants.end(); it++) { - AmArg r; - r.setBorrowedPointer(it->asArgArray()); - res->push(r); + res.push(it->asArgArray()); } return res; } diff --git a/apps/webconference/RoomInfo.h b/apps/webconference/RoomInfo.h index f8999838..532b2bc2 100644 --- a/apps/webconference/RoomInfo.h +++ b/apps/webconference/RoomInfo.h @@ -48,7 +48,7 @@ struct ConferenceRoomParticipant { inline void setMuted(int mute); - AmArgArray* asArgArray(); + AmArg asArgArray(); }; struct ConferenceRoom { @@ -61,7 +61,7 @@ struct ConferenceRoom { void cleanExpired(); - AmArgArray* asArgArray(); + AmArg asArgArray(); void newParticipant(const string& localtag, const string& number); diff --git a/apps/webconference/WebConference.cpp b/apps/webconference/WebConference.cpp index 927c7e84..0b0745b7 100644 --- a/apps/webconference/WebConference.cpp +++ b/apps/webconference/WebConference.cpp @@ -213,8 +213,8 @@ AmSession* WebConferenceFactory::onInvite(const AmSipRequest& req, } void WebConferenceFactory::invoke(const string& method, - const AmArgArray& args, - AmArgArray& ret) + const AmArg& args, + AmArg& ret) { if(method == "roomCreate"){ roomCreate(args, ret); @@ -251,7 +251,7 @@ string WebConferenceFactory::getRandomPin() { return res; } -void WebConferenceFactory::roomCreate(const AmArgArray& args, AmArgArray& ret) { +void WebConferenceFactory::roomCreate(const AmArg& args, AmArg& ret) { string room = args.get(0).asCStr(); rooms_mut.lock(); map::iterator it = rooms.find(room); @@ -268,7 +268,7 @@ void WebConferenceFactory::roomCreate(const AmArgArray& args, AmArgArray& ret) { rooms_mut.unlock(); } -void WebConferenceFactory::roomInfo(const AmArgArray& args, AmArgArray& ret) { +void WebConferenceFactory::roomInfo(const AmArg& args, AmArg& ret) { string room = args.get(0).asCStr(); string adminpin = args.get(1).asCStr();; @@ -278,21 +278,16 @@ void WebConferenceFactory::roomInfo(const AmArgArray& args, AmArgArray& ret) { ret.push(1); ret.push("wrong adminpin"); // for consistency, add an empty array - AmArgArray* a = new AmArgArray(); - AmArg res; - res.setBorrowedPointer(a); - ret.push(res); + ret.push(AmArg()); } else { ret.push(0); ret.push("OK"); - AmArg res; - res.setBorrowedPointer(r->asArgArray()); - ret.push(res); + ret.push(r->asArgArray()); } rooms_mut.unlock(); } -void WebConferenceFactory::dialout(const AmArgArray& args, AmArgArray& ret) { +void WebConferenceFactory::dialout(const AmArg& args, AmArg& ret) { string room = args.get(0).asCStr(); string adminpin = args.get(1).asCStr(); string callee = args.get(2).asCStr(); @@ -343,7 +338,7 @@ void WebConferenceFactory::dialout(const AmArgArray& args, AmArgArray& ret) { } } -void WebConferenceFactory::postConfEvent(const AmArgArray& args, AmArgArray& ret, +void WebConferenceFactory::postConfEvent(const AmArg& args, AmArg& ret, int id, int mute) { string room = args.get(0).asCStr(); string adminpin = args.get(1).asCStr(); @@ -376,19 +371,19 @@ void WebConferenceFactory::postConfEvent(const AmArgArray& args, AmArgArray& ret } } -void WebConferenceFactory::kickout(const AmArgArray& args, AmArgArray& ret) { +void WebConferenceFactory::kickout(const AmArg& args, AmArg& ret) { postConfEvent(args, ret, WebConferenceEvent::Kick, -1); } -void WebConferenceFactory::mute(const AmArgArray& args, AmArgArray& ret) { +void WebConferenceFactory::mute(const AmArg& args, AmArg& ret) { postConfEvent(args, ret, WebConferenceEvent::Mute, 1); } -void WebConferenceFactory::unmute(const AmArgArray& args, AmArgArray& ret) { +void WebConferenceFactory::unmute(const AmArg& args, AmArg& ret) { postConfEvent(args, ret, WebConferenceEvent::Unmute, 0); } -void WebConferenceFactory::serverInfo(const AmArgArray& args, AmArgArray& ret) { +void WebConferenceFactory::serverInfo(const AmArg& args, AmArg& ret) { ret.push("Not yet implemented"); } diff --git a/apps/webconference/WebConference.h b/apps/webconference/WebConference.h index 26a90494..7fa6fe5e 100644 --- a/apps/webconference/WebConference.h +++ b/apps/webconference/WebConference.h @@ -87,7 +87,7 @@ class WebConferenceFactory /** returns NULL if adminpin wrong */ ConferenceRoom* getRoom(const string& room, const string& adminpin); - void postConfEvent(const AmArgArray& args, AmArgArray& ret, + void postConfEvent(const AmArg& args, AmArg& ret, int id, int mute); regex_t direct_room_re; @@ -116,16 +116,16 @@ public: WebConferenceFactory* getInstance(){ return _instance; } - void invoke(const string& method, const AmArgArray& args, AmArgArray& ret); + void invoke(const string& method, const AmArg& args, AmArg& ret); // DI functions - void roomCreate(const AmArgArray& args, AmArgArray& ret); - void roomInfo(const AmArgArray& args, AmArgArray& ret); - void dialout(const AmArgArray& args, AmArgArray& ret); - void kickout(const AmArgArray& args, AmArgArray& ret); - void mute(const AmArgArray& args, AmArgArray& ret); - void unmute(const AmArgArray& args, AmArgArray& ret); - void serverInfo(const AmArgArray& args, AmArgArray& ret); + void roomCreate(const AmArg& args, AmArg& ret); + void roomInfo(const AmArg& args, AmArg& ret); + void dialout(const AmArg& args, AmArg& ret); + void kickout(const AmArg& args, AmArg& ret); + void mute(const AmArg& args, AmArg& ret); + void unmute(const AmArg& args, AmArg& ret); + void serverInfo(const AmArg& args, AmArg& ret); }; class WebConferenceDialog diff --git a/core/AmApi.cpp b/core/AmApi.cpp index 1a0dbe96..1eafdaef 100644 --- a/core/AmApi.cpp +++ b/core/AmApi.cpp @@ -33,7 +33,7 @@ AmDynInvoke::AmDynInvoke() {} AmDynInvoke::~AmDynInvoke() {} -void AmDynInvoke::invoke(const string& method, const AmArgArray& args, AmArgArray& ret) +void AmDynInvoke::invoke(const string& method, const AmArg& args, AmArg& ret) { throw NotImplemented(method); } diff --git a/core/AmApi.h b/core/AmApi.h index 4d914e07..620ab958 100644 --- a/core/AmApi.h +++ b/core/AmApi.h @@ -56,7 +56,7 @@ class AmDynInvoke AmDynInvoke(); virtual ~AmDynInvoke(); - virtual void invoke(const string& method, const AmArgArray& args, AmArgArray& ret); + virtual void invoke(const string& method, const AmArg& args, AmArg& ret); }; /** diff --git a/core/AmArg.cpp b/core/AmArg.cpp new file mode 100644 index 00000000..549d0122 --- /dev/null +++ b/core/AmArg.cpp @@ -0,0 +1,115 @@ +/* + * $Id: AmArg.h 368 2007-06-12 18:24:33Z sayer $ + * + * Copyright (C) 2002-2003 Fhg Fokus + * + * This file is part of sems, a free SIP media server. + * + * sems is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version + * + * For a license to use the ser software under conditions + * other than those described here, or to purchase support for this + * software, please contact iptel.org by e-mail at the following addresses: + * info@iptel.org + * + * sems is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "AmArg.h" + +AmArg::AmArg(const AmArg& v) +{ + type = Undef; + + if (this != &v) { + invalidate(); + + type = v.type; + switch(type){ + case Int: { v_int = v.v_int; } break; + case Double: { v_double = v.v_double; } break; + case CStr: { v_cstr = strdup(v.v_cstr); } break; + case AObject:{ v_obj = v.v_obj; } break; + case Array: { v_array = new ValueArray(*v.v_array); } break; + case Undef: break; + default: assert(0); + } + } +} + +void AmArg::assertArray() { + if (Array == type) + return; + if (Undef == type) { + type = Array; + v_array = new ValueArray(); + return; + } + throw TypeMismatchException(); +} + +void AmArg::assertArray() const { + if (Array != type) + throw TypeMismatchException(); +} + +void AmArg::assertArray(size_t s) { + + if (Undef == type) { + type = Array; + v_array = new ValueArray(); + } else if (Array != type) { + throw TypeMismatchException(); + } + if (v_array->size() < s) + v_array->resize(s); +} + +void AmArg::invalidate() { + if(type == CStr) { free((void*)v_cstr); } + if(type == Array) { delete v_array; } + + type = Undef; +} + +void AmArg::push(const AmArg& a) { + assertArray(); + v_array->push_back(a); +} + +const size_t AmArg::size() { + assertArray(); + return v_array->size(); +} + +AmArg& AmArg::get(size_t idx) { + assertArray(); + if (idx >= v_array->size()) + throw OutOfBoundsException(); + + return (*v_array)[idx]; +} + +AmArg& AmArg::get(size_t idx) const { + assertArray(); + if (idx >= v_array->size()) + throw OutOfBoundsException(); + + return (*v_array)[idx]; +} + +AmArg& AmArg::operator[](size_t idx) { + assertArray(idx+1); + return (*v_array)[idx]; +} + diff --git a/core/AmArg.h b/core/AmArg.h index e0c17b2e..4695287e 100644 --- a/core/AmArg.h +++ b/core/AmArg.h @@ -34,7 +34,7 @@ #include using std::vector; -/** base for Objects as @see AmArg parameter*/ +/** base for Objects as @see AmArg parameter, not owned by AmArg (!) */ class ArgObject { public: ArgObject() { } @@ -52,9 +52,20 @@ class AmArg Int, Double, CStr, - AObject, - APointer // for passing pointers that are not owned by AmArg + AObject, // for passing pointers to objects not owned by AmArg + + Array + }; + + struct OutOfBoundsException { + OutOfBoundsException() { } + }; + + struct TypeMismatchException { + TypeMismatchException() { } }; + + typedef std::vector ValueArray; private: // type @@ -62,40 +73,37 @@ class AmArg // value union { - int v_int; - double v_double; - const char* v_cstr; - ArgObject* v_obj; - void* v_ptr; + int v_int; + double v_double; + const char* v_cstr; + ArgObject* v_obj; + + ValueArray* v_array; }; - public: - AmArg(const AmArg& v) - : type(v.type){ - - switch(type){ - case Int: v_int = v.v_int; break; - case Double: v_double = v.v_double; break; - case CStr: v_cstr = strdup(v.v_cstr); break; - case AObject: v_obj = v.v_obj; break; - case APointer: v_ptr = v.v_ptr; break; - default: assert(0); - } - } + void assertArray(); + void assertArray() const; + + void assertArray(size_t s); + void invalidate(); + + public: AmArg() : type(Undef) - {} + { } + AmArg(const AmArg& v); + AmArg(const int& v) : type(Int), v_int(v) - {} + { } AmArg(const double& v) : type(Double), v_double(v) - {} + { } AmArg(const char* v) : type(CStr) @@ -103,57 +111,39 @@ class AmArg v_cstr = strdup(v); } - AmArg(void* v) - : type(APointer), - v_ptr(v) - { } - - ~AmArg() { - if(type == CStr) free((void*)v_cstr); - } + ~AmArg() { invalidate(); } short getType() const { return type; } +#define isArgArray(a) (AmArg::Array == a.getType()) +#define isArgDouble(a) (AmArg::Array == a.getType()) +#define isArgInt(a) (AmArg::Int == a.getType()) +#define isArgCStr(a) (AmArg::CStr == a.getType()) +#define isArgAObject(a) (AmArg::AObject == a.getType()) + void setBorrowedPointer(ArgObject* v) { type = AObject; v_obj = v; } - int asInt() const { return v_int; } double asDouble() const { return v_double; } const char* asCStr() const { return v_cstr; } ArgObject* asObject() const { return v_obj; } - void* asPointer() const { return v_ptr; } -}; - -/** \brief array of variable args for DI APIs*/ -class AmArgArray -: public ArgObject -{ - vector v; - public: - struct OutOfBoundsException { - OutOfBoundsException() { } - }; + // operations on arrays + void push(const AmArg& a); + + const size_t size(); - AmArgArray() : v() {} - AmArgArray(const AmArgArray& a) : v(a.v) {} - - void push(const AmArg& a){ - v.push_back(a); - } + /** throws OutOfBoundsException if array too small */ + AmArg& get(size_t idx); - const AmArg& get(size_t idx) const { - - if (idx >= v.size()) - throw OutOfBoundsException(); - // assert(idx < v.size()); - return v[idx]; - } + /** throws OutOfBoundsException if array too small */ + AmArg& get(size_t idx) const; - size_t size() { return v.size(); } + /** resizes array if too small */ + AmArg& operator[](size_t idx); }; #endif diff --git a/core/AmEvent.h b/core/AmEvent.h index 0099dba2..a7e6d53a 100644 --- a/core/AmEvent.h +++ b/core/AmEvent.h @@ -52,12 +52,12 @@ struct AmEvent struct AmPluginEvent: public AmEvent { string name; - AmArgArray data; + AmArg data; AmPluginEvent(const string& n) : AmEvent(E_PLUGIN), name(n), data() {} - AmPluginEvent(const string& n, const AmArgArray& d) + AmPluginEvent(const string& n, const AmArg& d) : AmEvent(E_PLUGIN), name(n), data(d) {} }; diff --git a/core/plug-in/session_timer/UserTimer.cpp b/core/plug-in/session_timer/UserTimer.cpp index 89e2a433..7e80b3d6 100644 --- a/core/plug-in/session_timer/UserTimer.cpp +++ b/core/plug-in/session_timer/UserTimer.cpp @@ -174,7 +174,7 @@ void UserTimer::removeUserTimers(const string& session_id) { timers_mut.unlock(); } -void UserTimer::invoke(const string& method, const AmArgArray& args, AmArgArray& ret) +void UserTimer::invoke(const string& method, const AmArg& args, AmArg& ret) { if(method == "setTimer"){ setTimer(args.get(0).asInt(), diff --git a/core/plug-in/session_timer/UserTimer.h b/core/plug-in/session_timer/UserTimer.h index 3ca143c4..89202d9f 100644 --- a/core/plug-in/session_timer/UserTimer.h +++ b/core/plug-in/session_timer/UserTimer.h @@ -96,7 +96,7 @@ class UserTimer: public AmDynInvoke void on_stop(); #endif - void invoke(const string& method, const AmArgArray& args, AmArgArray& ret); + void invoke(const string& method, const AmArg& args, AmArg& ret); }; #endif //AM_SESSION_TIMER_H diff --git a/core/plug-in/stats/StatsUDPServer.cpp b/core/plug-in/stats/StatsUDPServer.cpp index bebb589a..b32d2d80 100644 --- a/core/plug-in/stats/StatsUDPServer.cpp +++ b/core/plug-in/stats/StatsUDPServer.cpp @@ -306,7 +306,7 @@ int StatsUDPServer::execute(char* msg_buf, string& reply, s_args.push_back(string(cmd_str.substr(p, p2-p))); p=p2+1; } - AmArgArray args; + AmArg args; for (vector::iterator it = s_args.begin(); it != s_args.end(); it++) { args.push(it->c_str()); @@ -325,7 +325,7 @@ int StatsUDPServer::execute(char* msg_buf, string& reply, reply = "could not get DI instance from factory\n"; return 0; } - AmArgArray ret; + AmArg ret; di->invoke(fct_name, args, ret); if (ret.size()) { diff --git a/core/plug-in/uac_auth/UACAuth.cpp b/core/plug-in/uac_auth/UACAuth.cpp index 4e5bb93d..f0774fec 100644 --- a/core/plug-in/uac_auth/UACAuth.cpp +++ b/core/plug-in/uac_auth/UACAuth.cpp @@ -47,7 +47,7 @@ UACAuthFactory* UACAuthFactory::instance() return _instance; } -void UACAuthFactory::invoke(const string& method, const AmArgArray& args, AmArgArray& ret) +void UACAuthFactory::invoke(const string& method, const AmArg& args, AmArg& ret) { if(method == "getHandler"){ CredentialHolder* c = dynamic_cast(args.get(0).asObject()); diff --git a/core/plug-in/uac_auth/UACAuth.h b/core/plug-in/uac_auth/UACAuth.h index 9c026128..e084a0b9 100644 --- a/core/plug-in/uac_auth/UACAuth.h +++ b/core/plug-in/uac_auth/UACAuth.h @@ -75,7 +75,7 @@ class UACAuthFactory static UACAuthFactory* instance(); AmDynInvoke* getInstance() { return instance(); } - void invoke(const string& method, const AmArgArray& args, AmArgArray& ret); + void invoke(const string& method, const AmArg& args, AmArg& ret); }; struct SIPRequestInfo { diff --git a/doc/Readme.registrar_client b/doc/Readme.registrar_client index 928605d9..157c0338 100644 --- a/doc/Readme.registrar_client +++ b/doc/Readme.registrar_client @@ -50,7 +50,7 @@ example code AmDynInvokeFactory* di_f = AmPlugIn::instance()-> getFactory4Di("registrar_client"); - AmArgArray di_args,ret; + AmArg di_args,ret; di_args.push(domain.c_str()); di_args.push(user.c_str()); di_args.push(display_name.c_str()); // display name