C++-Programmierung/ Eine Matrix-Bibliothek – mitrax/ exception.hpp

Aus Wikibooks
Zur Navigation springen Zur Suche springen
Nuvola-inspired-terminal.svg
  1 #ifndef _mitrax_mitrax_exception_hpp_INCLUDED_
  2 #define _mitrax_mitrax_exception_hpp_INCLUDED_
  3 /// \file exception.hpp
  4 ///
  5 /// \brief Ausnahmeklassen von mitrax
  6 ///
  7 /// Diese Datei stellt die Klassen bereit, die innerhalb von mitrax als Ausnahmen geworfen werden.
  8 /// Sie sind von den Ausnahmeklassen der C++-Standardbibliothek abgeleitet.
  9 
 10 #include "dimension.hpp"
 11 #include "matrix.hpp"
 12 
 13 #include <stdexcept>
 14 #include <sstream>
 15 
 16 namespace mitrax{ namespace error {
 17 
 18 /// \brief Zugriff auf ungültige Position
 19 class access: public std::out_of_range{
 20 public:
 21     explicit access(std::string const& what):std::out_of_range(what){}
 22     explicit access(char const* what):std::out_of_range(what){}
 23 };
 24 
 25 /// \brief Zugriff auf ungültige Zeile
 26 class row_access: public access{
 27 public:
 28     template < typename SizeType >
 29     row_access(
 30         std::string const& text,
 31         mitrax::dimension< SizeType > const& size,
 32         SizeType const& row_number
 33     ):
 34         access(make_text(text, size, row_number))
 35         {}
 36 
 37 private:
 38     template < typename SizeType >
 39     static std::string const make_text(
 40         std::string const& text,
 41         mitrax::dimension< SizeType > const& size,
 42         SizeType const& row_number
 43     ){
 44         std::ostringstream error;
 45         error << text << ": Dimension(" << size.rows() << ", " << size.columns()
 46             << "): access in row " << row_number << " not allowed";
 47         return error.str();
 48     }
 49 };
 50 
 51 /// \brief Zugriff auf ungültige Spalte
 52 class column_access: public access{
 53 public:
 54     template < typename SizeType >
 55     column_access(
 56         std::string const& text,
 57         mitrax::dimension< SizeType > const& size,
 58         SizeType const& column_number
 59     ):
 60         access(make_text(text, size, column_number))
 61         {}
 62 
 63 private:
 64     template < typename SizeType >
 65     static std::string const make_text(
 66         std::string const& text,
 67         mitrax::dimension< SizeType > const& size,
 68         SizeType const& column_number
 69     ){
 70         std::ostringstream error;
 71         error << text << ": Dimension(" << size.rows() << ", " << size.columns()
 72             << "): access in column " << column_number << " not allowed";
 73         return error.str();
 74     }
 75 };
 76 
 77 /// \brief Container mit ungültiger Größe
 78 class data_size: public std::logic_error{
 79 public:
 80     template < typename SizeType, typename Container >
 81     data_size(std::string const& text, dimension< SizeType > const& size, Container const& data):
 82         std::logic_error(make_text(text, size, data))
 83         {}
 84 
 85 private:
 86     template < typename SizeType, typename Container >
 87     static std::string const make_text(
 88         std::string const& text,
 89         mitrax::dimension< SizeType > const& size,
 90         Container const& data
 91     ){
 92         std::ostringstream error;
 93         error << text << ": Dimension(" << size.rows() << ", " << size.columns()
 94             << ") not compatible with data-size " << data.size();
 95         return error.str();
 96     }
 97 };
 98 
 99 /// \brief Fehler in Zusammenhang mit einer Dimension
100 class dimension: public std::logic_error{
101 public:
102     explicit dimension(std::string const& what):std::logic_error(what){}
103     explicit dimension(char const* what):std::logic_error(what){}
104 };
105 
106 /// \brief Dimensionen müssen gleich sein
107 class dimension_unequal: public dimension{
108 public:
109     template < typename SizeType >
110     dimension_unequal(
111         std::string const& text,
112         mitrax::dimension< SizeType > const& lhs,
113         mitrax::dimension< SizeType > const& rhs
114     ):
115         dimension(make_text(text, lhs, rhs)){}
116 
117 private:
118     template < typename SizeType >
119     static std::string const make_text(
120         std::string const& text,
121         mitrax::dimension< SizeType > const& lhs,
122         mitrax::dimension< SizeType > const& rhs
123     ){
124         std::ostringstream error;
125         error << text << ": lhs.dimension(" << lhs.rows() << ", " << lhs.columns()
126             << ") != rhs.dimension(" << rhs.rows() << ", " << rhs.columns() << ")";
127         return error.str();
128     }
129 };
130 
131 /// \brief Dimensionen müssen kompatibel sein
132 class dimension_incompatible: public dimension{
133 public:
134     template < typename SizeType >
135     dimension_incompatible(
136         std::string const& text,
137         mitrax::dimension< SizeType > const& lhs,
138         mitrax::dimension< SizeType > const& rhs
139     ):
140         dimension(make_text(text, lhs, rhs)){}
141 
142 private:
143     template < typename SizeType >
144     static std::string const make_text(
145         std::string const& text,
146         mitrax::dimension< SizeType > const& lhs,
147         mitrax::dimension< SizeType > const& rhs
148     ){
149         std::ostringstream error;
150         error << text << ": lhs.dimension(" << lhs.rows() << ", " << lhs.columns()
151             << "), rhs.dimension(" << rhs.rows() << ", " << rhs.columns() << ")";
152         return error.str();
153     }
154 };
155 
156 /// \brief Anzahl der Zeilen und Spalten muss gleich sein
157 class not_quadratic: public dimension{
158 public:
159     template < typename SizeType >
160     not_quadratic(std::string const& text, mitrax::dimension< SizeType > const& dim):
161         dimension(make_text(text, dim)){}
162 
163 private:
164     template < typename SizeType >
165     static std::string const
166     make_text(std::string const& text, mitrax::dimension< SizeType > const& dim){
167         std::ostringstream error;
168         error << text << ": dimension(" << dim.rows() << ", " << dim.columns() << ")";
169         return error.str();
170     }
171 };
172 
173 /// \brief Operation für singuläre Matrizen nicht durchführbar
174 class singular_matrix: public std::logic_error{
175 public:
176     explicit singular_matrix(std::string const& what):std::logic_error(what){}
177     explicit singular_matrix(char const* what):std::logic_error(what){}
178 };
179 
180 /// \brief Größen müssen gleich sein
181 class size_unequal: public std::logic_error{
182 public:
183     template < typename SizeType >
184     size_unequal(std::string const& text, SizeType const& lhs, SizeType const& rhs):
185         std::logic_error(make_text(text, lhs, rhs)){}
186 
187 private:
188     template < typename SizeType >
189     static std::string const
190     make_text(std::string const& text, SizeType const& lhs, SizeType const& rhs){
191         std::ostringstream error;
192         error << text << ": " << lhs << " != " << rhs;
193         return error.str();
194     }
195 };
196 
197 } }
198 
199 #endif