sparrow 0.6.0
Loading...
Searching...
No Matches
private_data.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <optional>
18#include <string>
19#include <type_traits>
20
25
26namespace sparrow
27{
37 {
38 public:
39
40 using FormatType = std::string;
41 using NameType = std::optional<std::string>;
42 using MetadataType = std::optional<std::string>;
43
49
51
52 template <class F, class N, class M, std::ranges::input_range CHILDREN_OWNERSHIP>
53 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
54 && std::constructible_from<arrow_schema_private_data::NameType, N>
55 && std::constructible_from<arrow_schema_private_data::MetadataType, M>
56 && std::is_same_v<std::ranges::range_value_t<CHILDREN_OWNERSHIP>, bool>
58 F format,
59 N name,
60 M metadata,
61 const CHILDREN_OWNERSHIP& children_ownership,
63 );
64
65 template <class F, class N, input_metadata_container M = std::vector<metadata_pair>>
66 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
67 && std::constructible_from<arrow_schema_private_data::NameType, N>
69
70 [[nodiscard]] const char* format_ptr() const noexcept;
71 [[nodiscard]] FormatType& format() noexcept;
72 [[nodiscard]] const char* name_ptr() const noexcept;
73 [[nodiscard]] NameType& name() noexcept;
74 [[nodiscard]] const char* metadata_ptr() const noexcept;
75 [[nodiscard]] MetadataType& metadata() noexcept;
76
77 private:
78
79 FormatType m_format;
80 NameType m_name;
81 MetadataType m_metadata;
82 };
83
84 template <class T>
85 constexpr std::optional<std::string> to_optional_string(T&& t)
86 {
87 if constexpr (std::same_as<std::remove_cvref_t<T>, std::string>)
88 {
89 return std::forward<T>(t);
90 }
91 else if constexpr (std::same_as<std::nullopt_t, T>)
92 {
93 return std::nullopt;
94 }
95 else if constexpr (std::is_pointer_v<T>)
96 {
97 if (t == nullptr)
98 {
99 return std::nullopt;
100 }
101 else
102 {
103 return std::string(t);
104 }
105 }
106 else if constexpr (std::ranges::range<T>)
107 {
108 return std::string(t.cbegin(), t.cend());
109 }
111 {
112 if (t.has_value())
113 {
114 return to_optional_string(*t);
115 }
116 else
117 {
118 return std::nullopt;
119 }
120 }
121 else
122 {
123 static_assert(mpl::dependent_false<T, T>::value, "to_optional_string: unsupported type.");
125 }
126 }
127
128 template <class F, class N, class M, std::ranges::input_range CHILDREN_OWNERSHIP>
129 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
130 && std::constructible_from<arrow_schema_private_data::NameType, N>
131 && std::constructible_from<arrow_schema_private_data::MetadataType, M>
132 && std::is_same_v<std::ranges::range_value_t<CHILDREN_OWNERSHIP>, bool>
134 F format,
135 N name,
136 M metadata,
137 const CHILDREN_OWNERSHIP& children_ownership_range,
138 bool dictionary_ownership_value
139 )
140 : children_ownership(children_ownership_range)
141 , dictionary_ownership(dictionary_ownership_value)
142 , m_format(std::move(format))
143 , m_name(to_optional_string(std::forward<N>(name)))
144 , m_metadata(to_optional_string(std::forward<M>(metadata)))
145 {
146 SPARROW_ASSERT_TRUE(!m_format.empty())
147 }
148
149 template <class F, class N, input_metadata_container M>
150 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
151 && std::constructible_from<arrow_schema_private_data::NameType, N>
154 , m_format(std::move(format))
155 , m_name(to_optional_string(std::forward<N>(name)))
157 {
158 SPARROW_ASSERT_TRUE(!m_format.empty())
159 }
160
161 [[nodiscard]] inline const char* arrow_schema_private_data::format_ptr() const noexcept
162 {
163 return m_format.data();
164 }
165
167 {
168 return m_format;
169 }
170
171 [[nodiscard]] inline const char* arrow_schema_private_data::name_ptr() const noexcept
172 {
173 if (m_name.has_value())
174 {
175 return m_name->data();
176 }
177 return nullptr;
178 }
179
181 {
182 return m_name;
183 }
184
185 [[nodiscard]] inline const char* arrow_schema_private_data::metadata_ptr() const noexcept
186 {
187 if (m_metadata.has_value())
188 {
189 return m_metadata->data();
190 }
191 return nullptr;
192 }
193
195 {
196 return m_metadata;
197 }
198
199}
const char * format_ptr() const noexcept
arrow_schema_private_data & operator=(const arrow_schema_private_data &)=delete
arrow_schema_private_data & operator=(arrow_schema_private_data &&)=delete
MetadataType & metadata() noexcept
arrow_schema_private_data(const arrow_schema_private_data &)=delete
arrow_schema_private_data(arrow_schema_private_data &&)=delete
const char * name_ptr() const noexcept
const char * metadata_ptr() const noexcept
std::optional< std::string > NameType
std::optional< std::string > MetadataType
children_ownership(std::size_t size=0)
std::size_t children_size() const noexcept
#define SPARROW_ASSERT_TRUE(expr__)
constexpr bool is_type_instance_of_v
true if T is a concrete type template instanciation of U which is a type template.
Definition mp_utils.hpp:50
void unreachable()
Invokes undefined behavior.
Definition mp_utils.hpp:425
std::string get_metadata_from_key_values(const T &metadata)
Definition metadata.hpp:115
constexpr std::optional< std::string > to_optional_string(T &&t)
Workaround to replace static_assert(false) in template code.
Definition mp_utils.hpp:33