Browse Source

define a separate iterator type which can be copied easily and does not own the pointee. Add weird member pointer. \o/

master
roker 4 months ago
parent
commit
2b41928f54
2 changed files with 25 additions and 9 deletions
  1. +3
    -0
      src/types.cc
  2. +22
    -9
      src/wrapper.hh

+ 3
- 0
src/types.cc View File

@ -65,6 +65,9 @@ namespace pEp
{
free_stringpair(sp);
}
template<>
stringpair_t* stringpair_list_t::* const ListWrapper<stringpair_list_t*, stringpair_t*>::Value = &stringpair_list_t::value;
////////////////


+ 22
- 9
src/wrapper.hh View File

@ -116,25 +116,38 @@ class ListWrapper<T*, Element> : public Wrapper<T*>
public:
struct Trait;
typedef Wrapper<T*> Base;
typedef ListWrapper<T*, Element> iterator; // FIXME: different type necessary?
typedef ListWrapper<T*, Element> LW;
static Element T::* const Value; // to access the current value
using Base::Base;
// does not own the *value
class iterator
{
public:
iterator() = default;
iterator operator++() { return (value ? value = value->next : value); }
Element operator*() { return value->*LW::Value; }
bool operator==(const iterator& other) const { return value == other.value; }
bool operator!=(const iterator& other) const { return value != other.value; }
private:
iterator(T* _t) : value{_t} {}
T* value = nullptr;
friend class ListWrapper<T*, Element>;
};
using Base::value;
ListWrapper() : Base() {}
ListWrapper(const std::initializer_list<Element>& i);
ListWrapper(const std::initializer_list<pEp::Wrapper<Element> >& i);
iterator begin() { return value; }
iterator begin() { return iterator{value}; }
iterator end() const { return iterator{}; }
int size() const;
bool empty() const;
// I am my own iterator
iterator operator++() { return (value ? value = value->next : value); }
Element operator*() { return value->*Value; }
void erase(const iterator& it);
void clear();
void push_back(Element&&);


Loading…
Cancel
Save