Browse Source

generic wrapper for several C datatypes.

master
roker 1 year ago
parent
commit
e46c0b6477
1 changed files with 85 additions and 0 deletions
  1. +85
    -0
      src/wrapper.hh

+ 85
- 0
src/wrapper.hh View File

@ -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