examples/example.overload.2.custom_type.string_access_shims/example.overload.2.custom_type.string_access_shims.cpp
This is an example showing how to overload the
string access shims 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)
{}
public:
string_type name;
int weight;
int strength;
int goodness;
private:
class_type& operator =(class_type const&);
};
#include <stlsoft/stlsoft.h>
#include <stlsoft/string/shim_string.hpp>
#include <fastformat/sinks/shim_string.hpp>
# if _STLSOFT_VER < 0x010a0000 && \
!defined(_STLSOFT_1_10_VER)
typedef fastformat::sinks::shim_string_sink c_str_data_a_result_t;
# else
typedef stlsoft::basic_shim_string<char> c_str_data_a_result_t;
# endif
namespace stlsoft
{
c_str_data_a_result_t c_str_data_a(superhero const& hero);
size_t c_str_len_a(superhero const& hero);
}
#include <fastformat/sinks/ostream.hpp>
#include <fastformat/ff.hpp>
#include <stlsoft/string/shim_string.hpp>
#if _STLSOFT_VER < 0x010a0000 && \
!defined(_STLSOFT_1_10_VER)
# include <fastformat/sinks/shim_string.hpp>
#endif
#include <exception>
#include <iostream>
#include <string>
#include <stdlib.h>
namespace stlsoft
{
#if !defined(STLSOFT_COMPILER_IS_BORLAND) && \
!defined(STLSOFT_COMPILER_IS_DMC)
c_str_data_a_result_t c_str_data_a(superhero const& hero)
{
c_str_data_a_result_t result;
ff::fmt(result, "{0} {{weight={1}, strength={2}, goodness={3}}"
, hero.name, hero.weight
, hero.strength, hero.goodness);
return result;
}
size_t c_str_len_a(superhero const& hero)
{
#if 0
size_t n = hero.name.size() + 32;
char buff[21];
n += sprintf(buff, "%d", hero.weight);
n += sprintf(buff, "%d", hero.strength);
n += sprintf(buff, "%d", hero.goodness);
return n;
#else
return c_str_data_a(hero);
#endif
}
#endif
}
static int main_(int , char** )
{
superhero thing("The Thing", 200, 99, 100);
superhero batman("Batman", 100, 80, 95);
#if !defined(STLSOFT_COMPILER_IS_BORLAND) && \
!defined(STLSOFT_COMPILER_IS_DMC)
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;
}