From 9f2d01a2bff4472e5c6c67c20494864b6513e3dc Mon Sep 17 00:00:00 2001 From: roker Date: Mon, 21 Jun 2021 13:35:44 +0200 Subject: [PATCH] add identity & identity_list. --- src/identity.cc | 64 +++++++++++++++++++++++++++++++++++++++---------- src/types.hh | 2 ++ 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/identity.cc b/src/identity.cc index 782e349..b9b6637 100644 --- a/src/identity.cc +++ b/src/identity.cc @@ -1,28 +1,68 @@ #include "types.hh" +#include + +#include + namespace pEp { - template<> + void Wrapper<::identity_list*>::_free(::identity_list* sl) + { + ::free_identity_list(sl); + } + + template<> + pEp_identity* identity_list::* const ListWrapper::Value = &identity_list::ident; + template<> - pEp_identity* Wrapper<::pEp_identity*>::_new - (const char* address, const char* fpr, const char* user_id, const char* username) + int identity_list::size() const { - pEp_identity* id = new_identity(address, fpr, user_id, username); - if(!id) - { - throw EngineError(PEP_OUT_OF_MEMORY, "new_identity()"); - } - return id; + return identity_list_length(value); } + // faster than .size()==0 because it's not necessary to iterate throgh the whole list + template<> + bool identity_list::empty() const + { + return !(value && value->ident); + } + + template<> + void identity_list::clear() + { + free_identity_list(value); + value = nullptr; + } + template<> - void Wrapper<::pEp_identity*>::_free(::pEp_identity* id) + void identity_list::push_back(pEp_identity*&& id) { - free_identity(id); + auto last = identity_list_add(value, id); + if(value==nullptr) + value = last; + } + + + template<> + ListWrapper<::identity_list*, pEp_identity*>::ListWrapper(const std::initializer_list>& il) + : identity_list{} + { + ::identity_list* last = nullptr; + for(const Wrapper& id : il) + { + last = identity_list_add(last, identity_dup(id.get())); + 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 Wrapper<::pEp_identity*>; + template class ListWrapper<::identity_list*, ::pEp_identity*>; } // end of namespace pEp diff --git a/src/types.hh b/src/types.hh index 5239615..181064c 100644 --- a/src/types.hh +++ b/src/types.hh @@ -21,6 +21,8 @@ namespace pEp }; using Identity = Wrapper<::pEp_identity*>; + using IdentityList = ListWrapper<::pEp_identity_list*, ::pEp_identity*>; + using StringPair = Wrapper<::stringpair_t*>; using StringPairList = ListWrapper<::stringpair_list_t*, ::stringpair_t*>;