ROSE 0.11.145.147
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://gitlab.com/charger7534/sawyer.git.
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
358namespace Sawyer {
359
365SAWYER_EXPORT bool initializeLibrary(size_t vmajor=SAWYER_VERSION_MAJOR,
366 size_t vminor=SAWYER_VERSION_MINOR,
367 size_t vpatch=SAWYER_VERSION_PATCH,
368 bool withThreads=SAWYER_MULTI_THREADED);
369
373SAWYER_EXPORT boost::int64_t strtoll(const char*, char**, int);
374
378SAWYER_EXPORT boost::uint64_t strtoull(const char*, char**, int);
379
383SAWYER_EXPORT std::string readOneLine(FILE*);
384
386SAWYER_EXPORT FILE *popen(const std::string&, const char *how);
387
389SAWYER_EXPORT int pclose(FILE*);
390
394SAWYER_EXPORT std::string generateSequentialName(size_t length=3);
395
397SAWYER_EXPORT void checkBoost();
398
400SAWYER_EXPORT std::string thisExecutableName();
401
402} // namespace
403
404// Define only when we have the Boost Chrono library, which was first available in boost-1.47.
405//#define SAWYER_HAVE_BOOST_CHRONO
406
408// Boost serialization
410#define SAWYER_HAVE_BOOST_SERIALIZATION
411 #ifdef SAWYER_HAVE_BOOST_SERIALIZATION
412 #include <boost/serialization/access.hpp>
413 #include <boost/serialization/nvp.hpp>
414 #include <boost/serialization/split_member.hpp>
415#endif
416
418// Cereal serialization
420
421#if defined(CEREAL_SAVE_FUNCTION_NAME) && defined(CEREAL_LOAD_FUNCTION_NAME) && defined(CEREAL_SERIALIZE_FUNCTION_NAME)
422 #define SAWYER_HAVE_CEREAL
423 #include <cereal/cereal.hpp>
424#endif
425
427// Compiler portability issues
428//
429// The following macros are used to distinguish between different compilers:
430// _MSC_VER Defined only when compiled by Microsoft's MVC C++ compiler. This macro is predefined by Microsoft's
431// preprocessor.
432//
433// The following macros are used to distinguish between different target environments, regardless of what compiler is being
434// used or the environment which is doing the compiling. For instance, BOOST_WINDOWS will be defined when using the MinGW
435// compiler on Linux to target a Windows environment.
436// BOOST_WINDOWS The Windows API is present. This is defined (or not) by including <boost/config.hpp>.
437//
439
440// Suppress warnings about unused function formal arguments. Most of the time you can simply omit the argument name, but that's
441// not possible if the argument is integral to the documentation. In those cases, mention the argument with this macro.
442# define SAWYER_ARGUSED(X) (void)(X)
443
444#ifdef _MSC_VER
445//--------------------------
446// Microsoft Windows
447//--------------------------
448
449# define SAWYER_ATTR_UNUSED /*void*/
450# define SAWYER_PRETTY_FUNCTION __FUNCSIG__
451# define SAWYER_MAY_ALIAS /*void*/
452# define SAWYER_STATIC_INIT /*void*/
453# define SAWYER_DEPRECATED(WHY) /*void*/
454
455// Microsoft compiler doesn't support stack arrays whose size is not known at compile time. We fudge by using an STL vector,
456// which will be cleaned up propertly at end of scope or exceptions.
457# define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
458 std::vector<TYPE> NAME##Vec_(SIZE); \
459 TYPE *NAME = &(NAME##Vec_[0]);
460
461#elif defined(__sun)
462//--------------------------
463// Sun Solaris
464//--------------------------
465
466# define SAWYER_ATTR_UNUSED /*void*/
467# define SAWYER_PRETTY_FUNCTION __PRETTY_FUNCTION__
468# define SAWYER_MAY_ALIAS /*void*/
469# define SAWYER_STATIC_INIT /*void*/
470# define SAWYER_DEPRECATED(WHY) /*void*/
471
472# define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
473 TYPE NAME[SIZE]; memset(NAME, 0, (SIZE)*sizeof(TYPE))
474
475#elif defined(__APPLE__) && defined(__MACH__)
476//--------------------------
477// Apple OSX, iOS, Darwin
478//--------------------------
479
480# define SAWYER_ATTR_UNUSED /*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_PRETTY_FUNCTION __PRETTY_FUNCTION__
500# define SAWYER_MAY_ALIAS __attribute__((may_alias))
501# define SAWYER_DEPRECATED(WHY) __attribute__((deprecated))
502
503// Sawyer globals need to be initialized after the C++ standard runtime, but before other user-level stuff. The constant 101
504// causes the initialization to happen as early as possible after the C++ runtime.
505# define SAWYER_STATIC_INIT __attribute__((init_priority(101)))
506
507# define SAWYER_VARIABLE_LENGTH_ARRAY(TYPE, NAME, SIZE) \
508 TYPE NAME[SIZE]; memset(NAME, 0, (SIZE)*sizeof(TYPE))
509
510#endif
511
512#define SAWYER_CONFIGURED /*void*/
513
514#endif
515
516// Clean up namespace pollution (shame on Qt for attempting to unilaterally change the language!)
517// These need to be outside the #ifndef Sawyer_H that protects the rest of this file, otherwise the following
518// is possible:
519// #include "foo.h" // which includes Saywer.h"
520// #include "bar.h" // which includes #define emit...
521// #include "baz.h" // which has "emit" symbols clobbered by the pollution
522//
523// I decided it's better to fail early/fail often, therefore these are always deleted.
524// Please fix your Qt headers. Qt's "moc" tool has a command-line switch that prevents the pollution.
525#undef slot
526#undef emit
527
Sawyer support library.
int pclose(FILE *)
Semi-portable replacement for pclose.
std::string generateSequentialName(size_t length=3)
Generate a sequential name.
boost::int64_t strtoll(const char *, char **, int)
Portable replacement for strtoll.
std::string thisExecutableName()
Return the name of this program obtained from the operating system.
bool initializeLibrary(size_t vmajor=0, size_t vminor=1, size_t vpatch=0, bool withThreads=0)
Explicitly initialize the library.
std::string readOneLine(FILE *)
Reads one line of input from a file.
void checkBoost()
Check for valid boost version or abort.
boost::uint64_t strtoull(const char *, char **, int)
Portable replacement for strtoull.
FILE * popen(const std::string &, const char *how)
Semi-portable replacement for popen.