DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
ref_or_owned.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_REF_OR_OWNED_H
13 #define DOCWIRE_REF_OR_OWNED_H
14 
15 #include <memory>
16 #include <type_traits>
17 
18 namespace docwire
19 {
20 
21 template<typename U, typename T>
22 concept ref_or_owned_compatible = std::is_convertible_v<std::shared_ptr<std::remove_reference_t<U>>, std::shared_ptr<T>>;
23 
32 template <typename T>
34 {
35  static_assert(!std::is_reference<T>::value, "ref_or_owned<T> cannot be instantiated with a reference type");
36  static_assert(!std::is_void<T>::value, "ref_or_owned<T> cannot be instantiated with void type");
37 
38 public:
39  ref_or_owned(ref_or_owned<T>&& other) noexcept
40  : v{other.to_shared_ptr()}
41  {}
42 
43  ref_or_owned(const ref_or_owned<T>& other)
44  : v{other.to_shared_ptr()}
45  {}
46 
54  template<ref_or_owned_compatible<T> U>
55  ref_or_owned(U& value)
56  : v{std::shared_ptr<T>{&value, [](auto*) {}}}
57  {}
58 
66  template<ref_or_owned_compatible<T> U>
67  ref_or_owned(U&& value)
68  : v{std::make_shared<std::remove_reference_t<U>>(std::forward<U>(value))}
69  {}
70 
71  template<ref_or_owned_compatible<T> U>
72  ref_or_owned(std::shared_ptr<U> ptr)
73  : v{ptr}
74  {}
75 
81  const T& get() const {
82  return *v;
83  }
84 
90  T& get() {
91  return *v;
92  }
93 
94  std::shared_ptr<T> to_shared_ptr() const
95  {
96  return v;
97  }
98 
99 private:
100  std::shared_ptr<T> v;
101 };
102 
103 } // namespace docwire
104 
105 #endif //DOCWIRE_REF_OR_OWNED_H
A utility class that simplifies declaring function attributes that need to be stored without requirin...
Definition: ref_or_owned.h:34
ref_or_owned(U &&value)
Constructs a ref_or_owned object from a temporary object.
Definition: ref_or_owned.h:67
const T & get() const
Returns a const reference to the stored object, regardless of whether it is stored as a reference or ...
Definition: ref_or_owned.h:81
T & get()
Returns a non-const reference to the stored object, regardless of whether it is stored as a reference...
Definition: ref_or_owned.h:90
ref_or_owned(U &value)
Constructs a ref_or_owned object from a reference to an object.
Definition: ref_or_owned.h:55
The main namespace for the DocWire SDK.
Definition: ai_elements.h:19