Moved ReusableStringStream impl to generic singleton

This commit is contained in:
Phil Nash 2018-06-15 21:39:52 +01:00 committed by Martin Hořeňovský
parent 5884ec1e28
commit d6f2fd486c
3 changed files with 4 additions and 33 deletions

View File

@ -81,7 +81,6 @@ namespace Catch {
void cleanUp() { void cleanUp() {
cleanupSingletons(); cleanupSingletons();
cleanUpContext(); cleanUpContext();
ReusableStringStream::cleanup();
} }
std::string translateActiveException() { std::string translateActiveException() {
return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException(); return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException();

View File

@ -12,6 +12,7 @@
#include "catch_stream.h" #include "catch_stream.h"
#include "catch_debug_console.h" #include "catch_debug_console.h"
#include "catch_stringref.h" #include "catch_stringref.h"
#include "catch_singletons.hpp"
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
@ -20,11 +21,6 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wexit-time-destructors"
#endif
namespace Catch { namespace Catch {
Catch::IStream::~IStream() = default; Catch::IStream::~IStream() = default;
@ -145,7 +141,6 @@ namespace Catch {
std::vector<std::unique_ptr<std::ostringstream>> m_streams; std::vector<std::unique_ptr<std::ostringstream>> m_streams;
std::vector<std::size_t> m_unused; std::vector<std::size_t> m_unused;
std::ostringstream m_referenceStream; // Used for copy state/ flags from std::ostringstream m_referenceStream; // Used for copy state/ flags from
static StringStreams* s_instance;
auto add() -> std::size_t { auto add() -> std::size_t {
if( m_unused.empty() ) { if( m_unused.empty() ) {
@ -163,34 +158,17 @@ namespace Catch {
m_streams[index]->copyfmt( m_referenceStream ); // Restore initial flags and other state m_streams[index]->copyfmt( m_referenceStream ); // Restore initial flags and other state
m_unused.push_back(index); m_unused.push_back(index);
} }
// !TBD: put in TLS
static auto instance() -> StringStreams& {
if( !s_instance )
s_instance = new StringStreams();
return *s_instance;
}
static void cleanup() {
delete s_instance;
s_instance = nullptr;
}
}; };
StringStreams* StringStreams::s_instance = nullptr;
void ReusableStringStream::cleanup() {
StringStreams::cleanup();
}
ReusableStringStream::ReusableStringStream() ReusableStringStream::ReusableStringStream()
: m_index( StringStreams::instance().add() ), : m_index( Singleton<StringStreams>::getMutable().add() ),
m_oss( StringStreams::instance().m_streams[m_index].get() ) m_oss( Singleton<StringStreams>::getMutable().m_streams[m_index].get() )
{} {}
ReusableStringStream::~ReusableStringStream() { ReusableStringStream::~ReusableStringStream() {
static_cast<std::ostringstream*>( m_oss )->str(""); static_cast<std::ostringstream*>( m_oss )->str("");
m_oss->clear(); m_oss->clear();
StringStreams::instance().release( m_index ); Singleton<StringStreams>::getMutable().release( m_index );
} }
auto ReusableStringStream::str() const -> std::string { auto ReusableStringStream::str() const -> std::string {
@ -207,7 +185,3 @@ namespace Catch {
std::ostream& clog() { return std::clog; } std::ostream& clog() { return std::clog; }
#endif #endif
} }
#if defined(__clang__)
# pragma clang diagnostic pop
#endif

View File

@ -43,8 +43,6 @@ namespace Catch {
return *this; return *this;
} }
auto get() -> std::ostream& { return *m_oss; } auto get() -> std::ostream& { return *m_oss; }
static void cleanup();
}; };
} }