2019-12-19 03:24:12 +00:00
|
|
|
#ifndef cwCommonImpl_H
|
|
|
|
#define cwCommonImpl_H
|
|
|
|
|
2020-03-24 12:51:51 +00:00
|
|
|
#include "config.h"
|
2019-12-19 03:24:12 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cctype>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <climits>
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <cfloat>
|
2019-12-24 15:05:24 +00:00
|
|
|
#include <cmath>
|
2019-12-19 03:24:12 +00:00
|
|
|
#include <algorithm> // std::min,std::max
|
2019-12-24 15:05:24 +00:00
|
|
|
#include <utility> // std::forward
|
|
|
|
#include <limits> // std::numeric_limits<
|
|
|
|
#include <atomic>
|
2019-12-19 03:24:12 +00:00
|
|
|
|
2020-03-24 12:51:51 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_OSX)
|
2019-12-19 03:24:12 +00:00
|
|
|
#define cwPOSIX_FILE_SYS
|
2019-12-27 21:52:45 +00:00
|
|
|
#include <time.h> // timespec
|
|
|
|
#include <netinet/in.h> // struct sockaddr_in
|
2019-12-19 03:24:12 +00:00
|
|
|
#define cwPathSeparatorChar '/'
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define cwStringNullGuard(s) ((s)==nullptr ? "" : (s))
|
|
|
|
|
|
|
|
|
|
|
|
#define cwAllFlags(f,m) (((f) & (m)) == (m)) // Test if all of a group 'm' of binary flags in 'f' are set.
|
|
|
|
#define cwIsFlag(f,m) (((f) & (m)) ? true : false) // Test if any one of a the bits in 'm' is also set in 'f'.
|
|
|
|
#define cwIsNotFlag(f,m) (cwIsFlag(f,m)==false) // Test if none of the bits in 'm' are set in 'f'.
|
|
|
|
#define cwSetFlag(f,m) ((f) | (m)) // Return 'f' with the bits in 'm' set.
|
|
|
|
#define cwClrFlag(f,m) ((f) & (~(m))) // Return 'f' with the bits in 'm' cleared.
|
|
|
|
#define cwTogFlag(f,m) ((f)^(m)) // Return 'f' with the bits in 'm' toggled.
|
|
|
|
#define cwEnaFlag(f,m,b) ((b) ? cwSetFlag(f,m) : cwClrFlag(f,m)) // Set or clear bits in 'f' based on bits in 'm' and the state of 'b'.
|
|
|
|
|
|
|
|
// In-place assignment version of the above bit operations
|
|
|
|
#define cwSetBits(f,m) ((f) |= (m)) // Set 'f' with the bits in 'm' set.
|
|
|
|
#define cwClrBits(f,m) ((f) &= (~(m))) // Set 'f' with the bits in 'm' cleared.
|
|
|
|
#define cwTogBits(f,m) ((f)^=(m)) // Return 'f' with the bits in 'm' toggled.
|
|
|
|
#define cwEnaBits(f,m,b) ((b) ? cwSetBits(f,m) : cwClrBits(f,m)) // Set or clear bits in 'f' based on bits in 'm' and the state of 'b'.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace cw
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
#define cwAssert(cond) while(1){ if(!(cond)){ cwLogFatal(kAssertFailRC,"Assert failed on condition:%s",#cond ); } break; }
|
|
|
|
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
template< typename H, typename T >
|
|
|
|
T* handleToPtr( H h )
|
2019-12-19 03:24:12 +00:00
|
|
|
{
|
|
|
|
cwAssert( h.p != nullptr );
|
|
|
|
return h.p;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct idLabelPair_str
|
|
|
|
{
|
|
|
|
unsigned id;
|
|
|
|
const char* label;
|
|
|
|
} idLabelPair_t;
|
|
|
|
|
|
|
|
const char* idToLabel( const idLabelPair_t* array, unsigned id, unsigned eolId );
|
|
|
|
unsigned labelToId( const idLabelPair_t* array, const char* label, unsigned eolId );
|
|
|
|
|
2019-12-26 02:44:14 +00:00
|
|
|
|
|
|
|
inline rc_t rcSelect() { return kOkRC; }
|
|
|
|
|
|
|
|
template<typename T, typename... ARGS>
|
|
|
|
rc_t rcSelect(T rc, ARGS... args)
|
|
|
|
{
|
|
|
|
if( rc != kOkRC )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return rcSelect(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-19 03:24:12 +00:00
|
|
|
void sleepSec( unsigned secs ); // sleep seconds
|
|
|
|
void sleepMs( unsigned ms ); // sleep milliseconds
|
|
|
|
void sleepUs( unsigned us ); // sleep seconds
|
|
|
|
void sleepNs( unsigned ns ); // sleep nanoseconds
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|