VC2010 compile fixes
[novacoin.git] / src / util.h
index 31d3275..0af5b0a 100644 (file)
@@ -11,6 +11,8 @@
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/resource.h>
+#else
+typedef int pid_t; /* define for windows compatiblity */
 #endif
 #include <map>
 #include <vector>
@@ -85,10 +87,11 @@ T* alignup(T* p)
 #define strlwr(psz)         to_lower(psz)
 #define _strlwr(psz)        to_lower(psz)
 #define MAX_PATH            1024
-#define Beep(n1,n2)         (0)
 inline void Sleep(int64 n)
 {
-    boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n));
+    /*Boost has a year 2038 problem— if the request sleep time is past epoch+2^31 seconds the sleep returns instantly.
+      So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/
+    boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n));
 }
 #endif
 
@@ -127,8 +130,15 @@ void RandAddSeed();
 void RandAddSeedPerfmon();
 int OutputDebugStringF(const char* pszFormat, ...);
 int my_snprintf(char* buffer, size_t limit, const char* format, ...);
-std::string strprintf(const std::string &format, ...);
-bool error(const std::string &format, ...);
+
+/* It is not allowed to use va_start with a pass-by-reference argument.
+   (C++ standard, 18.7, paragraph 3). Use a dummy argument to work around this, and use a
+   macro to keep similar semantics.
+*/
+std::string real_strprintf(const std::string &format, int dummy, ...);
+#define strprintf(format, ...) real_strprintf(format, 0, __VA_ARGS__)
+
+bool error(const char *format, ...);
 void LogException(std::exception* pex, const char* pszThread);
 void PrintException(std::exception* pex, const char* pszThread);
 void PrintExceptionContinue(std::exception* pex, const char* pszThread);
@@ -178,7 +188,7 @@ void AddTimeData(const CNetAddr& ip, int64 nTime);
 
 
 
-// Wrapper to automatically initialize mutex
+/** Wrapper to automatically initialize mutex. */
 class CCriticalSection
 {
 protected:
@@ -191,7 +201,7 @@ public:
     bool TryEnter(const char* pszName, const char* pszFile, int nLine);
 };
 
-// Automatically leave critical section when leaving block, needed for exception safety
+/** RAII object that acquires mutex. Needed for exception safety. */
 class CCriticalBlock
 {
 protected:
@@ -224,6 +234,7 @@ public:
 #define LEAVE_CRITICAL_SECTION(cs) \
     (cs).Leave()
 
+/** RAII object that tries to acquire mutex. Needed for exception safety. */
 class CTryCriticalBlock
 {
 protected:
@@ -334,25 +345,6 @@ inline std::string HexStr(const std::vector<unsigned char>& vch, bool fSpaces=fa
 }
 
 template<typename T>
-std::string HexNumStr(const T itbegin, const T itend, bool f0x=true)
-{
-    if (itbegin == itend)
-        return "";
-    const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
-    const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
-    std::string str = (f0x ? "0x" : "");
-    str.reserve(str.size() + (pend-pbegin) * 2);
-    for (const unsigned char* p = pend-1; p >= pbegin; p--)
-        str += strprintf("%02x", *p);
-    return str;
-}
-
-inline std::string HexNumStr(const std::vector<unsigned char>& vch, bool f0x=true)
-{
-    return HexNumStr(vch.begin(), vch.end(), f0x);
-}
-
-template<typename T>
 void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSpaces=true)
 {
     printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str());
@@ -473,21 +465,6 @@ bool SoftSetBoolArg(const std::string& strArg, bool fValue);
         }                                       \
     }
 
-#define CATCH_PRINT_EXCEPTION(pszFn)     \
-    catch (std::exception& e) {          \
-        PrintException(&e, (pszFn));     \
-    } catch (...) {                      \
-        PrintException(NULL, (pszFn));   \
-    }
-
-
-
-
-
-
-
-
-
 
 template<typename T1>
 inline uint256 Hash(const T1 pbegin, const T1 pend)
@@ -556,8 +533,9 @@ inline uint160 Hash160(const std::vector<unsigned char>& vch)
 }
 
 
-// Median filter over a stream of values
-// Returns the median of the last N numbers
+/** Median filter over a stream of values. 
+ * Returns the median of the last N numbers
+ */
 template <typename T> class CMedianFilter
 {
 private:
@@ -687,11 +665,6 @@ inline void SetThreadPriority(int nPriority)
 #endif
 }
 
-inline bool TerminateThread(pthread_t hthread, unsigned int nExitCode)
-{
-    return (pthread_cancel(hthread) == 0);
-}
-
 inline void ExitThread(size_t nExitCode)
 {
     pthread_exit((void*)nExitCode);