generic wrapper for several C datatypes.

master
roker 2 years ago
parent b70c3eaff2
commit e46c0b6477

@ -0,0 +1,85 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#ifndef PEP_DATATYPES_WRAPPER_HH
#define PEP_DATATYPES_WRAPPER_HH
namespace pEp
{
template<class T>
class Wrapper
{
public:
template<class... Args>
Wrapper(Args... args) : value{ _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{ _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 = victim.value;
victim.value = nullptr;
}
~Wrapper()
{
_free();
}
private:
// must be defined for each wrapped type:
template<class... Args>
T* _new(Args...);
void _free();
T* value;
};
} // end of namespace pEp
#endif // PEP_DATATYPES_WRAPPER_HH
Loading…
Cancel
Save