From 94ef217778863d73c5d2999c2a53490230ef09c0 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sat, 13 Aug 2016 21:03:39 +1000 Subject: [PATCH] colour: add type_name specialisation --- colour.cpp | 30 +++++++++++++++++++++++++++--- colour.hpp | 17 ++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/colour.cpp b/colour.cpp index 42b99bfa..2f6b7cfb 100644 --- a/colour.cpp +++ b/colour.cpp @@ -440,14 +440,38 @@ util::operator>> (std::istream &is, util::colour &c) template std::istream& util::operator>> (std::istream&, util::colour<3,uint8_t>&); /////////////////////////////////////////////////////////////////////////////// -#define INSTANTIATE_S_T(S,T) \ -template struct util::colour; \ -template std::ostream& util::operator<< (std::ostream&, util::colour); +// we need to instantiate the various type_name specialisations for colour. +// +// we provide a declaration here, before then instantiating a routine that we +// know will cause an implicit instantiation (ie util::to_string) for each +// colour specialisation we require. +template +constexpr +const char util::type_name>::value[]; + +//----------------------------------------------------------------------------- +#define INSTANTIATE_S_T(S,T) \ +template \ +struct util::colour; \ + \ +template \ +std::ostream& \ +util::operator<< (std::ostream&, util::colour); \ + \ +template \ +const char* \ +util::to_string> (void); + + +//----------------------------------------------------------------------------- #define INSTANTIATE_S(S) \ INSTANTIATE_S_T(S,uint8_t) \ INSTANTIATE_S_T(S,float) \ INSTANTIATE_S_T(S,double) + +//----------------------------------------------------------------------------- +INSTANTIATE_S(1) INSTANTIATE_S(3) INSTANTIATE_S(4) diff --git a/colour.hpp b/colour.hpp index 5cda6a1a..ad717437 100644 --- a/colour.hpp +++ b/colour.hpp @@ -18,6 +18,7 @@ #define __UTIL_COLOUR_HPP #include "coord.hpp" +#include "introspection.hpp" #include @@ -48,6 +49,7 @@ namespace util { template using colour3 = colour<3,T>; template using colour4 = colour<4,T>; + typedef colour3 colour1u; typedef colour3 colour3u; typedef colour4 colour4u; @@ -55,15 +57,24 @@ namespace util { typedef colour3 colour3f; typedef colour4 colour4f; - // RGB <-> HSV + // RGB/HSV conversions colour3f rgb_to_hsv (colour3f); colour3f hsv_to_rgb (colour3f); + // ostream/istream operators template - std::ostream& operator<< (std::ostream&, util::colour); + std::ostream& + operator<< (std::ostream&, util::colour); template - std::istream& operator>> (std::istream&, util::colour&); + std::istream& + operator>> (std::istream&, util::colour&); + + // type name introspection specialisation + template + struct type_name> { + static constexpr const char value[] = "colour"; + }; } #include "colour.ipp"