34#include <unordered_map>
36#include <pdi/pdi_fwd.h>
37#include <pdi/array_datatype.h>
38#include <pdi/datatype.h>
40#include <pdi/record_datatype.h>
41#include <pdi/scalar_datatype.h>
49template <
bool R,
bool W>
58struct Ref_access<R, true> {
66struct Ref_access<true, false> {
68 using type =
void const *;
71template <
bool R,
bool W>
72using ref_access_t =
typename Ref_access<R, W>::type;
115 , m_read_locks{readable ? 0 : 1}
116 , m_write_locks{writable ? 0 : 1}
128 assert(m_read_locks == 0 || m_read_locks == 1);
129 assert(m_write_locks == 0 || m_write_locks == 1);
130 assert(m_notifications.empty());
140 mutable std::shared_ptr<Referenced_buffer>
m_buffer;
155 : m_buffer{
std::move(buffer)}
157 , m_type{
std::move(type)}
173 [data, freefunc, type]()
175 type->destroy_data(data);
201 if (!other.m_content)
return nullptr;
202 if (!other.m_content->m_data)
return nullptr;
203 return other.m_content;
229 size_t hash() const noexcept
231 return std::hash<Referenced_data*>()(get_content(*this).get());
246template <
bool R,
bool W>
250 template <
bool OR,
bool OW>
266 link(get_content(other));
275 template <
bool OR,
bool OW>
279 link(get_content(other));
288 if (!other.m_content)
return;
290 other.
m_content->m_buffer->m_notifications.erase(&other);
292 m_content = other.m_content;
293 other.m_content =
nullptr;
307 if (type->datasize() && !data && (readable || writable)) {
308 throw Type_error{
"Referencing null data with non-null size"};
311 link(std::make_shared<Referenced_data>(data, freefunc, std::move(type), readable, writable));
325 if (&other ==
this)
return *
this;
329 if (other.is_null())
return *
this;
331 other.m_content->m_buffer->m_notifications.erase(&other);
333 m_content = other.m_content;
334 other.m_content =
nullptr;
341 if (&other ==
this)
return *
this;
345 link(get_content(other));
352 return m_content == get_content(o);
358 return m_content != get_content(o);
364 return m_content < get_content(o);
370 return m_content > get_content(o);
376 return m_content <= get_content(o);
382 return m_content >= get_content(o);
390 Ref operator[] (
const std::string& member_name)
const
392 return this->operator[] (member_name.c_str());
400 Ref operator[] (
const char* member_name)
const
403 throw Type_error{
"Cannot access member from empty Ref: `{}'", member_name};
405 std::pair<void*, Datatype_sptr> subref_info = type()->member(member_name, m_content->m_data);
407 result.link(std::make_shared<Referenced_data>(m_content->m_buffer, subref_info.first, std::move(subref_info.second)));
417 std::enable_if_t<std::is_integral<T>::value,
Ref> operator[] (T index)
const
420 throw Type_error{
"Cannot access array index from empty Ref: `{}'", index};
422 std::pair<void*, Datatype_sptr> subref_info = type()->index(index, m_content->m_data);
424 result.link(std::make_shared<Referenced_data>(m_content->m_buffer, subref_info.first, std::move(subref_info.second)));
433 Ref operator[] (std::pair<std::size_t, std::size_t> slice)
const
436 throw Type_error(
"Cannot access array slice from empty Ref: `{}:{}'", slice.first, slice.second);
438 std::pair<void*, Datatype_sptr> subref_info = type()->slice(slice.first, slice.second, m_content->m_data);
440 result.link(std::make_shared<Referenced_data>(m_content->m_buffer, subref_info.first, std::move(subref_info.second)));
451 throw Type_error{
"Cannot dereference an empty Ref"};
454 if (
auto&& pointer_type = std::dynamic_pointer_cast<const PDI::Pointer_datatype>(type())) {
456 std::pair<void*, Datatype_sptr> subref_info = type()->dereference(m_content->m_data);
458 result.link(std::make_shared<Referenced_data>(m_content->m_buffer, subref_info.first, std::move(subref_info.second)));
464 throw Type_error{
"Cannot dereference a non pointer_type"};
472 operator ref_access_t<R, W> ()
const
481 ref_access_t<R, W>
get()
const
483 if (is_null())
throw Right_error{
"Trying to dereference a null reference"};
484 return m_content->m_data;
491 ref_access_t<R, W>
get(std::nothrow_t)
const noexcept
493 if (is_null())
return nullptr;
494 return m_content->m_data;
503 static_assert(R,
"Cannot get scalar_value from Ref without read access");
504 if (
auto&& scalar_type = std::dynamic_pointer_cast<const Scalar_datatype>(type())) {
506 switch (scalar_type->buffersize()) {
508 return *
static_cast<const uint8_t*
>(m_content->m_data);
510 return *
static_cast<const uint16_t*
>(m_content->m_data);
512 return *
static_cast<const uint32_t*
>(m_content->m_data);
514 return *
static_cast<const uint64_t*
>(m_content->m_data);
516 throw Type_error{
"Unknown size of unsigned integer datatype"};
519 switch (scalar_type->buffersize()) {
521 return *
static_cast<const int8_t*
>(m_content->m_data);
523 return *
static_cast<const int16_t*
>(m_content->m_data);
525 return *
static_cast<const int32_t*
>(m_content->m_data);
527 return *
static_cast<const int64_t*
>(m_content->m_data);
529 throw Type_error{
"Unknown size of integer datatype"};
532 switch (type()->buffersize()) {
534 return *
static_cast<const float*
>(m_content->m_data);
537 return *
static_cast<const double*
>(m_content->m_data);
540 throw Type_error{
"Unknown size of float datatype"};
543 throw Type_error{
"Unknown datatype to get value"};
546 throw Type_error{
"Expected scalar, found invalid type instead: {}", type()->debug_string()};
553 operator bool () const noexcept
562 if (m_content) unlink();
572 return do_copy(*
this);
583 if (is_null())
return nullptr;
586 while (!m_content->m_buffer->m_notifications.empty()) {
588 const Reference_base* key = m_content->m_buffer->m_notifications.begin()->first;
590 m_content->m_buffer->m_notifications.begin()->second(*
this);
592 m_content->m_buffer->m_notifications.erase(key);
595 void* result = m_content->m_data;
596 m_content->m_data =
nullptr;
597 m_content->m_buffer->m_deallocator = []() {
611 if (!is_null()) m_content->m_buffer->m_notifications[
this] = notifier;
622 bool PDI_NO_EXPORT is_null() const noexcept
624 if (!m_content)
return true;
625 if (!m_content->m_data) {
636 void PDI_NO_EXPORT unlink() const noexcept
639 m_content->m_buffer->m_notifications.erase(
this);
640 if (R || W) --m_content->m_buffer->m_write_locks;
641 if (W) --m_content->m_buffer->m_read_locks;
653 void PDI_NO_EXPORT link(std::shared_ptr<Referenced_data> content)
noexcept
656 if (!content || !content->m_data)
return;
657 if ((R && content->m_buffer->m_read_locks) || (W && content->m_buffer->m_write_locks)) {
660 m_content = std::move(content);
661 if (R || W) ++m_content->m_buffer->m_write_locks;
662 if (W) ++m_content->m_buffer->m_read_locks;
670template <
bool R,
bool W>
671struct hash<
PDI::Ref_any<R, W>> {
A dynamically typed reference to data with automatic memory management and read/write locking semanti...
Definition: ref_any.h:248
Ref_any(void *data, std::function< void(void *)> freefunc, Datatype_sptr type, bool readable, bool writable)
Creates a reference to currently unreferenced data.
Definition: ref_any.h:304
Ref_any()=default
Constructs a null reference.
Ref_any(const Ref_any< OR, OW > &other) noexcept
Copies an existing reference with different privileges.
Definition: ref_any.h:276
Ref_any(Ref_any &&other) noexcept
Moves an existing reference.
Definition: ref_any.h:285
~Ref_any()
Destructor.
Definition: ref_any.h:317
void * release() noexcept
Releases ownership of the referenced raw data by nullifying all existing references.
Definition: ref_any.h:581
ref_access_t< R, W > get() const
Offers access to the referenced raw data, throws on null references.
Definition: ref_any.h:481
Ref copy() const
Makes a copy of the raw content behind this reference and returns a new reference.
Definition: ref_any.h:570
Ref_any(const Ref_any &other) noexcept
Copies an existing reference.
Definition: ref_any.h:263
ref_access_t< R, W > get(std::nothrow_t) const noexcept
Offers access to the referenced raw data, returns null for null references.
Definition: ref_any.h:491
T scalar_value() const
Returns a scalar value of type T taken from the data buffer.
Definition: ref_any.h:501
Ref dereference() const
Create a reference to the pointed content in case the ref type is a reference.
Definition: ref_any.h:448
void on_nullify(std::function< void(Ref)> notifier) const noexcept
Registers a nullification callback.
Definition: ref_any.h:609
void reset() noexcept
Nullify the reference.
Definition: ref_any.h:560
A common base for all references, whatever their access privileges.
Definition: ref_any.h:82
std::shared_ptr< Referenced_data > m_content
Pointer on the data content, can be null if the ref is null.
Definition: ref_any.h:195
Reference_base(Reference_base &&)=delete
Reference_base() noexcept
Constructs a null reference.
Definition: ref_any.h:212
Reference_base(const Reference_base &)=delete
static Ref do_copy(Ref_r ref)
Datatype_sptr type() const noexcept
accesses the type of the referenced raw data
static std::shared_ptr< Referenced_data > get_content(const Reference_base &other) noexcept
Function to access the content from a reference with different access right.
Definition: ref_any.h:199
Definition: array_datatype.h:38
std::shared_ptr< const Datatype > Datatype_sptr
Definition: pdi_fwd.h:79
A descriptor for a buffer in which references can point.
Definition: ref_any.h:88
std::unordered_map< const Reference_base *, std::function< void(Ref)> > m_notifications
Nullification notifications registered on this instance.
Definition: ref_any.h:102
Referenced_buffer(const Referenced_buffer &)=delete
std::function< void()> m_deallocator
The function to call to deallocate the buffer memory.
Definition: ref_any.h:90
Referenced_buffer()=delete
int m_write_locks
Number of locks preventing write access.
Definition: ref_any.h:99
Referenced_buffer(Referenced_buffer &&)=delete
int m_read_locks
Number of locks preventing read access.
Definition: ref_any.h:93
Referenced_buffer(std::function< void()> deleter, bool readable, bool writable) noexcept
Constructs a new buffer descriptor.
Definition: ref_any.h:110
~Referenced_buffer()
Definition: ref_any.h:125
A descriptor for data on which references can point.
Definition: ref_any.h:138
std::shared_ptr< Referenced_buffer > m_buffer
The buffer in which the data lives.
Definition: ref_any.h:140
Referenced_data(std::shared_ptr< Referenced_buffer > buffer, void *data, Datatype_sptr type)
Constructs a new data descriptor from an already referenced buffer.
Definition: ref_any.h:154
Datatype_sptr m_type
Type of the data.
Definition: ref_any.h:146
Referenced_data(void *data, std::function< void(void *)> freefunc, Datatype_sptr type, bool readable, bool writable)
Constructs a new data descriptor.
Definition: ref_any.h:171
Referenced_data(const Referenced_data &)=delete
void * m_data
In-memory location of the data.
Definition: ref_any.h:143
Referenced_data(Referenced_data &&)=delete