basic_examples.cc (2815B)
1 /* 2 * this file demonstrates basic ggformat usage 3 * 4 * compile me with "cl.exe basic_examples.cc ggformat.cc" 5 * or "g++ -std=c++11 basic_examples.cc ggformat.cc" 6 */ 7 8 #include <stdint.h> 9 #include "ggformat.h" 10 11 struct v3 { 12 explicit v3( float x_, float y_, float z_ ) { x = x_; y = y_; z = z_; } 13 float x, y, z; 14 }; 15 16 v3 operator+( const v3 & lhs, const v3 & rhs ) { 17 return v3( lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z ); 18 } 19 20 void format( FormatBuffer * fb, const v3 & v, const FormatOpts & opts ) { 21 format( fb, "v3(" ); 22 format( fb, v.x, opts ); 23 format( fb, ", " ); 24 format( fb, v.y, opts ); 25 format( fb, ", " ); 26 format( fb, v.z, opts ); 27 format( fb, ")" ); 28 } 29 30 struct Thing { 31 // pretend this is more complicated 32 int a; 33 float b; 34 }; 35 36 void format( FormatBuffer * fb, const Thing & thing, const FormatOpts & opts ) { 37 // this is a bit of a hack but is occasionally useful 38 // note that opts are ignored, rather than forwarded to a and b 39 ggformat_impl( fb, "a = {}. b = {}", thing.a, thing.b ); 40 } 41 42 int main() { 43 // basic types 44 ggprint( "ints: {-5} {04} {+} {}\n", 1, 1, 1, 1 ); 45 ggprint( "hex: 0x{04x}\n", 123 ); 46 ggprint( "bin: 0b{b} 0b{b} 0b{b} 0b{b}\n", uint64_t( 123 ), int32_t( -123 ), uint16_t( 123 ), uint8_t( 123 ) ); 47 ggprint( "floats: {-10} {4.2} {+} {}\n", 1.23, 1.23, 1.23, 1.23 ); 48 ggprint( "bools: {} {}\n", true, false ); 49 ggprint( "strings: {-10} {} {{ }}\n", "hello", "world" ); 50 51 ggprint( "mins : {} {} {} {}\n", int64_t( INT64_MIN ), int32_t( INT32_MIN ), int16_t( INT16_MIN ), int8_t( INT8_MIN ) ); 52 ggprint( "maxs : {} {} {} {}\n", int64_t( INT64_MAX ), int32_t( INT32_MAX ), int16_t( INT16_MAX ), int8_t( INT8_MAX ) ); 53 ggprint( "umaxs: {} {} {} {}\n", uint64_t( UINT64_MAX ), uint32_t( UINT32_MAX ), uint16_t( UINT16_MAX ), uint8_t( UINT8_MAX ) ); 54 55 ggprint( "mins : {x} {x} {x} {x}\n", int64_t( INT64_MIN ), int32_t( INT32_MIN ), int16_t( INT16_MIN ), int8_t( INT8_MIN ) ); 56 ggprint( "maxs : {x} {x} {x} {x}\n", int64_t( INT64_MAX ), int32_t( INT32_MAX ), int16_t( INT16_MAX ), int8_t( INT8_MAX ) ); 57 ggprint( "umaxs: {x} {x} {x} {x}\n", uint64_t( UINT64_MAX ), uint32_t( UINT32_MAX ), uint16_t( UINT16_MAX ), uint8_t( UINT8_MAX ) ); 58 59 ggprint( "mins : {b} {b} {b} {b}\n", int64_t( INT64_MIN ), int32_t( INT32_MIN ), int16_t( INT16_MIN ), int8_t( INT8_MIN ) ); 60 ggprint( "maxs : {b} {b} {b} {b}\n", int64_t( INT64_MAX ), int32_t( INT32_MAX ), int16_t( INT16_MAX ), int8_t( INT8_MAX ) ); 61 ggprint( "umaxs: {b} {b} {b} {b}\n", uint64_t( UINT64_MAX ), uint32_t( UINT32_MAX ), uint16_t( UINT16_MAX ), uint8_t( UINT8_MAX ) ); 62 63 // user defined type 64 v3 a = v3( 1, 2, 3 ); 65 v3 b = v3( 4, 5, 6 ); 66 ggprint( "a = {}. b = {02.2}.\na + b = {+}\n", a, b, a + b ); 67 68 // more complicated user defined type 69 Thing thing; 70 thing.a = 12345; 71 thing.b = 67890; 72 ggprint( "{}\n", thing ); 73 74 return 0; 75 } 76