ROSE  0.9.10.91
Sawyer.h
1 // WARNING: Changes to this file must be contributed back to Sawyer or else they will
2 // be clobbered by the next update from Sawyer. The Sawyer repository is at
3 // https://github.com/matzke1/sawyer.
4 
5 
6 
7 
8 #ifndef Sawyer_H
9 #define Sawyer_H
10 
11 #include <boost/cstdint.hpp>
12 #include <boost/thread/recursive_mutex.hpp>
13 #include <cstdio>
14 #include <string>
15 
301 // Version numbers (conditional compiliation is only so we can test version mismatch handling)
302 #ifndef SAWYER_VERSION_MAJOR
303 #define SAWYER_VERSION_MAJOR 0
304 #define SAWYER_VERSION_MINOR 1
305 #define SAWYER_VERSION_PATCH 0
306 #endif
307 
308 // Macros for thread-safety portability. This allows Sawyer to be compiled with or without thread support and not have a huge
309 // proliferation of conditional compilation directives in the main body of source code.
310 #ifdef _REENTRANT
311  #define SAWYER_MULTI_THREADED 1
312  #define SAWYER_THREAD_TAG Sawyer::MultiThreadedTag
313 #else
314  #define SAWYER_MULTI_THREADED 0
315  #define SAWYER_THREAD_TAG Sawyer::SingleThreadedTag
316 #endif
317 #define SAWYER_THREAD_TRAITS Sawyer::SynchronizationTraits<SAWYER_THREAD_TAG>
318 
319 #ifdef _REENTRANT
320  #if __cplusplus >= 201103L
321  #define SAWYER_THREAD_LOCAL thread_local
322  #elif defined(_MSC_VER)
323  // Visual C++, Intel (Windows), C++ Builder, Digital Mars C++
324  #define SAWYER_THREAD_LOCAL __declspec(thread)
325  #else
326  // Solaris Studio, IBM XL, GNU, LLVM, Intel (linux)
327  #define SAWYER_THREAD_LOCAL __thread
328  #endif
329 #else
330  #define SAWYER_THREAD_LOCAL /*void*/
331 #endif
332 
333 #ifdef BOOST_WINDOWS
334  //--------------------------
335  // Microsoft Windows
336  //--------------------------
337  // FIXME[Robb Matzke 2014-06-18]: get rid of ROSE_UTIL_EXPORTS; cmake can only have one DEFINE_SYMBOL
338  #if defined(SAWYER_DO_EXPORTS) || defined(ROSE_UTIL_EXPORTS) // defined in CMake when compiling libsawyer
339  #define SAWYER_EXPORT __declspec(dllexport)
340  #if _MSC_VER
341  #define SAWYER_EXPORT_NORETURN __declspec(dllexport noreturn)
342  #else
343  // MinGW complains about __declspec(dllexport noreturn), so use only __declspec(dllexport) instead.
344  #define SAWYER_EXPORT_NORETURN __declspec(dllexport)
345  #endif
346  #else
347  #define SAWYER_EXPORT __declspec(dllimport)
348  #define SAWYER_EXPORT_NORETURN __declspec(noreturn)
349  #endif
350 #elif defined(__APPLE__) && defined(__MACH__)
351  //--------------------------
352  // Apple OSX, iOS, Darwin
353  //--------------------------
354  #define SAWYER_EXPORT /*void*/
355  #define SAWYER_EXPORT_NORETURN _Noreturn
356 #else
357  //--------------------------
358  // Other OS compilers
359  //--------------------------
360  #define SAWYER_EXPORT /*void*/
361  #define SAWYER_EXPORT_NORETURN /*void*/
362 #endif
363 
364 #define SAWYER_LINKAGE_INFO SAWYER_VERSION_MAJOR, SAWYER_VERSION_MINOR, SAWYER_VERSION_PATCH, SAWYER_MULTI_THREADED
365 #define SAWYER_CHECK_LINKAGE Sawyer::initializeLibrary(SAWYER_LINKAGE_INFO)
366 
367 
370 namespace Sawyer {
371 
377 SAWYER_EXPORT bool initializeLibrary(size_t vmajor=SAWYER_VERSION_MAJOR,
378  size_t vminor=SAWYER_VERSION_MINOR,
379  size_t vpatch=SAWYER_VERSION_PATCH,
380  bool withThreads=SAWYER_MULTI_THREADED);
381 
385 SAWYER_EXPORT boost::int64_t strtoll(const char*, char**, int);
386 
390 SAWYER_EXPORT boost::uint64_t strtoull(const char*, char**, int);
391 
395 SAWYER_EXPORT std::string readOneLine(FILE*);
396 
398 SAWYER_EXPORT FILE *popen(const std::string&, const char *how);
399 
401 SAWYER_EXPORT int pclose(FILE*);
402 
406 SAWYER_EXPORT std::string generateSequentialName(size_t length=3);
407 
409 SAWYER_EXPORT void checkBoost();
410 
412 SAWYER_EXPORT std::string thisExecutableName();
413 
414 } // namespace
415 
416 // Define only when we have the Boost Chrono library, which was first available in boost-1.47.
417 //#define SAWYER_HAVE_BOOST_CHRONO
418 
419 
421 // Compiler portability issues
422 //
423 // The following macros are used to distinguish between different compilers:
424 // _MSC_VER Defined only when compiled by Microsoft's MVC C++ compiler. This macro is predefined by Microsoft's
425 // preprocessor.
426 //
427 // The following macros are used to distinguish between different target environments, regardless of what compiler is being
428 // used or the environment which is doing the compiling. For instance, BOOST_WINDOWS will be defined when using the MinGW
429 // compiler on Linux to target a Windows environment.
430 // BOOST_WINDOWS The Windows API is present. This is defined (or not) by including <boost/config.hpp>.
431 //
433 
434 // Suppress warnings about unused function formal arguments. Most of the time you can simply omit the argument name, but that's
435 // not possible if the argument is integral to the documentation. In those cases, mention the argument with this macro.
436 # define SAWYER_ARGUSED(X) (void)(X)
437 
438 #ifdef _MSC_VER
439 //--------------------------
440 // Microsoft Windows
441 //--------------------------
442 
443 # define SAWYER_ATTR_UNUSED /*void*/
444 # define SAWYER_ATTR_NORETURN /*void*/
445 # define SAWYER_PRETTY_FUNCTION __FUNCSIG__
446 # define SAWYER_MAY_ALIAS /*void*/
447 # define SAWYER_STATIC_INIT /*void*/
448 # define SAWYER_DEPRECATED(WHY) /*void*/
449 
450 // Microsoft compiler doesn't support stack arrays whose size is not known at compile time. We fudge by using an STL vector,
451 // which will be cleaned up propertly at end of scope or exceptions.
452 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
453  std::vector<TYPE> NAME##Vec_(SIZE); \
454  TYPE *NAME = &(NAME##Vec_[0]);
455 
456 #elif defined(__APPLE__) && defined(__MACH__)
457 //--------------------------
458 // Apple OSX, iOS, Darwin
459 //--------------------------
460 
461 # define SAWYER_ATTR_UNUSED /*void*/
462 # define SAWYER_ATTR_NORETURN /*void*/
463 # define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
464 # define SAWYER_MAY_ALIAS /*void*/
465 # define SAWYER_STATIC_INIT /*void*/
466 # define SAWYER_DEPRECATED(WHY) /*void*/
467 
468 // Apple compilers don't support stack arrays whose size is not known at compile time. We fudge by using an STL vector,
469 // which will be cleaned up propertly at end of scope or exceptions.
470 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
471  std::vector<TYPE> NAME##Vec_(SIZE); \
472  TYPE *NAME = &(NAME##Vec_[0]);
473 
474 
475 #else
476 //--------------------------
477 // Other, GCC-based
478 //--------------------------
479 
480 # define SAWYER_ATTR_UNUSED __attribute__((unused))
481 # define SAWYER_ATTR_NORETURN __attribute__((noreturn))
482 # define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
483 # define SAWYER_MAY_ALIAS __attribute__((may_alias))
484 # define SAWYER_DEPRECATED(WHY) __attribute__((deprecated))
485 
486 // Sawyer globals need to be initialized after the C++ standard runtime, but before other user-level stuff. The constant 101
487 // causes the initialization to happen as early as possible after the C++ runtime.
488 # define SAWYER_STATIC_INIT __attribute__((init_priority(101)))
489 
490 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
491  TYPE NAME[SIZE]; memset(NAME, 0, (SIZE)*sizeof(TYPE))
492 
493 #endif
494 
495 #define SAWYER_CONFIGURED /*void*/
496 
497 #endif
498 
499 // Clean up namespace pollution (shame on Qt for attempting to unilaterally change the language!)
500 // These need to be outside the #ifndef Sawyer_H that protects the rest of this file, otherwise the following
501 // is possible:
502 // #include "foo.h" // which includes Saywer.h"
503 // #include "bar.h" // which includes #define emit...
504 // #include "baz.h" // which has "emit" symbols clobbered by the pollution
505 //
506 // I decided it's better to fail early/fail often, therefore these are always deleted.
507 // Please fix your Qt headers. Qt's "moc" tool has a command-line switch that prevents the pollution.
508 #undef slot
509 #undef emit
510 
int pclose(FILE *)
Semi-portable replacement for pclose.
std::string readOneLine(FILE *)
Reads one line of input from a file.
FILE * popen(const std::string &, const char *how)
Semi-portable replacement for popen.
std::string thisExecutableName()
Return the name of this program obtained from the operating system.
std::string generateSequentialName(size_t length=3)
Generate a sequential name.
Name space for the entire library.
Definition: Access.h:13
void checkBoost()
Check for valid boost version or abort.
boost::uint64_t strtoull(const char *, char **, int)
Portable replacement for strtoull.
boost::int64_t strtoll(const char *, char **, int)
Portable replacement for strtoll.
bool initializeLibrary(size_t vmajor=0, size_t vminor=1, size_t vpatch=0, bool withThreads=0)
Explicitly initialize the library.