diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 7 | ||||
-rw-r--r-- | src/common/common.h | 6 | ||||
-rw-r--r-- | src/common/common_types.h | 2 | ||||
-rw-r--r-- | src/common/console_listener.cpp | 16 | ||||
-rw-r--r-- | src/common/emu_window.cpp | 17 | ||||
-rw-r--r-- | src/common/emu_window.h | 8 | ||||
-rw-r--r-- | src/common/extended_trace.cpp | 2 | ||||
-rw-r--r-- | src/common/file_search.cpp | 10 | ||||
-rw-r--r-- | src/common/file_util.cpp | 33 | ||||
-rw-r--r-- | src/common/file_util.h | 2 | ||||
-rw-r--r-- | src/common/fixed_size_queue.h | 70 | ||||
-rw-r--r-- | src/common/key_map.cpp | 25 | ||||
-rw-r--r-- | src/common/key_map.h | 45 | ||||
-rw-r--r-- | src/common/log_manager.cpp | 3 | ||||
-rw-r--r-- | src/common/log_manager.h | 2 | ||||
-rw-r--r-- | src/common/math_util.cpp | 3 | ||||
-rw-r--r-- | src/common/mem_arena.cpp | 6 | ||||
-rw-r--r-- | src/common/memory_util.cpp | 5 | ||||
-rw-r--r-- | src/common/msg_handler.cpp | 2 | ||||
-rw-r--r-- | src/common/std_condition_variable.h | 168 | ||||
-rw-r--r-- | src/common/std_mutex.h | 362 | ||||
-rw-r--r-- | src/common/std_thread.h | 314 | ||||
-rw-r--r-- | src/common/string_util.cpp | 35 | ||||
-rw-r--r-- | src/common/string_util.h | 8 | ||||
-rw-r--r-- | src/common/thread.cpp | 7 | ||||
-rw-r--r-- | src/common/thread.h | 7 | ||||
-rw-r--r-- | src/common/utf8.cpp | 4 |
27 files changed, 159 insertions, 1010 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 55a5f9eba..9d5a90762 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -4,10 +4,12 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scm_rev.cpp.in" "${CMAKE_CURRENT_SOU set(SRCS break_points.cpp console_listener.cpp + emu_window.cpp extended_trace.cpp file_search.cpp file_util.cpp hash.cpp + key_map.cpp log_manager.cpp math_util.cpp mem_arena.cpp @@ -38,8 +40,8 @@ set(HEADERS fifo_queue.h file_search.h file_util.h - fixed_size_queue.h hash.h + key_map.h linear_disk_cache.h log.h log_manager.h @@ -49,9 +51,6 @@ set(HEADERS msg_handler.h platform.h scm_rev.h - std_condition_variable.h - std_mutex.h - std_thread.h string_util.h swap.h symbols.h diff --git a/src/common/common.h b/src/common/common.h index cb69eabe4..9f3016d34 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -20,11 +20,6 @@ #define STACKALIGN -#if __cplusplus >= 201103L || defined(_MSC_VER) || defined(__GXX_EXPERIMENTAL_CXX0X__) -#define HAVE_CXX11_SYNTAX 1 -#endif - -#if HAVE_CXX11_SYNTAX // An inheritable class to disallow the copy constructor and operator= functions class NonCopyable { @@ -36,7 +31,6 @@ private: NonCopyable(NonCopyable&); NonCopyable& operator=(NonCopyable& other); }; -#endif #include "common/log.h" #include "common/common_types.h" diff --git a/src/common/common_types.h b/src/common/common_types.h index 9d41e5971..00fde828d 100644 --- a/src/common/common_types.h +++ b/src/common/common_types.h @@ -100,7 +100,7 @@ union t128 { __m128 a; ///< 128-bit floating point (__m128 maps to the XMM[0-7] registers) }; -namespace common { +namespace Common { /// Rectangle data structure class Rect { public: diff --git a/src/common/console_listener.cpp b/src/common/console_listener.cpp index 27697ef1f..40122224c 100644 --- a/src/common/console_listener.cpp +++ b/src/common/console_listener.cpp @@ -3,14 +3,10 @@ // Refer to the license.txt file included. #include <algorithm> -#include <cmath> -#include <cstdio> -#include <string> + #ifdef _WIN32 #include <windows.h> #include <array> -#else -#include <cstdarg> #endif #include "common/common.h" @@ -47,7 +43,7 @@ void ConsoleListener::Open(bool Hidden, int Width, int Height, const char *Title // Save the window handle that AllocConsole() created hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Set the console window title - SetConsoleTitle(UTF8ToTStr(Title).c_str()); + SetConsoleTitle(Common::UTF8ToTStr(Title).c_str()); // Set letter space LetterSpace(80, 4000); //MoveWindow(GetConsoleWindow(), 200,200, 800,800, true); @@ -193,11 +189,11 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool { Str.resize(Str.size() + 1); if (!ReadConsoleOutputCharacter(hConsole, Str.back().data(), ReadBufferSize, coordScreen, &cCharsRead)) - SLog += StringFromFormat("WriteConsoleOutputCharacter error"); + SLog += Common::StringFromFormat("WriteConsoleOutputCharacter error"); Attr.resize(Attr.size() + 1); if (!ReadConsoleOutputAttribute(hConsole, Attr.back().data(), ReadBufferSize, coordScreen, &cAttrRead)) - SLog += StringFromFormat("WriteConsoleOutputAttribute error"); + SLog += Common::StringFromFormat("WriteConsoleOutputAttribute error"); // Break on error if (cAttrRead == 0) break; @@ -223,9 +219,9 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool for (size_t i = 0; i < Attr.size(); i++) { if (!WriteConsoleOutputCharacter(hConsole, Str[i].data(), ReadBufferSize, coordScreen, &cCharsWritten)) - SLog += StringFromFormat("WriteConsoleOutputCharacter error"); + SLog += Common::StringFromFormat("WriteConsoleOutputCharacter error"); if (!WriteConsoleOutputAttribute(hConsole, Attr[i].data(), ReadBufferSize, coordScreen, &cAttrWritten)) - SLog += StringFromFormat("WriteConsoleOutputAttribute error"); + SLog += Common::StringFromFormat("WriteConsoleOutputAttribute error"); BytesWritten += cAttrWritten; coordScreen = GetCoordinates(BytesWritten, LBufWidth); diff --git a/src/common/emu_window.cpp b/src/common/emu_window.cpp new file mode 100644 index 000000000..7a2c50ac8 --- /dev/null +++ b/src/common/emu_window.cpp @@ -0,0 +1,17 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "emu_window.h" + +void EmuWindow::KeyPressed(KeyMap::HostDeviceKey key) { + HID_User::PadState mapped_key = KeyMap::GetPadKey(key); + + HID_User::PadButtonPress(mapped_key); +} + +void EmuWindow::KeyReleased(KeyMap::HostDeviceKey key) { + HID_User::PadState mapped_key = KeyMap::GetPadKey(key); + + HID_User::PadButtonRelease(mapped_key); +} diff --git a/src/common/emu_window.h b/src/common/emu_window.h index 5e2c33d7a..23f178fdf 100644 --- a/src/common/emu_window.h +++ b/src/common/emu_window.h @@ -7,6 +7,8 @@ #include "common/common.h" #include "common/scm_rev.h" +#include "common/key_map.h" + // Abstraction class used to provide an interface between emulation code and the frontend (e.g. SDL, // QGLWidget, GLFW, etc...) class EmuWindow @@ -32,6 +34,12 @@ public: /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread virtual void DoneCurrent() = 0; + /// Signals a key press action to the HID module + static void KeyPressed(KeyMap::HostDeviceKey key); + + /// Signals a key release action to the HID module + static void KeyReleased(KeyMap::HostDeviceKey key); + Config GetConfig() const { return m_config; } diff --git a/src/common/extended_trace.cpp b/src/common/extended_trace.cpp index 66dae4935..9cd0398ed 100644 --- a/src/common/extended_trace.cpp +++ b/src/common/extended_trace.cpp @@ -278,7 +278,7 @@ void PrintFunctionAndSourceInfo(FILE* file, const STACKFRAME& callstack) GetFunctionInfoFromAddresses((ULONG)callstack.AddrPC.Offset, (ULONG)callstack.AddrFrame.Offset, symInfo); GetSourceInfoFromAddress((ULONG)callstack.AddrPC.Offset, srcInfo); - etfprint(file, " " + TStrToUTF8(srcInfo) + " : " + TStrToUTF8(symInfo) + "\n"); + etfprint(file, " " + Common::TStrToUTF8(srcInfo) + " : " + Common::TStrToUTF8(symInfo) + "\n"); } void StackTrace( HANDLE hThread, const char* lpszMessage, FILE *file ) diff --git a/src/common/file_search.cpp b/src/common/file_search.cpp index a9d19477d..63580f688 100644 --- a/src/common/file_search.cpp +++ b/src/common/file_search.cpp @@ -4,15 +4,13 @@ #include "common/common.h" -#include "common/common_paths.h" + #ifndef _WIN32 -#include <sys/types.h> #include <dirent.h> #else #include <windows.h> #endif -#include <string> #include <algorithm> #include "common/file_search.h" @@ -35,10 +33,10 @@ CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, cons void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath) { std::string GCMSearchPath; - BuildCompleteFilename(GCMSearchPath, _strPath, _searchString); + Common::BuildCompleteFilename(GCMSearchPath, _strPath, _searchString); #ifdef _WIN32 WIN32_FIND_DATA findData; - HANDLE FindFirst = FindFirstFile(UTF8ToTStr(GCMSearchPath).c_str(), &findData); + HANDLE FindFirst = FindFirstFile(Common::UTF8ToTStr(GCMSearchPath).c_str(), &findData); if (FindFirst != INVALID_HANDLE_VALUE) { @@ -49,7 +47,7 @@ void CFileSearch::FindFiles(const std::string& _searchString, const std::string& if (findData.cFileName[0] != '.') { std::string strFilename; - BuildCompleteFilename(strFilename, _strPath, TStrToUTF8(findData.cFileName)); + Common::BuildCompleteFilename(strFilename, _strPath, Common::TStrToUTF8(findData.cFileName)); m_FileNames.push_back(strFilename); } diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index b6ff2e40b..970041007 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -4,9 +4,7 @@ #include "common/common.h" -#include "common/common_paths.h" #include "common/file_util.h" -#include "common/string_util.h" #ifdef _WIN32 #include <windows.h> @@ -16,10 +14,7 @@ #include <io.h> #include <direct.h> // getcwd #else -#include <cerrno> -#include <cstdlib> #include <sys/param.h> -#include <sys/types.h> #include <dirent.h> #endif @@ -32,8 +27,6 @@ #include <algorithm> #include <sys/stat.h> -#include "common/string_util.h" - #ifndef S_ISDIR #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) #endif @@ -71,7 +64,7 @@ bool Exists(const std::string &filename) StripTailDirSlashes(copy); #ifdef _WIN32 - int result = _tstat64(UTF8ToTStr(copy).c_str(), &file_info); + int result = _tstat64(Common::UTF8ToTStr(copy).c_str(), &file_info); #else int result = stat64(copy.c_str(), &file_info); #endif @@ -88,7 +81,7 @@ bool IsDirectory(const std::string &filename) StripTailDirSlashes(copy); #ifdef _WIN32 - int result = _tstat64(UTF8ToTStr(copy).c_str(), &file_info); + int result = _tstat64(Common::UTF8ToTStr(copy).c_str(), &file_info); #else int result = stat64(copy.c_str(), &file_info); #endif @@ -124,7 +117,7 @@ bool Delete(const std::string &filename) } #ifdef _WIN32 - if (!DeleteFile(UTF8ToTStr(filename).c_str())) + if (!DeleteFile(Common::UTF8ToTStr(filename).c_str())) { WARN_LOG(COMMON, "Delete: DeleteFile failed on %s: %s", filename.c_str(), GetLastErrorMsg()); @@ -146,7 +139,7 @@ bool CreateDir(const std::string &path) { INFO_LOG(COMMON, "CreateDir: directory %s", path.c_str()); #ifdef _WIN32 - if (::CreateDirectory(UTF8ToTStr(path).c_str(), NULL)) + if (::CreateDirectory(Common::UTF8ToTStr(path).c_str(), NULL)) return true; DWORD error = GetLastError(); if (error == ERROR_ALREADY_EXISTS) @@ -225,7 +218,7 @@ bool DeleteDir(const std::string &filename) } #ifdef _WIN32 - if (::RemoveDirectory(UTF8ToTStr(filename).c_str())) + if (::RemoveDirectory(Common::UTF8ToTStr(filename).c_str())) return true; #else if (rmdir(filename.c_str()) == 0) @@ -254,7 +247,7 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename) INFO_LOG(COMMON, "Copy: %s --> %s", srcFilename.c_str(), destFilename.c_str()); #ifdef _WIN32 - if (CopyFile(UTF8ToTStr(srcFilename).c_str(), UTF8ToTStr(destFilename).c_str(), FALSE)) + if (CopyFile(Common::UTF8ToTStr(srcFilename).c_str(), Common::UTF8ToTStr(destFilename).c_str(), FALSE)) return true; ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s", @@ -342,7 +335,7 @@ u64 GetSize(const std::string &filename) struct stat64 buf; #ifdef _WIN32 - if (_tstat64(UTF8ToTStr(filename).c_str(), &buf) == 0) + if (_tstat64(Common::UTF8ToTStr(filename).c_str(), &buf) == 0) #else if (stat64(filename.c_str(), &buf) == 0) #endif @@ -415,7 +408,7 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) // Find the first file in the directory. WIN32_FIND_DATA ffd; - HANDLE hFind = FindFirstFile(UTF8ToTStr(directory + "\\*").c_str(), &ffd); + HANDLE hFind = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); @@ -425,7 +418,7 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) do { FSTEntry entry; - const std::string virtualName(TStrToUTF8(ffd.cFileName)); + const std::string virtualName(Common::TStrToUTF8(ffd.cFileName)); #else struct dirent dirent, *result = NULL; @@ -482,7 +475,7 @@ bool DeleteDirRecursively(const std::string &directory) #ifdef _WIN32 // Find the first file in the directory. WIN32_FIND_DATA ffd; - HANDLE hFind = FindFirstFile(UTF8ToTStr(directory + "\\*").c_str(), &ffd); + HANDLE hFind = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); if (hFind == INVALID_HANDLE_VALUE) { @@ -493,7 +486,7 @@ bool DeleteDirRecursively(const std::string &directory) // windows loop do { - const std::string virtualName(TStrToUTF8(ffd.cFileName)); + const std::string virtualName(Common::TStrToUTF8(ffd.cFileName)); #else struct dirent dirent, *result = NULL; DIR *dirp = opendir(directory.c_str()); @@ -631,7 +624,7 @@ std::string& GetExeDirectory() { TCHAR Dolphin_exe_Path[2048]; GetModuleFileName(NULL, Dolphin_exe_Path, 2048); - DolphinPath = TStrToUTF8(Dolphin_exe_Path); + DolphinPath = Common::TStrToUTF8(Dolphin_exe_Path); DolphinPath = DolphinPath.substr(0, DolphinPath.find_last_of('\\')); } return DolphinPath; @@ -826,7 +819,7 @@ bool IOFile::Open(const std::string& filename, const char openmode[]) { Close(); #ifdef _WIN32 - _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()); + _tfopen_s(&m_file, Common::UTF8ToTStr(filename).c_str(), Common::UTF8ToTStr(openmode).c_str()); #else m_file = fopen(filename.c_str(), openmode); #endif diff --git a/src/common/file_util.h b/src/common/file_util.h index 0871734d4..fdae5c9c8 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -213,7 +213,7 @@ template <typename T> void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) { #ifdef _WIN32 - fstream.open(UTF8ToTStr(filename).c_str(), openmode); + fstream.open(Common::UTF8ToTStr(filename).c_str(), openmode); #else fstream.open(filename.c_str(), openmode); #endif diff --git a/src/common/fixed_size_queue.h b/src/common/fixed_size_queue.h deleted file mode 100644 index 1e3a5dea6..000000000 --- a/src/common/fixed_size_queue.h +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2013 Dolphin Emulator Project -// Licensed under GPLv2 -// Refer to the license.txt file included. - -#pragma once - -// STL-look-a-like interface, but name is mixed case to distinguish it clearly from the -// real STL classes. - -// Not fully featured, no safety checking yet. Add features as needed. - -// TODO: "inline" storage? - -template <class T, int N> -class fixed_size_queue.h -{ - T *storage; - int head; - int tail; - int count; // sacrifice 4 bytes for a simpler implementation. may optimize away in the future. - - // Make copy constructor private for now. - fixed_size_queue.h(fixed_size_queue.h &other) { } - -public: - fixed_size_queue.h() - { - storage = new T[N]; - clear(); - } - - ~fixed_size_queue.h() - { - delete [] storage; - } - - void clear() { - head = 0; - tail = 0; - count = 0; - } - - void push(T t) { - storage[tail] = t; - tail++; - if (tail == N) - tail = 0; - count++; - } - - void pop() { - head++; - if (head == N) - head = 0; - count--; - } - - T pop_front() { - const T &temp = storage[head]; - pop(); - return temp; - } - - T &front() { return storage[head]; } - const T &front() const { return storage[head]; } - - size_t size() const { - return count; - } -}; diff --git a/src/common/key_map.cpp b/src/common/key_map.cpp new file mode 100644 index 000000000..309caab98 --- /dev/null +++ b/src/common/key_map.cpp @@ -0,0 +1,25 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "key_map.h" +#include <map> + +namespace KeyMap { + +static std::map<HostDeviceKey, HID_User::PadState> key_map; +static int next_device_id = 0; + +int NewDeviceId() { + return next_device_id++; +} + +void SetKeyMapping(HostDeviceKey key, HID_User::PadState padState) { + key_map[key].hex = padState.hex; +} + +HID_User::PadState GetPadKey(HostDeviceKey key) { + return key_map[key]; +} + +} diff --git a/src/common/key_map.h b/src/common/key_map.h new file mode 100644 index 000000000..b5acfbab0 --- /dev/null +++ b/src/common/key_map.h @@ -0,0 +1,45 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/hid.h" + +namespace KeyMap { + +/** + * Represents a key for a specific host device. + */ +struct HostDeviceKey { + int key_code; + int device_id; ///< Uniquely identifies a host device + + bool operator < (const HostDeviceKey &other) const { + if (device_id == other.device_id) { + return key_code < other.key_code; + } + return device_id < other.device_id; + } + + bool operator == (const HostDeviceKey &other) const { + return device_id == other.device_id && key_code == other.key_code; + } +}; + +/** + * Generates a new device id, which uniquely identifies a host device within KeyMap. + */ +int NewDeviceId(); + +/** + * Maps a device-specific key to a PadState. + */ +void SetKeyMapping(HostDeviceKey key, HID_User::PadState padState); + +/** + * Gets the PadState that's mapped to the provided device-specific key. + */ +HID_User::PadState GetPadKey(HostDeviceKey key); + +} diff --git a/src/common/log_manager.cpp b/src/common/log_manager.cpp index 4e1cb60bd..43346f279 100644 --- a/src/common/log_manager.cpp +++ b/src/common/log_manager.cpp @@ -8,7 +8,6 @@ #include "common/console_listener.h" #include "common/timer.h" #include "common/thread.h" -#include "common/file_util.h" void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file, int line, const char* function, const char* fmt, ...) @@ -121,7 +120,7 @@ void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const if (!log->IsEnabled() || level > log->GetLevel() || ! log->HasListeners()) return; - CharArrayFromFormatV(temp, MAX_MSGLEN, fmt, args); + Common::CharArrayFromFormatV(temp, MAX_MSGLEN, fmt, args); static const char level_to_char[7] = "ONEWID"; sprintf(msg, "%s %s:%u %c[%s] %s: %s\n", Common::Timer::GetTimeFormatted().c_str(), file, line, diff --git a/src/common/log_manager.h b/src/common/log_manager.h index 81d808825..ce62d0361 100644 --- a/src/common/log_manager.h +++ b/src/common/log_manager.h @@ -6,11 +6,11 @@ #include "common/log.h" #include "common/string_util.h" -#include "common/thread.h" #include "common/file_util.h" #include <cstring> #include <set> +#include <mutex> #define MAX_MESSAGES 8000 #define MAX_MSGLEN 1024 diff --git a/src/common/math_util.cpp b/src/common/math_util.cpp index 82eceab00..ab0e6b75c 100644 --- a/src/common/math_util.cpp +++ b/src/common/math_util.cpp @@ -6,8 +6,7 @@ #include "common/common.h" #include "common/math_util.h" -#include <cmath> -#include <numeric> +#include <numeric> // Necessary on OS X, but not Linux namespace MathUtil { diff --git a/src/common/mem_arena.cpp b/src/common/mem_arena.cpp index b76ac92d3..40d9c03a2 100644 --- a/src/common/mem_arena.cpp +++ b/src/common/mem_arena.cpp @@ -22,11 +22,7 @@ #include "common/string_util.h" #ifndef _WIN32 -#include <sys/stat.h> #include <fcntl.h> -#include <unistd.h> -#include <cerrno> -#include <cstring> #ifdef ANDROID #include <sys/ioctl.h> #include <linux/ashmem.h> @@ -143,7 +139,7 @@ void MemArena::GrabLowMemSpace(size_t size) // a bit more. for (int i = 0; i < 10000; i++) { - std::string file_name = StringFromFormat("/citramem.%d", i); + std::string file_name = Common::StringFromFormat("/citramem.%d", i); fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); if (fd != -1) { diff --git a/src/common/memory_util.cpp b/src/common/memory_util.cpp index e01e63175..bab7d9f7a 100644 --- a/src/common/memory_util.cpp +++ b/src/common/memory_util.cpp @@ -10,9 +10,6 @@ #ifdef _WIN32 #include <windows.h> #include <psapi.h> -#else -#include <cerrno> -#include <cstdio> #endif #if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) @@ -190,7 +187,7 @@ std::string MemUsage() if (NULL == hProcess) return "MemUsage Error"; if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) - Ret = StringFromFormat("%s K", ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); + Ret = Common::StringFromFormat("%s K", Common::ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); CloseHandle(hProcess); return Ret; diff --git a/src/common/msg_handler.cpp b/src/common/msg_handler.cpp index 3e02ec4d7..b3556aaa8 100644 --- a/src/common/msg_handler.cpp +++ b/src/common/msg_handler.cpp @@ -72,7 +72,7 @@ bool MsgAlert(bool yes_no, int Style, const char* format, ...) va_list args; va_start(args, format); - CharArrayFromFormatV(buffer, sizeof(buffer)-1, str_translator(format).c_str(), args); + Common::CharArrayFromFormatV(buffer, sizeof(buffer)-1, str_translator(format).c_str(), args); va_end(args); ERROR_LOG(MASTER_LOG, "%s: %s", caption.c_str(), buffer); diff --git a/src/common/std_condition_variable.h b/src/common/std_condition_variable.h deleted file mode 100644 index ad2022f5a..000000000 --- a/src/common/std_condition_variable.h +++ /dev/null @@ -1,168 +0,0 @@ - -#pragma once - -#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) -#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) - -#ifndef __has_include -#define __has_include(s) 0 -#endif - -#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ - -// GCC 4.4 provides <condition_variable> -#include <condition_variable> - -#elif __has_include(<condition_variable>) && !ANDROID - -// clang and libc++ provide <condition_variable> on OSX. However, the version -// of libc++ bundled with OSX 10.7 and 10.8 is buggy: it uses _ as a variable. -// -// We work around this issue by undefining and redefining _. - -#undef _ -#include <condition_variable> -#define _(s) wxGetTranslation((s)) - -#else - -// partial std::condition_variable implementation for win32/pthread - -#include "common/std_mutex.h" - -#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) -#define USE_RVALUE_REFERENCES -#endif - -#if defined(_WIN32) && defined(_M_X64) -#define USE_CONDITION_VARIABLES -#elif defined(_WIN32) -#define USE_EVENTS -#endif - -namespace std -{ - -class condition_variable -{ -#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) - typedef CONDITION_VARIABLE native_type; -#elif defined(_WIN32) - typedef HANDLE native_type; -#else - typedef pthread_cond_t native_type; -#endif - -public: - -#ifdef USE_EVENTS - typedef native_type native_handle_type; -#else - typedef native_type* native_handle_type; -#endif - - condition_variable() - { -#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) - InitializeConditionVariable(&m_handle); -#elif defined(_WIN32) - m_handle = CreateEvent(NULL, false, false, NULL); -#else - pthread_cond_init(&m_handle, NULL); -#endif - } - - ~condition_variable() - { -#if defined(_WIN32) && !defined(USE_CONDITION_VARIABLES) - CloseHandle(m_handle); -#elif !defined(_WIN32) - pthread_cond_destroy(&m_handle); -#endif - } - - condition_variable(const condition_variable&) /*= delete*/; - condition_variable& operator=(const condition_variable&) /*= delete*/; - - void notify_one() - { -#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) - WakeConditionVariable(&m_handle); -#elif defined(_WIN32) - SetEvent(m_handle); -#else - pthread_cond_signal(&m_handle); -#endif - } - - void notify_all() - { -#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) - WakeAllConditionVariable(&m_handle); -#elif defined(_WIN32) - // TODO: broken - SetEvent(m_handle); -#else - pthread_cond_broadcast(&m_handle); -#endif - } - - void wait(unique_lock<mutex>& lock) - { -#ifdef _WIN32 - #ifdef USE_SRWLOCKS - SleepConditionVariableSRW(&m_handle, lock.mutex()->native_handle(), INFINITE, 0); - #elif defined(USE_CONDITION_VARIABLES) - SleepConditionVariableCS(&m_handle, lock.mutex()->native_handle(), INFINITE); - #else - // TODO: broken, the unlock and wait need to be atomic - lock.unlock(); - WaitForSingleObject(m_handle, INFINITE); - lock.lock(); - #endif -#else - pthread_cond_wait(&m_handle, lock.mutex()->native_handle()); -#endif - } - - template <class Predicate> - void wait(unique_lock<mutex>& lock, Predicate pred) - { - while (!pred()) - wait(lock); - } - - //template <class Clock, class Duration> - //cv_status wait_until(unique_lock<mutex>& lock, - // const chrono::time_point<Clock, Duration>& abs_time); - - //template <class Clock, class Duration, class Predicate> - // bool wait_until(unique_lock<mutex>& lock, - // const chrono::time_point<Clock, Duration>& abs_time, - // Predicate pred); - - //template <class Rep, class Period> - //cv_status wait_for(unique_lock<mutex>& lock, - // const chrono::duration<Rep, Period>& rel_time); - - //template <class Rep, class Period, class Predicate> - // bool wait_for(unique_lock<mutex>& lock, - // const chrono::duration<Rep, Period>& rel_time, - // Predicate pred); - - native_handle_type native_handle() - { -#ifdef USE_EVENTS - return m_handle; -#else - return &m_handle; -#endif - } - -private: - native_type m_handle; -}; - -} - -#endif diff --git a/src/common/std_mutex.h b/src/common/std_mutex.h deleted file mode 100644 index 5711d791b..000000000 --- a/src/common/std_mutex.h +++ /dev/null @@ -1,362 +0,0 @@ -#pragma once - -#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) -#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) - -#ifndef __has_include -#define __has_include(s) 0 -#endif - -#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ -// GCC 4.4 provides <mutex> -#include <mutex> -#elif __has_include(<mutex>) && !ANDROID -// Clang + libc++ -#include <mutex> -#else - -// partial <mutex> implementation for win32/pthread - -#include <algorithm> - -#if defined(_WIN32) -// WIN32 -#define WIN32_LEAN_AND_MEAN -#include <Windows.h> - -#else -// POSIX -#include <pthread.h> - -#endif - -#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) -#define USE_RVALUE_REFERENCES -#endif - -#if defined(_WIN32) && defined(_M_X64) -#define USE_SRWLOCKS -#endif - -namespace std -{ - -class recursive_mutex -{ -#ifdef _WIN32 - typedef CRITICAL_SECTION native_type; -#else - typedef pthread_mutex_t native_type; -#endif - -public: - typedef native_type* native_handle_type; - - recursive_mutex(const recursive_mutex&) /*= delete*/; - recursive_mutex& operator=(const recursive_mutex&) /*= delete*/; - - recursive_mutex() - { -#ifdef _WIN32 - InitializeCriticalSection(&m_handle); -#else - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&m_handle, &attr); -#endif - } - - ~recursive_mutex() - { -#ifdef _WIN32 - DeleteCriticalSection(&m_handle); -#else - pthread_mutex_destroy(&m_handle); -#endif - } - - void lock() - { -#ifdef _WIN32 - EnterCriticalSection(&m_handle); -#else - pthread_mutex_lock(&m_handle); -#endif - } - - void unlock() - { -#ifdef _WIN32 - LeaveCriticalSection(&m_handle); -#else - pthread_mutex_unlock(&m_handle); -#endif - } - - bool try_lock() - { -#ifdef _WIN32 - return (0 != TryEnterCriticalSection(&m_handle)); -#else - return !pthread_mutex_trylock(&m_handle); -#endif - } - - native_handle_type native_handle() - { - return &m_handle; - } - -private: - native_type m_handle; -}; - -#if !defined(_WIN32) || defined(USE_SRWLOCKS) - -class mutex -{ -#ifdef _WIN32 - typedef SRWLOCK native_type; -#else - typedef pthread_mutex_t native_type; -#endif - -public: - typedef native_type* native_handle_type; - - mutex(const mutex&) /*= delete*/; - mutex& operator=(const mutex&) /*= delete*/; - - mutex() - { -#ifdef _WIN32 - InitializeSRWLock(&m_handle); -#else - pthread_mutex_init(&m_handle, NULL); -#endif - } - - ~mutex() - { -#ifdef _WIN32 -#else - pthread_mutex_destroy(&m_handle); -#endif - } - - void lock() - { -#ifdef _WIN32 - AcquireSRWLockExclusive(&m_handle); -#else - pthread_mutex_lock(&m_handle); -#endif - } - - void unlock() - { -#ifdef _WIN32 - ReleaseSRWLockExclusive(&m_handle); -#else - pthread_mutex_unlock(&m_handle); -#endif - } - - bool try_lock() - { -#ifdef _WIN32 - // XXX TryAcquireSRWLockExclusive requires Windows 7! - // return (0 != TryAcquireSRWLockExclusive(&m_handle)); - return false; -#else - return !pthread_mutex_trylock(&m_handle); -#endif - } - - native_handle_type native_handle() - { - return &m_handle; - } - -private: - native_type m_handle; -}; - -#else -typedef recursive_mutex mutex; // just use CriticalSections - -#endif - -enum defer_lock_t { defer_lock }; -enum try_to_lock_t { try_to_lock }; -enum adopt_lock_t { adopt_lock }; - -template <class Mutex> -class lock_guard -{ -public: - typedef Mutex mutex_type; - - explicit lock_guard(mutex_type& m) - : pm(m) - { - m.lock(); - } - - lock_guard(mutex_type& m, adopt_lock_t) - : pm(m) - { - } - - ~lock_guard() - { - pm.unlock(); - } - - lock_guard(lock_guard const&) /*= delete*/; - lock_guard& operator=(lock_guard const&) /*= delete*/; - -private: - mutex_type& pm; -}; - -template <class Mutex> -class unique_lock -{ -public: - typedef Mutex mutex_type; - - unique_lock() - : pm(NULL), owns(false) - {} - - /*explicit*/ unique_lock(mutex_type& m) - : pm(&m), owns(true) - { - m.lock(); - } - - unique_lock(mutex_type& m, defer_lock_t) - : pm(&m), owns(false) - {} - - unique_lock(mutex_type& m, try_to_lock_t) - : pm(&m), owns(m.try_lock()) - {} - - unique_lock(mutex_type& m, adopt_lock_t) - : pm(&m), owns(true) - {} - - //template <class Clock, class Duration> - //unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); - - //template <class Rep, class Period> - //unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); - - ~unique_lock() - { - if (owns_lock()) - mutex()->unlock(); - } - -#ifdef USE_RVALUE_REFERENCES - unique_lock& operator=(const unique_lock&) /*= delete*/; - - unique_lock& operator=(unique_lock&& other) - { -#else - unique_lock& operator=(const unique_lock& u) - { - // ugly const_cast to get around lack of rvalue references - unique_lock& other = const_cast<unique_lock&>(u); -#endif - swap(other); - return *this; - } - -#ifdef USE_RVALUE_REFERENCES - unique_lock(const unique_lock&) /*= delete*/; - - unique_lock(unique_lock&& other) - : pm(NULL), owns(false) - { -#else - unique_lock(const unique_lock& u) - : pm(NULL), owns(false) - { - // ugly const_cast to get around lack of rvalue references - unique_lock& other = const_cast<unique_lock&>(u); -#endif - swap(other); - } - - void lock() - { - mutex()->lock(); - owns = true; - } - - bool try_lock() - { - owns = mutex()->try_lock(); - return owns; - } - - //template <class Rep, class Period> - //bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); - //template <class Clock, class Duration> - //bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); - - void unlock() - { - mutex()->unlock(); - owns = false; - } - - void swap(unique_lock& u) - { - std::swap(pm, u.pm); - std::swap(owns, u.owns); - } - - mutex_type* release() - { - auto const ret = mutex(); - - pm = NULL; - owns = false; - - return ret; - } - - bool owns_lock() const - { - return owns; - } - - //explicit operator bool () const - //{ - // return owns_lock(); - //} - - mutex_type* mutex() const - { - return pm; - } - -private: - mutex_type* pm; - bool owns; -}; - -template <class Mutex> -void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) -{ - x.swap(y); -} - -} - -#endif diff --git a/src/common/std_thread.h b/src/common/std_thread.h deleted file mode 100644 index ce1336ee7..000000000 --- a/src/common/std_thread.h +++ /dev/null @@ -1,314 +0,0 @@ -#pragma once - -#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) -#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) - -#ifndef __has_include -#define __has_include(s) 0 -#endif - -#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ -// GCC 4.4 provides <thread> -#ifndef _GLIBCXX_USE_SCHED_YIELD -#define _GLIBCXX_USE_SCHED_YIELD -#endif -#include <thread> -#elif __has_include(<thread>) && !ANDROID -// Clang + libc++ -#include <thread> -#else - -// partial std::thread implementation for win32/pthread - -#include <algorithm> - -#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) -#define USE_RVALUE_REFERENCES -#endif - -#ifdef __APPLE__ -#import <Foundation/NSAutoreleasePool.h> -#endif - -#if defined(_WIN32) -// WIN32 - -#define WIN32_LEAN_AND_MEAN -#include <Windows.h> - -#if defined(_MSC_VER) && defined(_MT) -// When linking with LIBCMT (the multithreaded C library), Microsoft recommends -// using _beginthreadex instead of CreateThread. -#define USE_BEGINTHREADEX -#include <process.h> -#endif - -#ifdef USE_BEGINTHREADEX -#define THREAD_ID unsigned -#define THREAD_RETURN unsigned __stdcall -#else -#define THREAD_ID DWORD -#define THREAD_RETURN DWORD WINAPI -#endif -#define THREAD_HANDLE HANDLE - -#else -// PTHREAD - -#include <unistd.h> - -#ifndef _POSIX_THREADS -#error unsupported platform (no pthreads?) -#endif - -#include <pthread.h> - -#define THREAD_ID pthread_t -#define THREAD_HANDLE pthread_t -#define THREAD_RETURN void* - -#endif - -namespace std -{ - -class thread -{ -public: - typedef THREAD_HANDLE native_handle_type; - - class id - { - friend class thread; - public: - id() : m_thread(0) {} - id(THREAD_ID _id) : m_thread(_id) {} - - bool operator==(const id& rhs) const - { - return m_thread == rhs.m_thread; - } - - bool operator!=(const id& rhs) const - { - return !(*this == rhs); - } - - bool operator<(const id& rhs) const - { - return m_thread < rhs.m_thread; - } - - private: - THREAD_ID m_thread; - }; - - // no variadic template support in msvc - //template <typename C, typename... A> - //thread(C&& func, A&&... args); - - template <typename C> - thread(C func) - { - StartThread(new Func<C>(func)); - } - - template <typename C, typename A> - thread(C func, A arg) - { - StartThread(new FuncArg<C, A>(func, arg)); - } - - thread() /*= default;*/ {} - -#ifdef USE_RVALUE_REFERENCES - thread(const thread&) /*= delete*/; - - thread(thread&& other) - { -#else - thread(const thread& t) - { - // ugly const_cast to get around lack of rvalue references - thread& other = const_cast<thread&>(t); -#endif - swap(other); - } - -#ifdef USE_RVALUE_REFERENCES - thread& operator=(const thread&) /*= delete*/; - - thread& operator=(thread&& other) - { -#else - thread& operator=(const thread& t) - { - // ugly const_cast to get around lack of rvalue references - thread& other = const_cast<thread&>(t); -#endif - if (joinable()) - detach(); - swap(other); - return *this; - } - - ~thread() - { - if (joinable()) - detach(); - } - - bool joinable() const - { - return m_id != id(); - } - - id get_id() const - { - return m_id; - } - - native_handle_type native_handle() - { -#ifdef _WIN32 - return m_handle; -#else - return m_id.m_thread; -#endif - } - - void join() - { -#ifdef _WIN32 - WaitForSingleObject(m_handle, INFINITE); - detach(); -#else - pthread_join(m_id.m_thread, NULL); - m_id = id(); -#endif - } - - void detach() - { -#ifdef _WIN32 - CloseHandle(m_handle); -#else - pthread_detach(m_id.m_thread); -#endif - m_id = id(); - } - - void swap(thread& other) - { - std::swap(m_id, other.m_id); -#ifdef _WIN32 - std::swap(m_handle, other.m_handle); -#endif - } - - static unsigned hardware_concurrency() - { -#ifdef _WIN32 - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - return static_cast<unsigned>(sysinfo.dwNumberOfProcessors); -#else - return 0; -#endif - } - -private: - id m_id; - -#ifdef _WIN32 - native_handle_type m_handle; -#endif - - template <typename F> - void StartThread(F* param) - { -#ifdef USE_BEGINTHREADEX - m_handle = (HANDLE)_beginthreadex(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread); -#elif defined(_WIN32) - m_handle = CreateThread(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread); -#else - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setstacksize(&attr, 1024 * 1024); - if (pthread_create(&m_id.m_thread, &attr, &RunAndDelete<F>, param)) - m_id = id(); -#endif - } - - template <typename C> - class Func - { - public: - Func(C _func) : func(_func) {} - - void Run() { func(); } - - private: - C const func; - }; - - template <typename C, typename A> - class FuncArg - { - public: - FuncArg(C _func, A _arg) : func(_func), arg(_arg) {} - - void Run() { func(arg); } - - private: - C const func; - A arg; - }; - - template <typename F> - static THREAD_RETURN RunAndDelete(void* param) - { -#ifdef __APPLE__ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; -#endif - static_cast<F*>(param)->Run(); - delete static_cast<F*>(param); -#ifdef __APPLE__ - [pool release]; -#endif - return 0; - } -}; - -namespace this_thread -{ - -inline void yield() -{ -#ifdef _WIN32 - SwitchToThread(); -#else - sleep(0); -#endif -} - -inline thread::id get_id() -{ -#ifdef _WIN32 - return GetCurrentThreadId(); -#else - return pthread_self(); -#endif -} - -} // namespace this_thread - -} // namespace std - -#undef USE_RVALUE_REFERENCES -#undef USE_BEGINTHREADEX -#undef THREAD_ID -#undef THREAD_RETURN -#undef THREAD_HANDLE - -#endif diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index c1f22bda3..9199e30bc 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -3,34 +3,28 @@ // Refer to the license.txt file included. #include <algorithm> -#include <cstdlib> -#include <cstdio> #include "common/common.h" -#include "common/common_paths.h" #include "common/string_util.h" #ifdef _WIN32 #include <Windows.h> #else - #include <cerrno> #include <iconv.h> #endif +namespace Common { + /// Make a string lowercase -void LowerStr(char* str) { - for (int i = 0; str[i]; i++) { - str[i] = tolower(str[ i ]); - } +std::string ToLower(std::string str) { + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + return str; } /// Make a string uppercase -void UpperStr(char* str) { - for (int i=0; i < strlen(str); i++) { - if(str[i] >= 'a' && str[i] <= 'z') { - str[i] &= 0xDF; - } - } +std::string ToUpper(std::string str) { + std::transform(str.begin(), str.end(), str.begin(), ::toupper); + return str; } // faster than sscanf @@ -283,12 +277,17 @@ std::string TabsToSpaces(int tab_size, const std::string &in) std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) { - while(1) + size_t pos = 0; + + if (src == dest) + return result; + + while ((pos = result.find(src, pos)) != std::string::npos) { - size_t pos = result.find(src); - if (pos == std::string::npos) break; result.replace(pos, src.size(), dest); + pos += dest.length(); } + return result; } @@ -545,3 +544,5 @@ std::string UTF16ToUTF8(const std::wstring& input) } #endif + +} diff --git a/src/common/string_util.h b/src/common/string_util.h index ba4cd363e..16ce39bc1 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -12,11 +12,13 @@ #include "common/common.h" +namespace Common { + /// Make a string lowercase -void LowerStr(char* str); +std::string ToLower(std::string str); /// Make a string uppercase -void UpperStr(char* str); +std::string ToUpper(std::string str); std::string StringFromFormat(const char* format, ...); // Cheap! @@ -111,3 +113,5 @@ inline std::string UTF8ToTStr(const std::string& str) #endif #endif + +} diff --git a/src/common/thread.cpp b/src/common/thread.cpp index 7341035c2..60d8ed075 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp @@ -3,16 +3,13 @@ // Refer to the license.txt file included. #include "common/thread.h" -#include "common/common.h" #ifdef __APPLE__ #include <mach/mach.h> #elif defined(BSD4_4) || defined(__OpenBSD__) #include <pthread_np.h> -#endif - -#ifdef USE_BEGINTHREADEX -#include <process.h> +#elif defined(_WIN32) +#include <Windows.h> #endif namespace Common diff --git a/src/common/thread.h b/src/common/thread.h index dbb9da53b..f7ace21b4 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -4,14 +4,13 @@ #pragma once -#include "common/std_condition_variable.h" -#include "common/std_mutex.h" -#include "common/std_thread.h" - // Don't include common.h here as it will break LogManager #include "common/common_types.h" #include <cstdio> #include <cstring> +#include <thread> +#include <condition_variable> +#include <mutex> // This may not be defined outside _WIN32 #ifndef _WIN32 diff --git a/src/common/utf8.cpp b/src/common/utf8.cpp index c83824d35..be4ebc855 100644 --- a/src/common/utf8.cpp +++ b/src/common/utf8.cpp @@ -19,12 +19,8 @@ #endif #include <cstdlib> -#include <cstdio> #include <cstring> -#include <cstdarg> - #include <algorithm> -#include <string> #include "common/common_types.h" #include "common/utf8.h" |