ROSE  0.11.145.0
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 //--------------------------------------------------------------------------------------------------------------------------------
309 // Macros for thread-safety portability. This allows Sawyer to be compiled with or without thread support and not have a huge
310 // proliferation of conditional compilation directives in the main body of source code.
311 //--------------------------------------------------------------------------------------------------------------------------------
312 #ifdef _REENTRANT
313  #define SAWYER_MULTI_THREADED 1
314  #define SAWYER_THREAD_TAG Sawyer::MultiThreadedTag
315 #else
316  #define SAWYER_MULTI_THREADED 0
317  #define SAWYER_THREAD_TAG Sawyer::SingleThreadedTag
318 #endif
319 #define SAWYER_THREAD_TRAITS Sawyer::SynchronizationTraits<SAWYER_THREAD_TAG>
320 
321 #ifdef _REENTRANT
322  #if __cplusplus >= 201103L
323  #define SAWYER_THREAD_LOCAL thread_local
324  #elif defined(_MSC_VER)
325  // Visual C++, Intel (Windows), C++ Builder, Digital Mars C++
326  #define SAWYER_THREAD_LOCAL __declspec(thread)
327  #else
328  // Solaris Studio, IBM XL, GNU, LLVM, Intel (linux)
329  #define SAWYER_THREAD_LOCAL __thread
330  #endif
331 #else
332  #define SAWYER_THREAD_LOCAL /*void*/
333 #endif
334 
335 // SAWYER_EXPORT -- Workarounds for Microsoft compilers, otherwise non-static functions won't be callable in shared libraries.
336 #ifdef BOOST_WINDOWS
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  #else
341  #define SAWYER_EXPORT __declspec(dllimport)
342  #endif
343 #else
344  #define SAWYER_EXPORT /*void*/
345 #endif
346 
347 
348 #define SAWYER_LINKAGE_INFO SAWYER_VERSION_MAJOR, SAWYER_VERSION_MINOR, SAWYER_VERSION_PATCH, SAWYER_MULTI_THREADED
349 #define SAWYER_CHECK_LINKAGE Sawyer::initializeLibrary(SAWYER_LINKAGE_INFO)
350 
351 
354 namespace Sawyer {
355 
361 SAWYER_EXPORT bool initializeLibrary(size_t vmajor=SAWYER_VERSION_MAJOR,
362  size_t vminor=SAWYER_VERSION_MINOR,
363  size_t vpatch=SAWYER_VERSION_PATCH,
364  bool withThreads=SAWYER_MULTI_THREADED);
365 
369 SAWYER_EXPORT boost::int64_t strtoll(const char*, char**, int);
370 
374 SAWYER_EXPORT boost::uint64_t strtoull(const char*, char**, int);
375 
379 SAWYER_EXPORT std::string readOneLine(FILE*);
380 
382 SAWYER_EXPORT FILE *popen(const std::string&, const char *how);
383 
385 SAWYER_EXPORT int pclose(FILE*);
386 
390 SAWYER_EXPORT std::string generateSequentialName(size_t length=3);
391 
393 SAWYER_EXPORT void checkBoost();
394 
396 SAWYER_EXPORT std::string thisExecutableName();
397 
398 } // namespace
399 
400 // Define only when we have the Boost Chrono library, which was first available in boost-1.47.
401 //#define SAWYER_HAVE_BOOST_CHRONO
402 
403 
405 // Compiler portability issues
406 //
407 // The following macros are used to distinguish between different compilers:
408 // _MSC_VER Defined only when compiled by Microsoft's MVC C++ compiler. This macro is predefined by Microsoft's
409 // preprocessor.
410 //
411 // The following macros are used to distinguish between different target environments, regardless of what compiler is being
412 // used or the environment which is doing the compiling. For instance, BOOST_WINDOWS will be defined when using the MinGW
413 // compiler on Linux to target a Windows environment.
414 // BOOST_WINDOWS The Windows API is present. This is defined (or not) by including <boost/config.hpp>.
415 //
417 
418 // Suppress warnings about unused function formal arguments. Most of the time you can simply omit the argument name, but that's
419 // not possible if the argument is integral to the documentation. In those cases, mention the argument with this macro.
420 # define SAWYER_ARGUSED(X) (void)(X)
421 
422 #ifdef _MSC_VER
423 //--------------------------
424 // Microsoft Windows
425 //--------------------------
426 
427 # define SAWYER_ATTR_UNUSED /*void*/
428 # define SAWYER_PRETTY_FUNCTION __FUNCSIG__
429 # define SAWYER_MAY_ALIAS /*void*/
430 # define SAWYER_STATIC_INIT /*void*/
431 # define SAWYER_DEPRECATED(WHY) /*void*/
432 
433 // Microsoft compiler doesn't support stack arrays whose size is not known at compile time. We fudge by using an STL vector,
434 // which will be cleaned up propertly at end of scope or exceptions.
435 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
436  std::vector<TYPE> NAME##Vec_(SIZE); \
437  TYPE *NAME = &(NAME##Vec_[0]);
438 
439 #elif defined(__sun)
440 //--------------------------
441 // Sun Solaris
442 //--------------------------
443 
444 # define SAWYER_ATTR_UNUSED /*void*/
445 # define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
446 # define SAWYER_MAY_ALIAS /*void*/
447 # define SAWYER_STATIC_INIT /*void*/
448 # define SAWYER_DEPRECATED(WHY) /*void*/
449 
450 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
451  TYPE NAME[SIZE]; memset(NAME, 0, (SIZE)*sizeof(TYPE))
452 
453 #elif defined(__APPLE__) && defined(__MACH__)
454 //--------------------------
455 // Apple OSX, iOS, Darwin
456 //--------------------------
457 
458 # define SAWYER_ATTR_UNUSED /*void*/
459 # define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
460 # define SAWYER_MAY_ALIAS /*void*/
461 # define SAWYER_STATIC_INIT /*void*/
462 # define SAWYER_DEPRECATED(WHY) /*void*/
463 
464 // Apple compilers don't support stack arrays whose size is not known at compile time. We fudge by using an STL vector,
465 // which will be cleaned up propertly at end of scope or exceptions.
466 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
467  std::vector<TYPE> NAME##Vec_(SIZE); \
468  TYPE *NAME = &(NAME##Vec_[0]);
469 
470 
471 #else
472 //--------------------------
473 // Other, GCC-based
474 //--------------------------
475 
476 # define SAWYER_ATTR_UNUSED __attribute__((unused))
477 # define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
478 # define SAWYER_MAY_ALIAS __attribute__((may_alias))
479 # define SAWYER_DEPRECATED(WHY) __attribute__((deprecated))
480 
481 // Sawyer globals need to be initialized after the C++ standard runtime, but before other user-level stuff. The constant 101
482 // causes the initialization to happen as early as possible after the C++ runtime.
483 # define SAWYER_STATIC_INIT __attribute__((init_priority(101)))
484 
485 # define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
486  TYPE NAME[SIZE]; memset(NAME, 0, (SIZE)*sizeof(TYPE))
487 
488 #endif
489 
490 #define SAWYER_CONFIGURED /*void*/
491 
492 #endif
493 
494 // Clean up namespace pollution (shame on Qt for attempting to unilaterally change the language!)
495 // These need to be outside the #ifndef Sawyer_H that protects the rest of this file, otherwise the following
496 // is possible:
497 // #include "foo.h" // which includes Saywer.h"
498 // #include "bar.h" // which includes #define emit...
499 // #include "baz.h" // which has "emit" symbols clobbered by the pollution
500 //
501 // I decided it's better to fail early/fail often, therefore these are always deleted.
502 // Please fix your Qt headers. Qt's "moc" tool has a command-line switch that prevents the pollution.
503 #undef slot
504 #undef emit
505 
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: FeasiblePath.h:767
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.