PDI 1.7.0-alpha.2024-06-28

the PDI data interface

scalar_datatype.h
1/*******************************************************************************
2 * Copyright (C) 2015-2021 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_SCALAR_DATATYPE_H_
27#define PDI_SCALAR_DATATYPE_H_
28
29#include <functional>
30#include <string>
31#include <type_traits>
32
33#include <pdi/pdi_fwd.h>
34#include <pdi/datatype.h>
35
36namespace PDI {
37
38class PDI_EXPORT Scalar_datatype: public Datatype
39{
40 // Required to make_shared due to private ctor
41 struct Shared_enabler;
42
44 size_t m_size;
45
47 size_t m_dense_size;
48
50 size_t m_align;
51
53 Scalar_kind m_kind;
54
56 std::function<void* (void*, const void*) > m_copy;
57
59 std::function<void(void*) > m_destroy;
60
61 template <class T>
62 static std::shared_ptr<Scalar_datatype> const cv_type_for_v;
63
64 template <class T>
65 static constexpr inline Scalar_kind kind_of()
66 {
67 if constexpr (std::is_integral<T>::value) {
68 if constexpr (std::is_signed<T>::value) {
69 return Scalar_kind::SIGNED;
70 }
71 return Scalar_kind::UNSIGNED;
72 } else if constexpr (std::is_floating_point<T>::value) {
73 return Scalar_kind::FLOAT;
74 }
75 return Scalar_kind::UNKNOWN;
76 }
77
78public:
79 template <class T>
80 static constexpr auto kind_of_v = kind_of<std::remove_cv_t<T>>();
81
82 template <class T>
83 static constexpr auto const& type_for_v = cv_type_for_v<std::remove_cv_t<T>>;
84
88
89 Datatype_sptr densify() const override;
90
91 Datatype_sptr evaluate(Context&) const override;
92
93 bool dense() const override;
94
95 size_t datasize() const override;
96
97 size_t buffersize() const override;
98
99 size_t alignment() const override;
100
101 bool simple() const override;
102
103 void* data_to_dense_copy(void* to, const void* from) const override;
104
105 void* data_from_dense_copy(void* to, const void* from) const override;
106
107 void destroy_data(void* ptr) const override;
108
109 std::string debug_string() const override;
110
111 bool operator== (const Datatype&) const override;
112
113private:
120 Scalar_datatype(Scalar_kind kind, size_t size, const Attributes_map& attributes = {});
121
129 Scalar_datatype(Scalar_kind kind, size_t size, size_t align, const Attributes_map& attributes = {});
130
140 Scalar_datatype(
141 Scalar_kind kind,
142 size_t size,
143 size_t align,
144 size_t dense_size,
145 std::function<void* (void*, const void*) > copy,
146 std::function<void(void*) > destroy,
147 const Attributes_map& attributes = {}
148 );
149
150public:
157 static std::shared_ptr<Scalar_datatype> make(Scalar_kind kind, size_t size, const Attributes_map& attributes = {});
158
166 static std::shared_ptr<Scalar_datatype> make(Scalar_kind kind, size_t size, size_t align, const Attributes_map& attributes = {});
167
178 static std::shared_ptr<Scalar_datatype> make(
179 Scalar_kind kind,
180 size_t size,
181 size_t align,
182 size_t dense_size,
183 std::function<void* (void*, const void*) > copy,
184 std::function<void(void*) > destroy,
185 const Attributes_map& attributes = {}
186 );
187};
188
190
191extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<uint8_t>;
192
193extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<uint16_t>;
194
195extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<uint32_t>;
196
197extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<uint64_t>;
198
199extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<int8_t>;
200
201extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<int16_t>;
202
203extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<int32_t>;
204
205extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<int64_t>;
206
207extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<bool>;
208
209extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<float>;
210
211extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<double>;
212
213} // namespace PDI
214
215#endif // PDI_SCALAR_DATATYPE_H_
Definition: context.h:44
A Datatype is a Datatype_template that accepts no argument.
Definition: datatype.h:47
Definition: scalar_datatype.h:39
Datatype_sptr densify() const override
Creates a new datatype as the dense copy of this one.
void * data_from_dense_copy(void *to, const void *from) const override
Creates a sparse deep copy of dense data.
void destroy_data(void *ptr) const override
Delete data whose type is described by the Datatype.
size_t alignment() const override
Returns the required alignment for a type.
Scalar_kind kind() const
Interpretation of the content.
std::string debug_string() const override
Returns the datatype yaml representation as a string.
void * data_to_dense_copy(void *to, const void *from) const override
Creates a dense deep copy of data.
size_t buffersize() const override
Computes the data size of a type, including potentially unused memory from a sparse type.
static std::shared_ptr< Scalar_datatype > make(Scalar_kind kind, size_t size, const Attributes_map &attributes={})
Creates new scalar datatype.
bool dense() const override
Indicate if the datatype is dense or not.
static std::shared_ptr< Scalar_datatype > make(Scalar_kind kind, size_t size, size_t align, size_t dense_size, std::function< void *(void *, const void *) > copy, std::function< void(void *) > destroy, const Attributes_map &attributes={})
Creates new scalar datatype.
static std::shared_ptr< Scalar_datatype > make(Scalar_kind kind, size_t size, size_t align, const Attributes_map &attributes={})
Creates new scalar datatype.
bool simple() const override
Tells if data can be copied as bytes (if type is dense) and doesn't need a destroyer.
size_t datasize() const override
Computes the data size of a type, excluding potentially unused memory from a sparse type.
Datatype_sptr evaluate(Context &) const override
Creates a new datatype by resolving the value of all metadata references.
Definition: array_datatype.h:38
const auto UNDEF_TYPE
Definition: scalar_datatype.h:189
std::unordered_map< std::string, Expression > Attributes_map
Definition: datatype_template.h:40
Scalar_kind
Different possible interpretations for a scalar.
Definition: pdi_fwd.h:102
std::shared_ptr< const Datatype > Datatype_sptr
Definition: pdi_fwd.h:78