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