object.hh 5.43 KB
Newer Older
François Grisez's avatar
François Grisez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
object.hh
Copyright (C) 2017 Belledonne Communications SARL

This program 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.

This program 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

20 21 22 23 24 25 26 27 28
#ifndef _LINPHONE_OBJECT_HH
#define _LINPHONE_OBJECT_HH

#include <memory>
#include <list>
#include <map>
#include <belle-sip/object.h>
#include <bctoolbox/list.h>

29 30 31 32 33 34 35 36 37 38 39 40
#ifndef LINPHONECXX_PUBLIC
#if defined(_MSC_VER)
#ifdef LINPHONECXX_EXPORTS
#define LINPHONECXX_PUBLIC	__declspec(dllexport)
#else
#define LINPHONECXX_PUBLIC	__declspec(dllimport)
#endif
#else
#define LINPHONECXX_PUBLIC
#endif
#endif

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
namespace linphone {
	
	class Object;
	class Listener;
	
	
	class AbstractBctbxListWrapper {
	public:
		AbstractBctbxListWrapper(): mCList(NULL) {}
		virtual ~AbstractBctbxListWrapper() {}
		::bctbx_list_t *c_list() {return mCList;}
		
	protected:
		::bctbx_list_t *mCList;
	};
	
	
	template <class T>
	class ObjectBctbxListWrapper: public AbstractBctbxListWrapper {
	public:
		ObjectBctbxListWrapper(const std::list<std::shared_ptr<T> > &cppList);
		virtual ~ObjectBctbxListWrapper();
	};
	
	
	class StringBctbxListWrapper: public AbstractBctbxListWrapper {
	public:
		StringBctbxListWrapper(const std::list<std::string> &cppList);
		virtual ~StringBctbxListWrapper();
	};
	
	
	class Object: public std::enable_shared_from_this<Object> {
	public:
		Object(::belle_sip_object_t *ptr, bool takeRef=true);
		virtual ~Object();
		
	public:
		template <class T>
		void setData(const std::string &key, T &data) {
			std::map<std::string,void *> &userData = getUserData();
			userData[key] = &data;
		}
		template <class T>
		T &getData(const std::string &key) {
			std::map<std::string,void *> &userData = getUserData();
			void *ptr = userData[key];
			if (ptr == NULL) {
				throw std::out_of_range("unknown data key [" + key + "]");
			} else {
				return *(T *)ptr;
			}
		}
94 95
		LINPHONECXX_PUBLIC void unsetData(const std::string &key);
		LINPHONECXX_PUBLIC bool dataExists(const std::string &key);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	
	public:
		template <class T>
		static std::shared_ptr<T> cPtrToSharedPtr(::belle_sip_object_t *ptr, bool takeRef=true) {
			if (ptr == NULL) {
				return nullptr;
			} else {
				T *cppPtr = (T *)belle_sip_object_data_get(ptr, "cpp_object");
				if (cppPtr == NULL) {
					return std::make_shared<T>(ptr, takeRef);
				} else {
					return std::static_pointer_cast<T,Object>(cppPtr->shared_from_this());
				}
			}
		}
		static ::belle_sip_object_t *sharedPtrToCPtr(const std::shared_ptr<const Object> &sharedPtr);
		
	protected:
		static std::string cStringToCpp(const char *cstr);
		static std::string cStringToCpp(char *cstr);
		static const char *cppStringToC(const std::string &cppstr);
		
		template <class T>
		static std::list<std::shared_ptr<T> > bctbxObjectListToCppList(const ::bctbx_list_t *bctbxList) {
			std::list<std::shared_ptr<T> > cppList;
			for(const ::bctbx_list_t *it=bctbxList; it!=NULL; it=it->next) {
				std::shared_ptr<T> newObj = Object::cPtrToSharedPtr<T>((::belle_sip_object_t *)it->data);
				cppList.push_back(newObj);
			}
			return cppList;
		}
		
		static std::list<std::string> bctbxStringListToCppList(const ::bctbx_list_t *bctbxList);
		static std::list<std::string> cStringArrayToCppList(const char **cArray);
	
	private:
132
		LINPHONECXX_PUBLIC std::map<std::string,void *> &getUserData() const;
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
		template <class T> static void deleteSharedPtr(std::shared_ptr<T> *ptr) {if (ptr != NULL) delete ptr;}
		static void deleteString(std::string *str) {if (str != NULL) delete str;}
	
	protected:
		::belle_sip_object_t *mPrivPtr;
	
	private:
		static const std::string sUserDataKey;
	};
	
	
	class Listener {
		public:
			Listener(): mCbs(NULL) {}
			virtual ~Listener() {setCallbacks(NULL);}
		
		public:
			void setCallbacks(::belle_sip_object_t *cbs) {
				if (mCbs != NULL) belle_sip_object_unref(mCbs);
				if (cbs == NULL) mCbs = NULL;
				else mCbs = belle_sip_object_ref(cbs);
			}
			belle_sip_object_t *getCallbacks() {return mCbs;}
		
		private:
			::belle_sip_object_t *mCbs;
	};
	
	
	class ListenableObject: public Object {
	protected:
		ListenableObject(::belle_sip_object_t *ptr, bool takeRef=true);
		void setListener(const std::shared_ptr<Listener> &listener);
	
	protected:
		static std::shared_ptr<Listener> & getListenerFromObject(::belle_sip_object_t *object);
	
	private:
		static void deleteListenerPtr(std::shared_ptr<Listener> *ptr) {delete ptr;}
	
	private:
		static std::string sListenerDataName;
	};
	
	class MultiListenableObject: public Object {
		friend class Factory;
		
	protected:
		MultiListenableObject(::belle_sip_object_t *ptr, bool takeRef=true);
		virtual ~MultiListenableObject();
		
	protected:
		void addListener(const std::shared_ptr<Listener> &listener);
		void removeListener(const std::shared_ptr<Listener> &listener);
	
	private:
		static void deleteListenerList(std::list<std::shared_ptr<Listener> > *listeners) {delete listeners;}
	
	private:
		static std::string sListenerListName;
	};
	
};

#endif // _LINPHONE_OBJECT_HH