Effective implementation of algorithms (Master Thesis)
Effective and error-free implementation of algorithms
|
00001 #ifndef H_PRECONDITIONS 00002 #define H_PRECONDITIONS 00003 00004 #include <stdexcept> 00005 #include <stdio.h> 00006 #include "utils/branch_predict/branch_predict.h" 00007 00008 // Note: Preconditions is in global namespace! 00019 class Preconditions { 00020 public: 00028 static void check(bool expression) { 00029 Preconditions::check(expression, "Precondition failed"); 00030 } 00031 00040 static void check(bool expression, const char* message) { 00041 if (UNLIKELY(message == NULL)) { // self-check 00042 throw std::invalid_argument("message shoudn't be NULL"); 00043 } 00044 00045 if (UNLIKELY(!expression)) { 00046 throw std::invalid_argument(message); 00047 } 00048 } 00049 00060 template <typename T> 00061 static void checkRange(T index, T size) { 00062 // Note: conversion of zero to T is required! 00063 Preconditions::checkRange(index, (T)0, size); 00064 } 00065 00073 template<typename T> 00074 static void checkRange(T index, T low, T high) { 00075 Preconditions::check(low < high, "Bad range!"); 00076 Preconditions::check(index >= low, "Index out of range"); 00077 Preconditions::check(index < high, "Index out of range"); 00078 } 00079 00085 template<typename T> 00086 static void checkNotNull(const T* ptr) { 00087 Preconditions::check(ptr != NULL, "Variable can't be null pointer!"); 00088 } 00089 }; 00090 00091 #endif