adevs_exception.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2013, James Nutaro
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * The views and conclusions contained in the software and documentation are those
  26. * of the authors and should not be interpreted as representing official policies,
  27. * either expressed or implied, of the FreeBSD Project.
  28. *
  29. * Bugs, comments, and questions can be sent to nutaro@gmail.com
  30. */
  31. #ifndef _adevs_exception_h_
  32. #define _adevs_exception_h_
  33. #include <string>
  34. #include <exception>
  35. namespace adevs
  36. {
  37. /**
  38. * The adevs::exception class is derived from the standard template
  39. * library exception class.
  40. */
  41. class exception: public std::exception
  42. {
  43. public:
  44. /**
  45. * Create an exception with an error message and, if appropriate,
  46. * a pointer to the model that created the error. To avoid
  47. * templated exceptions, the model pointer is just a void*.
  48. */
  49. exception(const char* msg, void* model = NULL):
  50. std::exception(),
  51. msg(msg),
  52. model(model)
  53. {}
  54. /// Copy constructor.
  55. exception(const adevs::exception& src):
  56. std::exception(src),
  57. msg(src.msg),
  58. model(src.model)
  59. {}
  60. /// Get the error message.
  61. const char* what() const throw()
  62. {
  63. return msg.c_str();
  64. }
  65. /// Get a pointer to the model that created the error.
  66. void* who() const { return model; }
  67. /// Destructor.
  68. ~exception() throw(){}
  69. private:
  70. std::string msg;
  71. void* model;
  72. };
  73. /**
  74. * The unsupported method exception is raised if an optional virtual method
  75. * is not supported by a model.
  76. */
  77. class method_not_supported_exception:
  78. public exception
  79. {
  80. public:
  81. /**
  82. * Constructor should be supplied with the model throwing
  83. * the exception and the name of the method that is not supported.
  84. */
  85. method_not_supported_exception(const char* method, void* model):
  86. exception((std::string("Unsupported method: ")+std::string(method)).c_str(),
  87. model)
  88. {
  89. }
  90. };
  91. } // end of namespace
  92. #endif