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
18template <typename T>
20{
21 private:
22 MatrixType m_default_matrix_type;
23
24 std::unique_ptr<GenericMatrix<T>> createSelectedTypeUnique(MatrixType type);
25 std::shared_ptr<GenericMatrix<T>> createSelectedTypeShared(MatrixType type);
26
27 public:
29 MatrixFactory(const std::string& type);
30
31 std::unique_ptr<GenericMatrix<T>> createUnique();
32 std::unique_ptr<GenericMatrix<T>> createUnique(MatrixType type);
33
34 std::shared_ptr<GenericMatrix<T>> createShared();
35 std::shared_ptr<GenericMatrix<T>> createShared(MatrixType type);
36};
37
41template <typename T>
43 : m_default_matrix_type{setMatrixType(type)}
44{
45}
46
50template <typename T>
52{
53}
54
58template <typename T>
59std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createSelectedTypeUnique(MatrixType type)
60{
61 switch (type)
62 {
64 return std::make_unique<DenseMatrix<T>>();
65 case MatrixType::Csr:
66 std::cerr << "[Warning] CsrSparseMatrix type not yet tested!";
67 return std::make_unique<CsrSparseMatrix<T>>();
68 default:
69 throw std::invalid_argument("Unknown MatrixType in _create_selected_type_unique");
70 }
71}
72
76template <typename T>
77std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createSelectedTypeShared(MatrixType type)
78{
79 switch (type)
80 {
82 return std::make_shared<DenseMatrix<T>>();
83 case MatrixType::Csr:
84 std::cerr << "[Warning] CsrSparseMatrix type not yet tested!";
85 return std::make_shared<CsrSparseMatrix<T>>();
86 default:
87 throw std::invalid_argument("Unknown MatrixType in _create_selected_type_shared");
88 }
89}
90
94template <typename T>
95std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createUnique()
96{
97 return createSelectedTypeUnique(m_default_matrix_type);
98}
99
103template <typename T>
104std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createUnique(MatrixType type)
105{
106 return createSelectedTypeUnique(type);
107}
108
112template <typename T>
113std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createShared()
114{
115 return createSelectedTypeShared(m_default_matrix_type);
116}
117
121template <typename T>
122std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createShared(MatrixType type)
123{
124 return createSelectedTypeShared(type);
125}
126
127#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:20
std::shared_ptr< GenericMatrix< T > > createShared()
Creation function that creates a Matrix based on the default type.
Definition MatrixFactory.h:113
MatrixFactory(MatrixType type)
Constructor from enum (internal use) for the MatrixFactory class.
Definition MatrixFactory.h:51
std::unique_ptr< GenericMatrix< T > > createUnique()
Creation function that creates a Matrix based on the default type.
Definition MatrixFactory.h:95