Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/tensorwrapper/buffer/eigen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ class Eigen : public Contiguous<FloatType> {
/// Implements to_string
typename polymorphic_base::string_type to_string_() const override;

/// Implements add_to_stream
std::ostream& add_to_stream_(std::ostream& os) const override;

private:
/// True if *this has a PIMPL
bool has_pimpl_() const noexcept;
Expand Down
23 changes: 22 additions & 1 deletion include/tensorwrapper/detail_/polymorphic_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ class PolymorphicBase {
*/
auto to_string() const { return to_string_(); }

/** @brief Adds a string representation of *this to the stream.
*
* This uses `to_string` by default a polymorphic, but allows for better
* optimization of the stream output for specific instances
*
* @note This method is meant primarily for logging/debugging and NOT for
* serialization or archival.
*
* @param[in] os The stream to add *this to.
*
* @return The modified stream with *this added to it.
*/
std::ostream& add_to_stream(std::ostream& os) const {
return add_to_stream_(os);
}

protected:
/** @brief No-op default ctor
*
Expand Down Expand Up @@ -227,12 +243,17 @@ class PolymorphicBase {

/// Should be overridden by the derived class to provide logging details.
virtual string_type to_string_() const { return "{?}"; }

/// Should be overridden by the derived class to provide logging details
virtual std::ostream& add_to_stream_(std::ostream& os) const {
return os << this->to_string();
}
};

/// Implements printing via ostream for objects deriving from PolymorphicBase
template<typename T>
inline std::ostream& operator<<(std::ostream& os, const PolymorphicBase<T>& b) {
return os << b.to_string();
return b.add_to_stream(os);
}

} // namespace tensorwrapper::detail_
4 changes: 4 additions & 0 deletions src/tensorwrapper/buffer/detail_/eigen_tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class EigenTensor : public EigenPIMPL<FloatType> {
return ss.str();
}

std::ostream& add_to_stream_(std::ostream& os) const override {
return os << m_tensor_;
}

void addition_assignment_(label_type this_labels, label_type lhs_labels,
label_type rhs_labels, const_pimpl_reference lhs,
const_pimpl_reference rhs) override;
Expand Down
4 changes: 4 additions & 0 deletions src/tensorwrapper/buffer/eigen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ TPARAMS
typename EIGEN::polymorphic_base::string_type EIGEN::to_string_() const {
return m_pimpl_ ? m_pimpl_->to_string() : "";
}
TPARAMS
std::ostream& EIGEN::add_to_stream_(std::ostream& os) const {
return m_pimpl_ ? m_pimpl_->add_to_stream(os) : os;
}

// -- Private methods

Expand Down
11 changes: 11 additions & 0 deletions tests/cxx/unit_tests/tensorwrapper/buffer/detail_/eigen_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "../../testing/testing.hpp"
#include <iomanip>
#include <tensorwrapper/buffer/detail_/eigen_tensor.hpp>

using namespace tensorwrapper;
Expand Down Expand Up @@ -198,6 +199,16 @@ TEMPLATE_LIST_TEST_CASE("EigenTensor", "", types::floating_point_types) {
REQUIRE(vector.to_string() == sone.str() + " " + stwo.str());
}

SECTION("add_to_stream_") {
std::stringstream ss, ss_corr;
ss << std::fixed << std::setprecision(4);
scalar.add_to_stream(ss);
ss_corr << std::fixed << std::setprecision(4);
ss_corr << TestType{1.0};
REQUIRE(ss.str() == ss_corr.str());
REQUIRE_FALSE(ss.str() == scalar.to_string());
}

SECTION("addition_assignment_") {
SECTION("scalar") {
pimpl_type<TestType, 0> output;
Expand Down