cpp_macromap_predef.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. Definition of the predefined macros
  4. http://www.boost.org/
  5. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  6. Software License, Version 1.0. (See accompanying file
  7. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #if !defined(BOOST_CPP_MACROMAP_PREDEF_HPP_HK041119)
  10. #define BOOST_CPP_MACROMAP_PREDEF_HPP_HK041119
  11. #include <cstdio>
  12. #include <boost/assert.hpp>
  13. #include <boost/wave/wave_config.hpp>
  14. #include <boost/wave/wave_config_constant.hpp>
  15. #include <boost/wave/token_ids.hpp>
  16. #include <boost/wave/util/time_conversion_helper.hpp> // time_conversion_helper
  17. // this must occur after all of the includes and before any code appears
  18. #ifdef BOOST_HAS_ABI_HEADERS
  19. #include BOOST_ABI_PREFIX
  20. #endif
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //
  23. // This file contains the definition of functions needed for the management
  24. // of static and dynamic predefined macros, such as __DATE__, __TIME__ etc.
  25. //
  26. // Note: __FILE__, __LINE__ and __INCLUDE_LEVEL__ are handled in the file
  27. // cpp_macromap.hpp.
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. ///////////////////////////////////////////////////////////////////////////////
  31. namespace boost {
  32. namespace wave {
  33. namespace util {
  34. ///////////////////////////////////////////////////////////////////////////
  35. class predefined_macros
  36. {
  37. typedef BOOST_WAVE_STRINGTYPE string_type;
  38. public:
  39. // list of static predefined macros
  40. struct static_macros {
  41. char const *name;
  42. boost::wave::token_id token_id;
  43. char const *value;
  44. };
  45. // list of dynamic predefined macros
  46. struct dynamic_macros {
  47. char const *name;
  48. boost::wave::token_id token_id;
  49. string_type (predefined_macros:: *generator)() const;
  50. };
  51. private:
  52. boost::wave::util::time_conversion_helper compilation_time_;
  53. string_type datestr_; // __DATE__
  54. string_type timestr_; // __TIME__
  55. string_type version_; // __SPIRIT_PP_VERSION__/__WAVE_VERSION__
  56. string_type versionstr_; // __SPIRIT_PP_VERSION_STR__/__WAVE_VERSION_STR__
  57. protected:
  58. void reset_datestr()
  59. {
  60. static const char *const monthnames[] = {
  61. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  62. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  63. };
  64. // for some systems sprintf, time_t etc. is in namespace std
  65. using namespace std;
  66. time_t tt = time(0);
  67. struct tm *tb = 0;
  68. if (tt != (time_t)-1) {
  69. char buffer[sizeof("\"Oct 11 1347\"")+1];
  70. tb = localtime (&tt);
  71. sprintf (buffer, "\"%s %2d %4d\"",
  72. monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
  73. datestr_ = buffer;
  74. }
  75. else {
  76. datestr_ = "\"??? ?? ????\"";
  77. }
  78. }
  79. void reset_timestr()
  80. {
  81. // for some systems sprintf, time_t etc. is in namespace std
  82. using namespace std;
  83. time_t tt = time(0);
  84. struct tm *tb = 0;
  85. if (tt != (time_t)-1) {
  86. char buffer[sizeof("\"12:34:56\"")+1];
  87. tb = localtime (&tt);
  88. sprintf (buffer, "\"%02d:%02d:%02d\"", tb->tm_hour,
  89. tb->tm_min, tb->tm_sec);
  90. timestr_ = buffer;
  91. }
  92. else {
  93. timestr_ = "\"??:??:??\"";
  94. }
  95. }
  96. void reset_version()
  97. {
  98. char buffer[sizeof("0x00000000")+1];
  99. // for some systems sprintf, time_t etc. is in namespace std
  100. using namespace std;
  101. // calculate the number of days since Dec 13 2001
  102. // (the day the Wave project was started)
  103. tm first_day;
  104. using namespace std; // for some systems memset is in namespace std
  105. memset (&first_day, 0, sizeof(tm));
  106. first_day.tm_mon = 11; // Dec
  107. first_day.tm_mday = 13; // 13
  108. first_day.tm_year = 101; // 2001
  109. long seconds = long(difftime(compilation_time_.get_time(), mktime(&first_day)));
  110. sprintf(buffer, "0x%02d%1d%1d%04ld", BOOST_WAVE_VERSION_MAJOR,
  111. BOOST_WAVE_VERSION_MINOR, BOOST_WAVE_VERSION_SUBMINOR,
  112. seconds/(3600*24));
  113. version_ = buffer;
  114. }
  115. void reset_versionstr()
  116. {
  117. char buffer[sizeof("\"00.00.00.0000 \"")+sizeof(BOOST_PLATFORM)+sizeof(BOOST_COMPILER)+4];
  118. // for some systems sprintf, time_t etc. is in namespace std
  119. using namespace std;
  120. // calculate the number of days since Dec 13 2001
  121. // (the day the Wave project was started)
  122. tm first_day;
  123. memset (&first_day, 0, sizeof(tm));
  124. first_day.tm_mon = 11; // Dec
  125. first_day.tm_mday = 13; // 13
  126. first_day.tm_year = 101; // 2001
  127. long seconds = long(difftime(compilation_time_.get_time(), mktime(&first_day)));
  128. sprintf(buffer, "\"%d.%d.%d.%ld [%s/%s]\"", BOOST_WAVE_VERSION_MAJOR,
  129. BOOST_WAVE_VERSION_MINOR, BOOST_WAVE_VERSION_SUBMINOR,
  130. seconds/(3600*24), BOOST_PLATFORM, BOOST_COMPILER);
  131. versionstr_ = buffer;
  132. }
  133. // dynamic predefined macros
  134. string_type get_date() const { return datestr_; } // __DATE__
  135. string_type get_time() const { return timestr_; } // __TIME__
  136. // __SPIRIT_PP__/__WAVE__
  137. string_type get_version() const
  138. {
  139. char buffer[sizeof("0x0000")+1];
  140. using namespace std; // for some systems sprintf is in namespace std
  141. sprintf(buffer, "0x%02d%1d%1d", BOOST_WAVE_VERSION_MAJOR,
  142. BOOST_WAVE_VERSION_MINOR, BOOST_WAVE_VERSION_SUBMINOR);
  143. return buffer;
  144. }
  145. // __WAVE_CONFIG__
  146. string_type get_config() const
  147. {
  148. char buffer[sizeof("0x00000000")+1];
  149. using namespace std; // for some systems sprintf is in namespace std
  150. sprintf(buffer, "0x%08x", BOOST_WAVE_CONFIG);
  151. return buffer;
  152. }
  153. public:
  154. predefined_macros()
  155. : compilation_time_(__DATE__ " " __TIME__)
  156. {
  157. reset();
  158. reset_version();
  159. reset_versionstr();
  160. }
  161. void reset()
  162. {
  163. reset_datestr();
  164. reset_timestr();
  165. }
  166. // __SPIRIT_PP_VERSION__/__WAVE_VERSION__
  167. string_type get_fullversion() const { return version_; }
  168. // __SPIRIT_PP_VERSION_STR__/__WAVE_VERSION_STR__
  169. string_type get_versionstr() const { return versionstr_; }
  170. // C++ mode
  171. static_macros const& static_data_cpp(std::size_t i) const
  172. {
  173. static static_macros data[] = {
  174. { "__STDC__", T_INTLIT, "1" },
  175. { "__cplusplus", T_INTLIT, "199711L" },
  176. { 0, T_EOF, 0 }
  177. };
  178. BOOST_ASSERT(i < sizeof(data)/sizeof(data[0]));
  179. return data[i];
  180. }
  181. #if BOOST_WAVE_SUPPORT_CPP0X != 0
  182. // C++11 mode
  183. static_macros const& static_data_cpp0x(std::size_t i) const
  184. {
  185. static static_macros data[] = {
  186. { "__STDC__", T_INTLIT, "1" },
  187. { "__cplusplus", T_INTLIT, "201103L" },
  188. { "__STDC_VERSION__", T_INTLIT, "199901L" },
  189. { "__STDC_HOSTED__", T_INTLIT, "0" },
  190. { "__WAVE_HAS_VARIADICS__", T_INTLIT, "1" },
  191. { 0, T_EOF, 0 }
  192. };
  193. BOOST_ASSERT(i < sizeof(data)/sizeof(data[0]));
  194. return data[i];
  195. }
  196. #endif
  197. #if BOOST_WAVE_SUPPORT_CPP2A != 0
  198. // C++20 mode
  199. static_macros const& static_data_cpp2a(std::size_t i) const
  200. {
  201. static static_macros data[] = {
  202. { "__STDC__", T_INTLIT, "1" },
  203. { "__cplusplus", T_INTLIT, "202002L" },
  204. { "__STDC_VERSION__", T_INTLIT, "199901L" },
  205. { "__STDC_HOSTED__", T_INTLIT, "0" },
  206. { "__WAVE_HAS_VARIADICS__", T_INTLIT, "1" },
  207. { 0, T_EOF, 0 }
  208. };
  209. BOOST_ASSERT(i < sizeof(data)/sizeof(data[0]));
  210. return data[i];
  211. }
  212. #endif
  213. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  214. // C99 mode
  215. static_macros const& static_data_c99(std::size_t i) const
  216. {
  217. static static_macros data[] = {
  218. { "__STDC__", T_INTLIT, "1" },
  219. { "__STDC_VERSION__", T_INTLIT, "199901L" },
  220. { "__STDC_HOSTED__", T_INTLIT, "0" },
  221. { "__WAVE_HAS_VARIADICS__", T_INTLIT, "1" },
  222. { 0, T_EOF, 0 }
  223. };
  224. BOOST_ASSERT(i < sizeof(data)/sizeof(data[0]));
  225. return data[i];
  226. }
  227. #endif
  228. dynamic_macros const& dynamic_data(std::size_t i) const
  229. {
  230. static dynamic_macros data[] = {
  231. { "__DATE__", T_STRINGLIT, &predefined_macros::get_date },
  232. { "__TIME__", T_STRINGLIT, &predefined_macros::get_time },
  233. { "__SPIRIT_PP__", T_INTLIT, &predefined_macros::get_version },
  234. { "__SPIRIT_PP_VERSION__", T_INTLIT, &predefined_macros::get_fullversion },
  235. { "__SPIRIT_PP_VERSION_STR__", T_STRINGLIT, &predefined_macros::get_versionstr },
  236. { "__WAVE__", T_INTLIT, &predefined_macros::get_version },
  237. { "__WAVE_VERSION__", T_INTLIT, &predefined_macros::get_fullversion },
  238. { "__WAVE_VERSION_STR__", T_STRINGLIT, &predefined_macros::get_versionstr },
  239. { "__WAVE_CONFIG__", T_INTLIT, &predefined_macros::get_config },
  240. { 0, T_EOF, 0 }
  241. };
  242. BOOST_ASSERT(i < sizeof(data)/sizeof(data[0]));
  243. return data[i];
  244. }
  245. }; // predefined_macros
  246. ///////////////////////////////////////////////////////////////////////////////
  247. } // namespace util
  248. } // namespace wave
  249. } // namespace boost
  250. // the suffix header occurs after all of the code
  251. #ifdef BOOST_HAS_ABI_HEADERS
  252. #include BOOST_ABI_SUFFIX
  253. #endif
  254. #endif // !defined(BOOST_CPP_MACROMAP_PREDEF_HPP_HK041119)