spla
tscalar.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_TSCALAR_HPP
29 #define SPLA_TSCALAR_HPP
30 
31 #include <spla/scalar.hpp>
32 
33 #include <core/ttype.hpp>
34 
35 namespace spla {
36 
46  template<typename T>
47  class TScalar final : public Scalar {
48  public:
49  TScalar() = default;
50  explicit TScalar(T value);
51  ~TScalar() override = default;
52 
53  ref_ptr<Type> get_type() override;
54  Status set_int(std::int32_t value) override;
55  Status set_uint(std::uint32_t value) override;
56  Status set_float(float value) override;
57  Status get_int(std::int32_t& value) override;
58  Status get_uint(std::uint32_t& value) override;
59  Status get_float(float& value) override;
60  T_INT as_int() override { return static_cast<T_INT>(m_value); }
61  T_UINT as_uint() override { return static_cast<T_UINT>(m_value); }
62  T_FLOAT as_float() override { return static_cast<T_FLOAT>(m_value); }
63 
64  void set_label(std::string label) override;
65  const std::string& get_label() const override;
66 
67  T& get_value();
68  T get_value() const;
69 
70  private:
71  std::string m_label;
72  T m_value = T();
73  };
74 
75  template<typename T>
76  TScalar<T>::TScalar(T value) : m_value(value) {
77  }
78 
79  template<typename T>
81  return get_ttype<T>().template as<Type>();
82  }
83 
84  template<typename T>
85  Status TScalar<T>::set_int(std::int32_t value) {
86  m_value = static_cast<T>(value);
87  return Status::Ok;
88  }
89  template<typename T>
90  Status TScalar<T>::set_uint(std::uint32_t value) {
91  m_value = static_cast<T>(value);
92  return Status::Ok;
93  }
94  template<typename T>
96  m_value = static_cast<T>(value);
97  return Status::Ok;
98  }
99 
100  template<typename T>
101  Status TScalar<T>::get_int(std::int32_t& value) {
102  value = static_cast<std::int32_t>(m_value);
103  return Status::Ok;
104  }
105  template<typename T>
106  Status TScalar<T>::get_uint(std::uint32_t& value) {
107  value = static_cast<std::uint32_t>(m_value);
108  return Status::Ok;
109  }
110  template<typename T>
112  value = static_cast<float>(m_value);
113  return Status::Ok;
114  }
115 
116  template<typename T>
117  void TScalar<T>::set_label(std::string label) {
118  m_label = std::move(label);
119  }
120 
121  template<typename T>
122  const std::string& TScalar<T>::get_label() const {
123  return m_label;
124  }
125 
126  template<typename T>
128  return m_value;
129  }
130  template<typename T>
132  return m_value;
133  }
134 
139 }// namespace spla
140 
141 #endif//SPLA_TSCALAR_HPP
Status of library operation execution.
Box for a single typed scalar value.
Definition: scalar.hpp:45
Definition: tscalar.hpp:47
~TScalar() override=default
T_INT as_int() override
Definition: tscalar.hpp:60
T_FLOAT as_float() override
Definition: tscalar.hpp:62
TScalar()=default
T_UINT as_uint() override
Definition: tscalar.hpp:61
Automates reference counting and behaves as shared smart pointer.
Definition: ref.hpp:117
const std::string & get_label() const override
Definition: tscalar.hpp:122
void set_label(std::string label) override
Definition: tscalar.hpp:117
Status set_uint(std::uint32_t value) override
Definition: tscalar.hpp:90
Status get_float(float &value) override
Definition: tscalar.hpp:111
Status set_float(float value) override
Definition: tscalar.hpp:95
Status get_uint(std::uint32_t &value) override
Definition: tscalar.hpp:106
T & get_value()
Definition: tscalar.hpp:127
Status get_int(std::int32_t &value) override
Definition: tscalar.hpp:101
ref_ptr< Type > get_type() override
Definition: tscalar.hpp:80
Status set_int(std::int32_t value) override
Definition: tscalar.hpp:85
std::int32_t T_INT
Definition: type.hpp:58
std::uint32_t T_UINT
Definition: type.hpp:59
float T_FLOAT
Definition: type.hpp:60
Definition: algorithm.hpp:37