Eigene Klassen für Ausnahmen

Content

Anstatt assert bzw. ASSERT zu verwenden, wäre es auch denkbar, bei Zugriffen außerhalb des zulässigen Bereichs Ausnahmen zu erzeugen. Das hätte dann natürlich die Konsequenz, dass auf noexcept verzichtet werden muss.

Wenn eigene Ausnahmen erzeugt werden, empfiehlt es sich, eine eigene Klasse hierfür zu definieren, die von std::exception aus #include <exception> abgeleitet ist. Erstrebenswert wäre dann aber eine lesenswerte Meldung bei der Methode what, die in die Fehlermeldung aufnimmt, mit welchem Index zugegriffen wurde und welcher Indexbereich zulässig ist.

Zu beachten ist dabei folgendes:

Das lässt sich erreichen, indem alles einschließlich des auszugebenden Fehlertexts bei der Konstruktion der entsprechenden Ausnahme vorbereitet wird:

Aufgabe

Bauen Sie die Klasse Array so um, dass sie bei Array-Zugriffen außerhalb des zulässigen Bereichs eine entsprechende Ausnahmenbehandlung initiiert. Ergänzen Sie die Testsuite um entsprechende Tests.

Vorlage

#ifndef ARRAY_HPP
#define ARRAY_HPP

#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <initializer_list>
#include <iterator>
#include <new>
#include <utility>

template<typename T>
class Array {
   private:
      using Tag = enum {AllocateStorage};
      Array(Tag tag, std::size_t size) :
	 size(size),
	 data(size > 0?
	       static_cast<T*>(operator new(sizeof(T) * size))
	    :
	       nullptr) {
      }
   public:
      Array() noexcept : size(0), data(nullptr) {
      }
      Array(std::size_t size) : Array(AllocateStorage, size) {
	 for (std::size_t index = 0; index < size; ++index) {
	    new (data + index) T();
	 }
      }
      Array(std::size_t size, const T& t) : Array(AllocateStorage, size) {
	 for (std::size_t index = 0; index < size; ++index) {
	    new (data + index) T(t);
	 }
      }
      template<typename Iterator>
      Array(Iterator it1, Iterator it2) :
	    Array(AllocateStorage, std::distance(it1, it2)) {
	 std::size_t index = 0;
	 while (it1 != it2 && index < size) {
	    new (data + index++) T(*it1++);
	 }
	 size = index; // just in case
      }
      Array(std::initializer_list<T> elements) :
	    Array(elements.begin(), elements.end()) {
      }
      Array(const Array& other) : Array(AllocateStorage, other.size) {
	 for (std::size_t index = 0; index < size; ++index) {
	    new (data + index) T(other.data[index]);
	 }
      }
      friend void swap(Array& a1, Array& a2) noexcept {
	 using std::swap;
	 swap(a1.size, a2.size);
	 swap(a1.data, a2.data);
      }
      Array(Array&& other) noexcept : Array() {
	 swap(*this, other);
      }
      ~Array() {
	 for (std::size_t index = 0; index < size; ++index) {
	    data[index].~T();
	 }
	 operator delete(data);
      }
      Array& operator=(Array other) noexcept {
	 swap(*this, other);
	 return *this;
      }
      std::size_t get_size() const noexcept {
	 return size;
      }
      T& operator()(std::size_t index) {
	 assert(index < size);
	 return data[index];
      }
      const T& operator()(std::size_t index) const {
	 assert(index < size);
	 return data[index];
      }
      T* begin() noexcept {
	 return data;
      }
      const T* begin() const noexcept {
	 return data;
      }
      T* end() noexcept {
	 return data + size;
      }
      const T* end() const noexcept {
	 return data + size;
      }
   private:
      std::size_t size;
      T* data;
};

#endif
#include <exception>
#include <iomanip>
#include <iostream>
#include "array.hpp"

