move stream operator overloads into std namespace

This commit is contained in:
frank 2021-10-22 15:06:05 -04:00
parent 186d14cb48
commit 8972b0c868
11 changed files with 80 additions and 54 deletions

View File

@ -107,9 +107,6 @@
namespace sb
{
/* Calls to the stream operator from within this namespace will search the global namespace as well as the current */
using ::operator<<;
class Attributes;
std::ostream& operator<<(std::ostream&, const Attributes&);

View File

@ -584,7 +584,7 @@ std::string Box::string() const
/* Feed a string representation of the box to the passed ostream */
std::ostream& operator<< (std::ostream& out, const Box& box)
std::ostream& std::operator<<(std::ostream& out, const Box& box)
{
out << box.string();
return out;

View File

@ -87,6 +87,9 @@ public:
};
std::ostream& operator<<(std::ostream&, const Box&);
namespace std
{
std::ostream& operator<<(std::ostream&, const Box&);
}
#endif

View File

@ -81,7 +81,7 @@ bool Color::operator<(const Color& color) const
return r < color.r || g < color.g || b < color.b || a < color.a;
}
std::ostream& operator<<(std::ostream& out, const Color& color)
std::ostream& std::operator<<(std::ostream& out, const Color& color)
{
float h, s, v;
RGBtoHSV(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, h, s, v);

View File

@ -6,7 +6,6 @@
#include <cmath>
#include <ostream>
#include <type_traits>
#include "SDL_pixels.h"
struct Color : SDL_Color
@ -44,8 +43,12 @@ struct Color : SDL_Color
};
std::ostream& operator<<(std::ostream&, const Color&);
void RGBtoHSV(const float&, const float&, const float&, float&, float&, float&);
void HSVtoRGB(float&, float&, float&, const float&, const float&, const float&);
namespace std
{
std::ostream& operator<<(std::ostream&, const Color&);
}
#endif

View File

@ -121,3 +121,11 @@ void sb::buffer_deleter(GLuint* id)
SDL_Log("destroying buffer ID %i", *id);
glDeleteBuffers(1, id);
}
/* Overload the stream operator to support GLObject. Add a string representation of the object that
* displays its ID to the output stream. */
std::ostream& sb::operator<<(std::ostream& out, const GLObject& gl_object)
{
out << "<GL Object (id: " << gl_object.id() << ")>";
return out;
}

View File

@ -45,11 +45,19 @@
namespace sb
{
/* Calls to the stream operator from within this namespace will search the global namespace as well as the current */
using std::operator<<;
class GLObject;
std::ostream& operator<<(std::ostream&, const GLObject&);
class GLObject
{
private:
/* function types */
typedef std::function<void(GLsizei, GLuint*)> generator_function;
typedef std::function<void(GLuint*)> deleter_function;
@ -68,6 +76,7 @@ namespace sb
virtual GLuint id() const;
virtual bool generated() const;
virtual ~GLObject() = default;
friend std::ostream& operator<<(std::ostream&, const GLObject&);
};

View File

@ -164,7 +164,7 @@ std::vector<Segment> Segment::subsegments(int count) const
return subsegments;
}
std::ostream& operator<<(std::ostream& out, const Segment& segment)
std::ostream& std::operator<<(std::ostream& out, const Segment& segment)
{
out << "{(" << segment.start().x << ", " << segment.start().y << "), (" <<
segment.end().x << ", " << segment.end().y << ")}";

View File

@ -62,6 +62,9 @@ public:
};
std::ostream& operator<<(std::ostream&, const Segment&);
namespace std
{
std::ostream& operator<<(std::ostream&, const Segment&);
}
#endif

View File

@ -647,7 +647,7 @@ int lineColor(SDL_Renderer* renderer, const Segment& segment, const Color& color
}
}
std::ostream& operator<<(std::ostream& out, const SDL_Color& color)
std::ostream& std::operator<<(std::ostream& out, const SDL_Color& color)
{
out << "{" << static_cast<int>(color.r) << ", " << static_cast<int>(color.g) << ", " <<
static_cast<int>(color.b) << ", " << static_cast<int>(color.a) << "}";

View File

@ -190,63 +190,66 @@ int SDL_SetRenderDrawColor(SDL_Renderer*, const Color&);
int SDL_RenderFillRect(SDL_Renderer*, const Box&);
int lineColor(SDL_Renderer*, const Segment&, const Color&, std::uint8_t = 1);
/* Stream a text representation of a glm::vec of any type or dimension */
template<glm::length_t dimensions, typename Type, glm::qualifier qualifier>
std::ostream& operator<<(std::ostream& out, const glm::vec<dimensions, Type, qualifier>& vec)
namespace std
{
out << "{" << vec.x << ", " << vec.y;
if constexpr (dimensions > 2)
/* Stream a text representation of a glm::vec of any type or dimension */
template<glm::length_t dimensions, typename Type, glm::qualifier qualifier>
std::ostream& operator<<(std::ostream& out, const glm::vec<dimensions, Type, qualifier>& vec)
{
out << ", " << vec.z;
if constexpr (dimensions > 3)
out << "{" << vec.x << ", " << vec.y;
if constexpr (dimensions > 2)
{
out << ", " << vec.w;
out << ", " << vec.z;
if constexpr (dimensions > 3)
{
out << ", " << vec.w;
}
}
out << "}";
return out;
}
out << "}";
return out;
}
/* Add a GLM matrix to the stream, each row on its own line */
template<glm::length_t columns, glm::length_t rows, typename Type, glm::qualifier qualifier>
std::ostream& operator<<(std::ostream& out, const glm::mat<columns, rows, Type, qualifier>& mat)
{
/* get the transpose so we can iterate over the original matrix by row instead of column */
glm::mat<rows, columns, Type, qualifier> transpose = glm::transpose(mat);
out << std::endl << "{";
/* print each column of the transpose, therefore printing each row of the original */
for (std::size_t ii = 0; ii < transpose.length(); ii++)
/* Add a GLM matrix to the stream, each row on its own line */
template<glm::length_t columns, glm::length_t rows, typename Type, glm::qualifier qualifier>
std::ostream& operator<<(std::ostream& out, const glm::mat<columns, rows, Type, qualifier>& mat)
{
if (ii > 0)
/* get the transpose so we can iterate over the original matrix by row instead of column */
glm::mat<rows, columns, Type, qualifier> transpose = glm::transpose(mat);
out << std::endl << "{";
/* print each column of the transpose, therefore printing each row of the original */
for (std::size_t ii = 0; ii < transpose.length(); ii++)
{
out << " ";
}
out << transpose[ii];
if (ii == static_cast<std::size_t>(transpose.length() - 1))
{
out << "}";
}
else
{
out << std::endl;
if (ii > 0)
{
out << " ";
}
out << transpose[ii];
if (ii == static_cast<std::size_t>(transpose.length() - 1))
{
out << "}";
}
else
{
out << std::endl;
}
}
return out;
}
return out;
}
std::ostream& operator<<(std::ostream&, const SDL_Color&);
std::ostream& operator<<(std::ostream&, const SDL_Color&);
/* Add the contents of a vector to the output stream */
template <typename Type>
std::ostream& operator<<(std::ostream& out, const std::vector<Type>& members)
{
out << "{ ";
for (const Type& member : members)
/* Add the contents of a vector to the output stream */
template <typename Type>
std::ostream& operator<<(std::ostream& out, const std::vector<Type>& members)
{
out << member << " ";
out << "{ ";
for (const Type& member : members)
{
out << member << " ";
}
out << "}";
return out;
}
out << "}";
return out;
}
#endif