// ************************************************************************** // * This file is part of the FreeFileSync project. It is distributed under * // * GNU General Public License: http://www.gnu.org/licenses/gpl.html * // * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) * // ************************************************************************** // #ifndef C_DLL_HEADER #define C_DLL_HEADER #include #include #undef min #include namespace c_dll { void writeString(const std::wstring& input, wchar_t* output, size_t outputLen); //Convert handles to objects and vice versa template //T: prefer managed object to ensure cleanup if remove() is not called class HandleProvider { public: static HandleProvider& instance(); S insert(T object); void remove(S handle); T& retrieve(S handle); //return default-constructed object if not found private: HandleProvider() {} HandleProvider(const HandleProvider&); HandleProvider& operator=(const HandleProvider&); S generate(); std::map handleMap; }; /* Example: typedef HandleProvider > HandleTaskbarMap; HandleTaskbarMap::instance().insert(xyz); */ //########################## inline implementation ############################# inline void writeString(const std::wstring& input, wchar_t* output, size_t outputLen) { if (outputLen > 0) { const size_t maxSize = std::min(input.length(), outputLen - 1); std::copy(input.begin(), input.begin() + maxSize, output); output[maxSize] = 0; } } template inline HandleProvider& HandleProvider::instance() { static HandleProvider inst; return inst; } //convert handles to objects and vice versa template inline S HandleProvider::insert(T object) { S newHandle = generate(); handleMap.insert(std::make_pair(newHandle, object)); return newHandle; } template inline void HandleProvider::remove(S handle) { handleMap.erase(handle); } template inline T& HandleProvider::retrieve(S handle) //return default-constructed object if not found { return handleMap[handle]; } template inline S HandleProvider::generate() { static S handle = 0; return ++handle; //don't return 0! 0 is reserved for indicating failure } } #endif //C_DLL_HEADER