cgv
Loading...
Searching...
No Matches
informed_ptr.h
1#pragma once
2
3#include <array>
4#include <vector>
5
6namespace cgv {
7namespace data {
8
11public:
12 informed_ptr(const void* ptr) : ptr_(ptr) {}
13
14 // Get the stored pointer.
15 const void* get() const {
16 return ptr_;
17 }
18
20 template<typename T>
21 bool operator==(const T* ptr) const {
22 return ptr_ == reinterpret_cast<const void*>(ptr);
23 }
24
26 template<typename T>
27 bool is(const T* ptr) const {
28 return this->operator==(ptr);
29 }
30
32 template<typename T>
33 bool points_to(const T& ref) const {
34 return is(reinterpret_cast<const void*>(&ref));
35 }
36
38 template<typename T, typename... Ts>
39 bool points_to_one_of(const T& ref, const Ts&... refs) const {
40
41 /* C++ 17
42 if constexpr(sizeof...(refs))
43 return is(ref) || one_of(refs...);
44 else
45 return is(ref);
46 */
47
48 return points_to(ref) || points_to_one_of(refs...);
49 }
50
52 template<typename T>
53 bool points_to_member_of(const T& ref) const {
54 return is_in_counted_range(&ref, sizeof(T));
55 }
56
58 template<typename T>
59 bool points_inside(const T* array, size_t n) const {
60 return is_in_counted_range(array, sizeof(T) * n);
61 }
62
64 template<typename T, size_t N>
65 bool points_to_data_of(const std::array<T, N>& array) const {
66 return is_in_counted_range(array.data(), sizeof(T) * N);
67 }
68
70 template<typename T>
71 bool points_to_data_of(const std::vector<T>& vector) const {
72 return is_in_counted_range(vector.data(), sizeof(T) * vector.size());
73 }
74
76 template<typename T>
77 bool is_in_range(const T* first, const T* last) const {
78 return ptr_ >= reinterpret_cast<const void*>(first) && ptr_ < reinterpret_cast<const void*>(last);
79 }
80
82 template<typename T>
83 typename std::vector<T>::iterator find_in_data_of(std::vector<T>& vector) const {
84 if(points_to_data_of(vector)) {
85 for(auto it = vector.begin(); it != vector.end(); ++it) {
86 if(is(&*it))
87 return it;
88 }
89 }
90 return vector.end();
91 }
92
94 template<typename T>
95 typename std::vector<T>::const_iterator find_in_data_of(const std::vector<T>& vector) const {
96 return find_in_data_of(const_cast<std::vector<T>&>(vector));
97 }
98
99private:
101 bool points_to_one_of() const {
102 return false;
103 }
104
106 template<typename T>
107 bool is_in_counted_range(const T* ptr, size_t bytes) const {
108 const void* first = reinterpret_cast<const void*>(ptr);
109 const void* last = reinterpret_cast<const void*>(reinterpret_cast<size_t>(first) + bytes);
110 return ptr_ >= first && ptr_ < last;
111 }
112
114 const void* ptr_ = nullptr;
115};
116
117} // namespace data
118} // namespace cgv
This class provides methods to test if a stored pointer points to addresses of given variables or ins...
bool points_to_data_of(const std::array< T, N > &array) const
Return true if the stored pointer points to an element of the data of the given std::array.
bool points_to_one_of(const T &ref, const Ts &... refs) const
Return true if the stored pointer points to one of the given objects.
bool points_inside(const T *array, size_t n) const
Return true if the stored pointer points to an element inside the given c-style array.
bool points_to(const T &ref) const
Return true if the stored pointer points to the given object.
bool is_in_range(const T *first, const T *last) const
Return true if the stored pointer is inside the range [first, last).
bool points_to_member_of(const T &ref) const
Return true if the stored pointer points inside the address range of the given object.
bool points_to_data_of(const std::vector< T > &vector) const
Return true if the stored pointer points to an element of the data of the given std::vector.
bool is(const T *ptr) const
Return true if the stored pointer is equal to the given pointer.
bool operator==(const T *ptr) const
Return true if the stored pointer is equal to the given pointer.
std::vector< T >::const_iterator find_in_data_of(const std::vector< T > &vector) const
Return const iterator to the element in vector that the stored pointer points to or vector....
std::vector< T >::iterator find_in_data_of(std::vector< T > &vector) const
Return iterator to the element in vector that the stored pointer points to or vector....
this header is dependency free
Definition print.h:11