SFML/test/TestUtilities/SystemUtil.cpp
Chris Thrasher 28f273b9c9 Add sf::Angle
Similar to sf::Time, sf::Angle provides a typesafe API for working
with angles and provides named functions for converting to and from
degrees and radians.
2022-02-08 22:52:37 +00:00

82 lines
1.9 KiB
C++

#include "SystemUtil.hpp"
#include <SFML/System/Angle.hpp>
#include <SFML/System/String.hpp>
#include <SFML/System/Time.hpp>
// Work around GCC 8.x bug with `<filesystem>`.
#if !defined(__GNUC__) || (__GNUC__ >= 9)
#include <filesystem>
#endif // !defined(__GNUC__) || (__GNUC__ >= 9)
#include <cassert>
#include <fstream>
#include <iomanip>
#include <limits>
#include <ostream>
#include <sstream>
namespace sf
{
std::ostream& operator <<(std::ostream& os, const sf::Angle& angle)
{
os << std::fixed << std::setprecision(std::numeric_limits<float>::max_digits10);
os << angle.asDegrees() << " deg";
return os;
}
std::ostream& operator <<(std::ostream& os, const sf::String& string)
{
os << string.toAnsiString();
return os;
}
std::ostream& operator <<(std::ostream& os, sf::Time time)
{
os << time.asMicroseconds() << "us";
return os;
}
}
// Work around GCC 8.x bug with `<filesystem>`.
#if !defined(__GNUC__) || (__GNUC__ >= 9)
namespace sf::Testing
{
static std::string getTemporaryFilePath()
{
static int counter = 0;
std::ostringstream oss;
oss << "sfmltemp" << counter << ".tmp";
++counter;
std::filesystem::path result;
result /= std::filesystem::temp_directory_path();
result /= oss.str();
return result.string();
}
TemporaryFile::TemporaryFile(const std::string& contents)
: m_path(getTemporaryFilePath())
{
std::ofstream ofs(m_path);
assert(ofs);
ofs << contents;
assert(ofs);
}
TemporaryFile::~TemporaryFile()
{
[[maybe_unused]] const bool removed = std::filesystem::remove(m_path);
assert(removed);
}
const std::string& TemporaryFile::getPath() const
{
return m_path;
}
}
#endif // !defined(__GNUC__) || (__GNUC__ >= 9)