template<typename T>
void test_operation(unsigned int test, std::string description,
      T&& operation) {
   std::cout << "Test " << std::setw(2) << std::right << test << ": ";
   std::cout << std::setw(30) << std::left << std::move(description) << " ";
   try {
      operation();
      std::cout << "ok" << std::endl;
   } catch (std::exception& e) {
      std::cout << "exception: " << e.what() << std::endl;
   } catch (...) {
      std::cout << "unknown exception" << std::endl;
   }
}

struct FailingObject {
   class Exception: public std::exception {
      const char* what() const noexcept override {
	 return "FailingObject exception";
      }
   };
   FailingObject() {
      throw Exception();
   }
   FailingObject(const FailingObject& other) {
      throw Exception();
   }
   FailingObject(int i) {
      if (i < 0) throw Exception();
   }
   FailingObject& operator=(const FailingObject& other) {
      throw Exception();
   }
};

int main() {
   unsigned int test = 0;
   test_operation(++test, "Array()", []() { Array<FailingObject>(); });
   test_operation(++test, "Array(0)", []() { Array<FailingObject>(0); });
   test_operation(++test, "Array(1)", []() { Array<FailingObject>(1); });
   test_operation(++test, "Array(1, FailingObject(1))",
      []() { Array<FailingObject>(1, FailingObject(1)); });
   test_operation(++test, "Array(it1, it2) from {1, 2}", []() {
      int values[] = {1, 2, 3};
      Array<FailingObject>(values, values + sizeof(values)/sizeof(values[0]));
   });
   test_operation(++test, "Array(it1, it2) from {1, -2}", []() {
      int values[] = {1, -2};
      Array<FailingObject>(values, values + sizeof(values)/sizeof(values[0]));
   });
   test_operation(++test, "Array{1, 2}", []() {
      Array<FailingObject>{1, 2};
   });
   test_operation(++test, "Array{1, -2}", []() {
      Array<FailingObject>{1, -2};
   });
   test_operation(++test, "Array copy constructor", []() {
      int values[] = {1, 2};
      Array<FailingObject> array(values,
	 values + sizeof(values)/sizeof(values[0]));
      Array<FailingObject> array2(array);
   });
   test_operation(++test, "Array move constructor", []() {
      int values[] = {1, 2};
      Array<FailingObject> array(values,
	 values + sizeof(values)/sizeof(values[0]));
      Array<FailingObject>(std::move(array));
   });
   test_operation(++test, "Array swap", []() {
      int values[] = {1, 2};
      Array<FailingObject> array1(values,
	 values + sizeof(values)/sizeof(values[0]));
      Array<FailingObject> array2(values,
	 values + sizeof(values)/sizeof(values[0]));
      using std::swap;
      swap(array1, array2);
   });
   test_operation(++test, "Array assignment", []() {
      int values[] = {1, 2};
      Array<FailingObject> array1(values,
	 values + sizeof(values)/sizeof(values[0]));
      Array<FailingObject> array2;
      array2 = std::move(array1);
   });
   test_operation(++test, "Array get_size", []() {
      int values[] = {1, 2};
      Array<FailingObject> array(values,
	 values + sizeof(values)/sizeof(values[0]));
      array.get_size();
   });
   test_operation(++test, "Array access by index", []() {
      int values[] = {1, 2};
      Array<FailingObject> array(values,
	 values + sizeof(values)/sizeof(values[0]));
      array(1);
   });
   test_operation(++test, "Array access by index const", []() {
      int values[] = {1, 2};
      const Array<FailingObject> array(values,
	 values + sizeof(values)/sizeof(values[0]));
      array(1);
   });
   test_operation(++test, "Array iterators", []() {
      int values[] = {1, 2};
      Array<FailingObject> array(values,
	 values + sizeof(values)/sizeof(values[0]));
      for (auto& object: array) {
      }
   });
   test_operation(++test, "Array const iterators", []() {
      int values[] = {1, 2};
      const Array<FailingObject> array(values,
	 values + sizeof(values)/sizeof(values[0]));
      for (auto& object: array) {
      }
   });
   std::cout << test << " tests survived." << std::endl;
}