cgv
Loading...
Searching...
No Matches
pointer_test.h
1#pragma once
2
3#include <vector>
4
5namespace cgv {
6namespace utils {
7
8/* This class provides methods to test if a stored pointer points to addresses of given variables or inside the range of object instances. */
10private:
12 bool one_of() const {
13 return false;
14 }
15
16public:
18 const void* ptr = nullptr;
19
21 pointer_test(const void* ptr) : ptr(ptr) {}
22
24 bool is(const void* ptr) const {
25 return this->ptr == ptr;
26 }
27
29 template<typename T>
30 bool is(const T& ref) const {
31 return is(static_cast<const void*>(&ref));
32 }
33
35 template <typename T, typename... Ts>
36 bool one_of(const T& ref, const Ts&... refs) const {
37
38 /* C++ 17
39 if constexpr(sizeof...(refs))
40 return is(ref) || one_of(refs...);
41 else
42 return is(ref);
43 */
44
45 return is(ref) || one_of(refs...);
46 }
47
49 bool one_of(const std::vector<const void*>& ptrs) const {
50 for(size_t i = 0; i < ptrs.size(); ++i)
51 if(is(ptrs[i])) return true;
52 return false;
53 }
54
56 template <typename T>
57 bool member_of(const T& ref) const {
58 const void* addr_begin = reinterpret_cast<const void*>(&ref);
59 const void* addr_end = reinterpret_cast<const void*>(reinterpret_cast<size_t>(addr_begin) + sizeof(T));
60 return ptr >= addr_begin && ptr < addr_end;
61 }
62};
63
64}
65}
bool is(const T &ref) const
Test if the stored pointer points to the given variable/object instance.
const void * ptr
The stored pointer.
pointer_test(const void *ptr)
Instantiate a pointer_test with a const void pointer.
bool one_of(const std::vector< const void * > &ptrs) const
Test if the stored pointer points to one of the given pointer addresses.
bool one_of(const T &ref, const Ts &... refs) const
Test if the stored pointer points to one of the given variable/object instances.
bool member_of(const T &ref) const
Test if the stored pointer points inside the address range of the given object instance.
bool is(const void *ptr) const
Test if the stored pointer points to the given pointer address.
the cgv namespace
Definition print.h:11