Newer
Older
libj / modules / j / base / src / throwable.cpp
  1. #include <cerrno>
  2. #include <cstring>
  3.  
  4. #include <j/lang/errno.hpp>
  5. #include <j/lang/throwable.hpp>
  6.  
  7. namespace j
  8. {
  9. namespace lang
  10. {
  11.  
  12. /**
  13. * Throwable を構築します。
  14. */
  15. Throwable::Throwable() noexcept : message(Errno::message(Errno::get()))
  16. {
  17. // NOP
  18. }
  19.  
  20. /**
  21. * Throwable を構築します。
  22. *
  23. * @param msg メッセージ
  24. */
  25. Throwable::Throwable(const String &msg) noexcept : message(msg)
  26. {
  27. // NOP
  28. }
  29.  
  30. /**
  31. * Throwable のコピーコンストラクタ。
  32. *
  33. * @param t コピー元 Throwable
  34. */
  35. Throwable::Throwable(const Throwable &t) noexcept : Object(t), message(t.message)
  36. {
  37. // NOP
  38. }
  39.  
  40. /**
  41. * Throwable のムーブコンストラクタ。
  42. *
  43. * @param str ムーブ元 String
  44. */
  45. Throwable::Throwable(Throwable &&t) noexcept : Object(std::move(t)), message(std::move(t.message))
  46. {
  47. t.message = nullptr;
  48. }
  49.  
  50. /**
  51. * デストラクタ。
  52. */
  53. Throwable::~Throwable() noexcept
  54. {
  55. // NOP
  56. }
  57.  
  58. /**
  59. * コピー代入演算子。
  60. * コピーして代入します。
  61. *
  62. * @param str コピー元 String
  63. * @return 本オブジェクトへの参照
  64. */
  65. Throwable &Throwable::operator=(const Throwable &t) noexcept
  66. {
  67. if (this != &t)
  68. {
  69. Object::operator=(t);
  70. message = t.message;
  71. }
  72. return *this;
  73. }
  74.  
  75. /**
  76. * ムーブ代入演算子。
  77. *
  78. * @param obj ムーブ元オブジェクト
  79. * @return 本オブジェクトへの参照
  80. */
  81. Throwable &Throwable::operator=(Throwable &&t) noexcept
  82. {
  83. if (this != &t)
  84. {
  85. Object::operator=(std::move(t));
  86. message = std::move(t.message);
  87. t.message = nullptr;
  88. }
  89. return *this;
  90. }
  91.  
  92. /**
  93. * エラーメッセージを取得します。
  94. *
  95. * @return エラーメッセージ
  96. */
  97. String Throwable::getMessage() const noexcept
  98. {
  99. return message;
  100. }
  101.  
  102. /**
  103. * 本オブジェクトの文字列表現を返します。
  104. *
  105. * @return 本オブジェクトの文字列表現
  106. */
  107. String Throwable::toString() const noexcept
  108. {
  109. return getMessage();
  110. }
  111.  
  112. ////////////////////////////////////////////////////////////////////////////
  113. //
  114. // protected
  115. //
  116.  
  117. /**
  118. * 本オブジェクトの複製を取得します。
  119. *
  120. * [備考]
  121. * 派生クラスが、ポリモーフィズムをサポートするために、
  122. * unique_ptr<Object> を返すようにしています。
  123. *
  124. * @return 本オブジェクトの複製
  125. */
  126. std::unique_ptr<Object> Throwable::clone() const noexcept
  127. {
  128. return std::make_unique<Throwable>(*this);
  129. }
  130.  
  131. } // namespace lang
  132. } // namespace j