MovableScalar.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2020 Sebastien Boisvert <seb@boisvert.info>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_MISC_MOVABLE_SCALAR_H
11 #define EIGEN_MISC_MOVABLE_SCALAR_H
12 
13 namespace Eigen {
14 template <typename Scalar>
15 struct MovableScalar {
17  ~MovableScalar() { delete m_data; }
18  MovableScalar(const MovableScalar& other) : m_data(new Scalar) { set(other.get()); }
19  MovableScalar(MovableScalar&& other) noexcept : m_data(other.m_data) { other.m_data = nullptr; }
21  set(other.get());
22  return *this;
23  }
24  MovableScalar& operator=(MovableScalar&& other) noexcept {
25  m_data = other.m_data;
26  other.m_data = nullptr;
27  return *this;
28  }
29  MovableScalar(const Scalar& scalar) : m_data(new Scalar) { set(scalar); }
30 
31  operator Scalar() const { return get(); }
32 
33  private:
34  void set(const Scalar& value) {
35  eigen_assert(m_data != nullptr);
36  // suppress compiler warnings
37  if (m_data != nullptr) *m_data = value;
38  }
39  Scalar get() const {
40  eigen_assert(m_data != nullptr);
41  // suppress compiler warnings
42  return m_data == nullptr ? Scalar() : *m_data;
43  }
44  Scalar* m_data = nullptr;
45 };
46 
47 template <typename Scalar>
49  enum { RequireInitialization = 1 };
50 };
51 
52 } // namespace Eigen
53 
54 #endif
#define eigen_assert(x)
Definition: Macros.h:910
SCALAR Scalar
Definition: bench_gemm.cpp:45
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
@ RequireInitialization
Definition: NumTraits.h:177
Definition: MovableScalar.h:15
MovableScalar(MovableScalar &&other) noexcept
Definition: MovableScalar.h:19
MovableScalar & operator=(const MovableScalar &other)
Definition: MovableScalar.h:20
Scalar get() const
Definition: MovableScalar.h:39
MovableScalar(const MovableScalar &other)
Definition: MovableScalar.h:18
MovableScalar()
Definition: MovableScalar.h:16
Scalar * m_data
Definition: MovableScalar.h:44
MovableScalar & operator=(MovableScalar &&other) noexcept
Definition: MovableScalar.h:24
~MovableScalar()
Definition: MovableScalar.h:17
MovableScalar(const Scalar &scalar)
Definition: MovableScalar.h:29
void set(const Scalar &value)
Definition: MovableScalar.h:34
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217