You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.7 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/inserters/example.cpp.inserter.xi/example.cpp.inserter.xi.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of Pantheios inserters for hex integral types
  7. * - use of pantheios::logputs() in bail-out conditions
  8. * - use of shorthand pan for namespace qualification
  9. * - use of shorthand xi for hex integer inserter
  10. *
  11. * Created: 30th October 2010
  12. * Updated: 6th December 2010
  13. *
  14. * www: http://www.pantheios.org/
  15. *
  16. * License: This source code is placed into the public domain 2006
  17. * by Synesis Software Pty Ltd. There are no restrictions
  18. * whatsoever to your use of the software.
  19. *
  20. * This software is provided "as is", and any warranties,
  21. * express or implied, of any kind and for any purpose, are
  22. * disclaimed.
  23. *
  24. * ////////////////////////////////////////////////////////////////////// */
  25. #define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS // Faster compilation
  26. /* Pantheios Header Files */
  27. #include <pantheios/pan.hpp> // Pantheios C++ main header
  28. #include <pantheios/inserters/xi.hpp> // for pan::xi
  29. /* STLSoft Header Files */
  30. #include <stlsoft/stlsoft.h> // for sized integer types
  31. #include <stlsoft/util/limit_traits.h>
  32. /* Standard C/C++ Header Files */
  33. #include <exception> // for std::exception
  34. #include <new> // for std::bad_alloc
  35. #include <string> // for std::string
  36. #include <stdlib.h> // for exit codes
  37. #include <limits.h> // SHRT_MIN, etc.
  38. /* ////////////////////////////////////////////////////////////////////// */
  39. /* Define the stock front-end process identity, so that it links when using
  40. * fe.N, fe.simple, etc. */
  41. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.inserter.xi");
  42. /* ////////////////////////////////////////////////////////////////////// */
  43. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  44. /* We define PANTHEIOS_OBSOLETE here, while still in beta, but it will be
  45. * defined properly, and removed from here, in release candidate phase.
  46. */
  47. #ifndef PANTHEIOS_OBSOLETE
  48. # define PANTHEIOS_OBSOLETE
  49. #endif /* !PANTHEIOS_OBSOLETE */
  50. /* ////////////////////////////////////////////////////////////////////// */
  51. int main()
  52. {
  53. try
  54. {
  55. unsigned char ucharMax = UCHAR_MAX;
  56. unsigned short ushortMax = USHRT_MAX;
  57. unsigned int uintMax = UINT_MAX;
  58. unsigned long ulongMax = ULONG_MAX;
  59. stlsoft::uint64_t uint64Max = STLSOFT_LIMIT_TRAITS__UINT64_MAX;
  60. // Log a unsigned short as full hexadecimal; Output: "unsigned char max: [ffff]"
  61. pantheios::log_NOTICE(PSTR("unsigned char max: ["), pan::xi(ucharMax), PSTR("]"));
  62. // Log a unsigned short as full hexadecimal; Output: "unsigned short max: [ffff]"
  63. pantheios::log_NOTICE(PSTR("unsigned short max: ["), pan::xi(ushortMax), PSTR("]"));
  64. // Log an unsigned int as full hexadecimal; Output: "unsigned int max: [0xffffffff]"
  65. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax), PSTR("]"));
  66. // Log a unsigned short as full hexadecimal; Output: "unsigned long max: [ffff]"
  67. pantheios::log_NOTICE(PSTR("unsigned long max: ["), pan::xi(ulongMax), PSTR("]"));
  68. // Log an unsigned int as full hexadecimal; Output: "unsigned int64 max: [0xffffffff]"
  69. pantheios::log_NOTICE(PSTR("unsigned int64 max: ["), pan::xi(uint64Max), PSTR("]"));
  70. // Log an unsigned int as full hexadecimal, into a width of -5; Output: "unsigned int max: [0xffffffff]"
  71. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax, -5), PSTR("]"));
  72. // Log an unsigned int as full hexadecimal, into a width of +5; Output: "unsigned int max: [0xffffffff]"
  73. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax, +5), PSTR("]"));
  74. // Log an unsigned int as full hexadecimal, into a width of -10; Output: "unsigned int max: [0xffffffff]"
  75. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax, -10), PSTR("]"));
  76. // Log an unsigned int as full hexadecimal, into a width of +10; Output: "unsigned int max: [0xffffffff]"
  77. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax, +10), PSTR("]"));
  78. // Log an unsigned int as full hexadecimal, into a width of -15; Output: "unsigned int max: [0xffffffff ]"
  79. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax, -15), PSTR("]"));
  80. // Log an unsigned int as full hexadecimal, into a width of +15; Output: "unsigned int max: [0x00000ffffffff]"
  81. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax, +15), PSTR("]"));
  82. // Log an unsigned int as full hexadecimal, into a width of -20; Output: "unsigned int max: [0xffffffff ]"
  83. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax, -20), PSTR("]"));
  84. // Log an unsigned int as full hexadecimal, into a width of +20; Output: "unsigned int max: [0x0000000000ffffffff]"
  85. pantheios::log_NOTICE(PSTR("unsigned int max: ["), pan::xi(uintMax, +20), PSTR("]"));
  86. return EXIT_SUCCESS;
  87. }
  88. catch(std::bad_alloc&)
  89. {
  90. pantheios::log(pantheios::alert, PSTR("out of memory"));
  91. }
  92. catch(std::exception& x)
  93. {
  94. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  95. }
  96. catch(...)
  97. {
  98. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  99. }
  100. return EXIT_FAILURE;
  101. }
  102. /* ///////////////////////////// end of file //////////////////////////// */