[core.ADT.tensor] Add 'TextFormatted<Index>' class (#202)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 4 May 2018 08:37:44 +0000 (17:37 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 4 May 2018 08:37:44 +0000 (17:37 +0900)
This commit adds 'TextFormatted<Index' class which is useful for
logging.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
libs/core/include/nncc/core/ADT/tensor/TextFormatted.h [new file with mode: 0644]
libs/core/include/nncc/core/ADT/tensor/TextFormattedIndex.h [new file with mode: 0644]
libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.cpp [new file with mode: 0644]
libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.test.cpp [new file with mode: 0644]

diff --git a/libs/core/include/nncc/core/ADT/tensor/TextFormatted.h b/libs/core/include/nncc/core/ADT/tensor/TextFormatted.h
new file mode 100644 (file)
index 0000000..261deca
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_H__
+#define __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_H__
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace tensor
+{
+
+template <typename T> struct TextFormatted;
+
+template <typename T> TextFormatted<T> txtfmt(const T &obj) { return TextFormatted<T>{obj}; }
+
+} // namespace tensor
+} // namespace ADT
+} // namespace core
+} // namespace nncc
+
+#endif // __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_INDEX_H__
diff --git a/libs/core/include/nncc/core/ADT/tensor/TextFormattedIndex.h b/libs/core/include/nncc/core/ADT/tensor/TextFormattedIndex.h
new file mode 100644 (file)
index 0000000..58c3636
--- /dev/null
@@ -0,0 +1,38 @@
+#ifndef __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_INDEX_H__
+#define __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_INDEX_H__
+
+#include "nncc/core/ADT/tensor/Index.h"
+#include "nncc/core/ADT/tensor/TextFormatted.h"
+
+#include "nncc/core/ADT/Printable.h"
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace tensor
+{
+
+template <> class TextFormatted<Index> final : public nncc::core::ADT::Printable
+{
+public:
+  explicit TextFormatted(const Index &index) : _index(index)
+  {
+    // DO NOTHING
+  }
+
+public:
+  void print(std::ostream &os) const override;
+
+private:
+  const Index &_index;
+};
+
+} // namespace tensor
+} // namespace ADT
+} // namespace core
+} // namespace nncc
+
+#endif // __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_INDEX_H__
diff --git a/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.cpp b/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.cpp
new file mode 100644 (file)
index 0000000..7bb1ba5
--- /dev/null
@@ -0,0 +1,33 @@
+#include "nncc/core/ADT/tensor/TextFormattedIndex.h"
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace tensor
+{
+
+void TextFormatted<Index>::print(std::ostream &os) const
+{
+  const auto rank = _index.rank();
+
+  if (rank > 0)
+  {
+    os << _index.at(0);
+
+    if (rank > 1)
+    {
+      for (uint32_t axis = 1; axis < rank; ++axis)
+      {
+        os << ", " << _index.at(axis);
+      }
+    }
+  }
+}
+
+} // namespace tensor
+} // namespace ADT
+} // namespace core
+} // namespace nncc
diff --git a/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.test.cpp b/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.test.cpp
new file mode 100644 (file)
index 0000000..8ebe318
--- /dev/null
@@ -0,0 +1,17 @@
+#include "nncc/core/ADT/tensor/TextFormattedIndex.h"
+
+#include <string>
+#include <sstream>
+
+#include <gtest/gtest.h>
+
+TEST(ADT_TENSOR_TEXT_FORMATTED_INDEX, message)
+{
+  const nncc::core::ADT::tensor::Index index{2, 3};
+
+  std::stringstream ss;
+
+  ss << nncc::core::ADT::tensor::txtfmt(index);
+
+  ASSERT_EQ(ss.str(), "2, 3");
+}