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.

220 lines
8.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/inserters/example.cpp.inserter.i/example.cpp.inserter.i.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of Pantheios inserters for integral types
  7. * - use of pantheios::logputs() in bail-out conditions
  8. * - use of shorthand pan for namespace qualification
  9. * - use of shorthand i for 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/i.hpp> // for pan::i
  29. /* STLSoft Header Files */
  30. #include <stlsoft/stlsoft.h> // for sized integer types
  31. /* Standard C/C++ Header Files */
  32. #include <exception> // for std::exception
  33. #include <new> // for std::bad_alloc
  34. #include <string> // for std::string
  35. #include <stdlib.h> // for exit codes
  36. #include <limits.h> // SHRT_MIN, etc.
  37. /* ////////////////////////////////////////////////////////////////////// */
  38. /* Define the stock front-end process identity, so that it links when using
  39. * fe.N, fe.simple, etc. */
  40. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.inserter.i");
  41. /* ////////////////////////////////////////////////////////////////////// */
  42. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  43. /* We define PANTHEIOS_OBSOLETE here, while still in beta, but it will be
  44. * defined properly, and removed from here, in release candidate phase.
  45. */
  46. #ifndef PANTHEIOS_OBSOLETE
  47. # define PANTHEIOS_OBSOLETE
  48. #endif /* !PANTHEIOS_OBSOLETE */
  49. /* ////////////////////////////////////////////////////////////////////// */
  50. int main()
  51. {
  52. try
  53. {
  54. short shortMin = SHRT_MIN;
  55. short shortMax = SHRT_MAX;
  56. unsigned short ushortMax = USHRT_MAX;
  57. int intMin = INT_MIN;
  58. int intMax = INT_MAX;
  59. unsigned int uintMax = UINT_MAX;
  60. long longMin = LONG_MIN;
  61. long longMax = LONG_MAX;
  62. unsigned long ulongMax = ULONG_MAX;
  63. stlsoft::sint32_t si32Val = -123;
  64. stlsoft::uint32_t ui32Val = 456;
  65. // Log a short in decimal; Output: "SHRT_MIN: [-32768]"
  66. pantheios::log_NOTICE(PSTR("SHRT_MIN: ["), pan::i(shortMin), PSTR("]"));
  67. // Log a short in decimal; Output: "SHRT_MAX: [32767]"
  68. pantheios::log_NOTICE(PSTR("SHRT_MAX: ["), pan::i(shortMax), PSTR("]"));
  69. #ifdef PANTHEIOS_OBSOLETE // 2-parameter constructors are deprecated, and will be removed
  70. // Log a unsigned short as hexadecimal; Output: "USHRT_MAX: [ffff]"
  71. pantheios::log_NOTICE(PSTR("USHRT_MAX: ["), pan::i(ushortMax, pantheios::fmt::hex), PSTR("]"));
  72. // Log an int, into a width of 20; Output: "INT_MIN: [-2147483648 ]"
  73. pantheios::log_NOTICE(PSTR("INT_MIN: ["), pan::i(intMin, -20), PSTR("]"));
  74. // Log an int, into a width of 20; Output: "INT_MAX: [2147483647 ]"
  75. pantheios::log_NOTICE(PSTR("INT_MAX: ["), pan::i(intMax, -20), PSTR("]"));
  76. // Log an unsigned int as hexadecimal with 0x prefix; Output: "UINT_MAX: [0xffffffff]"
  77. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pan::i(uintMax, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  78. // Log a long; Output: "LONG_MIN: [ -2147483648]"
  79. pantheios::log_NOTICE(PSTR("LONG_MIN: ["), pan::i(longMin, 20), PSTR("]"));
  80. // Log a long; Output: "LONG_MAX: [ 2147483647]"
  81. pantheios::log_NOTICE(PSTR("LONG_MAX: ["), pan::i(longMax, 20), PSTR("]"));
  82. #endif /* PANTHEIOS_OBSOLETE */
  83. // Log a unsigned short as hexadecimal; Output: "USHRT_MAX: [ffff]"
  84. pantheios::log_NOTICE(PSTR("USHRT_MAX: ["), pan::i(ushortMax, 0, pantheios::fmt::hex), PSTR("]"));
  85. // Log an int, into a width of -20; Output: "INT_MIN: [-2147483648 ]"
  86. pantheios::log_NOTICE(PSTR("INT_MIN: ["), pan::i(intMin, -20, 0), PSTR("]"));
  87. // Log an int, into a width of +20; Output: "INT_MIN: [ -2147483648]"
  88. pantheios::log_NOTICE(PSTR("INT_MIN: ["), pan::i(intMin, +20, 0), PSTR("]"));
  89. // Log an int, into a width of -20; Output: "INT_MAX: [2147483647 ]"
  90. pantheios::log_NOTICE(PSTR("INT_MAX: ["), pan::i(intMax, -20, 0), PSTR("]"));
  91. // Log an int, into a width of +20; Output: "INT_MAX: [ 2147483647]"
  92. pantheios::log_NOTICE(PSTR("INT_MAX: ["), pan::i(intMax, +20, 0), PSTR("]"));
  93. // Log an unsigned int as hexadecimal with 0x prefix; Output: "UINT_MAX: [0xffffffff]"
  94. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pan::i(uintMax, 0, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  95. // Log an unsigned int as hexadecimal with 0x prefix, into a width of -20; Output: "UINT_MAX: [0xffffffff ]"
  96. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pan::i(uintMax, -20, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  97. // Log an unsigned int as hexadecimal with 0x prefix, into a width of +20; Output: "UINT_MAX: [ 0xffffffff]"
  98. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pan::i(uintMax, +20, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  99. // Log an unsigned int as hexadecimal with 0x prefix, into a width of -10; Output: "UINT_MAX: [0xffffffff]"
  100. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pan::i(uintMax, -10, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  101. // Log an unsigned int as hexadecimal with 0x prefix, into a width of +10; Output: "UINT_MAX: [0xffffffff]"
  102. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pan::i(uintMax, +10, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  103. // Log an unsigned int as hexadecimal with 0x prefix, into a width of -5; Output: "UINT_MAX: [0xffffffff]"
  104. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pan::i(uintMax, -5, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  105. // Log an unsigned int as hexadecimal with 0x prefix, into a width of +5; Output: "UINT_MAX: [0xffffffff]"
  106. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pan::i(uintMax, +5, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  107. // Log a long, into a width of +20; Output: "LONG_MIN: [ -2147483648]"
  108. pantheios::log_NOTICE(PSTR("LONG_MIN: ["), pan::i(longMin, 20, 0), PSTR("]"));
  109. // Log a long, into a width of +20; Output: "LONG_MAX: [ 2147483647]"
  110. pantheios::log_NOTICE(PSTR("LONG_MAX: ["), pan::i(longMax, 20, 0), PSTR("]"));
  111. // Log a long, into a width of +10; Output: "LONG_MIN: [-2147483648]"
  112. pantheios::log_NOTICE(PSTR("LONG_MIN: ["), pan::i(longMin, 10, 0), PSTR("]"));
  113. // Log a long, into a width of +10; Output: "LONG_MAX: [2147483647]"
  114. pantheios::log_NOTICE(PSTR("LONG_MAX: ["), pan::i(longMax, 10, 0), PSTR("]"));
  115. // Log a long, into a width of +5; Output: "LONG_MIN: [-2147483648]"
  116. pantheios::log_NOTICE(PSTR("LONG_MIN: ["), pan::i(longMin, 5, 0), PSTR("]"));
  117. // Log a long, into a width of +5; Output: "LONG_MAX: [2147483647]"
  118. pantheios::log_NOTICE(PSTR("LONG_MAX: ["), pan::i(longMax, 5, 0), PSTR("]"));
  119. // Log an unsigned long; Output: "ULONG_MAX: [4294967295]"
  120. pantheios::log_NOTICE(PSTR("ULONG_MAX: ["), pan::i(ulongMax), PSTR("]"));
  121. // Log a signed 32-bit integer; Output: "sint32_t: [-123]"
  122. pantheios::log_NOTICE(PSTR("sint32_t: ["), pan::i(si32Val), PSTR("]"));
  123. // Log an unsigned 32-bit integer; Output: "uint32_t: [ 456]"
  124. pantheios::log_NOTICE(PSTR("uint32_t: ["), pan::i(ui32Val, 8, 0), PSTR("]"));
  125. return EXIT_SUCCESS;
  126. }
  127. catch(std::bad_alloc&)
  128. {
  129. pantheios::log(pantheios::alert, PSTR("out of memory"));
  130. }
  131. catch(std::exception& x)
  132. {
  133. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  134. }
  135. catch(...)
  136. {
  137. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  138. }
  139. return EXIT_FAILURE;
  140. }
  141. /* ///////////////////////////// end of file //////////////////////////// */