This is an example showing how to write an inserter class for a custom type, as featured in the
.
#include <fastformat/test/util/compiler_warnings_suppression.first_include.h>
#include <stlsoft/stlsoft.h>
#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;
};
#include <fastformat/sinks/ostream.hpp>
#include <fastformat/ff.hpp>
#include <fastformat/bundles/properties_bundle.hpp>
#include <exception>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER >= 1400
# include <windows.h>
# define sprintf wsprintf
#endif
#include <fastformat/test/util/compiler_warnings_suppression.last_include.h>
std::string edna_1(superhero const& hero);
std::string edna_2(superhero const& hero);
#if !defined(STLSOFT_COMPILER_IS_BORLAND) && \
!defined(STLSOFT_COMPILER_IS_DMC)
std::string edna_3(superhero const& hero);
std::string edna_4(superhero const& hero);
std::string edna_5(superhero const& hero);
#endif
std::string edna_1(superhero const& hero)
{
std::string result;
char num[21];
result += hero.name;
result += " {weight=";
result.append(num, (size_t)sprintf(num, "%d", hero.weight));
result += ", strength=";
result.append(num, (size_t)sprintf(num, "%d", hero.strength));
result += ", goodness=";
result.append(num, (size_t)sprintf(num, "%d", hero.goodness));
result += '}';
return result;
}
std::string edna_2(superhero const& hero)
{
std::string result;
char num[21];
result.reserve(hero.name.size() + 32 + (3 * 20));
result += hero.name;
result += " {weight=";
result.append(num, (size_t)sprintf(num, "%d", hero.weight));
result += ", strength=";
result.append(num, (size_t)sprintf(num, "%d", hero.strength));
result += ", goodness=";
result.append(num, (size_t)sprintf(num, "%d", hero.goodness));
result += '}';
return result;
}
#if !defined(STLSOFT_COMPILER_IS_BORLAND) && \
!defined(STLSOFT_COMPILER_IS_DMC)
std::string edna_3(superhero const& hero)
{
std::string result;
ff::write(result, hero.name, " {weight=", hero.weight
, ", strength=", hero.strength, ", goodness=", hero.goodness
, "}");
return result;
}
std::string edna_4(superhero const& hero)
{
std::string result;
ff::fmt(result, "{0} {{weight={1}, strength={2}, goodness={3}}"
, hero.name, hero.weight
, hero.strength, hero.goodness);
return result;
}
ff::properties_bundle const& getAppBundle();
std::string edna_5(superhero const& hero)
{
std::string result;
ff::properties_bundle const& bundle = getAppBundle();
ff::fmt(result, bundle["superhero.format"]
, hero.name, hero.weight
, hero.strength, hero.goodness);
return result;
}
#endif
ff::properties_bundle* bundle;
ff::properties_bundle const& getAppBundle()
{
STLSOFT_ASSERT(NULL != bundle);
return *bundle;
}
static int main_(int argc, char** argv)
{
char const* bundlePath = "superhero.properties";
if(argc > 1)
{
bundlePath = argv[1];
}
#if 1
superhero thing("The Thing", 200, 99, 100);
superhero batman("Batman", 100, 80, 95);
# if 0
ff::writeln(std::cout, "Ben Grimm is ", thing);
ff::fmtln(std::cout, "Bruce Wayne is {0}", batman);
# else
ff::properties_bundle bundle_(bundlePath);
bundle = &bundle_;
ff::writeln(std::cout, "Ben Grimm is ", edna_1(thing));
ff::writeln(std::cout, "Ben Grimm is ", edna_2(thing));
#if !defined(STLSOFT_COMPILER_IS_BORLAND) && \
!defined(STLSOFT_COMPILER_IS_DMC)
ff::writeln(std::cout, "Ben Grimm is ", edna_3(thing));
ff::writeln(std::cout, "Ben Grimm is ", edna_4(thing));
ff::writeln(std::cout, "Ben Grimm is ", edna_5(thing));
#endif
ff::fmtln(std::cout, "Bruce Wayne is {0}", edna_1(batman));
ff::fmtln(std::cout, "Bruce Wayne is {0}", edna_2(batman));
#if !defined(STLSOFT_COMPILER_IS_BORLAND) && \
!defined(STLSOFT_COMPILER_IS_DMC)
ff::fmtln(std::cout, "Bruce Wayne is {0}", edna_3(batman));
ff::fmtln(std::cout, "Bruce Wayne is {0}", edna_4(batman));
ff::fmtln(std::cout, "Bruce Wayne is {0}", edna_5(batman));
#endif
bundle = NULL;
# endif
#else
#if 0
std::string sink1, sink2;
ff::fmt(ff::fmt(sink1, "{0}", 1), "{0}", 2);
ff::write(ff::write(sink2, 1), 2);
#endif
OutputDebugString(NULL);
#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;
}