PDI 1.9.0-alpha.2025-01-15

the PDI data interface

ref_any.h
1/*******************************************************************************
2 * Copyright (C) 2015-2024 Commissariat a l'energie atomique et aux energies alternatives (CEA)
3 * Copyright (C) 2020-2021 Institute of Bioorganic Chemistry Polish Academy of Science (PSNC)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of CEA nor the names of its contributors may be used to
14 * endorse or promote products derived from this software without specific
15 * prior written permission.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 ******************************************************************************/
25
26#ifndef PDI_REF_ANY_H_
27#define PDI_REF_ANY_H_
28
29#include <algorithm>
30#include <cassert>
31#include <functional>
32#include <memory>
33#include <new>
34#include <unordered_map>
35
36#include <pdi/pdi_fwd.h>
37#include <pdi/array_datatype.h>
38#include <pdi/datatype.h>
39#include <pdi/error.h>
40#include <pdi/record_datatype.h>
41#include <pdi/scalar_datatype.h>
42
43namespace PDI {
44
45namespace {
46
49template <bool R, bool W>
50struct Ref_access {
52 using type = void;
53};
54
57template <bool R>
58struct Ref_access<R, true> {
60 using type = void*;
61};
62
65template <>
66struct Ref_access<true, false> {
68 using type = void const *;
69};
70
71template <bool R, bool W>
72using ref_access_t = typename Ref_access<R, W>::type;
73
74} // namespace
75
81class PDI_EXPORT Reference_base
82{
83protected:
88 struct PDI_NO_EXPORT Referenced_buffer {
90 std::function<void()> m_deallocator;
91
94
100
102 std::unordered_map<const Reference_base*, std::function<void(Ref)> > m_notifications;
103
110 Referenced_buffer(std::function<void()> deleter, bool readable, bool writable) noexcept
111 : m_deallocator{deleter}
112 , m_read_locks{readable ? 0 : 1}
113 , m_write_locks{writable ? 0 : 1}
114 {}
115
117
119
121
123 {
124 m_deallocator();
125 assert(m_read_locks == 0 || m_read_locks == 1);
126 assert(m_write_locks == 0 || m_write_locks == 1);
127 assert(m_notifications.empty());
128 }
129 };
130
135 struct PDI_NO_EXPORT Referenced_data {
137 mutable std::shared_ptr<Referenced_buffer> m_buffer;
138
140 void* m_data;
141
144
151 Referenced_data(std::shared_ptr<Referenced_buffer> buffer, void* data, Datatype_sptr type)
152 : m_buffer{std::move(buffer)}
153 , m_data{data}
154 , m_type{std::move(type)}
155 {
156 assert(m_buffer);
157 assert(data);
158 }
159
168 Referenced_data(void* data, std::function<void(void*)> freefunc, Datatype_sptr type, bool readable, bool writable)
169 : m_buffer(std::make_shared<Referenced_buffer>(
170 [data, freefunc, type]() {
171 type->destroy_data(data);
172 freefunc(data);
173 },
174 readable,
175 writable
176 ))
177 , m_data{data}
178 , m_type{type}
179 {
180 assert(data);
181 }
182
183 Referenced_data() = delete;
184
186
188 };
189
192 mutable std::shared_ptr<Referenced_data> m_content;
193
196 static std::shared_ptr<Referenced_data> PDI_NO_EXPORT get_content(const Reference_base& other) noexcept
197 {
198 if (!other.m_content) return nullptr;
199 if (!other.m_content->m_data) return nullptr;
200 return other.m_content;
201 }
202
203 // Symbol should not be exported, but it required to force
204 // generation of all 4 variants of `Ref_any::copy`
205 static Ref do_copy(Ref_r ref);
206
209 Reference_base() noexcept
210 : m_content(nullptr)
211 {}
212
214
216
217 Reference_base& operator= (const Reference_base&) = delete;
218
219 Reference_base& operator= (Reference_base&&) = delete;
220
221public:
224 Datatype_sptr type() const noexcept;
225
226 size_t hash() const noexcept { return std::hash<Referenced_data*>()(get_content(*this).get()); }
227
228}; // class Data_ref_base
229
240template <bool R, bool W>
241class PDI_EXPORT Ref_any: public Reference_base
242{
243public:
244 template <bool OR, bool OW>
245 friend class Ref_any;
246
249 Ref_any() = default;
250
257 Ref_any(const Ref_any& other) noexcept
259 {
260 link(get_content(other));
261 }
262
269 template <bool OR, bool OW>
270 Ref_any(const Ref_any<OR, OW>& other) noexcept
272 {
273 link(get_content(other));
274 }
275
279 Ref_any(Ref_any&& other) noexcept
281 {
282 if (!other.m_content) return;
283 // the other ref notification disappears
284 other.m_content->m_buffer->m_notifications.erase(&other);
285 // since we get the same privileges as those we release we can just steal the content
286 m_content = other.m_content;
287 other.m_content = nullptr;
288 }
289
298 Ref_any(void* data, std::function<void(void*)> freefunc, Datatype_sptr type, bool readable, bool writable)
300 {
301 if (type->datasize() && !data && (readable || writable)) {
302 throw Type_error{"Referencing null data with non-null size"};
303 }
304 if (data) {
305 link(std::make_shared<Referenced_data>(data, freefunc, std::move(type), readable, writable));
306 }
307 }
308
311 ~Ref_any() { reset(); }
312
313 Ref_any& operator= (Ref_any&& other) noexcept
314 {
315 // self-copy: nothing to do
316 if (&other == this) return *this;
317 // we'll be copied into, start nullifying ourselves first
318 reset();
319 // if the other is null also, we're done
320 if (other.is_null()) return *this;
321 // the other ref notification disappears
322 other.m_content->m_buffer->m_notifications.erase(&other);
323 // since we get the same privileges as those we release we can just steal the content
324 m_content = other.m_content;
325 other.m_content = nullptr;
326 return *this;
327 }
328
329 Ref_any& operator= (const Ref_any& other) const noexcept
330 {
331 // self-copy: nothing to do
332 if (&other == this) return *this;
333 // we'll be copied into, start nullifying ourselves first
334 reset();
335 // and copy the content from the other
336 link(get_content(other));
337 return *this;
338 }
339
340 bool operator== (const Reference_base& o) const noexcept
341 {
342 is_null();
343 return m_content == get_content(o);
344 }
345
346 bool operator!= (const Reference_base& o) const noexcept
347 {
348 is_null();
349 return m_content != get_content(o);
350 }
351
352 bool operator< (const Reference_base& o) const noexcept
353 {
354 is_null();
355 return m_content < get_content(o);
356 }
357
358 bool operator> (const Reference_base& o) const noexcept
359 {
360 is_null();
361 return m_content > get_content(o);
362 }
363
364 bool operator<= (const Reference_base& o) const noexcept
365 {
366 is_null();
367 return m_content <= get_content(o);
368 }
369
370 bool operator>= (const Reference_base& o) const noexcept
371 {
372 is_null();
373 return m_content >= get_content(o);
374 }
375
381 Ref operator[] (const std::string& member_name) const { return this->operator[] (member_name.c_str()); }
382
388 Ref operator[] (const char* member_name) const
389 {
390 if (is_null()) {
391 throw Type_error{"Cannot access member from empty Ref: `{}'", member_name};
392 }
393 std::pair<void*, Datatype_sptr> subref_info = type()->member(member_name, m_content->m_data);
394 Ref result;
395 result.link(std::make_shared<Referenced_data>(m_content->m_buffer, subref_info.first, std::move(subref_info.second)));
396 return result;
397 }
398
404 template <class T>
405 std::enable_if_t<std::is_integral<T>::value, Ref> operator[] (T index) const
406 {
407 if (is_null()) {
408 throw Type_error{"Cannot access array index from empty Ref: `{}'", index};
409 }
410 std::pair<void*, Datatype_sptr> subref_info = type()->index(index, m_content->m_data);
411 Ref result;
412 result.link(std::make_shared<Referenced_data>(m_content->m_buffer, subref_info.first, std::move(subref_info.second)));
413 return result;
414 }
415
421 Ref operator[] (std::pair<std::size_t, std::size_t> slice) const
422 {
423 if (is_null()) {
424 throw Type_error("Cannot access array slice from empty Ref: `{}:{}'", slice.first, slice.second);
425 }
426 std::pair<void*, Datatype_sptr> subref_info = type()->slice(slice.first, slice.second, m_content->m_data);
427 Ref result;
428 result.link(std::make_shared<Referenced_data>(m_content->m_buffer, subref_info.first, std::move(subref_info.second)));
429 return result;
430 }
431
437 {
438 if (is_null()) {
439 throw Type_error{"Cannot dereference an empty Ref"};
440 }
441
442 if (auto&& pointer_type = std::dynamic_pointer_cast<const PDI::Pointer_datatype>(type())) {
443 if constexpr (R) {
444 std::pair<void*, Datatype_sptr> subref_info = type()->dereference(m_content->m_data);
445 Ref result;
446 result.link(std::make_shared<Referenced_data>(m_content->m_buffer, subref_info.first, std::move(subref_info.second)));
447 return result;
448 } else {
449 return Ref_r(*this).dereference();
450 }
451 } else {
452 throw Type_error{"Cannot dereference a non pointer_type"};
453 }
454 }
455
460 operator ref_access_t<R, W> () const { return get(); }
461
466 ref_access_t<R, W> get() const
467 {
468 if (is_null()) throw Right_error{"Trying to dereference a null reference"};
469 return m_content->m_data;
470 }
471
476 ref_access_t<R, W> get(std::nothrow_t) const noexcept
477 {
478 if (is_null()) return nullptr;
479 return m_content->m_data;
480 }
481
485 template <class T>
486 T scalar_value() const
487 {
488 static_assert(R, "Cannot get scalar_value from Ref without read access");
489 if (auto&& scalar_type = std::dynamic_pointer_cast<const Scalar_datatype>(type())) {
490 if (scalar_type->kind() == PDI::Scalar_kind::UNSIGNED) {
491 switch (scalar_type->buffersize()) {
492 case 1L:
493 return *static_cast<const uint8_t*>(m_content->m_data);
494 case 2L:
495 return *static_cast<const uint16_t*>(m_content->m_data);
496 case 4L:
497 return *static_cast<const uint32_t*>(m_content->m_data);
498 case 8L:
499 return *static_cast<const uint64_t*>(m_content->m_data);
500 default:
501 throw Type_error{"Unknown size of unsigned integer datatype"};
502 }
503 } else if (scalar_type->kind() == PDI::Scalar_kind::SIGNED) {
504 switch (scalar_type->buffersize()) {
505 case 1L:
506 return *static_cast<const int8_t*>(m_content->m_data);
507 case 2L:
508 return *static_cast<const int16_t*>(m_content->m_data);
509 case 4L:
510 return *static_cast<const int32_t*>(m_content->m_data);
511 case 8L:
512 return *static_cast<const int64_t*>(m_content->m_data);
513 default:
514 throw Type_error{"Unknown size of integer datatype"};
515 }
516 } else if (scalar_type->kind() == PDI::Scalar_kind::FLOAT) {
517 switch (type()->buffersize()) {
518 case 4L: {
519 return *static_cast<const float*>(m_content->m_data);
520 }
521 case 8L: {
522 return *static_cast<const double*>(m_content->m_data);
523 }
524 default:
525 throw Type_error{"Unknown size of float datatype"};
526 }
527 } else {
528 throw Type_error{"Unknown datatype to get value"};
529 }
530 }
531 throw Type_error{"Expected scalar, found invalid type instead: {}", type()->debug_string()};
532 }
533
537 template <class T>
538 void scalar_assign(T value)
539 {
540 static_assert(std::is_scalar<T>::value, "T is not a scalar type");
541 static_assert(W, "Cannot assign a scalar value to Ref without write access");
542 if (auto&& scalar_type = std::dynamic_pointer_cast<const Scalar_datatype>(type())) {
543 if (scalar_type->kind() == PDI::Scalar_kind::UNSIGNED) {
544 switch (scalar_type->buffersize()) {
545 case 1L:
546 *static_cast<uint8_t*>(this->get()) = value;
547 return;
548 case 2L:
549 *static_cast<uint16_t*>(this->get()) = value;
550 return;
551 case 4L:
552 *static_cast<uint32_t*>(this->get()) = value;
553 return;
554 case 8L:
555 *static_cast<uint64_t*>(this->get()) = value;
556 return;
557 default:
558 throw Type_error{"Unknown size of unsigned integer datatype"};
559 }
560 } else if (scalar_type->kind() == PDI::Scalar_kind::SIGNED) {
561 switch (scalar_type->buffersize()) {
562 case 1L:
563 *static_cast<int8_t*>(this->get()) = value;
564 return;
565 case 2L:
566 *static_cast<int16_t*>(this->get()) = value;
567 return;
568 case 4L:
569 *static_cast<int32_t*>(this->get()) = value;
570 return;
571 case 8L:
572 *static_cast<int64_t*>(this->get()) = value;
573 return;
574 default:
575 throw Type_error{"Unknown size of integer datatype"};
576 }
577 } else if (scalar_type->kind() == PDI::Scalar_kind::FLOAT) {
578 switch (type()->buffersize()) {
579 case 4L: {
580 *static_cast<float*>(this->get()) = value;
581 return;
582 }
583 case 8L: {
584 *static_cast<double*>(this->get()) = value;
585 return;
586 }
587 default:
588 throw Type_error{"Unknown size of float datatype"};
589 }
590 } else {
591 throw Type_error{"Unknown datatype to get value"};
592 }
593 } else {
594 throw Type_error{"Expected scalar, found invalid type instead: {}", type()->debug_string()};
595 }
596 }
597
602 operator bool () const noexcept { return !is_null(); }
603
606 void reset() noexcept
607 {
608 if (m_content) unlink();
609 }
610
616 Ref copy() const { return do_copy(*this); }
617
624 void* release() noexcept
625 {
626 if (is_null()) return nullptr;
627
628 // notify everybody of the nullification
629 while (!m_content->m_buffer->m_notifications.empty()) {
630 // get the key of a notification
631 const Reference_base* key = m_content->m_buffer->m_notifications.begin()->first;
632 // call this notification, this might invalidate any iterator
633 m_content->m_buffer->m_notifications.begin()->second(*this);
634 // remove the notification we just called
635 m_content->m_buffer->m_notifications.erase(key);
636 }
637
638 void* result = m_content->m_data;
639 m_content->m_data = nullptr;
640 m_content->m_buffer->m_deallocator = []() {
641 }; // Referenced_metadata won't delete data
642
643 unlink();
644
645 return result;
646 }
647
652 void on_nullify(std::function<void(Ref)> notifier) const noexcept
653 {
654 if (!is_null()) m_content->m_buffer->m_notifications[this] = notifier;
655 }
656
657private:
665 bool PDI_NO_EXPORT is_null() const noexcept
666 {
667 if (!m_content) return true;
668 if (!m_content->m_data) {
669 unlink();
670 return true;
671 }
672 return false;
673 }
674
679 void PDI_NO_EXPORT unlink() const noexcept
680 {
681 assert(m_content);
682 m_content->m_buffer->m_notifications.erase(this);
683 if (R || W) --m_content->m_buffer->m_write_locks;
684 if (W) --m_content->m_buffer->m_read_locks;
685 m_content.reset();
686 }
687
696 void PDI_NO_EXPORT link(std::shared_ptr<Referenced_data> content) noexcept
697 {
698 assert(!m_content);
699 if (!content || !content->m_data) return; // null ref
700 if ((R && content->m_buffer->m_read_locks) || (W && content->m_buffer->m_write_locks)) {
701 return;
702 }
703 m_content = std::move(content);
704 if (R || W) ++m_content->m_buffer->m_write_locks;
705 if (W) ++m_content->m_buffer->m_read_locks;
706 }
707};
708
709} // namespace PDI
710
711namespace std {
712
713template <bool R, bool W>
714struct hash<PDI::Ref_any<R, W>> {
715 size_t operator() (const PDI::Ref_any<R, W>& r) const noexcept { return r.hash(); }
716};
717
718} // namespace std
719
720#endif // PDI_REF_ANY_H_
A dynamically typed reference to data with automatic memory management and read/write locking semanti...
Definition ref_any.h:242
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:298
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:270
void scalar_assign(T value)
Assign a scalar value to the data buffer according to its type.
Definition ref_any.h:538
Ref_any(Ref_any &&other) noexcept
Moves an existing reference.
Definition ref_any.h:279
~Ref_any()
Destructor.
Definition ref_any.h:311
void * release() noexcept
Releases ownership of the referenced raw data by nullifying all existing references.
Definition ref_any.h:624
ref_access_t< R, W > get() const
Offers access to the referenced raw data, throws on null references.
Definition ref_any.h:466
Ref copy() const
Makes a copy of the raw content behind this reference and returns a new reference.
Definition ref_any.h:616
Ref_any(const Ref_any &other) noexcept
Copies an existing reference.
Definition ref_any.h:257
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:476
T scalar_value() const
Returns a scalar value of type T taken from the data buffer.
Definition ref_any.h:486
Ref dereference() const
Create a reference to the pointed content in case the ref type is a reference.
Definition ref_any.h:436
void on_nullify(std::function< void(Ref)> notifier) const noexcept
Registers a nullification callback.
Definition ref_any.h:652
void reset() noexcept
Nullify the reference.
Definition ref_any.h:606
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:192
Reference_base(Reference_base &&)=delete
Reference_base() noexcept
Constructs a null reference.
Definition ref_any.h:209
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:196
Definition error.h:187
Definition error.h:200
Definition array_datatype.h:38
std::shared_ptr< const Datatype > Datatype_sptr
Definition pdi_fwd.h:79
STL namespace.
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
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:122
A descriptor for data on which references can point.
Definition ref_any.h:135
std::shared_ptr< Referenced_buffer > m_buffer
The buffer in which the data lives.
Definition ref_any.h:137
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:151
Datatype_sptr m_type
Type of the data.
Definition ref_any.h:143
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:168
Referenced_data(const Referenced_data &)=delete
void * m_data
In-memory location of the data.
Definition ref_any.h:140
Referenced_data(Referenced_data &&)=delete