Source: OmException.h


Annotated List
Files
Globals
Hierarchy
Index
#ifndef OM_EXCEPTION_H
#define OM_EXCEPTION_H

/**
 * The class for this library exceptions handling.
 *
 * This class provides a set of features useful when throwing or catching exceptions of this library.
 * All exceptionnal situations and errors should be handled in this way, so that it allows a powerful
 * recovery mechanism for the users at each stage of the system.
 *
 * Each exception has a unique type which identifies its nature. In addition, some details can serve
 * as a complementary indication, and an OmStatus value can rely on the underlying C library to get
 * more specific information.
 */
class OmException
{
public:
  /**
   * The types of exceptions.
   */
  enum Type
  {
    FileError,
    NotIterating,
    EmptyString,
    Disabled,
    MalformedNode,
    NullPointer,
    OutOfRange,
    Underlying,
    UnsupportedBase,
    Other
  };

public:
  /**
   * Constructor.
   */
  explicit OmException(Type type_in, const char * details_in="", OmStatus status_in=OmSuccessStatus)
    : type_(type_in), details_(details_in), status_(status_in) {}

  /**
   * Destructor.
   */
  virtual ~OmException() {}

  /**
   * Get the type.
   */
  virtual Type getType() const {
    return type_; }

  /**
   * Get the details.
   */
  virtual const char * getDetails() const {
    return details_.c_str(); }

  /**
   * Get the status.
   */
  virtual OmStatus getStatus() const {
    return status_; }

  /**
   * Translate the carried information explaining the situation.
   * The verbose parameter can be:
   * @li 0 to be silent
   * @li 1 to get a simple message
   * @li 2 to get a full message with details
   */
  virtual const char * translate(short verbose_in=2);

  /**
   * Get the result of the last translation.
   */
  virtual const char * retranslate() const {
    return translation_.c_str(); }

public:
  /**
   * Check for unsupported base.
   */
  static void checkUnsupportedBase(unsigned int base_in) {
    if (base_in!=10 && base_in!=16) throw OmException(UnsupportedBase); }

  /**
   * Check for file error.
   */
  static void checkFileError(const FILE * file_in) {
    if (file_in==0) throw OmException(FileError); }

  /**
   * Check for iteration status.
   */
  static void checkNotIterating(bool done_in) {
    if (done_in) throw OmException(NotIterating); }

  /**
   * Check for empty string.
   */
  static void checkEmptyString(const char * string_in) {
    assert(string_in!=0);
    if (string_in[0]=='\0') throw OmException(EmptyString); }

  /**
   * Check for empty wstring.
   */
  static void checkEmptyString(const wchar_t * string_in) {
    assert(string_in!=0);
    if (string_in[0]==L'\0') throw OmException(EmptyString); }

  /**
   * Check for disabled operation.
   */
  static void checkDisabled(bool disabled_in, const char * details_in) {
    if (disabled_in) throw OmException(Disabled, details_in); }

  /**
   * Check for malformed node.
   */
  static void checkMalformedNode(bool malformed_in, const char * details_in) {
    assert(details_in!=0);
    if (malformed_in) throw OmException(MalformedNode, details_in); }

  /**
   * Check for null pointer.
   */
  static void checkNullPointer(const void * pointer_in) {
    if (pointer_in==0) throw OmException(NullPointer); }

  /**
   * Check for out of range.
   */
  static void checkOutOfRange(bool outOfRange_in) {
    if (outOfRange_in) throw OmException(OutOfRange); }

  /**
   * Check for an underlying error from the C library.
   */
  static void checkUnderlying(OMstatus status_in) {
    if (status_in!=0) throw OmException(Underlying, OMstatusToString(status_in), OmStatus(status_in)); }

private:
  Type type_;
  string details_;
  OmStatus status_;
  string translation_;
};

#endif // OM_EXCEPTION_H

Generated by: root@localhost.localdomain on Tue Oct 12 21:02:30 199.