From 16cf6ce9c3968a50edcba90c39f20eb94988729e Mon Sep 17 00:00:00 2001 From: roker Date: Fri, 18 Jun 2021 10:10:03 +0200 Subject: [PATCH] add stringlist wrapper. --- src/stringlist.cc | 78 +++++++++++++++++++++++++++++++++++++++++++++++ src/types.cc | 9 +++--- src/types.hh | 3 ++ src/wrapper.hh | 7 +++-- 4 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/stringlist.cc diff --git a/src/stringlist.cc b/src/stringlist.cc new file mode 100644 index 0000000..360abaa --- /dev/null +++ b/src/stringlist.cc @@ -0,0 +1,78 @@ +#include "types.hh" + +#include // from libpEpAdapter +#include + +#include + +namespace pEp +{ + template<> + void Wrapper<::stringlist_t*>::_free(::stringlist_t* sl) + { + ::free_stringlist(sl); + } + + template<> + char* stringlist_t::* const ListWrapper::Value = &stringlist_t::value; + + template<> + int StringList::size() const + { + return stringlist_length(value); + } + + // faster than .size()==0 because it's not necessary to iterate throgh the whole list + template<> + bool StringList::empty() const + { + return !(value && value->value); + } + + template<> + void StringList::erase( const StringList::iterator& it) + { + if(it.value && it.value->value) + { + value = stringlist_delete(value, it.value->value); + } + } + + template<> + void StringList::clear() + { + free_stringlist(value); + value = nullptr; + } + + template<> + void StringList::push_back(const char*&& s) + { + auto last = stringlist_add(value, s); + if(value==nullptr) + value = last; + } + + + template<> + ListWrapper<::stringlist_t*, const char*>::ListWrapper(const std::initializer_list& il) + : StringList{} + { + ::stringlist_t* last = nullptr; + for(const char* s : il) + { + last = stringlist_add(last, s); + if(last==nullptr) + { + throw std::runtime_error("Cannot create StringPairList from {}: Out Of Memory."); + } + if(value==nullptr) + value = last; // save the head of linked list. + } + } + +//////////////// + + template class ListWrapper<::stringlist_t*, const char*>; + +} // end of namespace pEp diff --git a/src/types.cc b/src/types.cc index c52f1ff..d6fe3b2 100644 --- a/src/types.cc +++ b/src/types.cc @@ -152,10 +152,11 @@ namespace pEp //////////////// - template class Wrapper<::pEp_identity>; - template class Wrapper<::stringpair_t>; - - template class Wrapper<::message>; + template class Wrapper<::pEp_identity*>; + template class Wrapper<::stringpair_t*>; + + template class Wrapper<::message*>; + template class ListWrapper<::stringpair_list_t*, ::stringpair_t*>; } // end of namespace pEp diff --git a/src/types.hh b/src/types.hh index 3c14eb2..8a6dbd9 100644 --- a/src/types.hh +++ b/src/types.hh @@ -23,6 +23,9 @@ namespace pEp using StringPair = Wrapper<::stringpair_t*>; using StringPairList = ListWrapper<::stringpair_list_t*, ::stringpair_t*>; + using StringList = ListWrapper<::stringlist_t*, const char*>; + using BlobList = ListWrapper<::bloblist_t*, ::bloblist_t*>; + using Message = Wrapper<::message*>; } // end of namespace pEp diff --git a/src/wrapper.hh b/src/wrapper.hh index d2b0703..2c42aba 100644 --- a/src/wrapper.hh +++ b/src/wrapper.hh @@ -65,11 +65,12 @@ public: victim.value = nullptr; } - Wrapper&& operator=(Wrapper&& victim) + Wrapper& operator=(Wrapper&& victim) { _free(value); value = victim.value; victim.value = nullptr; + return *this; } ~Wrapper() @@ -142,7 +143,9 @@ public: using Base::value; ListWrapper() : Base() {} - ListWrapper(const std::initializer_list >& i); + + ListWrapper(const std::initializer_list>& i); + ListWrapper(const std::initializer_list& i); iterator begin() { return iterator{value}; } iterator end() const { return iterator{}; }