spla
ttype.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of spla project */
3 /* https://github.com/SparseLinearAlgebra/spla */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2023 SparseLinearAlgebra */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef SPLA_TTYPE_HPP
29 #define SPLA_TTYPE_HPP
30 
31 #include <spla/type.hpp>
32 
33 namespace spla {
34 
46  template<typename T>
47  class TType final : public Type {
48  public:
49  SPLA_API ~TType() override = default;
50  SPLA_API const std::string& get_name() override;
51  SPLA_API const std::string& get_code() override;
52  SPLA_API const std::string& get_cpp() override;
53  SPLA_API const std::string& get_description() override;
54  SPLA_API int get_size() override;
55  SPLA_API int get_id() override;
56 
57  static ref_ptr<Type> make_type(std::string name, std::string code, std::string cpp, std::string desc, int id);
58 
59  private:
60  std::string m_name;
61  std::string m_code;
62  std::string m_cpp;
63  std::string m_desc;
64  int m_size = -1;
65  int m_id = -1;
66  };
67 
68  template<typename T>
69  const std::string& TType<T>::get_name() {
70  return m_name;
71  }
72 
73  template<typename T>
74  const std::string& TType<T>::get_code() {
75  return m_code;
76  }
77 
78  template<typename T>
79  const std::string& TType<T>::get_cpp() {
80  return m_cpp;
81  }
82 
83  template<typename T>
84  const std::string& TType<T>::get_description() {
85  return m_desc;
86  }
87 
88  template<typename T>
90  return m_size;
91  }
92 
93  template<typename T>
95  return m_id;
96  }
97 
98  template<typename T>
99  ref_ptr<Type> TType<T>::make_type(std::string name, std::string code, std::string cpp, std::string desc, int id) {
100  ref_ptr<TType<T>> t(new TType<T>());
101  t->m_name = std::move(name);
102  t->m_code = std::move(code);
103  t->m_cpp = std::move(cpp);
104  t->m_desc = std::move(desc);
105  t->m_size = static_cast<int>(sizeof(T));
106  t->m_id = id;
107  return t.template as<Type>();
108  }
109 
110  template<typename T>
111  static ref_ptr<TType<T>> get_ttype() {
112  assert(false && "not supported type");
113  return ref_ptr<TType<T>>();
114  }
115 
116  template<>
117  ref_ptr<TType<T_BOOL>> get_ttype() {
118  return BOOL.cast_safe<TType<T_BOOL>>();
119  }
120  template<>
121  ref_ptr<TType<T_INT>> get_ttype() {
122  return INT.cast_safe<TType<T_INT>>();
123  }
124  template<>
125  ref_ptr<TType<T_UINT>> get_ttype() {
126  return UINT.cast_safe<TType<T_UINT>>();
127  }
128  template<>
129  ref_ptr<TType<T_FLOAT>> get_ttype() {
130  return FLOAT.cast_safe<TType<T_FLOAT>>();
131  }
132 
137 }// namespace spla
138 
139 #endif//SPLA_TTYPE_HPP
Type interface implementation with actual type info bound.
Definition: ttype.hpp:47
SPLA_API ~TType() override=default
Type representation for parametrisation of containers stored values.
Definition: type.hpp:46
Automates reference counting and behaves as shared smart pointer.
Definition: ref.hpp:117
#define SPLA_API
Definition: config.hpp:43
SPLA_API const std::string & get_name() override
Definition: ttype.hpp:69
SPLA_API const std::string & get_description() override
Definition: ttype.hpp:84
SPLA_API int get_size() override
Definition: ttype.hpp:89
SPLA_API int get_id() override
Definition: ttype.hpp:94
static ref_ptr< Type > make_type(std::string name, std::string code, std::string cpp, std::string desc, int id)
Definition: ttype.hpp:99
SPLA_API const std::string & get_code() override
Definition: ttype.hpp:74
SPLA_API const std::string & get_cpp() override
Definition: ttype.hpp:79
ref_ptr< Type > BOOL
Definition: type.cpp:32
ref_ptr< Type > INT
Definition: type.cpp:33
ref_ptr< Type > FLOAT
Definition: type.cpp:35
ref_ptr< Type > UINT
Definition: type.cpp:34
Definition: algorithm.hpp:37