examples/example.overload.2.custom_type.filter_type/example.overload.2.custom_type.filter_type.cpp
This is an example showing how to overload the
fastformat::filters::filter_type()
conversion shim for a custom type, as featured in the
Overload article
An Introduction to FastFormat, part 2: Custom Argument and Sink Types.
#include <string>
class superhero
{
public:
typedef std::string string_type;
typedef superhero class_type;
public:
superhero(string_type const& name, int weight, int strength, int goodness)
: name(name)
, weight(weight)
, strength(strength)
, goodness(goodness)
{}
private:
class_type& operator =(class_type const&);
public:
const string_type name;
const int weight;
const int strength;
const int goodness;
};
class extrasuperhero
: public superhero
{
public:
extrasuperhero(string_type const& name, int weight, int strength, int goodness)
: superhero(name, weight, strength, goodness)
{}
};
#include <stlsoft/stlsoft.h>
#if !defined(STLSOFT_COMPILER_IS_BORLAND) && \
!defined(STLSOFT_COMPILER_IS_DMC) && \
( !defined(STLSOFT_COMPILER_IS_MSVC) || \
_MSC_VER >= 1310)
# define FF_COMPILER_SUPPORTS_FILTER_TYPE
#endif
std::string superhero_to_string(superhero const& hero);
#ifdef FF_COMPILER_SUPPORTS_FILTER_TYPE
namespace fastformat
{
namespace filters
{
inline std::string filter_type(superhero const& hero, superhero const*, char const volatile*)
{
return superhero_to_string(hero);
}
inline std::string filter_type(extrasuperhero const& hero, extrasuperhero const*, char const volatile* p)
{
superhero const& regular_hero = hero;
return filter_type(regular_hero, ®ular_hero, p);
}
}
}
#endif
#include <fastformat/sinks/ostream.hpp>
#include <fastformat/ff.hpp>
#include <exception>
#include <iostream>
#include <string>
#include <stdlib.h>
std::string superhero_to_string(superhero const& hero)
{
std::string result;
#ifdef FF_COMPILER_SUPPORTS_FILTER_TYPE
ff::fmt(result, "{0} {{weight={1}, strength={2}, goodness={3}}"
, hero.name, hero.weight
, hero.strength, hero.goodness);
#else
STLSOFT_SUPPRESS_UNUSED(hero);
#endif
return result;
}
static int main_(int , char** )
{
superhero thing("The Thing", 200, 99, 100);
extrasuperhero batman("Batman", 100, 80, 95);
#ifdef FF_COMPILER_SUPPORTS_FILTER_TYPE
ff::writeln(std::cout, "Ben Grimm is ", thing);
ff::fmtln(std::cout, "Bruce Wayne is {0}", batman);
#endif
return EXIT_SUCCESS;
}
int main(int argc, char** argv)
{
try
{
return main_(argc, argv);
}
catch(std::exception& x)
{
ff::fmtln(std::cerr, "exception: {0}", x);
}
catch(...)
{
ff::writeln(std::cerr, "unhandled unknown error");
}
return EXIT_FAILURE;
}