Keep thread-local instances of sf::err() backing implementation (WIP)

This commit is contained in:
Jan Haller 2020-12-06 18:03:57 +01:00
parent 43b2e9dc12
commit cfdba14e35

View File

@ -26,6 +26,8 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <SFML/System/ThreadLocalPtr.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <streambuf> #include <streambuf>
#include <cstdio> #include <cstdio>
@ -93,6 +95,23 @@ private:
return 0; return 0;
} }
}; };
// Groups a std::ostream with its std::streambuf
struct ThreadLocalErr : sf::NonCopyable
{
DefaultErrStreamBuf buffer;
std::ostream stream;
ThreadLocalErr() :
buffer(),
stream(&buffer)
{
}
};
// This per-thread variable holds the current instance of DefaultErrStreamBuf
sf::ThreadLocalPtr<ThreadLocalErr> currentErr(NULL);
} }
namespace sf namespace sf
@ -100,11 +119,12 @@ namespace sf
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::ostream& err() std::ostream& err()
{ {
static DefaultErrStreamBuf buffer; if (currentErr == NULL)
static std::ostream stream(&buffer); {
currentErr = new ThreadLocalErr();
}
return stream; return currentErr->stream;
} }
} // namespace sf } // namespace sf