A C++ wrapper for the basic C datatypes defined by the pEpEngine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

128 lines
2.7 KiB

// This file is under GNU General Public License 3.0
// see LICENSE.txt
#ifndef LIBPEPDATATYPES_WRAPPER_HH
#define LIBPEPDATATYPES_WRAPPER_HH
#include <initializer_list>
namespace pEp
{
template<class T>
class Wrapper
{
public:
template<class... Args>
Wrapper(Args... args) : value{ this->_new(args...) } {}
// no implicit copying... (yet?)
Wrapper(const Wrapper<T>&) = delete;
void operator=(const Wrapper<T>&) = delete;
// must be implemented separately for each T
Wrapper(Wrapper<T>&& victim);
Wrapper<T>& operator=(Wrapper<T>&& victim);
~Wrapper();
Wrapper<T> copy() const;
private:
// must be defined for each wrapped type:
template<class... Args>
T _new(Args...);
T value;
};
// many wrapped datatypes are pointers, we can generalize a lot for them:
template<class T>
class Wrapper<T*>
{
public:
Wrapper() : value{nullptr} {}
template<class... Args>
Wrapper(Args... args) : value{ this->_new(args...) } {}
// no implicit copying... (yet?)
Wrapper(const Wrapper<T*>&) = delete;
void operator=(const Wrapper<T*>&) = delete;
// must be implemented separately for each T
Wrapper(Wrapper<T*>&& victim)
: value{ victim.value}
{
victim.value = nullptr;
}
Wrapper<T*>&& operator=(Wrapper<T*>&& victim)
{
_free(value);
value = victim.value;
victim.value = nullptr;
}
~Wrapper()
{
_free(value);
}
protected:
Wrapper(T* _value) : value{_value} {}
// must be defined for each wrapped type:
template<class... Args>
T* _new(Args...);
void _free(T*);
T* value;
};
// Wraps single-linked lists and provides an interface compatible
// to std::forward_list
template<class T, class Element>
class ListWrapper;
template<class T, class Element>
class ListWrapper<T*, Element> : public Wrapper<T*>
{
public:
struct Trait;
typedef Wrapper<T*> Base;
typedef ListWrapper<T*, Element> iterator; // FIXME: different type necessary?
static Element T::* const Value; // to access the current value
using Base::Base;
using Base::value;
ListWrapper() : Base() {}
ListWrapper(const std::initializer_list<Element>& i);
iterator begin() { return 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 clear();
void push_back(Element&&);
};
} // end of namespace pEp
#endif // LIBPEPDATATYPES_WRAPPER_HH