diff options
author | Brandon Dahler <brandon.dahler@gmail.com> | 2013-04-13 00:13:08 -0500 |
---|---|---|
committer | Brandon Dahler <brandon.dahler@gmail.com> | 2013-11-10 09:36:28 -0600 |
commit | 51ed9ec971614aebdbfbd9527aba365dd0afd437 (patch) | |
tree | d2f910390e55aef857023812fbdaefdd66cd99ff /src/util.h | |
parent | 7c4c207be8420d394a5abc4368d1bb69ad4f8067 (diff) |
Cleanup code using forward declarations.
Use misc methods of avoiding unnecesary header includes.
Replace int typedefs with int##_t from stdint.h.
Replace PRI64[xdu] with PRI[xdu]64 from inttypes.h.
Normalize QT_VERSION ifs where possible.
Resolve some indirect dependencies as direct ones.
Remove extern declarations from .cpp files.
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 108 |
1 files changed, 51 insertions, 57 deletions
diff --git a/src/util.h b/src/util.h index 2573694046..e52e6986b5 100644 --- a/src/util.h +++ b/src/util.h @@ -2,38 +2,43 @@ // Copyright (c) 2009-2013 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. + #ifndef BITCOIN_UTIL_H #define BITCOIN_UTIL_H -#include "uint256.h" +#if defined(HAVE_CONFIG_H) +#include "bitcoin-config.h" +#endif + +#include "compat.h" +#include "serialize.h" +#include <cstdio> +#include <exception> +#include <inttypes.h> +#include <map> #include <stdarg.h> +#include <stdint.h> +#include <string> +#include <utility> +#include <vector> #ifndef WIN32 -#include <sys/types.h> -#include <sys/time.h> #include <sys/resource.h> +#include <sys/time.h> +#include <sys/types.h> +#else +typedef int pid_t; /* define for Windows compatibility */ #endif -#include <map> -#include <list> -#include <utility> -#include <vector> -#include <string> -#include <boost/version.hpp> -#include <boost/thread.hpp> -#include <boost/filesystem.hpp> #include <boost/filesystem/path.hpp> -#include <boost/date_time/gregorian/gregorian_types.hpp> -#include <boost/date_time/posix_time/posix_time_types.hpp> - -#include "netbase.h" // for AddTimeData +#include <boost/thread.hpp> -typedef long long int64; -typedef unsigned long long uint64; +class CNetAddr; +class uint256; -static const int64 COIN = 100000000; -static const int64 CENT = 1000000; +static const int64_t COIN = 100000000; +static const int64_t CENT = 1000000; #define BEGIN(a) ((char*)&(a)) #define END(a) ((char*)&((&(a))[1])) @@ -41,18 +46,6 @@ static const int64 CENT = 1000000; #define UEND(a) ((unsigned char*)&((&(a))[1])) #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0])) -#ifndef PRI64d -#if defined(_MSC_VER) || defined(__MSVCRT__) -#define PRI64d "I64d" -#define PRI64u "I64u" -#define PRI64x "I64x" -#else -#define PRI64d "lld" -#define PRI64u "llu" -#define PRI64x "llx" -#endif -#endif - /* Format characters for (s)size_t and ptrdiff_t */ #if defined(_MSC_VER) || defined(__MSVCRT__) /* (s)size_t and ptrdiff_t have the same size specifier in MSVC: @@ -73,6 +66,7 @@ static const int64 CENT = 1000000; #define PRIpdd "td" #endif + // This is needed because the foreach macro can't get over the comma in pair<t1, t2> #define PAIRTYPE(t1, t2) std::pair<t1, t2> @@ -101,11 +95,11 @@ T* alignup(T* p) #define MAX_PATH 1024 #endif // As Solaris does not have the MSG_NOSIGNAL flag for send(2) syscall, it is defined as 0 -#ifndef MSG_NOSIGNAL +#if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL) #define MSG_NOSIGNAL 0 #endif -inline void MilliSleep(int64 n) +inline void MilliSleep(int64_t n) { // Boost's sleep_for was uninterruptable when backed by nanosleep from 1.50 // until fixed in 1.52. Use the deprecated sleep method for the broken case. @@ -178,9 +172,9 @@ void LogException(std::exception* pex, const char* pszThread); void PrintException(std::exception* pex, const char* pszThread); void PrintExceptionContinue(std::exception* pex, const char* pszThread); void ParseString(const std::string& str, char c, std::vector<std::string>& v); -std::string FormatMoney(int64 n, bool fPlus=false); -bool ParseMoney(const std::string& str, int64& nRet); -bool ParseMoney(const char* pszIn, int64& nRet); +std::string FormatMoney(int64_t n, bool fPlus=false); +bool ParseMoney(const std::string& str, int64_t& nRet); +bool ParseMoney(const char* pszIn, int64_t& nRet); std::vector<unsigned char> ParseHex(const char* psz); std::vector<unsigned char> ParseHex(const std::string& str); bool IsHex(const std::string& str); @@ -215,15 +209,15 @@ boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true); boost::filesystem::path GetTempPath(); void ShrinkDebugFile(); int GetRandInt(int nMax); -uint64 GetRand(uint64 nMax); +uint64_t GetRand(uint64_t nMax); uint256 GetRandHash(); -int64 GetTime(); -void SetMockTime(int64 nMockTimeIn); -int64 GetAdjustedTime(); -int64 GetTimeOffset(); +int64_t GetTime(); +void SetMockTime(int64_t nMockTimeIn); +int64_t GetAdjustedTime(); +int64_t GetTimeOffset(); std::string FormatFullVersion(); std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments); -void AddTimeData(const CNetAddr& ip, int64 nTime); +void AddTimeData(const CNetAddr& ip, int64_t nTime); void runCommand(std::string strCommand); @@ -234,9 +228,9 @@ void runCommand(std::string strCommand); -inline std::string i64tostr(int64 n) +inline std::string i64tostr(int64_t n) { - return strprintf("%"PRI64d, n); + return strprintf("%"PRId64, n); } inline std::string itostr(int n) @@ -244,7 +238,7 @@ inline std::string itostr(int n) return strprintf("%d", n); } -inline int64 atoi64(const char* psz) +inline int64_t atoi64(const char* psz) { #ifdef _MSC_VER return _atoi64(psz); @@ -253,7 +247,7 @@ inline int64 atoi64(const char* psz) #endif } -inline int64 atoi64(const std::string& str) +inline int64_t atoi64(const std::string& str) { #ifdef _MSC_VER return _atoi64(str.c_str()); @@ -272,12 +266,12 @@ inline int roundint(double d) return (int)(d > 0 ? d + 0.5 : d - 0.5); } -inline int64 roundint64(double d) +inline int64_t roundint64(double d) { - return (int64)(d > 0 ? d + 0.5 : d - 0.5); + return (int64_t)(d > 0 ? d + 0.5 : d - 0.5); } -inline int64 abs64(int64 n) +inline int64_t abs64(int64_t n) { return (n >= 0 ? n : -n); } @@ -318,32 +312,32 @@ inline void PrintHex(const std::vector<unsigned char>& vch, const char* pszForma LogPrintf(pszFormat, HexStr(vch, fSpaces).c_str()); } -inline int64 GetPerformanceCounter() +inline int64_t GetPerformanceCounter() { - int64 nCounter = 0; + int64_t nCounter = 0; #ifdef WIN32 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter); #else timeval t; gettimeofday(&t, NULL); - nCounter = (int64) t.tv_sec * 1000000 + t.tv_usec; + nCounter = (int64_t) t.tv_sec * 1000000 + t.tv_usec; #endif return nCounter; } -inline int64 GetTimeMillis() +inline int64_t GetTimeMillis() { return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) - boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds(); } -inline int64 GetTimeMicros() +inline int64_t GetTimeMicros() { return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) - boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds(); } -inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime) +inline std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime) { time_t n = nTime; struct tm* ptmTime = gmtime(&n); @@ -384,7 +378,7 @@ std::string GetArg(const std::string& strArg, const std::string& strDefault); * @param default (e.g. 1) * @return command-line argument (0 if invalid number) or default value */ -int64 GetArg(const std::string& strArg, int64 nDefault); +int64_t GetArg(const std::string& strArg, int64_t nDefault); /** * Return boolean argument or default value @@ -549,7 +543,7 @@ inline uint32_t ByteReverse(uint32_t value) // or maybe: // boost::function<void()> f = boost::bind(&FunctionWithArg, argument); // threadGroup.create_thread(boost::bind(&LoopForever<boost::function<void()> >, "nothing", f, milliseconds)); -template <typename Callable> void LoopForever(const char* name, Callable func, int64 msecs) +template <typename Callable> void LoopForever(const char* name, Callable func, int64_t msecs) { std::string s = strprintf("bitcoin-%s", name); RenameThread(s.c_str()); |