1 | /*
|
---|
2 | */
|
---|
3 |
|
---|
4 | /*
|
---|
5 | * Copyright (c) 2008 Broadcom Corporation
|
---|
6 | *
|
---|
7 | * All rights reserved.
|
---|
8 | *
|
---|
9 | * This source code contains proprietary, confidential and trade secret
|
---|
10 | * information of Broadcom Corp, and except as provided by written agreement
|
---|
11 | * with Broadcom Corp no part may be disclosed, distributed, reproduced,
|
---|
12 | * transmitted, transcribed, stored in a retrieval system, adapted or
|
---|
13 | * translated in any form or by any means electronic, mechanical, magnetic,
|
---|
14 | * optical, chemical, manual or otherwise.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include "inttypes.h"
|
---|
18 | #include "bcu/format.hpp"
|
---|
19 | #include <iostream>
|
---|
20 | #include <iomanip>
|
---|
21 | #include <cassert>
|
---|
22 |
|
---|
23 | using namespace std;
|
---|
24 |
|
---|
25 | template<typename T>
|
---|
26 | class format_container_t {
|
---|
27 | public:
|
---|
28 | format_container_t(const T& y) : x(y) { }
|
---|
29 | const T& x;
|
---|
30 | };
|
---|
31 |
|
---|
32 | template<typename T> format_container_t<T> make_container(const T& x) { return format_container_t<T>(x); }
|
---|
33 |
|
---|
34 | //
|
---|
35 | // format_container_t insertion operators only behave like char/string when all the basefield bits are clear
|
---|
36 | // this relies on % having cleared all of these bits
|
---|
37 | //
|
---|
38 |
|
---|
39 | template<typename T> ostream &operator<<(ostream &s, const format_container_t<T>& c) { return s << c.x; }
|
---|
40 |
|
---|
41 | ostream &operator<<(ostream &s, const format_container_t<int8_t> c) { if (s.flags() & ios_base::basefield) { return s << (int) c.x; } else { return s << c.x; } }
|
---|
42 | ostream &operator<<(ostream &s, const format_container_t<uint8_t> c) { if (s.flags() & ios_base::basefield) { return s << (unsigned) c.x; } else { return s << c.x; } }
|
---|
43 | ostream &operator<<(ostream &s, const format_container_t<char> c) { if (s.flags() & ios_base::basefield) { return s << (int) c.x; } else { return s << c.x; } }
|
---|
44 | ostream &operator<<(ostream &s, const format_container_t<int8_t *> c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } }
|
---|
45 | ostream &operator<<(ostream &s, const format_container_t<uint8_t *> c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } }
|
---|
46 | ostream &operator<<(ostream &s, const format_container_t<char *> c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } }
|
---|
47 |
|
---|
48 | ostream &operator<<(ostream &s, const format_container_t<const int8_t> c) { if (s.flags() & ios_base::basefield) { return s << (int) c.x; } else { return s << c.x; } }
|
---|
49 | ostream &operator<<(ostream &s, const format_container_t<const uint8_t> c) { if (s.flags() & ios_base::basefield) { return s << (unsigned) c.x; } else { return s << c.x; } }
|
---|
50 | ostream &operator<<(ostream &s, const format_container_t<const char> c) { if (s.flags() & ios_base::basefield) { return s << (int) c.x; } else { return s << c.x; } }
|
---|
51 | ostream &operator<<(ostream &s, const format_container_t<const int8_t *> c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } }
|
---|
52 | ostream &operator<<(ostream &s, const format_container_t<const uint8_t *> c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } }
|
---|
53 | ostream &operator<<(ostream &s, const format_container_t<const char *> c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } }
|
---|
54 |
|
---|
55 | int
|
---|
56 | main() {
|
---|
57 | uint8_t xx = 0;
|
---|
58 | const char * str = "Hello!!!";
|
---|
59 |
|
---|
60 | // printing a variable const char * as a string - these are all OK
|
---|
61 | std::cout << boost::format("BOOST %s\n") % (str); std::cout << std::flush;
|
---|
62 | printf("PRINT %s\n", str); fflush(stdout);
|
---|
63 | std::cout << boost::format("FIXED %s\n") % make_container(str); std::cout << std::flush;
|
---|
64 |
|
---|
65 | // printing a variable const char * as a pointer - BOOST is NOK
|
---|
66 | std::cout << boost::format("BOOST %p\n") % (str); std::cout << std::flush;
|
---|
67 | printf("PRINT %p\n", str); fflush(stdout);
|
---|
68 | std::cout << boost::format("FIXED %p\n") % make_container(str); std::cout << std::flush;
|
---|
69 |
|
---|
70 | // printing a literal string as a string - these are all OK
|
---|
71 | std::cout << boost::format("BOOST %s\n") % ("Hello, world!"); std::cout << std::flush;
|
---|
72 | printf("PRINT %s\n", "Hello, world!"); fflush(stdout);
|
---|
73 | std::cout << boost::format("FIXED %s\n") % make_container("Hello, world!"); std::cout << std::flush;
|
---|
74 |
|
---|
75 | // printing a literal string as a pointer - these are NOK - are we bothered? - what needs to be overloaded to get it right?
|
---|
76 | std::cout << boost::format("BOOST %p\n") % ("Hello, world!"); std::cout << std::flush;
|
---|
77 | printf("PRINT %p\n", "Hello, world!"); fflush(stdout);
|
---|
78 | std::cout << boost::format("FIXED %p\n") % make_container("Hello, world!"); std::cout << std::flush;
|
---|
79 |
|
---|
80 | // printing a uint8_t * as an unsigned - BOOST is NOK - FIXED prints 0x so not perfect
|
---|
81 | std::cout << boost::format("BOOST %x\n") % &xx; std::cout << std::flush;
|
---|
82 | printf("PRINT %x\n", &xx); fflush(stdout);
|
---|
83 | std::cout << boost::format("FIXED %x\n") % make_container(&xx); std::cout << std::flush;
|
---|
84 |
|
---|
85 | return 0;
|
---|
86 | }
|
---|