/* */ /* * Copyright (c) 2008 Broadcom Corporation * * All rights reserved. * * This source code contains proprietary, confidential and trade secret * information of Broadcom Corp, and except as provided by written agreement * with Broadcom Corp no part may be disclosed, distributed, reproduced, * transmitted, transcribed, stored in a retrieval system, adapted or * translated in any form or by any means electronic, mechanical, magnetic, * optical, chemical, manual or otherwise. */ #include "inttypes.h" #include "bcu/format.hpp" #include #include #include using namespace std; template class format_container_t { public: format_container_t(const T& y) : x(y) { } const T& x; }; template format_container_t make_container(const T& x) { return format_container_t(x); } // // format_container_t insertion operators only behave like char/string when all the basefield bits are clear // this relies on % having cleared all of these bits // template ostream &operator<<(ostream &s, const format_container_t& c) { return s << c.x; } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (int) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (unsigned) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (int) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (int) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (unsigned) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (int) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } } ostream &operator<<(ostream &s, const format_container_t c) { if (s.flags() & ios_base::basefield) { return s << (void *) c.x; } else { return s << c.x; } } int main() { uint8_t xx = 0; const char * str = "Hello!!!"; // printing a variable const char * as a string - these are all OK std::cout << boost::format("BOOST %s\n") % (str); std::cout << std::flush; printf("PRINT %s\n", str); fflush(stdout); std::cout << boost::format("FIXED %s\n") % make_container(str); std::cout << std::flush; // printing a variable const char * as a pointer - BOOST is NOK std::cout << boost::format("BOOST %p\n") % (str); std::cout << std::flush; printf("PRINT %p\n", str); fflush(stdout); std::cout << boost::format("FIXED %p\n") % make_container(str); std::cout << std::flush; // printing a literal string as a string - these are all OK std::cout << boost::format("BOOST %s\n") % ("Hello, world!"); std::cout << std::flush; printf("PRINT %s\n", "Hello, world!"); fflush(stdout); std::cout << boost::format("FIXED %s\n") % make_container("Hello, world!"); std::cout << std::flush; // printing a literal string as a pointer - these are NOK - are we bothered? - what needs to be overloaded to get it right? std::cout << boost::format("BOOST %p\n") % ("Hello, world!"); std::cout << std::flush; printf("PRINT %p\n", "Hello, world!"); fflush(stdout); std::cout << boost::format("FIXED %p\n") % make_container("Hello, world!"); std::cout << std::flush; // printing a uint8_t * as an unsigned - BOOST is NOK - FIXED prints 0x so not perfect std::cout << boost::format("BOOST %x\n") % &xx; std::cout << std::flush; printf("PRINT %x\n", &xx); fflush(stdout); std::cout << boost::format("FIXED %x\n") % make_container(&xx); std::cout << std::flush; return 0; }