From f92b725b0e2f2a124d4653a5f929a644ff40677e Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Senior=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Fri, 4 May 2018 15:30:11 +0900 Subject: [PATCH] [core] Introduce 'Printable' interface (#199) This commit introduces 'Printable' interface for printable objects. This commit also introduces related tests. Signed-off-by: Jonghyun Park --- libs/core/include/nncc/core/ADT/Printable.h | 26 ++++++++++++++++++ libs/core/src/nncc/core/ADT/Printable.cpp | 18 +++++++++++++ libs/core/src/nncc/core/ADT/Printable.test.cpp | 37 ++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 libs/core/include/nncc/core/ADT/Printable.h create mode 100644 libs/core/src/nncc/core/ADT/Printable.cpp create mode 100644 libs/core/src/nncc/core/ADT/Printable.test.cpp diff --git a/libs/core/include/nncc/core/ADT/Printable.h b/libs/core/include/nncc/core/ADT/Printable.h new file mode 100644 index 0000000..7ae903a --- /dev/null +++ b/libs/core/include/nncc/core/ADT/Printable.h @@ -0,0 +1,26 @@ +#ifndef __NNCC_CORE_ADT_PRINTABLE_H__ +#define __NNCC_CORE_ADT_PRINTABLE_H__ + +#include + +namespace nncc +{ +namespace core +{ +namespace ADT +{ + +struct Printable +{ + virtual ~Printable() = default; + + virtual void print(std::ostream &os) const = 0; +}; + +std::ostream &operator<<(std::ostream &os, const Printable &prn); + +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_PRINTABLE_H__ diff --git a/libs/core/src/nncc/core/ADT/Printable.cpp b/libs/core/src/nncc/core/ADT/Printable.cpp new file mode 100644 index 0000000..2af3881 --- /dev/null +++ b/libs/core/src/nncc/core/ADT/Printable.cpp @@ -0,0 +1,18 @@ +#include "nncc/core/ADT/Printable.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ + +std::ostream &operator<<(std::ostream &os, const Printable &prn) +{ + prn.print(os); + return os; +} + +} // namespace ADT +} // namespace core +} // namespace nncc diff --git a/libs/core/src/nncc/core/ADT/Printable.test.cpp b/libs/core/src/nncc/core/ADT/Printable.test.cpp new file mode 100644 index 0000000..10d0c22 --- /dev/null +++ b/libs/core/src/nncc/core/ADT/Printable.test.cpp @@ -0,0 +1,37 @@ +#include + +#include +#include + +#include + +namespace +{ + +class PrintableObject final : public nncc::core::ADT::Printable +{ +public: + PrintableObject(const std::string &message) : _message{message} + { + // DO NOTHING + } + +public: + void print(std::ostream &os) const override { os << _message; } + +private: + const std::string _message; +}; +} + +TEST(ADT_PRINTABLE, shift_operator) +{ + const std::string message{"hello"}; + const ::PrintableObject object{message}; + + std::stringstream ss; + + ss << object; + + ASSERT_EQ(ss.str(), message); +} -- 2.7.4