MoCSI API Reference
Loading...
Searching...
No Matches
MatrixFactory.h
Go to the documentation of this file.
1#ifndef MATRIX_FACTORY_H
2#define MATRIX_FACTORY_H
3
4#include <memory>
5#include <stdexcept>
6#include <string>
7#include <valarray>
8#include <vector>
9
10#include "CoreEnums.h"
11#include "CsrMatrix.h"
12#include "DenseMatrix.h"
13#include "GenericMatrix.h"
14
20template <typename T>
22{
23 private:
24 MatrixType m_default_matrix_type;
25
26 std::unique_ptr<GenericMatrix<T>> createSelectedTypeUnique(MatrixType type);
27 std::shared_ptr<GenericMatrix<T>> createSelectedTypeShared(MatrixType type);
28
29 public:
31 MatrixFactory(const std::string& type);
32
33 std::unique_ptr<GenericMatrix<T>> createUnique();
34 std::unique_ptr<GenericMatrix<T>> createUnique(MatrixType type);
35
36 std::shared_ptr<GenericMatrix<T>> createShared();
37 std::shared_ptr<GenericMatrix<T>> createShared(MatrixType type);
38};
39
46template <typename T>
48 : m_default_matrix_type{setMatrixType(type)}
49{
50}
51
59template <typename T>
61{
62}
63
71template <typename T>
72std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createSelectedTypeUnique(MatrixType type)
73{
74 switch (type)
75 {
77 return std::make_unique<DenseMatrix<T>>();
78 case MatrixType::Csr:
79 return std::make_unique<CsrSparseMatrix<T>>();
80 default:
81 throw std::invalid_argument("Unknown MatrixType in _create_selected_type_unique");
82 }
83}
84
92template <typename T>
93std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createSelectedTypeShared(MatrixType type)
94{
95 switch (type)
96 {
98 return std::make_shared<DenseMatrix<T>>();
99 case MatrixType::Csr:
100 return std::make_shared<CsrSparseMatrix<T>>();
101 default:
102 throw std::invalid_argument("Unknown MatrixType in _create_selected_type_shared");
103 }
104}
105
111template <typename T>
112std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createUnique()
113{
114 return createSelectedTypeUnique(m_default_matrix_type);
115}
116
124template <typename T>
125std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createUnique(MatrixType type)
126{
127 return createSelectedTypeUnique(type);
128}
129
137template <typename T>
138std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createShared()
139{
140 return createSelectedTypeShared(m_default_matrix_type);
141}
142
150template <typename T>
151std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createShared(MatrixType type)
152{
153 return createSelectedTypeShared(type);
154}
155
156#endif
constexpr MatrixType setMatrixType(const std::string &type)
Definition CoreEnums.h:38
MatrixType
Definition CoreEnums.h:33
Error class to throw when a module chain has circular or missing dependencies. Inherits from std::exc...
Definition ChainManager.h:28
Class that creates empty matrix objects of the specified type.
Definition MatrixFactory.h:22
std::shared_ptr< GenericMatrix< T > > createShared()
Creation function that creates a Matrix based on the default type.
Definition MatrixFactory.h:138
MatrixFactory(MatrixType type)
Constructor from enum (internal use) for the MatrixFactory class.
Definition MatrixFactory.h:60
std::unique_ptr< GenericMatrix< T > > createUnique()
Creation function that creates a Matrix based on the default type.
Definition MatrixFactory.h:112