/*! \file piincludes.h * \brief Global includes of PIP * * This file include all needed STL and declare many useful * macros and functions */ /* PIP - Platform Independent Primitives Global includes Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef PIINCLUDES_H #define PIINCLUDES_H //! Version of PIP in hex - 0x##(Major)##(Minor)##(Revision) #define PIP_VERSION 0x000304 //! Major value of PIP version #define PIP_VERSION_MAJOR (PIP_VERSION & 0xFF0000) >> 16 //! Minor value of PIP version #define PIP_VERSION_MINOR (PIP_VERSION & 0xFF00) >> 8 //! Revision value of PIP version #define PIP_VERSION_REVISION PIP_VERSION & 0xFF //! Suffix of PIP version #define PIP_VERSION_SUFFIX "" #ifdef DOXYGEN //! Macro is defined when compile-time debug is enabled # define PIP_DEBUG //! Macro is defined when host is any Windows # define WINDOWS //! Macro is defined when host is QNX # define QNX //! Macro is defined when host is FreeBSD # define FREE_BSD //! Macro is defined when host is Mac OS # define MAC_OS //! Macro is defined when host is any Linux # define LINUX //! Macro is defined when compiler is GCC or MinGW # define CC_GCC //! Macro is defined when PIP is decided that host is support language # define HAS_LOCALE //! Macro is defined when compiler is Visual Studio # define CC_VC //! Macro is defined when compiler is unknown # define CC_OTHER //! Macro is defined when PIP use "rt" library for timers implementation # define PIP_TIMER_RT #endif #if WIN32 || WIN64 || _WIN32 || _WIN64 || __WIN32__ || __WIN64__ # define WINDOWS #endif #if __QNX__ || __QNXNTO__ # define QNX #endif #if __FreeBSD__ # define FREE_BSD #endif #if __APPLE__ || __MACH__ # define MAC_OS #endif #ifndef WINDOWS # ifndef QNX # ifndef FREE_BSD # ifndef MAC_OS # define LINUX # endif # endif # endif #endif #if __GNUC__ # define CC_GCC # define CC_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__) # if CC_GCC_VERSION > 0x025F // > 2.95 # ifdef LINUX # define HAS_LOCALE # endif # endif #elif _MSC_VER # define CC_VC #else # define CC_OTHER #endif #ifdef PIP_DEBUG # undef NDEBUG #else # ifndef NDEBUG # define NDEBUG # endif #endif #ifdef WINDOWS # ifdef CC_GCC # define typeof __typeof # endif #else # define typeof __typeof__ #endif #include "pip_export.h" #if defined(DOXYGEN) || defined(CC_GCC) # undef PIP_EXPORT # define PIP_EXPORT #endif #include #ifdef CC_GCC # include #endif #include #include #ifndef QNX # include # include # include #else # include # include #endif #include #include #include #include #include #include #include #include //#include #include #include #include #include #include #include #include #include #include #include #include #ifdef WINDOWS typedef int socklen_t; # include # include # include # ifdef CC_VC # define SHUT_RDWR 2 # pragma comment(lib, "Ws2_32.lib") # pragma comment(lib, "Iphlpapi.lib") # else # define SHUT_RDWR SD_BOTH # endif # include # include # include #else # include # include # include # include # include # include #endif #ifdef MAC_OS # include # include # include # include # define environ (*_NSGetEnviron()) typedef long time_t; extern clock_serv_t __pi_mac_clock; #endif #ifndef QNX # ifndef WINDOWS # ifndef FREE_BSD # ifndef MAC_OS # define environ __environ # endif # endif # endif #endif #if !defined(WINDOWS) && !defined(MAC_OS) # define PIP_TIMER_RT #endif #ifdef FREE_BSD extern char ** environ; #endif #include "pimonitor.h" extern PIMonitor piMonitor; //! Macro used for infinite loop #define FOREVER for (;;) //! Macro used for infinite wait #define FOREVER_WAIT FOREVER msleep(1); typedef long long llong; typedef unsigned char uchar; typedef unsigned short int ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned long long ullong; typedef long double ldouble; using std::cout; using std::cin; using std::endl; using std::flush; using std::vector; using std::list; using std::queue; using std::deque; using std::stack; using std::set; using std::map; using std::multimap; using std::string; #ifndef QNX using std::wstring; //# ifndef WINDOWS //static locale_t currentLocale_t = 0; //# endif #else typedef std::basic_string wstring; #endif #ifdef HAS_LOCALE static locale_t currentLocale_t = 0; #endif /*! \brief Templated function for swap two values * \details Example:\n \snippet piincludes.cpp swap */ template inline void piSwap(T & f, T & s) {T t = f; f = s; s = t;} /*! \brief Templated function return round of float falue * \details Round is the nearest integer value \n * There is some macros: * - \c piRoundf for "float" * - \c piRoundd for "double" * * Example: * \snippet piincludes.cpp round */ template inline int piRound(const T & v) {return int(v >= T(0.) ? v + T(0.5) : v - T(0.5));} /*! \brief Templated function return floor of float falue * \details Floor is the largest integer that is not greater than value \n * There is some macros: * - \c piFloorf for "float" * - \c piFloord for "double" * * Example: * \snippet piincludes.cpp floor */ template inline int piFloor(const T & v) {return v < T(0) ? int(v) - 1 : int(v);} /*! \brief Templated function return ceil of float falue * \details Ceil is the smallest integer that is not less than value \n * There is some macros: * - \c piCeilf for "float" * - \c piCeild for "double" * * Example: * \snippet piincludes.cpp ceil */ template inline int piCeil(const T & v) {return v < T(0) ? int(v) : int(v) + 1;} /*! \brief Templated function return absolute of numeric falue * \details Absolute is the positive or equal 0 value \n * There is some macros: * - \c piAbss for "short" * - \c piAbsi for "int" * - \c piAbsl for "long" * - \c piAbsll for "llong" * - \c piAbsf for "float" * - \c piAbsd for "double" * * Example: * \snippet piincludes.cpp abs */ template inline T piAbs(const T & v) {return (v >= T(0) ? v : -v);} /*! \brief Templated function return minimum of two values * \details There is some macros: * - \c piMins for "short" * - \c piMini for "int" * - \c piMinl for "long" * - \c piMinll for "llong" * - \c piMinf for "float" * - \c piMind for "double" * * Example: * \snippet piincludes.cpp min2 */ template inline T piMin(const T & f, const T & s) {return (f > s) ? s : f;} /*! \brief Templated function return minimum of tree values * \details There is some macros: * - \c piMins for "short" * - \c piMini for "int" * - \c piMinl for "long" * - \c piMinll for "llong" * - \c piMinf for "float" * - \c piMind for "double" * * Example: * \snippet piincludes.cpp min3 */ template inline T piMin(const T & f, const T & s, const T & t) {return (f < s && f < t) ? f : ((s < t) ? s : t);} /*! \brief Templated function return maximum of two values * \details There is some macros: * - \c piMaxs for "short" * - \c piMaxi for "int" * - \c piMaxl for "long" * - \c piMaxll for "llong" * - \c piMaxf for "float" * - \c piMaxd for "double" * * Example: * \snippet piincludes.cpp max2 */ template inline T piMax(const T & f, const T & s) {return (f < s) ? s : f;} /*! \brief Templated function return maximum of tree values * \details There is some macros: * - \c piMaxs for "short" * - \c piMaxi for "int" * - \c piMaxl for "long" * - \c piMaxll for "llong" * - \c piMaxf for "float" * - \c piMaxd for "double" * * Example: * \snippet piincludes.cpp max3 */ template inline T piMax(const T & f, const T & s, const T & t) {return (f > s && f > t) ? f : ((s > t) ? s : t);} /*! \brief Templated function return clamped value * \details Clamped is the not greater than "max" and not lesser than "min" value \n * There is some macros: * - \c piClamps for "short" * - \c piClampi for "int" * - \c piClampl for "long" * - \c piClampll for "llong" * - \c piClampf for "float" * - \c piClampd for "double" * * Example: * \snippet piincludes.cpp clamp */ template inline T piClamp(const T & v, const T & min, const T & max) {return (v > max ? max : (v < min ? min : v));} #define piRoundf piRound #define piRoundd piRound #define piFloorf piFloor #define piFloord piFloor #define piCeilf piCeil #define piCeild piCeil #define piAbss piAbs #define piAbsi piAbs #define piAbsl piAbs #define piAbsll piAbs #define piAbsf piAbs #define piAbsd piAbs #define piMins piMin #define piMini piMin #define piMinl piMin #define piMinll piMin #define piMinf piMin #define piMind piMin #define piMaxs piMax #define piMaxi piMax #define piMaxl piMax #define piMaxll piMax #define piMaxf piMax #define piMaxd piMax #define piClamps piClamp #define piClampi piClamp #define piClampl piClamp #define piClampll piClamp #define piClampf piClamp #define piClampd piClamp extern bool isPIInit; //! global variable enabling output to piCout extern bool piDebug; extern string ifconfigPath; class PIInit { public: PIInit() { if (isPIInit) return; isPIInit = true; #ifndef WINDOWS sigset_t ss; sigemptyset(&ss); sigaddset(&ss, SIGALRM); sigprocmask(SIG_BLOCK, &ss, 0); pthread_sigmask(SIG_BLOCK, &ss, 0); ifconfigPath = "/bin/ifconfig"; if (!fileExists(ifconfigPath)) { ifconfigPath = "/sbin/ifconfig"; if (!fileExists(ifconfigPath)) { ifconfigPath = "/usr/bin/ifconfig"; if (!fileExists(ifconfigPath)) { ifconfigPath = "/usr/sbin/ifconfig"; if (!fileExists(ifconfigPath)) { ifconfigPath = ""; } } } } #else WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endif //piDebug = true; #ifdef HAS_LOCALE //cout << "has locale" << endl; if (currentLocale_t != 0) { freelocale(currentLocale_t); currentLocale_t = 0; } currentLocale_t = newlocale(LC_ALL, setlocale(LC_ALL, ""), 0); #else setlocale(LC_ALL, ""); #endif #ifdef MAC_OS host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &__pi_mac_clock); #endif } ~PIInit() { #ifdef WINDOWS WSACleanup(); #endif #ifdef MAC_OS mach_port_deallocate(mach_task_self(), __pi_mac_clock); #endif //if (currentLocale_t != 0) freelocale(currentLocale_t); } private: bool fileExists(const string & p) {FILE * f = fopen(p.c_str(), "r"); if (f == 0) return false; fclose(f); return true;} }; extern PIInit piInit; extern lconv * currentLocale; #ifdef WINDOWS inline int random() {return rand();} # ifdef CC_VC inline double round(const double & v) {return floor(v + 0.5);} # endif #endif inline ushort letobe_s(ushort v) {return v = (v << 8) | (v >> 8);} inline bool atob(const string & str) {return str == "1" ? true : false;} inline string btos(const bool num) {return num ? "0" : "1";} inline string itos(const int num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%d", num); #else sprintf_s(ch, 256, "%d", num); #endif return string(ch);} inline string ltos(const long num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%ld", num); #else sprintf_s(ch, 256, "%ld", num); #endif return string(ch);} inline string uitos(const uint num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%u", num); #else sprintf_s(ch, 256, "%u", num); #endif return string(ch);} inline string ultos(const ulong num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%lu", num); #else sprintf_s(ch, 256, "%lu", num); #endif return string(ch);} inline string ftos(const float num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%g", num); #else sprintf_s(ch, 256, "%g", num); #endif return string(ch);} inline string dtos(const double num) { char ch[256]; #ifndef CC_VC sprintf(ch, "%g", num); #else sprintf_s(ch, 256, "%g", num); #endif return string(ch);} /*! \fn errorString() * \brief Return readable error description in format "code - " */ #ifdef WINDOWS inline string errorString() { char * msg; int err = GetLastError(); FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL); return "code " + itos(err) + " - " + string(msg); } #else inline void errorClear() {errno = 0;} inline string errorString() {int e = errno; return "code " + itos(e) + " - " + string(strerror(e));} #endif /// Return readable version of PIP inline string PIPVersion() {return itos(PIP_VERSION_MAJOR) + "." + itos(PIP_VERSION_MINOR) + "." + itos(PIP_VERSION_REVISION) + PIP_VERSION_SUFFIX;} /*! \brief This class used as container for bit flags * \details PIFlags is wrapper around \c "int". There are many * bit-wise operators, native conversion to int and function * to test flag. \n Example: * \snippet piincludes.cpp flags */ template class PIP_EXPORT PIFlags { public: //! Constructor with flags = 0 PIFlags(): flags(0) {;} //! Constructor with flags = Enum "e" PIFlags(Enum e): flags(e) {;} //! Constructor with flags = PIFlags "f" PIFlags(const PIFlags & f): flags(f.flags) {;} //! Constructor with flags = int "i" PIFlags(const int i): flags(i) {;} //! Set flags "f" to value "on" PIFlags & setFlag(const PIFlags & f, bool on = true) {if (on) flags |= f.flags; else flags &= ~f.flags; return *this;} //! Set flag "e" to value "on" PIFlags & setFlag(const Enum & e, bool on = true) {if (on) flags |= e; else flags &= ~e; return *this;} //! Set flag "i" to value "on" PIFlags & setFlag(const int & i, bool on = true) {if (on) flags |= i; else flags &= ~i; return *this;} //! copy operator void operator =(const PIFlags & f) {flags = f.flags;} //! copy operator void operator =(const Enum & e) {flags = e;} //! copy operator void operator =(const int & i) {flags = i;} //! compare operator void operator ==(const PIFlags & f) {flags == f.flags;} //! compare operator void operator ==(const Enum & e) {flags == e;} //! compare operator void operator ==(const int i) {flags == i;} //! compare operator void operator !=(const PIFlags & f) {flags != f.flags;} //! compare operator void operator !=(const Enum & e) {flags != e;} //! compare operator void operator !=(const int i) {flags != i;} //! compare operator void operator >(const PIFlags & f) {flags > f.flags;} //! compare operator void operator >(const Enum & e) {flags > e;} //! compare operator void operator >(const int i) {flags > i;} //! compare operator void operator <(const PIFlags & f) {flags < f.flags;} //! compare operator void operator <(const Enum & e) {flags < e;} //! compare operator void operator <(const int i) {flags < i;} //! compare operator void operator >=(const PIFlags & f) {flags >= f.flags;} //! compare operator void operator >=(const Enum & e) {flags >= e;} //! compare operator void operator >=(const int i) {flags >= i;} //! compare operator void operator <=(const PIFlags & f) {flags <= f.flags;} //! compare operator void operator <=(const Enum & e) {flags <= e;} //! compare operator void operator <=(const int i) {flags <= i;} //! Bit-wise AND operator void operator &=(const PIFlags & f) {flags &= f.flags;} //! Bit-wise AND operator void operator &=(const Enum & e) {flags &= e;} //! Bit-wise AND operator void operator &=(const int i) {flags &= i;} //! Bit-wise OR operator void operator |=(const PIFlags & f) {flags |= f.flags;} //! Bit-wise OR operator void operator |=(const Enum & e) {flags |= e;} //! Bit-wise OR operator void operator |=(const int i) {flags |= i;} //! Bit-wise XOR operator void operator ^=(const PIFlags & f) {flags ^= f.flags;} //! Bit-wise XOR operator void operator ^=(const Enum & e) {flags ^= e;} //! Bit-wise XOR operator void operator ^=(const int i) {flags ^= i;} //! Bit-wise AND operator PIFlags operator &(PIFlags f) const {PIFlags tf(flags & f.flags); return tf;} //! Bit-wise AND operator PIFlags operator &(Enum e) const {PIFlags tf(flags & e); return tf;} //! Bit-wise AND operator PIFlags operator &(int i) const {PIFlags tf(flags & i); return tf;} //! Bit-wise OR operator PIFlags operator |(PIFlags f) const {PIFlags tf(flags | f.flags); return tf;} //! Bit-wise OR operator PIFlags operator |(Enum e) const {PIFlags tf(flags | e); return tf;} //! Bit-wise OR operator PIFlags operator |(int i) const {PIFlags tf(flags | i); return tf;} //! Bit-wise XOR operator PIFlags operator ^(PIFlags f) const {PIFlags tf(flags ^ f.flags); return tf;} //! Bit-wise XOR operator PIFlags operator ^(Enum e) const {PIFlags tf(flags ^ e); return tf;} //! Bit-wise XOR operator PIFlags operator ^(int i) const {PIFlags tf(flags ^ i); return tf;} //! Test flag operator bool operator [](Enum e) const {return (flags & e) == e;} //! Implicity conversion to \c int operator int() const {return flags;} private: int flags; }; //! Macro used for conditional (piDebug) output to PICout #define piCout if (piDebug) PICout() class PIMutex; extern PIMutex __PICout_mutex__; //! \brief Namespace contains enums controlled PICout namespace PICoutManipulators { //! \brief Enum contains special characters enum PIP_EXPORT PICoutSpecialChar { Null /*! Null-character, '\\0' */, NewLine /*! New line character, '\\n' */, Tab /*! Tab character, '\\t' */, Esc /*! Escape character, '\\e' */, Quote /*! Quote character, '"' */ }; //! \brief Enum contains immediate action enum PIP_EXPORT PICoutAction { Flush /*! Flush the output */ }; //! \brief Enum contains control of PICout enum PIP_EXPORT PICoutControl { AddNone /*! No controls */ = 0x0, AddSpaces /*! Spaces will be appear after each output */ = 0x1, AddNewLine /*! New line will be appear after all output */ = 0x2, AddQuotes /*! Each string will be quoted */ = 0x4, AddAll /*! All controls */ = 0xFFFFFFFF }; }; using namespace PICoutManipulators; typedef PIFlags PICoutControls; class PIP_EXPORT PICout { public: //! Default constructor with default features PICout(PIFlags controls = AddSpaces | AddNewLine); PICout(const PICout & other): fo_(other.fo_), cc_(true), co_(other.co_) {;} ~PICout(); //! Output operator for strings with "const char * " type PICout operator <<(const char * v) {space(); quote(); std::cout << v; quote(); return *this;} //! Output operator for strings with "std::string" type PICout operator <<(const string & v) {space(); quote(); std::cout << v; quote(); return *this;} //! Output operator for boolean values PICout operator <<(const bool v) {space(); std::cout << v; return *this;} //! Output operator for "char" values PICout operator <<(const char v) {space(); std::cout << v; return *this;} //! Output operator for "unsigned char" values PICout operator <<(const uchar v) {space(); std::cout << ushort(v); return *this;} //! Output operator for "short" values PICout operator <<(const short v) {space(); std::cout << v; return *this;} //! Output operator for "unsigned short" values PICout operator <<(const ushort v) {space(); std::cout << v; return *this;} //! Output operator for "int" values PICout operator <<(const int v) {space(); std::cout << v; return *this;} //! Output operator for "unsigned int" values PICout operator <<(const uint v) {space(); std::cout << v; return *this;} //! Output operator for "long" values PICout operator <<(const long v) {space(); std::cout << v; return *this;} //! Output operator for "unsigned long" values PICout operator <<(const ulong v) {space(); std::cout << v; return *this;} //! Output operator for "long long" values PICout operator <<(const llong v) {space(); std::cout << v; return *this;} //! Output operator for "unsigned long long" values PICout operator <<(const ullong v) {space(); std::cout << v; return *this;} //! Output operator for "float" values PICout operator <<(const float v) {space(); std::cout << v; return *this;} //! Output operator for "double" values PICout operator <<(const double v) {space(); std::cout << v; return *this;} //! Output operator for \a PICoutSpecialChar values PICout operator <<(const PICoutSpecialChar v) { switch (v) { case Null: std::cout << char(0); case NewLine: std::cout << '\n'; case Tab: std::cout << '\t'; case Esc: std::cout << '\e'; case Quote: std::cout << '"'; }; return *this; } //! Do some action PICout operator <<(const PICoutAction v) { switch (v) { case Flush: std::cout << std::flush; }; return *this; } //! Set control flag "c" is "on" state void setControl(PICoutControl c, bool on = true) {co_.setFlag(c, on);} //! Set control flags "c" and if "save" exec \a saveControl() void setControl(PICoutControls c, bool save = false) {if (save) saveControl(); co_ = c;} //! Save control flags to internal stack \sa \a restoreControl() void saveControl() {cos_.push(co_);} //! Restore control flags from internal stack \sa \a saveControl() void restoreControl() {if (!cos_.empty()) {co_ = cos_.top(); cos_.pop();}} /*! \brief Conditional put space character to output * \details If it is not a first output and control \a AddSpaces is set * space character is put \sa \a quote(), \a newLine() */ inline void space() {if (!fo_ && co_[AddSpaces]) std::cout << ' '; fo_ = false;} /*! \brief Conditional put quote character to output * \details If control \a AddQuotes is set * quote character is put \sa \a space(), \a newLine() */ inline void quote() {if (co_[AddQuotes]) std::cout << '"'; fo_ = false;} /*! \brief Conditional put new line character to output * \details If control \a AddNewLine is set * new line character is put \sa \a space(), \a quote() */ inline void newLine() {if (co_[AddNewLine]) std::cout << std::endl; fo_ = false;} private: bool fo_, cc_; PICoutControls co_; std::stack cos_; }; #endif // PIINCLUDES_H