cpp_macromap_predef.hpp 11 KB

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