From 8ae07fa87bc6623d77e23088005adcaf4fe5834d Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Fri, 22 May 2020 13:32:50 +0200 Subject: [PATCH] Rework output of Variant This should have been done much earlier. It consists in defining simply an operator<< --- .../node_processor/OStreamProcessor.hpp | 22 +------- src/language/utils/CMakeLists.txt | 1 + src/language/utils/DataVariant.cpp | 45 +++++++++++++++++ src/language/utils/DataVariant.hpp | 50 +++---------------- src/language/utils/SymbolTable.hpp | 20 +------- 5 files changed, 54 insertions(+), 84 deletions(-) create mode 100644 src/language/utils/DataVariant.cpp diff --git a/src/language/node_processor/OStreamProcessor.hpp b/src/language/node_processor/OStreamProcessor.hpp index 43d07364c..606247653 100644 --- a/src/language/node_processor/OStreamProcessor.hpp +++ b/src/language/node_processor/OStreamProcessor.hpp @@ -15,27 +15,7 @@ class OStreamProcessor final : public INodeProcessor execute(ExecutionPolicy& exec_policy) { for (size_t i = 0; i < m_node.children.size(); ++i) { - std::visit( - [&](auto&& value) { - using ValueT = std::decay_t<decltype(value)>; - if constexpr (not std::is_same_v<std::monostate, ValueT>) { - if constexpr (std::is_same_v<bool, ValueT>) { - m_os << std::boolalpha << value; - } else if constexpr (std::is_same_v<std::vector<EmbeddedData>, ValueT>) { - m_os << '('; - if (value.size() > 0) { - m_os << value[0]; - } - for (size_t i = 1; i < value.size(); ++i) { - m_os << ", " << value[i]; - } - m_os << ')'; - } else { - m_os << value; - } - } - }, - m_node.children[i]->execute(exec_policy)); + m_os << m_node.children[i]->execute(exec_policy); } return {}; diff --git a/src/language/utils/CMakeLists.txt b/src/language/utils/CMakeLists.txt index 3c6733cd4..6a8216a9c 100644 --- a/src/language/utils/CMakeLists.txt +++ b/src/language/utils/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(PugsLanguageUtils ASTDotPrinter.cpp ASTPrinter.cpp + DataVariant.cpp EmbeddedData.cpp ) diff --git a/src/language/utils/DataVariant.cpp b/src/language/utils/DataVariant.cpp new file mode 100644 index 000000000..879683325 --- /dev/null +++ b/src/language/utils/DataVariant.cpp @@ -0,0 +1,45 @@ +#include <language/utils/DataVariant.hpp> + +#include <utils/PugsAssert.hpp> +#include <utils/PugsTraits.hpp> + +std::ostream& +operator<<(std::ostream& os, const DataVariant& v) +{ + std::visit( + [&](auto&& v) { + using ValueT = std::decay_t<decltype(v)>; + if constexpr (std::is_same_v<ValueT, std::monostate>) { + os << "--"; + } else if constexpr (is_vector_v<ValueT>) { + os << '('; + if (v.size() > 0) { + os << v[0]; + } + for (size_t i = 1; i < v.size(); ++i) { + os << ", " << v[i]; + } + os << ')'; + } else if constexpr (std::is_same_v<bool, ValueT>) { + os << std::boolalpha << v; + } else { + os << v; + } + }, + v); + + return os; +} + +std::ostream& +operator<<(std::ostream& os, const AggregateDataVariant& compound) +{ + Assert(compound.m_data_vector.size() > 0, "unexpected compound data size"); + os << '('; + os << compound.m_data_vector[0]; + for (size_t i = 1; i < compound.m_data_vector.size(); ++i) { + os << ", " << compound.m_data_vector[i]; + } + os << ')'; + return os; +} diff --git a/src/language/utils/DataVariant.hpp b/src/language/utils/DataVariant.hpp index 0bb9a656c..eac6388e0 100644 --- a/src/language/utils/DataVariant.hpp +++ b/src/language/utils/DataVariant.hpp @@ -4,8 +4,8 @@ #include <algebra/TinyVector.hpp> #include <language/utils/EmbeddedData.hpp> #include <language/utils/FunctionSymbolId.hpp> -#include <utils/PugsAssert.hpp> +#include <iostream> #include <tuple> #include <variant> #include <vector> @@ -26,63 +26,25 @@ using DataVariant = std::variant<std::monostate, TinyVector<2>, TinyVector<3>>; +std::ostream& operator<<(std::ostream& os, const DataVariant& v); + class AggregateDataVariant // LCOV_EXCL_LINE { private: std::vector<DataVariant> m_data_vector; bool m_is_flattenable = true; - std::ostream& - _printComponent(std::ostream& os, const DataVariant& value) const - { - std::visit( - [&](auto&& v) { - if constexpr (std::is_same_v<std::decay_t<decltype(v)>, std::monostate>) { - os << " -- "; - } else if constexpr (std::is_same_v<std::decay_t<decltype(v)>, std::vector<EmbeddedData>>) { - os << '('; - if (v.size() > 0) { - os << v[0]; - } - for (size_t i = 1; i < v.size(); ++i) { - os << ", " << v[i]; - } - os << ')'; - } else { - os << v; - } - }, - value); - return os; - } - - std::ostream& - _print(std::ostream& os) const - { - Assert(m_data_vector.size() > 0, "unexpected compound data size"); - os << '('; - this->_printComponent(os, m_data_vector[0]); - for (size_t i = 1; i < m_data_vector.size(); ++i) { - os << ", "; - this->_printComponent(os, m_data_vector[i]); - } - os << ')'; - return os; - } - public: - friend std::ostream& - operator<<(std::ostream& os, const AggregateDataVariant& compound) - { - return compound._print(os); - } + friend std::ostream& operator<<(std::ostream& os, const AggregateDataVariant& compound); + PUGS_INLINE void setIsFlattenable(bool is_flattenable) { m_is_flattenable = is_flattenable; } + PUGS_INLINE bool isFlattenable() const { diff --git a/src/language/utils/SymbolTable.hpp b/src/language/utils/SymbolTable.hpp index 75baa7e73..ed4e2549f 100644 --- a/src/language/utils/SymbolTable.hpp +++ b/src/language/utils/SymbolTable.hpp @@ -95,25 +95,7 @@ class SymbolTable } else if (attributes.m_data_type == ASTNodeDataType::tuple_t) { os << attributes.m_data_type.typeName() << ':'; } - std::visit( - [&](auto&& value) { - using T = std::decay_t<decltype(value)>; - if constexpr (std::is_same_v<T, std::monostate>) { - os << "--"; - } else if constexpr (std::is_same_v<T, std::vector<EmbeddedData>>) { - os << '('; - if (value.size() > 0) { - os << value[0]; - } - for (size_t i = 1; i < value.size(); ++i) { - os << ", " << value[i]; - } - os << ')'; - } else { - os << value; - } - }, - attributes.m_value); + os << attributes.m_value; return os; } -- GitLab