ROSE  0.9.11.158
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 
356  // This was changed from _Noreturn to compile with g++17. /*void*/ doesn't seem to cause problems for OSX [Rasmussen
357  // 2019.09.03]
358  #define SAWYER_EXPORT_NORETURN /*void*/
359 #else
360  //--------------------------
361  // Other OS compilers
362  //--------------------------
363  #define SAWYER_EXPORT /*void*/
364  #define SAWYER_EXPORT_NORETURN /*void*/
365 #endif
366 
367 #define SAWYER_LINKAGE_INFO SAWYER_VERSION_MAJOR, SAWYER_VERSION_MINOR, SAWYER_VERSION_PATCH, SAWYER_MULTI_THREADED
368 #define SAWYER_CHECK_LINKAGE Sawyer::initializeLibrary(SAWYER_LINKAGE_INFO)
369 
370 
373 namespace Sawyer {
374 
380 SAWYER_EXPORT bool initializeLibrary(size_t vmajor=SAWYER_VERSION_MAJOR,
381  size_t vminor=SAWYER_VERSION_MINOR,
382  size_t vpatch=SAWYER_VERSION_PATCH,
383  bool withThreads=SAWYER_MULTI_THREADED);
384 
388 SAWYER_EXPORT boost::int64_t strtoll(const char*, char**, int);
389 
393 SAWYER_EXPORT boost::uint64_t strtoull(const char*, char**, int);
394 
398 SAWYER_EXPORT std::string readOneLine(FILE*);
399 
401 SAWYER_EXPORT FILE *popen(const std::string&, const char *how);
402 
404 SAWYER_EXPORT int pclose(FILE*);
405 
409 SAWYER_EXPORT std::string generateSequentialName(size_t length=3);
410 
412 SAWYER_EXPORT void checkBoost();
413 
415 SAWYER_EXPORT std::string thisExecutableName();
416 
417 } // namespace
418 
419 // Define only when we have the Boost Chrono library, which was first available in boost-1.47.
420 //#define SAWYER_HAVE_BOOST_CHRONO
421 
422 
424 // Compiler portability issues
425 //
426 // The following macros are used to distinguish between different compilers:
427 // _MSC_VER Defined only when compiled by Microsoft's MVC C++ compiler. This macro is predefined by Microsoft's
428 // preprocessor.
429 //
430 // The following macros are used to distinguish between different target environments, regardless of what compiler is being
431 // used or the environment which is doing the compiling. For instance, BOOST_WINDOWS will be defined when using the MinGW
432 // compiler on Linux to target a Windows environment.
433 // BOOST_WINDOWS The Windows API is present. This is defined (or not) by including <boost/config.hpp>.
434 //
436 
437 // Suppress warnings about unused function formal arguments. Most of the time you can simply omit the argument name, but that's
438 // not possible if the argument is integral to the documentation. In those cases, mention the argument with this macro.
439 # define SAWYER_ARGUSED(X) (void)(X)
440 
441 #ifdef _MSC_VER
442 //--------------------------
443 // Microsoft Windows
444 //--------------------------
445 
446 # define SAWYER_ATTR_UNUSED /*void*/
447 # define SAWYER_ATTR_NORETURN /*void*/
448 # define SAWYER_PRETTY_FUNCTION __FUNCSIG__
449 # define SAWYER_MAY_ALIAS /*void*/
450 # define SAWYER_STATIC_INIT /*void*/
451 # define SAWYER_DEPRECATED(WHY) /*void*/
452 
453 // Microsoft compiler doesn't support stack arrays whose size is not known at compile time. We fudge by using an STL vector,
454 // which will be cleaned up propertly at end of scope or exceptions.
455 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
456  std::vector<TYPE> NAME##Vec_(SIZE); \
457  TYPE *NAME = &(NAME##Vec_[0]);
458 
459 #elif defined(__sun)
460 //--------------------------
461 // Sun Solaris
462 //--------------------------
463 
464 # define SAWYER_ATTR_UNUSED /*void*/
465 # define SAWYER_ATTR_NORETURN /*void*/
466 # define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
467 # define SAWYER_MAY_ALIAS /*void*/
468 # define SAWYER_STATIC_INIT /*void*/
469 # define SAWYER_DEPRECATED(WHY) /*void*/
470 
471 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
472  TYPE NAME[SIZE]; memset(NAME, 0, (SIZE)*sizeof(TYPE))
473 
474 #elif defined(__APPLE__) && defined(__MACH__)
475 //--------------------------
476 // Apple OSX, iOS, Darwin
477 //--------------------------
478 
479 # define SAWYER_ATTR_UNUSED /*void*/
480 # define SAWYER_ATTR_NORETURN /*void*/
481 # define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
482 # define SAWYER_MAY_ALIAS /*void*/
483 # define SAWYER_STATIC_INIT /*void*/
484 # define SAWYER_DEPRECATED(WHY) /*void*/
485 
486 // Apple compilers don't support stack arrays whose size is not known at compile time. We fudge by using an STL vector,
487 // which will be cleaned up propertly at end of scope or exceptions.
488 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
489  std::vector<TYPE> NAME##Vec_(SIZE); \
490  TYPE *NAME = &(NAME##Vec_[0]);
491 
492 
493 #else
494 //--------------------------
495 // Other, GCC-based
496 //--------------------------
497 
498 # define SAWYER_ATTR_UNUSED __attribute__((unused))
499 # define SAWYER_ATTR_NORETURN __attribute__((noreturn))
500 # define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
501 # define SAWYER_MAY_ALIAS __attribute__((may_alias))
502 # define SAWYER_DEPRECATED(WHY) __attribute__((deprecated))
503 
504 // Sawyer globals need to be initialized after the C++ standard runtime, but before other user-level stuff. The constant 101
505 // causes the initialization to happen as early as possible after the C++ runtime.
506 # define SAWYER_STATIC_INIT __attribute__((init_priority(101)))
507 
508 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
509  TYPE NAME[SIZE]; memset(NAME, 0, (SIZE)*sizeof(TYPE))
510 
511 #endif
512 
513 #define SAWYER_CONFIGURED /*void*/
514 
515 #endif
516 
517 // Clean up namespace pollution (shame on Qt for attempting to unilaterally change the language!)
518 // These need to be outside the #ifndef Sawyer_H that protects the rest of this file, otherwise the following
519 // is possible:
520 // #include "foo.h" // which includes Saywer.h"
521 // #include "bar.h" // which includes #define emit...
522 // #include "baz.h" // which has "emit" symbols clobbered by the pollution
523 //
524 // I decided it's better to fail early/fail often, therefore these are always deleted.
525 // Please fix your Qt headers. Qt's "moc" tool has a command-line switch that prevents the pollution.
526 #undef slot
527 #undef emit
528 
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.
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.