PDI 1.6.0

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
36
37namespace PDI {
38
39class PDI_EXPORT Scalar_datatype:
40 public Datatype
41{
42 // Required to make_shared due to private ctor
43 struct Shared_enabler;
44
46 size_t m_size;
47
49 size_t m_dense_size;
50
52 size_t m_align;
53
55 Scalar_kind m_kind;
56
58 std::function<void* ( void*, const void* ) > m_copy;
59
61 std::function<void ( void* ) > m_destroy;
62
63 template <class T>
64 static std::shared_ptr<Scalar_datatype> const cv_type_for_v;
65
66 template <class T>
67 static constexpr inline Scalar_kind kind_of()
68 {
69 if constexpr ( std::is_integral<T>::value ) {
70 if constexpr ( std::is_signed<T>::value ) {
71 return Scalar_kind::SIGNED;
72 }
73 return Scalar_kind::UNSIGNED;
74 } else if constexpr ( std::is_floating_point<T>::value ) {
75 return Scalar_kind::FLOAT;
76 }
77 return Scalar_kind::UNKNOWN;
78 }
79
80public:
81 template <class T>
82 static constexpr auto kind_of_v = kind_of<std::remove_cv_t<T>>();
83
84 template <class T>
85 static constexpr auto const& type_for_v = cv_type_for_v<std::remove_cv_t<T>>;
86
90
91 Datatype_sptr densify() const override;
92
93 Datatype_sptr evaluate ( Context& ) const override;
94
95 bool dense() const override;
96
97 size_t datasize() const override;
98
99 size_t buffersize() const override;
100
101 size_t alignment() const override;
102
103 bool simple() const override;
104
105 void* data_to_dense_copy ( void* to, const void* from ) const override;
106
107 void* data_from_dense_copy ( void* to, const void* from ) const override;
108
109 void destroy_data ( void* ptr ) const override;
110
111 std::string debug_string() const override;
112
113 bool operator== ( const Datatype& ) const override;
114
115private:
122 Scalar_datatype ( Scalar_kind kind, size_t size, const Attributes_map& attributes = {} );
123
131 Scalar_datatype ( Scalar_kind kind, size_t size, size_t align, const Attributes_map& attributes = {} );
132
142 Scalar_datatype ( 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 = {} );
143
144public:
151 static std::shared_ptr<Scalar_datatype> make ( Scalar_kind kind, size_t size, const Attributes_map& attributes = {} );
152
160 static std::shared_ptr<Scalar_datatype> make ( Scalar_kind kind, size_t size, size_t align, const Attributes_map& attributes = {} );
161
172 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 = {} );
173};
174
176
177extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<uint8_t>;
178
179extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<uint16_t>;
180
181extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<uint32_t>;
182
183extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<uint64_t>;
184
185extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<int8_t>;
186
187extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<int16_t>;
188
189extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<int32_t>;
190
191extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<int64_t>;
192
193extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<bool>;
194
195extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<float>;
196
197extern template std::shared_ptr<Scalar_datatype> const Scalar_datatype::cv_type_for_v<double>;
198
199} // namespace PDI
200
201#endif // PDI_SCALAR_DATATYPE_H_
Definition: context.h:45
A Datatype is a Datatype_template that accepts no argument.
Definition: datatype.h:49
Definition: scalar_datatype.h:41
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:175
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:101
std::shared_ptr< const Datatype > Datatype_sptr
Definition: pdi_fwd.h:78