From 28ae60d5283ed4654a9d4c67a43ae1e70187e97a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vitaliy=20Cherepanov/SRR-AI=20Tools=20Lab/=2E/=EC=82=BC?= =?utf8?q?=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 15 May 2018 02:19:59 +0300 Subject: [PATCH] foundation: Create base exception classes (#165) * Core: Create base exception classes Created base exceptions classes. This classes will be used by compiler components like frontend, optimizers, backend to inform main compiler part about some problems during compilation Signed-off-by: Vitaliy Cherepanov * core: Create base exception classes move libs/core/.../plugin to libs/foundation/.../plugin Signed-off-by: Vitaliy Cherepanov * foundation: Create base exception classe split class methods realisation Signed-off-by: Vitaliy Cherepanov * foundation: Create base exception class fix format coding style Signed-off-by: Vitaliy Cherepanov * foundation: Create base exception class this commit to fix PR Signed-off-by: Vitaliy Cherepanov * foundation: Create base exception class this commit to fix PR refactor BaseException class fix build Signed-off-by: Vitaliy Cherepanov * foundation: Create base exception class this commit to fix PR fix include add gtest Signed-off-by: Vitaliy Cherepanov * foundation: Create base exception class fix coding style Signed-off-by: Vitaliy Cherepanov --- libs/foundation/include/exception/BaseException.h | 39 +++++++++++++++++ libs/foundation/src/exception.test.cpp | 53 +++++++++++++++++++++++ libs/foundation/src/exception/BaseException.cpp | 51 ++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 libs/foundation/include/exception/BaseException.h create mode 100644 libs/foundation/src/exception.test.cpp create mode 100644 libs/foundation/src/exception/BaseException.cpp diff --git a/libs/foundation/include/exception/BaseException.h b/libs/foundation/include/exception/BaseException.h new file mode 100644 index 0000000..c314803 --- /dev/null +++ b/libs/foundation/include/exception/BaseException.h @@ -0,0 +1,39 @@ +// +// Created by v.cherepanov on 28.03.18. +// + +#ifndef __BASEEXCEPTION_H__ +#define __BASEEXCEPTION_H__ + +#include +#include +#include + +namespace nncc +{ +namespace foundation +{ + +class BaseException : public std::exception +{ +public: + BaseException() = default; + ~BaseException() throw() override = default; + + explicit BaseException(const std::string &info); + explicit BaseException(BaseException &e, const std::string &info); + + BaseException &append(const std::string &info); + const std::vector &getInfo() const; + const char *what() const throw() override; + +private: + std::vector _info; +}; + +std::ostream &operator<<(std::ostream &s, const BaseException &e); + +} // namespace foundation +} // namespace nncc + +#endif // __BASEEXCEPTION_H__ diff --git a/libs/foundation/src/exception.test.cpp b/libs/foundation/src/exception.test.cpp new file mode 100644 index 0000000..0e78c8d --- /dev/null +++ b/libs/foundation/src/exception.test.cpp @@ -0,0 +1,53 @@ +#include + +#include + +using namespace nncc::foundation; + +std::string errorMsg1 = "error constructor"; +std::string errorMsg2 = "error append"; +std::string errorMsg3 = "error second constructor"; + +std::vector msgs = {errorMsg1, errorMsg2, errorMsg3}; + +void err1() { throw BaseException(errorMsg1); } + +void err2() +{ + try + { + err1(); + } + catch (BaseException &e) + { + throw e.append(errorMsg2); + } +} + +void err3() +{ + try + { + err2(); + } + catch (BaseException &e) + { + throw BaseException(e, errorMsg3); + } +} + +TEST(FOUNDATION_EXCEPTION, BaseException) +{ + try + { + err3(); + } + catch (BaseException &e) + { + ASSERT_TRUE(msgs == e.getInfo()); + return; + } + + // should not be happened + ASSERT_TRUE(false); +} diff --git a/libs/foundation/src/exception/BaseException.cpp b/libs/foundation/src/exception/BaseException.cpp new file mode 100644 index 0000000..eb9cde7 --- /dev/null +++ b/libs/foundation/src/exception/BaseException.cpp @@ -0,0 +1,51 @@ +// +// Created by v.cherepanov@samsung.com on 28.03.18. +// + +#include +#include +#include + +#include "exception/BaseException.h" + +namespace nncc +{ +namespace foundation +{ + +BaseException::BaseException(const std::string &info) { _info.push_back(info); } + +BaseException::BaseException(BaseException &e, const std::string &info) +{ + for (auto errors : e.getInfo()) + { + _info.push_back(errors); + } + _info.push_back(info); +} + +const char *BaseException::what() const throw() +{ + // TODO implement it + throw std::runtime_error{"Not implemented, yet"}; +} + +const std::vector &BaseException::getInfo() const { return _info; } + +BaseException &BaseException::append(const std::string &info) +{ + _info.push_back(info); + return *this; +} + +std::ostream &operator<<(std::ostream &s, const BaseException &e) +{ + for (auto &errors : e.getInfo()) + { + s << errors << std::endl; + } + return s; +} + +} // namespace foundation +} // namespace nncc -- 2.7.4