sparrow 0.3.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
24
25namespace sparrow
26{
35 {
36 public:
37
38 using FormatType = std::string;
39 using NameType = std::optional<std::string>;
40 using MetadataType = std::optional<std::string>;
41
47
49
50 template <class F, class N, class M>
51 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
52 && std::constructible_from<arrow_schema_private_data::NameType, N>
53 && std::constructible_from<arrow_schema_private_data::MetadataType, M>
55
56 [[nodiscard]] const char* format_ptr() const noexcept;
57 [[nodiscard]] FormatType& format() noexcept;
58 [[nodiscard]] const char* name_ptr() const noexcept;
59 [[nodiscard]] NameType& name() noexcept;
60 [[nodiscard]] const char* metadata_ptr() const noexcept;
61 [[nodiscard]] MetadataType& metadata() noexcept;
62
63 private:
64
65 FormatType m_format;
66 NameType m_name;
67 MetadataType m_metadata;
68 };
69
70 template <class T>
71 constexpr std::optional<std::string> to_optional_string(T&& t)
72 {
73 if constexpr (std::same_as<std::remove_cvref_t<T>, std::string>)
74 {
75 return std::forward<T>(t);
76 }
77 else if constexpr (std::same_as<std::nullopt_t, T>)
78 {
79 return std::nullopt;
80 }
81 else if constexpr (std::is_pointer_v<T>)
82 {
83 if (t == nullptr)
84 {
85 return std::nullopt;
86 }
87 else
88 {
89 return std::string(t);
90 }
91 }
92 else if constexpr (std::ranges::range<T>)
93 {
94 return std::string(t.cbegin(), t.cend());
95 }
97 {
98 if (t.has_value())
99 {
100 return to_optional_string(*t);
101 }
102 else
103 {
104 return std::nullopt;
105 }
106 }
107 else
108 {
109 static_assert(mpl::dependent_false<T, T>::value, "to_optional_string: unsupported type.");
111 }
112 }
113
114 template <class F, class N, class M>
115 requires std::constructible_from<arrow_schema_private_data::FormatType, F>
116 && std::constructible_from<arrow_schema_private_data::NameType, N>
117 && std::constructible_from<arrow_schema_private_data::MetadataType, M>
120 , m_format(std::move(format))
121 , m_name(to_optional_string(std::forward<N>(name)))
122 , m_metadata(to_optional_string(std::forward<M>(metadata)))
123 {
124 SPARROW_ASSERT_TRUE(!m_format.empty())
125 }
126
127 [[nodiscard]] inline const char* arrow_schema_private_data::format_ptr() const noexcept
128 {
129 return m_format.data();
130 }
131
133 {
134 return m_format;
135 }
136
137 [[nodiscard]] inline const char* arrow_schema_private_data::name_ptr() const noexcept
138 {
139 if (m_name.has_value())
140 {
141 return m_name->data();
142 }
143 return nullptr;
144 }
145
147 {
148 return m_name;
149 }
150
151 [[nodiscard]] inline const char* arrow_schema_private_data::metadata_ptr() const noexcept
152 {
153 if (m_metadata.has_value())
154 {
155 return m_metadata->data();
156 }
157 return nullptr;
158 }
159
161 {
162 return m_metadata;
163 }
164
165}
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
constexpr children_ownership(std::size_t size=0)
constexpr 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
constexpr std::optional< std::string > to_optional_string(T &&t)
Workaround to replace static_assert(false) in template code.
Definition mp_utils.hpp:33