#ifndef EXCEPTIONS_HPP #define EXCEPTIONS_HPP #include #include #include namespace sftk { /** * @brief # SFTK exceptions super class * * @tparam T - The SFTK object types */ template class SFTKException : public std::exception { public: SFTKException(T *obj) : sftkObj(obj) {} /** * @brief # Address formatter * * This formatter function use 16 `*`s as address placeholder. * * When throwing an exception, Exceptions can use placeholder with this function * to fill the `what()` message. So the reported exception can tell where the crashed * SFTK object at. * * # Example * * ```c * const char *SomeException::what() const noexcept * { * static char msg[] = "sftk::SomeClass 0x********_******** reported an exception."; * SFTKException::formatAddress(msg, sizeof(msg)); * return const_cast(msg); * } * ``` * * @param msg exception message * @param len message length */ void formatAddress(char *msg, int len) const noexcept { auto ptr = reinterpret_cast(sftkObj); while (--len >= 0) { if (msg[len] == '*') { auto bit = ptr-- & 0xf; if (bit < 10) { bit += '0'; } else { bit -= 10 - 'a'; } msg[len] = bit; } } } private: T *sftkObj; }; }; #endif