-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwrapper_base.hpp
More file actions
90 lines (69 loc) · 1.76 KB
/
wrapper_base.hpp
File metadata and controls
90 lines (69 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once
#include <utility>
#include <vector>
template <class T>
class wrapper_base
{
public:
using resource_type = T;
wrapper_base(const wrapper_base&) = delete;
wrapper_base& operator=(const wrapper_base&) = delete;
wrapper_base(wrapper_base&& rhs) noexcept
: p_resource(rhs.p_resource)
{
rhs.p_resource = nullptr;
}
wrapper_base& operator=(wrapper_base&& rhs) noexcept
{
std::swap(p_resource, rhs.p_resource);
return *this;
}
operator resource_type*() const noexcept
{
return p_resource;
}
protected:
// Allocation and deletion of p_resource must be handled by inheriting class.
explicit wrapper_base(resource_type* resource = nullptr)
: p_resource(resource)
{
}
~wrapper_base() = default;
resource_type* p_resource = nullptr;
};
template <class T>
class list_wrapper : public wrapper_base<typename T::resource_type*>
{
public:
using base_type = wrapper_base<typename T::resource_type*>;
explicit list_wrapper(std::vector<T> list)
: m_list(std::move(list))
{
this->p_resource = new base_type::resource_type[m_list.size()];
for (size_t i = 0; i < m_list.size(); ++i)
{
this->p_resource[i] = m_list[i];
}
}
~list_wrapper()
{
delete[] this->p_resource;
this->p_resource = nullptr;
}
list_wrapper(list_wrapper&&) noexcept = default;
list_wrapper& operator=(list_wrapper&&) noexcept = default;
size_t size() const
{
return m_list.size();
}
const T& operator[](size_t pos) const
{
return m_list[pos];
}
const T& front() const
{
return m_list.front();
}
private:
std::vector<T> m_list;
};