Code::Blocks  SVN r11506
prep.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  */
5 
6 #if ( !defined(PREP_H) && defined(__cplusplus) )
7 #define PREP_H
8 
9 #include <stdint.h>
10 
11 #include <wx/defs.h> // wxIntPtr
12 #ifndef wxMAJOR_VERSION
13  #include <wx/version.h>
14 #endif
15 
16 /* ---------------------------------------------------------------------------------------------------------
17  Version<major, minor, revision>::eval
18  Integer compile-time constant that represents a major.minor.revision style version number.
19  This is the most convenient and efficient way to check for API-version dependent features,
20  as it can be done as easily as: if(foobar_version >= Version<1,2>::eval)
21  The range of possible version numbers is [Version<0,0,0> , Version<63,1023,32767>]
22 
23  wxMinimumVersion<...>::eval, wxExactVersion<...>::eval
24  Boolean values that are true if the compiled wxWidgets version is at least (exactly)
25  the version given as parameters.
26 
27  Example:
28  if(!wxMinimumVersion<2.8>::eval && (foo_version < Version<1.2>::eval))
29  ErrorMessage("This feature is only supported under wxWidgets 2.8, or with Foo Component 1.2 or higher.");
30 */
31 template <int major, int minor = 0, int revision = 0> struct Version { enum { eval = (major<<25) + (minor<<15) + revision }; };
32 inline void Version2MMR(int v, int& major, int& minor, int& revision) { major = v>>25; minor = (v>>15) & ((1<<10) -1); revision = v & ((1<<15) -1); }
33 
34 template <int major, int minor, int rel = 0> struct wxMinimumVersion { enum { eval = ((unsigned int) Version<wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER>::eval >= (unsigned int) Version<major, minor, rel>::eval) }; };
35 template <int major, int minor, int rel = 0> struct wxExactVersion { enum { eval = ((unsigned int) Version<wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER>::eval == (unsigned int) Version<major, minor, rel>::eval) }; };
36 
37 
38 /* ---------------------------------------------------------------------------------------------------------
39  Conditional typedef, works the same way as the C++ ternary operator for lvalues does
40 
41  You want to do something like:
42  typedef (condition ? true_type : false_type) my_type;
43 
44  This can for example be used to define a type as
45  FILE under Unix / HANDLE under Windows
46  int under wxWidgets 2.6 / size_t under wxWidgets 2.8
47  STL iterator / wxContainer iterator / pointer ...
48  and not having to worry about what it really is, without giving up type safety and without
49  the nasty side effects that a #define might have.
50 
51  Example:
52  typedef TernaryCondTypedef<wxMinimumVersion<2,5>::eval, wxTreeItemIdValue, long int>::eval tree_cookie_t;
53 */
54 template <bool cond, class true_t, class false_t> struct TernaryCondTypedef { typedef true_t eval; };
55 template <class true_t, class false_t> struct TernaryCondTypedef<false, true_t, false_t> { typedef false_t eval; };
56 
57 
58 
59 /* ---------------------------------------------------------------------------------------------------------
60  Size of an array (if the compiler can determine it at compile time)
61  Write:
62  int widths[] = {5, 3, 8};
63  myListControl->SetWidths(widths, array_size(widths));
64  instead of:
65  int widths[] = {5, 3, 8};
66  myListControl->SetWidths(widths, 4); // oh crap, why does this crash?
67 */
68 template <typename T> unsigned int array_size(const T& array) { enum {result = sizeof(array) / sizeof(array[0])}; return result; }
69 
70 
71 
72 /* ---------------------------------------------------------------------------------------------------------
73  Delete a pointer and (semi-atomically) set it to zero.
74  In class destructors, please continue using the normal C++ delete operator (it is unnecessary overhead
75  to set a pointer to zero in the destructor, as it can never be used again).
76  In _all_ other cases, use Delete(), which prevents accidential double-deletes.
77 */
78 template<typename T>inline void Delete(T*& p){delete p; p = nullptr;}
79 template<typename T>inline void DeleteArray(T*& p){delete[] p; p = nullptr;}
80 
81 
82 
83 /* ---------------------------------------------------------------------------------------------------------
84  platform::id
85  Value of type platform::identifier describing the target platform
86 
87  platform::windows, platform::macosx, platform::Linux
88  platform::freebsd, platform::netbsd, platform::openbsd
89  platform::darwin, platform::solaris, platform::Unix
90  Boolean value that evaluates to true if the target platform is the same as the variable's name, false otherwise.
91  Using the platform booleans is equivalent to using platform::id, but results in nicer code.
92 
93  platform::unicode
94  Boolean value that evaluates to true if this application was built in Unicode mode. Of course this does not
95  tell anything about the host system's actual capabilities.
96 
97  platform::gtk
98  platform::carbon
99  platform::cocoa
100  Boolean values showing the presence of very specific toolkits. Use only for workarounds to specific problems with these.
101 
102  platform::bits
103  Size of pointer in bits as a measure of CPU architecture (32 or 64 bits).
104 
105  platform::gcc
106  The gcc version number as Version<...> if gcc was used to build the program, zero otherwise.
107 */
108 namespace platform
109 {
110  enum identifier
111  {
112  platform_unknown,
113  platform_windows,
114  platform_linux,
115  platform_freebsd,
116  platform_netbsd,
117  platform_openbsd,
118  platform_darwin,
119  platform_solaris,
120  platform_macosx
121  };
122 
123  // unfortunately we still need to use the preprocessor here...
124  #if ( wxUSE_UNICODE )
125  const bool unicode = true;
126  #else
127  const bool unicode = false;
128  #endif
129 
130  #if defined ( __WIN32__ ) || defined ( _WIN64 )
131  const identifier id = platform_windows;
132  #elif defined ( __WXMAC__ ) || defined ( __WXCOCOA__ )
133  const identifier id = platform_macosx;
134  #elif defined ( __linux__ ) || defined ( LINUX )
135  const identifier id = platform_linux;
136  #elif defined ( FREEBSD ) || defined ( __FREEBSD__ )
137  const identifier id = platform_freebsd;
138  #elif defined ( NETBSD ) || defined ( __NETBSD__ )
139  const identifier id = platform_netbsd;
140  #elif defined ( OPENBSD ) || defined ( __OPENBSD__ )
141  const identifier id = platform_openbsd;
142  #elif defined ( DARWIN ) || defined ( __APPLE__ )
143  const identifier id = platform_darwin;
144  #elif defined(sun) || defined(__sun)
145  const identifier id = platform_solaris;
146  #else
147  const identifier id = platform_unknown;
148  #endif
149 
150  #if defined ( __WXGTK__ )
151  const bool gtk = true;
152  #else
153  const bool gtk = false;
154  #endif
155 
156  #if defined ( __WXMAC__ )
157  const bool carbon = true;
158  #else
159  const bool carbon = false;
160  #endif
161 
162  #if defined ( __WXCOCOA__ )
163  const bool cocoa = true;
164  #else
165  const bool cocoa = false;
166  #endif
167 
168  const bool windows = (id == platform_windows);
169  const bool macosx = (id == platform_macosx);
170  const bool Linux = (id == platform_linux);
171  const bool freebsd = (id == platform_freebsd);
172  const bool netbsd = (id == platform_netbsd);
173  const bool openbsd = (id == platform_openbsd);
174  const bool darwin = (id == platform_darwin);
175  const bool solaris = (id == platform_solaris);
176  const bool Unix = (Linux | freebsd | netbsd | openbsd | darwin | solaris);
177 
178  const int bits = 8*sizeof(void*);
179 
180  // Function and parameter attributes
181  // ----------------------------------
182  //
183  // These parameters possibly allow more fine-grained optimization and better diagnostics.
184  // They are implemented for the GCC compiler family and 'quiet' for all others
185  //
186  // IMPORTANT: Do not lie to the compiler when marking functions pure or const, or you will cause great evil.
187  //
188  // a) Optimization hints:
189  //
190  // cb_pure_function Function has no observable effects other than the return value
191  // and the return value depends only on parameters and global variables
192  // ALSO: function does not throw (makes sense with the above requirement).
193  //
194  // cb_const_function Function has no observable effects other than the return value
195  // and the return value depends only on parameters.
196  // ALSO: No external memory, including memory referenced by parameters, may be touched.
197  // ALSO: function does not throw (makes sense with the above requirement).
198  //
199  // cb_force_inline What the name says. This is the strongest available inline hint (the compiler may still ignore it).
200  //
201  //
202  // b) Usage hints:
203  //
204  // cb_must_consume_result The return value of a function must be consumed (usually because of taking ownership), i.e.
205  // ObjType* result = function(); ---> valid
206  // function(); ---> will raise a warning
207  //
208  // cb_deprecated_function The function is deprecated and may be removed in a future revision of the API.
209  // New code should not call the function. Doing so will work, but will raise a warning.
210  //
211  // cb_optional No warning will be raised if the parameter is not used. Identical to "unused",
212  // but the wording sounds more like "you may omit using this" rather than "we are not using this"
213  // Used for interfaces or base classes to convey the message that a (generally useful) parameter is passed,
214  // but it is allowable to ignore the parameter (and maybe a base class implementation does just that).
215  //
216  // cb_unused No warning will be raised if the parameter is not used.
217  // Use this if you want to convey that you are aware of a parameter but you are intentionally not using it.
218  //
219  // POISON(message) If you touch this, you'll die. The message tells you why.
220  // ALSO: It will break the build, so nobody else must die.
221 
222  #if defined(__GNUC__) && ((100 * __GNUC__ + 10 * __GNUC_MINOR__ + __GNUC_PATCHLEVEL__) >= 332)
223  const int gcc = Version<__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__>::eval;
224  #define cb_pure_function __attribute__ ((__pure__, __nothrow__))
225  #define cb_const_function __attribute__ ((__const__, __nothrow__))
226  #define cb_force_inline __attribute__ ((__always_inline__))
227  #define cb_must_consume_result __attribute__ ((__warn_unused_result__))
228  #define cb_deprecated_function __attribute__ ((__deprecated__))
229  #define cb_unused __attribute__ ((__unused__))
230 
231  #if((100 * __GNUC__ + 10 *__GNUC_MINOR__ + __GNUC_PATCHLEVEL__) >= 436)
232  #define POISON(message) __attribute__((__error__(#message))
233  #else
234  #define POISON(message)
235  #endif
236  #else
237  const int gcc = 0;
238  #define cb_pure_function
239  #define cb_const_function
240  #define cb_force_inline
241  #define cb_must_consume_result
242  #define cb_deprecated_function
243  #define cb_unused
244  #define POISON(message)
245  #endif
246 
247  #define cb_optional cb_unused
248 }
249 
250 
251 
252 /* ---------------------------------------------------------------------------------------------------------
253  Nobody except Yiannis touches these. You don't need to know, you don't want to know.
254 */
255 namespace sdk
256 {
257  const int version = Version<1>::eval;
258  const int buildsystem_version = Version<1>::eval;
259  const int plugin_api_version = Version<1,11,10>::eval;
260 }
261 
262 
263 
264 /* ---------------------------------------------------------------------------------------------------------
265  The compatibility namespace is intended for workarounds that try to cope with incompatibilities in different
266  wxWidgets versions. Since these often involve missing functions or constants, #ifdef is explicitly allowed
267  and not frowned upon in this namespace.
268 
269  wxHideReadonly
270  use in file selector dialog to keep wxWidgets 2.4/2.6 users happy (this flag is important for normal operation!),
271  without breaking the 2.8 build.
272 */
273 namespace compatibility
274 {
275  #if defined(WXWIN_COMPATIBILITY_2_4) && defined(wxHIDE_READONLY)
276  const int wxHideReadonly = wxHIDE_READONLY;
277  #else
278  const int wxHideReadonly = 0;
279  #endif
280 }
281 
282 
283 
284 /* ---------------------------------------------------------------------------------------------------------
285  Utility function for an incrementing (unique per instance) unsigned integer ID.
286  - ID is unsigned, starting at zero (best choice for many cases, e.g. for use as array index)
287  - if it _must_ start with 1 for some odd reason, simply add one extra call to GetID()<YourClass>
288  - use GetID() for an application-wide unique integer ID (alias for GetID<void>())
289  - use GetID<YourClass>() for your own private class-internal ID
290  - use GetID<SomeStruct>() for your own private cross-class shareable ID
291 
292  (( This is implementation is more general and uses one less temporary copy
293  than the implementation found in several places of the SDK.
294  NOTE: remove this paragraph once the SDK has been updated ))
295 
296  Example:
297 
298  struct foo; // used for sharing an ID between A and B
299 
300  class A
301  {
302  public:
303  unsigned int X(){return GetID(); }; // application-global
304  unsigned int Y(){return GetID<A>(); }; // private for A
305  unsigned int Z(){return GetID<foo>(); }; // shared with B
306 
307  };
308 
309  class B
310  {
311  public:
312  unsigned int Y(){return GetID<B>(); }; // private for B
313  unsigned int Z(){return GetID<foo>(); }; // shared with A
314  };
315 
316  In this example, A::X() will return a counter that is globally unique throughout the lifetime of the application.
317  A::Y() and B::Y() will return counters that increment independently for A and B. In other words,
318  B does not know about A's counter, nor can it influence it.
319  A::Z() and B::Z() will return a shared counter that increments if either A or B is asked to return a value.
320 */
321 
322 class ID
323 {
324  wxIntPtr value;
325 
326  ID(wxIntPtr in) : value(in) {};
327 
328  template<typename> friend ID GetID();
329  friend ID ConstructID(wxIntPtr);
330 
331 public:
332 
333  ID() : value ((wxIntPtr) -1) {};
334 
335  operator wxIntPtr() const { return value; };
336  operator void*() const { return reinterpret_cast<void*>(static_cast<uintptr_t>(value)); };
337 
338  bool Valid() const { return value != ((wxIntPtr) -1); };
339  bool operator!() const { return !Valid(); };
340 
341  friend bool operator==(ID a, ID b) { return a.value == b.value; };
342  friend bool operator==(ID a, int b) { return a.value == (wxIntPtr) b; };
343 
344  friend bool operator!=(ID a, ID b) { return a.value != b.value; };
345  friend bool operator!=(ID a, int b) { return a.value != (wxIntPtr) b; };
346 };
347 
348 
349 template<typename whatever> inline ID GetID()
350 {
351  static wxIntPtr id = (wxIntPtr) -1;
352  return ID(++id);
353 }
354 
355 inline ID GetID() { return GetID<void>(); }
356 inline ID ConstructID(wxIntPtr i) { return ID(i); }
357 
358 // Just included to possibly set _LIBCPP_VERSION
359 #include <ciso646>
360 
361 #include <memory>
362 
363 // Add std::shared_ptr in a namespace, so different implementations can be used with different compilers
364 namespace cb
365 {
366  using std::shared_ptr;
367  using std::static_pointer_cast;
368  using std::weak_ptr;
369 }
370 
371 #if defined(__APPLE__) && defined(__MACH__)
372  #define CB_LIBRARY_ENVVAR _T("DYLD_LIBRARY_PATH")
373 #elif !defined(__WXMSW__)
374  #define CB_LIBRARY_ENVVAR _T("LD_LIBRARY_PATH")
375 #else
376  #define CB_LIBRARY_ENVVAR _T("PATH")
377 #endif
378 
379 #endif
const int version
void Delete(std::vector< T > &s)
Definition: safedelete.h:20
bool operator==(wxString const &s, cbWatch const &w)
bool operator!=(const wxString &s1, const wxString &s2)
Definition: id.h:15
ssize_t wxIntPtr