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

  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #ifndef LIBPEPDATATYPES_WRAPPER_HH
  4. #define LIBPEPDATATYPES_WRAPPER_HH
  5. #include <initializer_list>
  6. namespace pEp
  7. {
  8. template<class T>
  9. class Wrapper
  10. {
  11. public:
  12. template<class... Args>
  13. Wrapper(Args... args) : value{ this->_new(args...) } {}
  14. // no implicit copying... (yet?)
  15. Wrapper(const Wrapper<T>&) = delete;
  16. void operator=(const Wrapper<T>&) = delete;
  17. // must be implemented separately for each T
  18. Wrapper(Wrapper<T>&& victim);
  19. Wrapper<T>& operator=(Wrapper<T>&& victim);
  20. ~Wrapper();
  21. Wrapper<T> copy() const;
  22. private:
  23. // must be defined for each wrapped type:
  24. template<class... Args>
  25. T _new(Args...);
  26. T value;
  27. };
  28. // many wrapped datatypes are pointers, we can generalize a lot for them:
  29. template<class T>
  30. class Wrapper<T*>
  31. {
  32. public:
  33. Wrapper() : value{nullptr} {}
  34. template<class... Args>
  35. Wrapper(Args... args) : value{ this->_new(args...) } {}
  36. // no implicit copying... (yet?)
  37. Wrapper(const Wrapper<T*>&) = delete;
  38. void operator=(const Wrapper<T*>&) = delete;
  39. // must be implemented separately for each T
  40. Wrapper(Wrapper<T*>&& victim)
  41. : value{ victim.value}
  42. {
  43. victim.value = nullptr;
  44. }
  45. Wrapper<T*>&& operator=(Wrapper<T*>&& victim)
  46. {
  47. _free(value);
  48. value = victim.value;
  49. victim.value = nullptr;
  50. }
  51. ~Wrapper()
  52. {
  53. _free(value);
  54. }
  55. protected:
  56. Wrapper(T* _value) : value{_value} {}
  57. // must be defined for each wrapped type:
  58. template<class... Args>
  59. T* _new(Args...);
  60. void _free(T*);
  61. T* value;
  62. };
  63. // Wraps single-linked lists and provides an interface compatible
  64. // to std::forward_list
  65. template<class T, class Element>
  66. class ListWrapper;
  67. template<class T, class Element>
  68. class ListWrapper<T*, Element> : public Wrapper<T*>
  69. {
  70. public:
  71. struct Trait;
  72. typedef Wrapper<T*> Base;
  73. typedef ListWrapper<T*, Element> iterator; // FIXME: different type necessary?
  74. static Element T::* const Value; // to access the current value
  75. using Base::Base;
  76. using Base::value;
  77. ListWrapper() : Base() {}
  78. ListWrapper(const std::initializer_list<Element>& i);
  79. iterator begin() { return value; }
  80. iterator end() const { return iterator{}; }
  81. int size() const;
  82. bool empty() const;
  83. // I am my own iterator
  84. iterator operator++() { return (value ? value = value->next : value); }
  85. Element operator*() { return value->Value; }
  86. void clear();
  87. void push_back(Element&&);
  88. };
  89. } // end of namespace pEp
  90. #endif // LIBPEPDATATYPES_WRAPPER_HH