exceptions.h 7.35 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_BASE_EXCEPTIONS_H_
#define EMBB_BASE_EXCEPTIONS_H_

30
#ifdef EMBB_PLATFORM_COMPILER_MSVC
31 32 33
#pragma warning(push)
// Disable warning that exceptions are disabled but try/catch is used.
#pragma warning(disable : 4530)
34
#endif // EMBB_PLATFORM_COMPILER_MSVC
35 36 37 38

#include <string>
#include <exception>

39
#ifdef EMBB_PLATFORM_COMPILER_MSVC
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
#pragma warning(pop)
#endif

#include <embb/base/internal/cmake_config.h>
#include <embb/base/c/errors.h>

/**
 * \defgroup CPP_BASE_EXCEPTIONS Exception
 *
 * Exception types.
 *
 * If exceptions are disabled, i.e., if the library was built without support
 * for exceptions, no exceptions will be thrown. Instead, an error message is
 * printed to \c stderr and the program exits with the code representing the
 * exception.
 *
 * \ingroup CPP_BASE
 */

/**
 * Macros to be used within EMBB when throwing and catching exceptions.
 *
 * Example:
 * - Throwing an exception:
 *   EMBB_THROW(NoMemoryException, "Could not create thread.");
 *   --> If exceptions are disabled, this will write an error to stderr,
 *       containing the exception's message, and exit the program with the code
 *       of the exception.
 * - Try/catch block:
 *   EMBB_TRY{ ... things to try ... }
 *   EMBB_CATCH(ExceptionXYZ& e){ ... things to catch ... }
 *   --> If exceptions are disabled, this will execute the try block without and
 *       replace the catch() statement by "if (false)", such that
 *       the catch block will be compiled but never executed.
 */
#ifdef EMBB_USE_EXCEPTIONS
#define EMBB_TRY try
#define EMBB_THROW(Type, Message) throw Type(Message);
#define EMBB_CATCH(Statement) catch(Statement)
#else /* EMBB_USE_EXCEPTIONS */
#include <stdio.h>
#include <stdlib.h>
#include <embb/base/c/internal/unused.h>
#define EMBB_TRY
/**
 * Concatenates the inputs.
 */
#define EMBB_CATCH_VAR_CAT2(X, Y) X##Y
/**
 * Is a necessary intermediate macro to create EMBB_CATCH_VAR.
 */
#define EMBB_CATCH_VAR_CAT1(X, Y) EMBB_CATCH_VAR_CAT2(X, Y)
/**
 * Defines a unique variable name for each line of code.
 *
 * The line number is concatenated to a base name.
 */
#define EMBB_CATCH_VAR EMBB_CATCH_VAR_CAT1(embb_catch_var_, __LINE__)
/**
 * Replaces catch(xyz) by an if-statement that is always false.
 *
 * To avoid a compiler warning that the condition is constant, a variable is
 * used instead of "false". A unique name for every catch-statement is
 * necessary, which is achieved by macro EMBB_CATCH_VAR and helper macros. To
 * avoid the unused variable warning, in addition the EMBB_UNUSED macro is
 * used.
 */
#define EMBB_CATCH(Statement) \
        int EMBB_CATCH_VAR = false; \
        EMBB_UNUSED(EMBB_CATCH_VAR); \
        if (EMBB_CATCH_VAR)

/**
 * Replaces a throw by an error message and program exit.
 */
#define EMBB_THROW(Type, Message) \
  { \
    Type e(Message); \
    fprintf(stderr, \
      "Exit program due to (not thrown) " #Type ": %s\n", e.what()); \
    exit(e.Code()); \
  }
#endif /* else EMBB_USE_EXCEPTIONS */

namespace embb {
namespace base {

/**
 * Abstract base class for exceptions.
 *
 * \ingroup CPP_BASE_EXCEPTIONS
 */
class Exception : public std::exception {
 public:
  /**
   * Constructs an exception with a custom message.
   */
  explicit Exception(
    const char* message
    /**< [IN] Error message */
    ) : message_(message) {}

  /**
   * Destructs the exception.
   */
  virtual ~Exception() throw() {}

  /**
   * Constructs an exception by copying from an existing one.
   */
  Exception(
    const Exception& e
    /**< [IN] %Exception to be copied */
    ) : message_(e.message_) {}

  /**
   * Assigns an existing exception.
   *
   * \return Reference to \c *this
   */
  Exception& operator=(
    const Exception& e
    /**< [IN] %Exception to assign */
    ) {
    message_ = e.message_;
    return *this;
  }

  /**
   * Returns the error message.
   *
   * \return Pointer to error message
   */
  virtual const char* What() const throw() {
    return message_;
  }

  /**
   * Returns an integer code representing the exception.
   *
   * \return %Exception code
   */
  virtual int Code() const = 0;

 private:
  /**
   * Holds error message
   */
  const char* message_;
};

/**
 * Indicates lack of memory necessary to allocate a resource.
 *
 * \ingroup CPP_BASE_EXCEPTIONS
 */
class NoMemoryException : public Exception {
 public:
   /**
    * Constructs an exception with the specified message
    */
  explicit NoMemoryException(
    const char* message
    /**< [IN] Error message */
    ) : Exception(message) {}
  virtual int Code() const { return EMBB_NOMEM; }
};

/**
 * Indicates business (unavailability) of a required resource.
 *
 * \ingroup CPP_BASE_EXCEPTIONS
 */
class ResourceBusyException : public Exception {
 public:
  /**
   * Constructs an exception with the specified message
   */
  explicit ResourceBusyException(
    const char* message
    /**< [IN] Error message */
    ) : Exception(message) {}
  virtual int Code() const { return EMBB_BUSY; }
};

/**
 * Indicates a numeric underflow.
 *
 * \ingroup CPP_BASE_EXCEPTIONS
 */
class UnderflowException : public Exception {
 public:
  /**
   * Constructs an exception with the specified message
   */
  explicit UnderflowException(
    const char* message
    /**< [IN] Error message */
    ) : Exception(message) {}
  virtual int Code() const { return EMBB_UNDERFLOW; }
};

/**
 * Indicates a numeric overflow.
 *
 * \ingroup CPP_BASE_EXCEPTIONS
 */
class OverflowException : public Exception {
 public:
  /**
   * Constructs an exception with the specified message
   */
  explicit OverflowException(
    const char* message
    /**< [IN] Error message */
    ) : Exception(message) {}
  virtual int Code() const { return EMBB_OVERFLOW; }
};

/**
 * Indicates a general error.
 *
 * \ingroup CPP_BASE_EXCEPTIONS
 */
class ErrorException : public Exception {
 public:
  /**
   * Constructs an exception with the specified message
   */
  explicit ErrorException(
    const char* message
    /**< [IN] Error message */
    ) : Exception(message) {}
  virtual int Code() const { return EMBB_ERROR; }
};

} // namespace base
} // namespace embb

279
#endif  // EMBB_BASE_EXCEPTIONS_H_