tools.hh 3.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
tools.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.
*/

#ifndef _TOOLS_HH
#define _TOOLS_HH

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

namespace linphone {
	
	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) {
46
			mCList = cppListToBctbxList(cppList);
47 48
		}
		virtual ~ObjectBctbxListWrapper() {
49 50 51
			if (mCList != NULL) {
				bctbx_list_free_with_data(mCList, unrefData);
			}
52 53 54 55 56 57 58 59 60
		}
		static std::list<std::shared_ptr<T> > bctbxListToCppList(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>(it->data);
				cppList.push_back(newObj);
			}
			return cppList;
		}
61 62 63 64 65 66 67 68 69
		static ::bctbx_list_t *cppListToBctbxList(const std::list<std::shared_ptr<T> > &cppList) {
			bctbx_list_t *cList = NULL;
			for(auto it=cppList.cbegin(); it!=cppList.cend(); it++) {
				::belle_sip_object_t *cPtr = (::belle_sip_object_t *)Object::sharedPtrToCPtr(std::static_pointer_cast<Object,T>(*it));
				if (cPtr != NULL) belle_sip_object_ref(cPtr);
				cList = bctbx_list_append(cList, cPtr);
			}
			return cList;
		}
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	
	private:
		static void unrefData(void *data) {
			if (data != NULL) belle_sip_object_unref(data);
		}
	};
	
	
	class StringBctbxListWrapper: public AbstractBctbxListWrapper {
	public:
		StringBctbxListWrapper(const std::list<std::string> &cppList);
		virtual ~StringBctbxListWrapper();
		static std::list<std::string> bctbxListToCppList(const ::bctbx_list_t *bctbxList);
	};
	
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	
	template <class T, class U>
	class StructBctbxListWrapper: public AbstractBctbxListWrapper {
	public:
		StructBctbxListWrapper(const std::list<T> &cppList): AbstractBctbxListWrapper() {
			mCList = cppListToBctbxList(cppList);
		}
		virtual ~StructBctbxListWrapper() {
			bctbx_list_free_with_data(mCList, (bctbx_list_free_func)deleteCStruct);
		}
		static std::list<T> bctbxListToCppList(const ::bctbx_list_t *bctbxList) {
			std::list<T> cppList;
			for(const bctbx_list_t *it = bctbx_list_first_elem(bctbxList); it != NULL; it = bctbx_list_next(it)) {
				cppList->push_back(T(it->data));
			}
			return cppList;
		}
		static bctbx_list_t *cppListToBctbxList(const std::list<T> &cppList) {
			bctbx_list_t *cList = NULL;
			for(auto it=cppList.cbegin(); it!=cppList.cend(); it++) {
				cList = bctbx_list_append(cList, new U(it->c_struct()));
			}
			return cList;
		}
	
	private:
		static void deleteCStruct(U *cStruct) {delete cStruct;}
	};
	
114 115 116 117 118 119 120 121
	class StringUtilities {
	public:
		static std::string cStringToCpp(const char *cstr);
		static std::string cStringToCpp(char *cstr);
		static const char *cppStringToC(const std::string &cppstr);
		static std::list<std::string> cStringArrayToCppList(const char **cArray);
	};
	
122 123 124 125 126 127 128 129 130 131 132 133 134 135
	template <class T>
	class StructWrapper {
	public:
		StructWrapper(const T &s) {
			mStruct = s;
		}
		const void *ptr() const {
			return &mStruct;
		}
		
	private:
		T mStruct;
	};

136 137 138
};

#endif // _TOOLS_HH