Skip to content

File streamcacher.h

File List > code_source > templat > include > TempLat > util > log > streamcacher.h

Go to the documentation of this file

#ifndef TEMPLAT_UTIL_LOG_STREAMCACHER_H
#define TEMPLAT_UTIL_LOG_STREAMCACHER_H

/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
   Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
   Released under the MIT license, see LICENSE.md. */

// File info: Main contributor(s): Wessel Valkenburg, Year: 2019

#include <sstream>

#include "TempLat/util/log/logmutex.h"
#include "TempLat/util/tdd/tddmacros.h"

namespace TempLat
{

  class StreamCacher
  {
  public:
    // Put public methods here. These should change very little over time.
    StreamCacher() : cache(std::make_shared<std::stringstream>()) {}

    ~StreamCacher()
    {
      if (cache.use_count() < 2) {
        if (cache->rdbuf()->in_avail()) {
          auto guard = LogMutex::guard();
          std::cout << cache->rdbuf() << std::endl;
        }
      }
    }

    template <typename T> friend StreamCacher operator<<(StreamCacher stream, const T &other)
    {
      *(stream.cache) << other;
      return stream;
    }

  private:
    /* Put all member variables and private methods here. These may change arbitrarily. */
    std::shared_ptr<std::stringstream> cache;

  public:
    template <typename TestObjectUnknownHere> static inline void Test(TestObjectUnknownHere &tdd);
  };
} // namespace TempLat

#endif