Cbeam
Loading...
Searching...
No Matches
pointer.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/container/stable_reference_buffer.hpp> // for cbeam::container::stable_reference_buffer
29#include <cbeam/convert/string.hpp> // for cbeam::convert::from_string, cbeam::convert::to_string
30#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
31
32#include <iosfwd> // for std::ostream
33#include <memory> // for std::shared_ptr, std::allocator, std::make_shared, std::__shared_ptr_access
34#include <string> // for std::operator+, std::char_traits, std::string, std::operator<<
35
36namespace cbeam::memory
37{
45 class pointer
46 {
47 public:
48 pointer() = default;
49
56 pointer(void* ptr)
57 : _ptr(ptr)
58 {
60 {
61 _mem = std::make_shared<container::stable_reference_buffer>(ptr);
62 }
63 }
64
72 pointer(const std::string& str_ptr)
73 : pointer(convert::from_string<void*>(str_ptr))
74 {
75 }
76
83 : _ptr((void*)mem.get())
84 , _mem(std::make_shared<container::stable_reference_buffer>(mem))
85 {
86 }
87
103 template <typename T>
104 pointer(const std::shared_ptr<T>& ptr)
105 : _ptr(static_cast<void*>(ptr.get()))
106 , _holder(new std::shared_ptr<T>(ptr))
107 {
108 }
109
115 bool is_managed() const
116 {
117 return (bool)_mem;
118 }
119
125 operator void*() const
126 {
127 return _ptr;
128 }
129
136 pointer& operator=(const pointer& other)
137 {
138 if (this == &other)
139 {
140 return *this;
141 }
142
143 _ptr = other._ptr;
144 _holder = other._holder;
145 if (other._mem)
146 {
147 _mem = std::make_shared<container::stable_reference_buffer>(*other._mem);
148 }
149 else
150 {
151 _mem.reset();
152 }
153
154 return *this;
155 }
156
163 explicit operator container::stable_reference_buffer() const
164 {
165 if (!_mem)
166 {
167 throw cbeam::error::runtime_error("cbeam::pointer(\"" + (std::string) * this + "\") is not an cbeam::container::stable_reference_buffer");
168 }
169 return *_mem;
170 }
171
177 explicit operator std::string() const
178 {
179 return convert::to_string(_ptr);
180 }
181
182 private:
183 void* _ptr{nullptr};
184 std::shared_ptr<container::stable_reference_buffer> _mem;
185 std::shared_ptr<void> _holder;
186 };
187
195 inline std::ostream& operator<<(std::ostream& os, const pointer& p)
196 {
197 os << static_cast<std::string>(p);
198 return os;
199 }
200
208 inline bool operator==(const pointer& lhs, const pointer& rhs)
209 {
210 return static_cast<void*>(lhs) == static_cast<void*>(rhs);
211 }
212
216 inline bool operator!=(const pointer& lhs, const pointer& rhs)
217 {
218 return !(lhs == rhs);
219 }
220
226 inline bool operator<(const pointer& lhs, const pointer& rhs)
227 {
228 return static_cast<void*>(lhs) < static_cast<void*>(rhs);
229 }
230
234 inline bool operator<=(const pointer& lhs, const pointer& rhs)
235 {
236 return (lhs == rhs) || (lhs < rhs);
237 }
238
242 inline bool operator>(const pointer& lhs, const pointer& rhs)
243 {
244 return !(lhs <= rhs);
245 }
246
250 inline bool operator>=(const pointer& lhs, const pointer& rhs)
251 {
252 return !(lhs < rhs);
253 }
254}
Manages memory buffers with stable reference counting, optimized for shared library contexts.
Definition stable_reference_buffer.hpp:63
static bool is_known(const void *address)
return if the given address is one of the managed addresses
Definition stable_reference_buffer.hpp:305
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
The pointer class is one of the types supported by container::xpod::type.
Definition pointer.hpp:46
pointer(void *ptr)
Construct from the given raw pointer. Checks if this ptr is managed by stable_reference_buffer.
Definition pointer.hpp:56
pointer(const std::shared_ptr< T > &ptr)
Definition pointer.hpp:104
pointer(const container::stable_reference_buffer &mem)
Construct from an existing stable_reference_buffer, incrementing its reference counter.
Definition pointer.hpp:82
bool is_managed() const
Checks whether the pointer is managed by stable_reference_buffer (or a std::shared_ptr) or not.
Definition pointer.hpp:115
pointer & operator=(const pointer &other)
Copy assignment operator. Increments the reference counter if the other pointer is managed.
Definition pointer.hpp:136
pointer(const std::string &str_ptr)
Construct from the given pointer in string representation (e.g. "0x...").
Definition pointer.hpp:72
operator container::stable_reference_buffer() const
Converts the pointer to a stable_reference_buffer if it is managed, otherwise throws a runtime_error.
Definition pointer.hpp:163
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44
Contains conversion utilities to transform data between different formats and types....
Definition buffer.hpp:35
std::string to_string(const container::buffer &b)
Creates a std::string from the contents of a container::buffer.
Definition buffer.hpp:42
Houses abstractions for shared-memory and interprocess data exchange. This includes interprocess_shar...
Definition interprocess_shared_memory.hpp:49
bool operator>(const pointer &lhs, const pointer &rhs)
Greater-than comparison operator for pointer.
Definition pointer.hpp:242
bool operator<(const pointer &lhs, const pointer &rhs)
Less-than comparison operator for pointer.
Definition pointer.hpp:226
bool operator<=(const pointer &lhs, const pointer &rhs)
Less-or-equal comparison operator for pointer.
Definition pointer.hpp:234
std::ostream & operator<<(std::ostream &os, const pointer &p)
Outputs the pointer object as its hex string representation.
Definition pointer.hpp:195
bool operator!=(const pointer &lhs, const pointer &rhs)
Inequality comparison operator for pointer.
Definition pointer.hpp:216
bool operator==(const pointer &lhs, const pointer &rhs)
Equality comparison operator for pointer.
Definition pointer.hpp:208
bool operator>=(const pointer &lhs, const pointer &rhs)
Greater-or-equal comparison operator for pointer.
Definition pointer.hpp:250