PageRenderTime 73ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/GoIO_SDK/src/GoIO_cpp/GUtils.h

https://github.com/concord-consortium/goio-jna
C Header | 259 lines | 157 code | 57 blank | 45 comment | 6 complexity | 4277f066f0428c3ac91155869b49af06 MD5 | raw file
  1. // GUtils.h
  2. //
  3. // GUtils is a collection of static utility methods designed to wrap
  4. // common (but platform- or compiler-dependent) facilities.
  5. #ifndef _GUTILS_H_
  6. #define _GUTILS_H_
  7. #include "GTypes.h"
  8. // REVISIT - factor out this module...
  9. // GUtils should be the absolute core debug, assert, and trace utils,
  10. // for all source development (including DDK).
  11. // Message-box stuff should be in a separate module, as should
  12. // numerical utilities.
  13. #define GSTD_ASSERT(x) GUtils::Assert((x),WIDENMACRO(__FILE__),__LINE__)
  14. #define GSTD_TRACE(x) GUtils::Trace((x),WIDENMACRO(__FILE__),__LINE__)
  15. // macros for converting __FILE__ to wide
  16. #define WIDENMACRO(macro) GSTD_S(macro) // widens a macro
  17. #define WIDEN(x) GTextUtils::ConvertNarrowStringToWide(x)
  18. #ifdef TARGET_OS_MAC
  19. #if TARGET_CPU_X86
  20. #include <cmath>
  21. #else
  22. #include <math.h>
  23. extern long __isnand( double x );
  24. extern long __isfinited( double x );
  25. #endif
  26. #endif
  27. class GFileRef;
  28. class GUtils
  29. {
  30. public:
  31. // Debugging / diagnostic utilities
  32. static bool Assert(bool bArgument, const gchar * sFile = GSTD_S(""), int nLine = -1); // ASSERTS if argument is false (also returns argument)
  33. static void AssertDialog(const gchar * sFile, int nLine, const cppstring &sStackTrace = GSTD_S("")); // Cross platform error dialog
  34. static void Trace(cppstring msg, gchar * psFile = NULL, int nLine = -1); // output a string to trace output
  35. static void Trace(gchar * pMsg, gchar * psFile = NULL, int nLine = -1); // output a null-terminated string to trace output
  36. static void Trace(void * pointer, gchar * psFile = NULL, int nLine = -1); // output a null-terminated string to trace output
  37. static void OSTrace(const gchar * pMsg); // output a null-terminated string to trace output
  38. static unsigned long TraceClock(void); // output the result of std::clock() to trace output; returns clock time
  39. static void OSFloatingPointExceptionReset(void);
  40. static void Beep(void); // Your basic beep.
  41. static void MessageBox(const cppstring &msg); // Put up a message box showing passed-in cppstring
  42. static bool YesNo(const cppstring &msg); // Put up a Yes/No message box (showing sppstring) -- return true if user chooses Yes
  43. static bool OKCancel(const cppstring &msg); // Put up an OK/Cancel message box (showing cppstring) -- return true if user chooses OK
  44. static EYesNoCancel YesNoCancel(const cppstring &msg); // Put up a Yes/No/Cancel message box (showing cppstring) -- return enum EYesNoCancel
  45. static int CustomMessageBox(cppstring sTitle, cppstring msg, StringVector * pvButtonTitles, int nDefaultButton = 0); // list of buttons, in order, default button first
  46. static void Sleep(unsigned long msToSleep) { OSSleep(msToSleep); }
  47. static void OSSleep(unsigned long msToSleep);
  48. static bool IsBetween(real range1, real value, real range2); // RETURN true if value is between the ranges (inclusive)
  49. // Exception handling
  50. static void OSDeleteExceptionPtr(OSMemoryExceptionPtr p); // Delete the OS-specific memory execption
  51. static void OSDeleteExceptionPtr(OSFileExceptionPtr p); // Delete the OS-specific file execption
  52. static cppstring OSGetExceptionErrorString(OSMemoryExceptionPtr p); // return a cppstring of the error in the passed-in memory exception
  53. static cppstring OSGetExceptionErrorString(OSFileExceptionPtr p); // return a cppstring of the error in the passed-in file exception
  54. // static StdIDVector ResolveIDVector(StdIDVector *pvIDs); // RETURN a StdIDVector based on passed-in vector
  55. // static StdIDVector ConvertIDListToVector(const StdIDList * pIDList); // RETURN a StdIDVector based on a passed-in StdIDList
  56. // static StdIDList ConvertIDVectorToList(const StdIDVector * pIDList); // RETURN a StdIDList based on a passed-in StdIDVector
  57. // Date and time
  58. static cppstring OSGetBuildDateTimeText(void); // RETURN the current date and time this module was compiled as a cppstring
  59. static cppstring GetCurrentStdDateTimeText(void); // uses standard C facilities
  60. static cppstring GetCurrentStdTimeText(void); // uses standard C facilities
  61. static real OSGetSystemClockTime(void); // return time since system startup in seconds, with at least ms resolution
  62. static unsigned int OSGetTimeStamp(void); // RETURN a time stamp (in milliseconds)
  63. // application specific strings
  64. static cppstring GetApplicationString(const cppstring & sKey);
  65. static cppstring GetApplicationName(void);
  66. static cppstring GetApplicationISBN(void);
  67. static cppstring GetNativeFileExtension(void);
  68. static cppstring GetNativeFileFilter(bool bOpen);
  69. // System Info
  70. static cppstring OSGetUserName(void);
  71. static bool OSIsQuickTimeAvailable(void);
  72. static bool OSAllowThreadedFirmwareUpdate(void);
  73. // Default Folder
  74. static void OSSetDefaultFolder(const GFileRef & theFolderRef);
  75. // App Error
  76. static void OSAssertDialog(SAppError * pAppError);
  77. // Unique ID
  78. static cppstring GetUniqueIDString(void); // RETURN a unique string for an ID
  79. // File IO
  80. static cppstring OSChooseTextFileForImport(void);
  81. // Checksum:
  82. static short MakeDataChecksum(unsigned char * pBuffer, unsigned short nSize);
  83. // Byte order
  84. static bool OSConvertShortToBytes(short nInNum, unsigned char * pLSB, unsigned char * pMSB);
  85. static bool OSConvertBytesToShort(char chLSB, char chMSB, short * pOutShort);
  86. static bool OSConvertIntToBytes(int nInNum,
  87. unsigned char * pLSB,
  88. unsigned char * pLMidB,
  89. unsigned char * pMMidB,
  90. unsigned char * pMSB);
  91. static bool OSConvertBytesToInt(unsigned char chLSB,
  92. unsigned char chLMidB,
  93. unsigned char chMMidB,
  94. unsigned char chMSB,
  95. int * pOutInt);
  96. static bool OSConvertFloatToBytes(float fInNum,
  97. unsigned char * pLSB,
  98. unsigned char * pLMidB,
  99. unsigned char * pMMidB,
  100. unsigned char * pMSB);
  101. static bool OSConvertBytesToFloat(unsigned char chLSB,
  102. unsigned char chLMidB,
  103. unsigned char chMMidB,
  104. unsigned char chMSB,
  105. float * pOutFloat);
  106. static int OSConvertIntelIntToPlatformInt(int nSourceInt);
  107. static int OSConvertMacIntToPlatformInt(int nSourceInt);
  108. static bool OSIsMainThread(void);
  109. // ID Vectors and list back and forth to/from strings
  110. // static cppstring ConvertIDVectorToString(const StdIDVector &idVect);
  111. // static StdIDVector ConvertStringToIDVector(const cppstring &sVect);
  112. // static cppstring ConvertIDListToString(const StdIDList &idList);
  113. // static StdIDList ConvertStringToIDList(const cppstring &sList);
  114. // static cppstring ConvertIDPairsToString(const SStdIDPairVector &vIDPairs);
  115. // static SStdIDPairVector ConvertStringToIDPairs(const cppstring &sVect);
  116. //
  117. // static StdIDVector GetUniqueIDs(const StdIDVector &vIDs);
  118. // static StringVector GetUniqueStrings(const StringVector &vs);
  119. // static StringVector GetNewStrings(const StringVector &oldVec, const StringVector &newVec);
  120. // static StringVector GetOldStrings(const StringVector &oldVec, const StringVector &newVec);
  121. // static StdIDList GetPrunedIDList(StdIDList theList);
  122. // static StdIDVector GetPrunedIDVector(StdIDVector *pvIDs);
  123. static realvector ConvertStringToRealVector(const cppstring & sText);
  124. static cppstring ConvertRealVectorToString(const realvector & vfVals);
  125. static intVector ConvertStringToIntVector(const cppstring & sText);
  126. static cppstring ConvertIntVectorToString(const intVector & vnVals);
  127. static cppstring ConvertBytesToString(unsigned char *pBytes, int nNumBytes);
  128. // Keyboard event conversion
  129. static EKeyboardEvent OSTranslateKeyboardEvent(void *pData);
  130. // Curve Fitting functions and helper functions.
  131. static long CalculateLinearFit(SRealPointVector const &vPoints, real *pSlope, real *pIintercept, real *pCorrelation = NULL, real *pfStdDevSlope = NULL, real *pfStdDevIntercept = NULL); // RETURN kResponse_OK or IDSX error string ID
  132. static long CalculateLinearFit(realvector const &vX, realvector const &vY, real *pSlope, real *pIintercept, real *pCorrelation = NULL, real *pfStdDevSlope = NULL, real *pfStdDevIntercept = NULL); // RETURN kResponse_OK or IDSX error string ID
  133. static bool IsMonotonic(SRealPointVector const &vPoints); // RETURN true if x component of points is monotonic (not both increasing and decreasing)
  134. static bool IsMonotonic(realvector const &vReal); // RETURN true if points are monotonic
  135. static int GetDecimalPlacesUsed(real fValue); // RETURN the number of places that are not 0
  136. static size_t CalculateNumberOfDivisions(real fStart, real fEnd, real fIncrement); // RETURN number of increments to use
  137. #ifdef TARGET_OS_WIN
  138. static bool OSIsValidNumber(real fVal) { return (!_isnan(fVal) && _finite(fVal)); } // RETURN true if fVal is OK
  139. #endif
  140. #ifdef TARGET_OS_MAC
  141. #if TARGET_CPU_X86
  142. static bool OSIsValidNumber(real fVal) { return (!std::isnan(fVal) && std::isfinite(fVal)); } // RETURN true if fVal is OK
  143. #else
  144. static bool OSIsValidNumber(real fVal) { return (!__isnand(fVal) && __isfinited(fVal)); } // RETURN true if fVal is OK
  145. #endif
  146. #endif
  147. #ifdef TARGET_OS_LINUX
  148. static bool OSIsValidNumber(real fVal) { return (!isnan(fVal) && isfinite(fVal)); } // RETURN true if fVal is OK
  149. #endif
  150. static real kRealMin;
  151. static real kRealMax;
  152. static real kfMetersPerFoot;
  153. static real RestrictReal(real fValue); // return fValue restricted to the range defined by kRealMin and kRealMax
  154. static bool RealEquivalence(real fTolerance, real fValue1, real fValue2); // returns true if to real as equivlent withint the specificed tolerance
  155. static cppstring GetStringFromTimeUnit(ETimeUnit eTimeUnit, bool bAbbreviation = true, bool bPlural = true);
  156. static StringVector GetTimeUnitStrings(bool bAbbreviation = true, bool bPlural = true);
  157. static ETimeUnit GetTimeUnitFromString(const cppstring & sUnit, bool bStrict = false);
  158. static SColumnHeader GetTimeColumnHeader(ETimeUnit eTime);
  159. static real ConvertTimeToSeconds(real fTime, ETimeUnit eTimeUnit);
  160. static real ConvertSecondsToTime(real fTime, ETimeUnit eTimeUnit);
  161. static real ConvertRateInSecondsToTime(real fRateInSeconds, ETimeUnit eTimeUnit);
  162. static real ConvertFeetToMeters(real fFeet) { return fFeet * 0.3048; }
  163. static real ConvertMetersToFeet(real fMeters) { return fMeters / 0.3048; }
  164. // static StdIDVector GetIDVectorIntersection(StdIDVector const &vIDs1, StdIDVector const &vIDs2); // RETURN the IDs that are in both v1 and v2
  165. static cppstring ToLower(const cppstring &s); // RETURN s, in lower case
  166. static narrowstring ToNarrowLower(const narrowstring &s); // RETURN s, in lower case
  167. // Logfile methods
  168. static cppofstream * pLogOStream;
  169. static bool IsLogOpen(void);
  170. static cppstring OpenLog(void);
  171. static void CloseLog(void);
  172. static void WriteToLog(cppstring sText, cppstring sFile, int nLine, cppstring sFunction);
  173. static void WriteAddressToLog(void * pointer, cppstring sFile, int nLine);
  174. // Used to calculate Live Readouts strings for the toolbar.
  175. static StringVector GenerateIdealStringVector(OSPtr pOSData, int nIdealSize, int *nActualSize, const StringVector & vsLongStrings, const StringVector & vsShortStrings,int nLongSize, int nShortSize);
  176. static int CalculateStringVectorSize(OSPtr pOSData, int nSeperatorSize, const StringVector & vsStrings);
  177. };
  178. // class to stop and then restart MBL related things (live readouts and initial sensor polling)
  179. class StStopMBLActivity
  180. {
  181. public:
  182. StStopMBLActivity(void);
  183. virtual ~StStopMBLActivity();
  184. private:
  185. bool m_bRestartLiveReadouts;
  186. };
  187. #ifdef TARGET_OS_WIN
  188. #ifdef _DEBUG
  189. #define OPUS_NEW DEBUG_NEW
  190. #else
  191. #define OPUS_NEW new
  192. #endif
  193. #endif
  194. // global allocation macro
  195. #define GSTD_NEW(pointer, cast, constructor) pointer = cast OPUS_NEW constructor; // GSTD_LOGADDRESS(pointer); // uncomment to track memory allocations in the logfile
  196. #ifdef TARGET_OS_WIN
  197. #define GSTD_LOG(x) if (GUtils::IsLogOpen()) GUtils::WriteToLog((x), WIDENMACRO(__FILE__), __LINE__, GSTD_S("")); // No function macro on Windows. bummer.
  198. #else
  199. #define GSTD_LOG(x) if (GUtils::IsLogOpen()) GUtils::WriteToLog((x), WIDENMACRO(__FILE__), __LINE__, WIDENMACRO(__FUNCTION__));
  200. #endif
  201. #define GSTD_LOGADDRESS(x)if (GUtils::IsLogOpen()) GUtils::WriteAddressToLog((x), WIDENMACRO(__FILE__), __LINE__);
  202. #endif // _GUTILS_H_