foundation: Create base exception classes (#165)
authorVitaliy Cherepanov/SRR-AI Tools Lab/./삼성전자 <v.cherepanov@samsung.com>
Mon, 14 May 2018 23:19:59 +0000 (02:19 +0300)
committer박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 14 May 2018 23:19:59 +0000 (08:19 +0900)
* 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 <v.cherepanov@samsung.com>
* core: Create base exception classes

move libs/core/.../plugin to libs/foundation/.../plugin

Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
* foundation: Create base exception classe

split class methods realisation

Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
* foundation: Create base exception class

fix format coding style

Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
* foundation: Create base exception class

this commit to fix PR

Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
* foundation: Create base exception class

this commit to fix PR
refactor BaseException class
fix build

Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
* foundation: Create base exception class

this commit to fix PR
fix include
add gtest

Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
* foundation: Create base exception class

fix coding style

Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
libs/foundation/include/exception/BaseException.h [new file with mode: 0644]
libs/foundation/src/exception.test.cpp [new file with mode: 0644]
libs/foundation/src/exception/BaseException.cpp [new file with mode: 0644]

diff --git a/libs/foundation/include/exception/BaseException.h b/libs/foundation/include/exception/BaseException.h
new file mode 100644 (file)
index 0000000..c314803
--- /dev/null
@@ -0,0 +1,39 @@
+//
+// Created by v.cherepanov on 28.03.18.
+//
+
+#ifndef __BASEEXCEPTION_H__
+#define __BASEEXCEPTION_H__
+
+#include <vector>
+#include <string>
+#include <ostream>
+
+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<std::string> &getInfo() const;
+  const char *what() const throw() override;
+
+private:
+  std::vector<std::string> _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 (file)
index 0000000..0e78c8d
--- /dev/null
@@ -0,0 +1,53 @@
+#include <exception/BaseException.h>
+
+#include <gtest/gtest.h>
+
+using namespace nncc::foundation;
+
+std::string errorMsg1 = "error constructor";
+std::string errorMsg2 = "error append";
+std::string errorMsg3 = "error second constructor";
+
+std::vector<std::string> 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 (file)
index 0000000..eb9cde7
--- /dev/null
@@ -0,0 +1,51 @@
+//
+// Created by v.cherepanov@samsung.com on 28.03.18.
+//
+
+#include <string>
+#include <iostream>
+#include <stdexcept>
+
+#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<std::string> &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