Browse Source

add identity & identity_list.

master
roker 4 years ago
parent
commit
9f2d01a2bf
  1. 64
      src/identity.cc
  2. 2
      src/types.hh

64
src/identity.cc

@ -1,28 +1,68 @@
#include "types.hh" #include "types.hh"
#include <pEp/identity_list.h>
#include <string>
namespace pEp namespace pEp
{ {
template<> template<>
void Wrapper<::identity_list*>::_free(::identity_list* sl)
{
::free_identity_list(sl);
}
template<>
pEp_identity* identity_list::* const ListWrapper<identity_list*, const char*>::Value = &identity_list::ident;
template<> template<>
pEp_identity* Wrapper<::pEp_identity*>::_new<const char*, const char*, const char*, const char*> int identity_list::size() const
(const char* address, const char* fpr, const char* user_id, const char* username)
{ {
pEp_identity* id = new_identity(address, fpr, user_id, username); return identity_list_length(value);
if(!id)
{
throw EngineError(PEP_OUT_OF_MEMORY, "new_identity()");
}
return id;
} }
// 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<> 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<Wrapper<pEp_identity*>>& il)
: identity_list{}
{
::identity_list* last = nullptr;
for(const Wrapper<pEp_identity*>& 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 } // end of namespace pEp

2
src/types.hh

@ -21,6 +21,8 @@ namespace pEp
}; };
using Identity = Wrapper<::pEp_identity*>; using Identity = Wrapper<::pEp_identity*>;
using IdentityList = ListWrapper<::pEp_identity_list*, ::pEp_identity*>;
using StringPair = Wrapper<::stringpair_t*>; using StringPair = Wrapper<::stringpair_t*>;
using StringPairList = ListWrapper<::stringpair_list_t*, ::stringpair_t*>; using StringPairList = ListWrapper<::stringpair_list_t*, ::stringpair_t*>;

Loading…
Cancel
Save