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.

218 lines
8.8 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/inserters/example.cpp.inserter.integer/example.cpp.inserter.integer.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. *
  9. * Created: 22nd June 2006
  10. * Updated: 7th December 2010
  11. *
  12. * www: http://www.pantheios.org/
  13. *
  14. * License: This source code is placed into the public domain 2006
  15. * by Synesis Software Pty Ltd. There are no restrictions
  16. * whatsoever to your use of the software.
  17. *
  18. * This software is provided "as is", and any warranties,
  19. * express or implied, of any kind and for any purpose, are
  20. * disclaimed.
  21. *
  22. * ////////////////////////////////////////////////////////////////////// */
  23. #define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS // Faster compilation
  24. /* Pantheios Header Files */
  25. #include <pantheios/pantheios.hpp> // Pantheios C++ main header
  26. #include <pantheios/inserters/integer.hpp> // for pantheios::integer
  27. /* STLSoft Header Files */
  28. #include <stlsoft/stlsoft.h> // for sized integer types
  29. /* Standard C/C++ Header Files */
  30. #include <exception> // for std::exception
  31. #include <new> // for std::bad_alloc
  32. #include <string> // for std::string
  33. #include <stdlib.h> // for exit codes
  34. #include <limits.h> // SHRT_MIN, etc.
  35. /* ////////////////////////////////////////////////////////////////////// */
  36. /* Define the stock front-end process identity, so that it links when using
  37. * fe.N, fe.simple, etc. */
  38. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.inserter.integer");
  39. /* ////////////////////////////////////////////////////////////////////// */
  40. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  41. /* We define PANTHEIOS_OBSOLETE here, while still in beta, but it will be
  42. * defined properly, and removed from here, in release candidate phase.
  43. */
  44. #ifndef PANTHEIOS_OBSOLETE
  45. # define PANTHEIOS_OBSOLETE
  46. #endif /* !PANTHEIOS_OBSOLETE */
  47. /* ////////////////////////////////////////////////////////////////////// */
  48. int main()
  49. {
  50. try
  51. {
  52. short shortMin = SHRT_MIN;
  53. short shortMax = SHRT_MAX;
  54. unsigned short ushortMax = USHRT_MAX;
  55. int intMin = INT_MIN;
  56. int intMax = INT_MAX;
  57. unsigned int uintMax = UINT_MAX;
  58. long longMin = LONG_MIN;
  59. long longMax = LONG_MAX;
  60. unsigned long ulongMax = ULONG_MAX;
  61. stlsoft::sint32_t si32Val = -123;
  62. stlsoft::uint32_t ui32Val = 456;
  63. // Log a short in decimal; Output: "SHRT_MIN: [-32768]"
  64. pantheios::log_NOTICE(PSTR("SHRT_MIN: ["), pantheios::integer(shortMin), PSTR("]"));
  65. // Log a short in decimal; Output: "SHRT_MAX: [32767]"
  66. pantheios::log_NOTICE(PSTR("SHRT_MAX: ["), pantheios::integer(shortMax), PSTR("]"));
  67. #ifdef PANTHEIOS_OBSOLETE // 2-parameter constructors are deprecated, and will be removed
  68. // Log a unsigned short as hexadecimal; Output: "USHRT_MAX: [ffff]"
  69. pantheios::log_NOTICE(PSTR("USHRT_MAX: ["), pantheios::integer(ushortMax, pantheios::fmt::hex), PSTR("]"));
  70. // Log an int, into a width of 20; Output: "INT_MIN: [-2147483648 ]"
  71. pantheios::log_NOTICE(PSTR("INT_MIN: ["), pantheios::integer(intMin, -20), PSTR("]"));
  72. // Log an int, into a width of 20; Output: "INT_MAX: [2147483647 ]"
  73. pantheios::log_NOTICE(PSTR("INT_MAX: ["), pantheios::integer(intMax, -20), PSTR("]"));
  74. // Log an unsigned int as hexadecimal with 0x prefix; Output: "UINT_MAX: [0xffffffff]"
  75. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pantheios::integer(uintMax, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  76. // Log a long; Output: "LONG_MIN: [ -2147483648]"
  77. pantheios::log_NOTICE(PSTR("LONG_MIN: ["), pantheios::integer(longMin, 20), PSTR("]"));
  78. // Log a long; Output: "LONG_MAX: [ 2147483647]"
  79. pantheios::log_NOTICE(PSTR("LONG_MAX: ["), pantheios::integer(longMax, 20), PSTR("]"));
  80. #endif /* PANTHEIOS_OBSOLETE */
  81. // Log a unsigned short as hexadecimal; Output: "USHRT_MAX: [ffff]"
  82. pantheios::log_NOTICE(PSTR("USHRT_MAX: ["), pantheios::integer(ushortMax, 0, pantheios::fmt::hex), PSTR("]"));
  83. // Log an int, into a width of -20; Output: "INT_MIN: [-2147483648 ]"
  84. pantheios::log_NOTICE(PSTR("INT_MIN: ["), pantheios::integer(intMin, -20, 0), PSTR("]"));
  85. // Log an int, into a width of +20; Output: "INT_MIN: [ -2147483648]"
  86. pantheios::log_NOTICE(PSTR("INT_MIN: ["), pantheios::integer(intMin, +20, 0), PSTR("]"));
  87. // Log an int, into a width of -20; Output: "INT_MAX: [2147483647 ]"
  88. pantheios::log_NOTICE(PSTR("INT_MAX: ["), pantheios::integer(intMax, -20, 0), PSTR("]"));
  89. // Log an int, into a width of +20; Output: "INT_MAX: [ 2147483647]"
  90. pantheios::log_NOTICE(PSTR("INT_MAX: ["), pantheios::integer(intMax, +20, 0), PSTR("]"));
  91. // Log an unsigned int as hexadecimal with 0x prefix; Output: "UINT_MAX: [0xffffffff]"
  92. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pantheios::integer(uintMax, 0, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  93. // Log an unsigned int as hexadecimal with 0x prefix, into a width of -20; Output: "UINT_MAX: [0xffffffff ]"
  94. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pantheios::integer(uintMax, -20, 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: ["), pantheios::integer(uintMax, +20, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  97. // Log an unsigned int as hexadecimal with 0x prefix, into a width of -10; Output: "UINT_MAX: [0xffffffff]"
  98. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pantheios::integer(uintMax, -10, 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: ["), pantheios::integer(uintMax, +10, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  101. // Log an unsigned int as hexadecimal with 0x prefix, into a width of -5; Output: "UINT_MAX: [0xffffffff]"
  102. pantheios::log_NOTICE(PSTR("UINT_MAX: ["), pantheios::integer(uintMax, -5, 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: ["), pantheios::integer(uintMax, +5, pantheios::fmt::hex | pantheios::fmt::zeroXPrefix), PSTR("]"));
  105. // Log a long, into a width of +20; Output: "LONG_MIN: [ -2147483648]"
  106. pantheios::log_NOTICE(PSTR("LONG_MIN: ["), pantheios::integer(longMin, 20, 0), PSTR("]"));
  107. // Log a long, into a width of +20; Output: "LONG_MAX: [ 2147483647]"
  108. pantheios::log_NOTICE(PSTR("LONG_MAX: ["), pantheios::integer(longMax, 20, 0), PSTR("]"));
  109. // Log a long, into a width of +10; Output: "LONG_MIN: [-2147483648]"
  110. pantheios::log_NOTICE(PSTR("LONG_MIN: ["), pantheios::integer(longMin, 10, 0), PSTR("]"));
  111. // Log a long, into a width of +10; Output: "LONG_MAX: [2147483647]"
  112. pantheios::log_NOTICE(PSTR("LONG_MAX: ["), pantheios::integer(longMax, 10, 0), PSTR("]"));
  113. // Log a long, into a width of +5; Output: "LONG_MIN: [-2147483648]"
  114. pantheios::log_NOTICE(PSTR("LONG_MIN: ["), pantheios::integer(longMin, 5, 0), PSTR("]"));
  115. // Log a long, into a width of +5; Output: "LONG_MAX: [2147483647]"
  116. pantheios::log_NOTICE(PSTR("LONG_MAX: ["), pantheios::integer(longMax, 5, 0), PSTR("]"));
  117. // Log an unsigned long; Output: "ULONG_MAX: [4294967295]"
  118. pantheios::log_NOTICE(PSTR("ULONG_MAX: ["), pantheios::integer(ulongMax), PSTR("]"));
  119. // Log a signed 32-bit integer; Output: "sint32_t: [-123]"
  120. pantheios::log_NOTICE(PSTR("sint32_t: ["), pantheios::integer(si32Val), PSTR("]"));
  121. // Log an unsigned 32-bit integer; Output: "uint32_t: [ 456]"
  122. pantheios::log_NOTICE(PSTR("uint32_t: ["), pantheios::integer(ui32Val, 8, 0), PSTR("]"));
  123. return EXIT_SUCCESS;
  124. }
  125. catch(std::bad_alloc&)
  126. {
  127. pantheios::log(pantheios::alert, PSTR("out of memory"));
  128. }
  129. catch(std::exception& x)
  130. {
  131. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  132. }
  133. catch(...)
  134. {
  135. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  136. }
  137. return EXIT_FAILURE;
  138. }
  139. /* ///////////////////////////// end of file //////////////////////////// */