Home | History | Annotate | Download | only in port
      1 /*
      2  * Copyright 2011 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 // Exceptions used in sfntly
     18 
     19 #ifndef SFNTLY_CPP_SRC_SFNTLY_PORT_EXCEPTION_TYPE_H_
     20 #define SFNTLY_CPP_SRC_SFNTLY_PORT_EXCEPTION_TYPE_H_
     21 
     22 #if !defined (SFNTLY_NO_EXCEPTION)
     23 
     24 #include <exception>
     25 #include <string>
     26 #include <sstream>
     27 
     28 namespace sfntly {
     29 
     30 class Exception : public std::exception {
     31  public:
     32   Exception() : what_("Unknown exception") {}
     33   explicit Exception(const char* message) throw() { SetMessage(message); }
     34   virtual ~Exception() throw() {}
     35   virtual const char* what() const throw() { return what_.c_str(); }
     36 
     37  protected:
     38   void SetMessage(const char* message) throw() {
     39     try {
     40       what_ = message;
     41     } catch (...) {}
     42   }
     43 
     44  private:
     45   std::string what_;
     46 };
     47 
     48 class IndexOutOfBoundException : public Exception {
     49  public:
     50   IndexOutOfBoundException() throw() : Exception("Index out of bound") {}
     51   explicit IndexOutOfBoundException(const char* message) throw()
     52       : Exception(message) {}
     53   IndexOutOfBoundException(const char* message, int32_t index) throw() {
     54     try {
     55       std::ostringstream msg;
     56       msg << message;
     57       msg << ":";
     58       msg << index;
     59       SetMessage(msg.str().c_str());
     60     } catch (...) {}
     61   }
     62   virtual ~IndexOutOfBoundException() throw() {}
     63 };
     64 
     65 class IOException : public Exception {
     66  public:
     67   IOException() throw() : Exception("I/O exception") {}
     68   explicit IOException(const char* message) throw() : Exception(message) {}
     69   virtual ~IOException() throw() {}
     70 };
     71 
     72 class ArithmeticException : public Exception {
     73  public:
     74   ArithmeticException() throw() : Exception("Arithmetic exception") {}
     75   explicit ArithmeticException(const char* message) throw()
     76       : Exception(message) {}
     77   virtual ~ArithmeticException() throw() {}
     78 };
     79 
     80 class UnsupportedOperationException : public Exception {
     81  public:
     82   UnsupportedOperationException() throw() :
     83       Exception("Operation not supported") {}
     84   explicit UnsupportedOperationException(const char* message) throw()
     85       : Exception(message) {}
     86   virtual ~UnsupportedOperationException() throw() {}
     87 };
     88 
     89 class RuntimeException : public Exception {
     90  public:
     91   RuntimeException() throw() : Exception("Runtime exception") {}
     92   explicit RuntimeException(const char* message) throw()
     93       : Exception(message) {}
     94   virtual ~RuntimeException() throw() {}
     95 };
     96 
     97 class NoSuchElementException : public Exception {
     98  public:
     99   NoSuchElementException() throw() : Exception("No such element") {}
    100   explicit NoSuchElementException(const char* message) throw()
    101       : Exception(message) {}
    102   virtual ~NoSuchElementException() throw() {}
    103 };
    104 
    105 class IllegalArgumentException : public Exception {
    106  public:
    107   IllegalArgumentException() throw() : Exception("Illegal argument") {}
    108   explicit IllegalArgumentException(const char* message) throw()
    109       : Exception(message) {}
    110   virtual ~IllegalArgumentException() throw() {}
    111 };
    112 
    113 class IllegalStateException : public Exception {
    114  public:
    115   IllegalStateException() throw() : Exception("Illegal state") {}
    116   explicit IllegalStateException(const char* message) throw()
    117       : Exception(message) {}
    118   virtual ~IllegalStateException() throw() {}
    119 };
    120 
    121 }  // namespace sfntly
    122 
    123 #endif  // #if !defined (SFNTLY_NO_EXCEPTION)
    124 
    125 #endif  // SFNTLY_CPP_SRC_SFNTLY_PORT_EXCEPTION_TYPE_H_
    126