4.06.2013 - Version 0.3.4 - PIOBJECT() macro, ethernet improvement, documentation based on Doxygen

This commit is contained in:
peri4
2013-06-04 21:28:15 +04:00
parent 02c629d6a8
commit 9111640ca8
53 changed files with 3019 additions and 910 deletions

View File

@@ -1,3 +1,9 @@
/*! \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
@@ -20,12 +26,58 @@
#ifndef PIINCLUDES_H
#define PIINCLUDES_H
#define PIP_VERSION 0x000300
//! 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
@@ -60,6 +112,13 @@
#else
# define CC_OTHER
#endif
#ifdef PIP_DEBUG
# undef NDEBUG
#else
# ifndef NDEBUG
# define NDEBUG
# endif
#endif
#ifdef WINDOWS
# ifdef CC_GCC
@@ -69,6 +128,11 @@
# define typeof __typeof__
#endif
#include "pip_export.h"
#if defined(DOXYGEN) || defined(CC_GCC)
# undef PIP_EXPORT
# define PIP_EXPORT
#endif
#include <iostream>
#ifdef CC_GCC
# include <unistd.h>
@@ -90,6 +154,7 @@
#include <cctype>
#include <ctime>
#include <csignal>
#include <cassert>
//#include <signal.h>
#include <typeinfo>
#include <algorithm>
@@ -153,7 +218,10 @@ extern char ** environ;
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;
@@ -175,6 +243,7 @@ using std::deque;
using std::stack;
using std::set;
using std::map;
using std::multimap;
using std::string;
#ifndef QNX
using std::wstring;
@@ -188,15 +257,118 @@ typedef std::basic_string<wchar_t> wstring;
static locale_t currentLocale_t = 0;
#endif
/*! \brief Templated function for swap two values
* \details Example:\n \snippet piincludes.cpp swap */
template<typename T> 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<typename T> 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<typename T> 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<typename T> 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<typename T> 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<typename T> 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<typename T> 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<typename T> 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<typename T> 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<typename T> inline T piClamp(const T & v, const T & min, const T & max) {return (v > max ? max : (v < min ? min : v));}
#define piRoundf piRound<float>
@@ -231,10 +403,11 @@ template<typename T> inline T piClamp(const T & v, const T & min, const T & max)
#define piClampd piClamp<double>
extern bool isPIInit;
extern bool piDebug;
extern string ifconfigPath;
#define piCout if (piDebug) cout
//! global variable enabling output to piCout
extern bool piDebug;
extern string ifconfigPath;
class PIInit {
public:
@@ -245,10 +418,8 @@ public:
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGALRM);
if (pthread_sigmask(SIG_BLOCK, &ss, 0) == -1) {
//cout << "[PITimer] sigaction error: " << errorString() << endl;
//return 0;
}
sigprocmask(SIG_BLOCK, &ss, 0);
pthread_sigmask(SIG_BLOCK, &ss, 0);
ifconfigPath = "/bin/ifconfig";
if (!fileExists(ifconfigPath)) {
ifconfigPath = "/sbin/ifconfig";
@@ -305,8 +476,8 @@ inline double round(const double & v) {return floor(v + 0.5);}
#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 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
@@ -314,7 +485,7 @@ inline string itos(const int num) {
#else
sprintf_s(ch, 256, "%d", num);
#endif
return string(ch); };
return string(ch);}
inline string ltos(const long num) {
char ch[256];
#ifndef CC_VC
@@ -322,23 +493,23 @@ inline string ltos(const long num) {
#else
sprintf_s(ch, 256, "%ld", num);
#endif
return string(ch); };
return string(ch);}
inline string uitos(const uint num) {
char ch[256];
#ifndef CC_VC
sprintf(ch, "%ud", num);
sprintf(ch, "%u", num);
#else
sprintf_s(ch, 256, "%ud", num);
sprintf_s(ch, 256, "%u", num);
#endif
return string(ch); };
return string(ch);}
inline string ultos(const ulong num) {
char ch[256];
#ifndef CC_VC
sprintf(ch, "%lud", num);
sprintf(ch, "%lu", num);
#else
sprintf_s(ch, 256, "%lud", num);
sprintf_s(ch, 256, "%lu", num);
#endif
return string(ch); };
return string(ch);}
inline string ftos(const float num) {
char ch[256];
#ifndef CC_VC
@@ -346,7 +517,7 @@ inline string ftos(const float num) {
#else
sprintf_s(ch, 256, "%g", num);
#endif
return string(ch); };
return string(ch);}
inline string dtos(const double num) {
char ch[256];
#ifndef CC_VC
@@ -354,9 +525,11 @@ inline string dtos(const double num) {
#else
sprintf_s(ch, 256, "%g", num);
#endif
return string(ch); };
return string(ch);}
#ifdef CC_VC
/*! \fn errorString()
* \brief Return readable error description in format "code <number> - <description>" */
#ifdef WINDOWS
inline string errorString() {
char * msg;
int err = GetLastError();
@@ -368,6 +541,259 @@ 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<typename Enum>
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<PICoutControl> PICoutControls;
class PIP_EXPORT PICout {
public:
//! Default constructor with default features
PICout(PIFlags<PICoutControl> controls = AddSpaces | AddNewLine);
PICout(const PICout & other): fo_(other.fo_), cc_(true), co_(other.co_) {;}
~PICout();
//! Output operator for strings with <tt>"const char * "</tt> type
PICout operator <<(const char * v) {space(); quote(); std::cout << v; quote(); return *this;}
//! Output operator for strings with <tt>"std::string"</tt> 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 <tt>"char"</tt> values
PICout operator <<(const char v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"unsigned char"</tt> values
PICout operator <<(const uchar v) {space(); std::cout << ushort(v); return *this;}
//! Output operator for <tt>"short"</tt> values
PICout operator <<(const short v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"unsigned short"</tt> values
PICout operator <<(const ushort v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"int"</tt> values
PICout operator <<(const int v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"unsigned int"</tt> values
PICout operator <<(const uint v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"long"</tt> values
PICout operator <<(const long v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"unsigned long"</tt> values
PICout operator <<(const ulong v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"long long"</tt> values
PICout operator <<(const llong v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"unsigned long long"</tt> values
PICout operator <<(const ullong v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"float"</tt> values
PICout operator <<(const float v) {space(); std::cout << v; return *this;}
//! Output operator for <tt>"double"</tt> 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<PICoutControls> cos_;
};
#endif // PIINCLUDES_H