From 6412b43d6c84359f37649fa8049c686c10192f03 Mon Sep 17 00:00:00 2001 From: artemp Date: Mon, 20 May 2013 12:05:22 +0100 Subject: [PATCH] + utf8 to/from utf16 implementation --- include/mapnik/utils.hpp | 147 ++++++++++++++++++++++++--------------- 1 file changed, 92 insertions(+), 55 deletions(-) diff --git a/include/mapnik/utils.hpp b/include/mapnik/utils.hpp index 552fafaf3..5c2dde914 100644 --- a/include/mapnik/utils.hpp +++ b/include/mapnik/utils.hpp @@ -31,6 +31,7 @@ #endif // stl +#include #include #include #include @@ -40,6 +41,7 @@ namespace mapnik { + #ifdef MAPNIK_THREADSAFE using boost::mutex; #endif @@ -84,10 +86,10 @@ public: return new(&staticMemory) T; } #ifdef __SUNPRO_CC - // Sun C++ Compiler doesn't handle `volatile` keyword same as GCC. +// Sun C++ Compiler doesn't handle `volatile` keyword same as GCC. static void destroy(T* obj) #else - static void destroy(volatile T* obj) + static void destroy(volatile T* obj) #endif { obj->~T(); @@ -99,78 +101,113 @@ template class CreatePolicy=CreateStatic> class MAPNIK_DECL singleton { #else -template class CreatePolicy=CreateStatic> class singleton -{ + template class CreatePolicy=CreateStatic> class singleton + { #endif #ifdef __SUNPRO_CC - /* Sun's C++ compiler will issue the following errors if CreatePolicy is used: - Error: A class template name was expected instead of mapnik::CreatePolicy - Error: A "friend" declaration must specify a class or function. - */ - friend class CreatePolicy; +/* Sun's C++ compiler will issue the following errors if CreatePolicy is used: + Error: A class template name was expected instead of mapnik::CreatePolicy + Error: A "friend" declaration must specify a class or function. +*/ + friend class CreatePolicy; #else - friend class CreatePolicy; + friend class CreatePolicy; #endif - static T* pInstance_; - static bool destroyed_; - singleton(const singleton &rhs); - singleton& operator=(const singleton&); + static T* pInstance_; + static bool destroyed_; + singleton(const singleton &rhs); + singleton& operator=(const singleton&); - static void onDeadReference() - { - throw std::runtime_error("dead reference!"); - } - - static void DestroySingleton() - { - CreatePolicy::destroy(pInstance_); - pInstance_ = 0; - destroyed_ = true; - } - -protected: -#ifdef MAPNIK_THREADSAFE - static mutex mutex_; -#endif - singleton() {} -public: - static T& instance() - { - if (! pInstance_) + static void onDeadReference() { + throw std::runtime_error("dead reference!"); + } + + static void DestroySingleton() + { + CreatePolicy::destroy(pInstance_); + pInstance_ = 0; + destroyed_ = true; + } + + protected: #ifdef MAPNIK_THREADSAFE - mutex::scoped_lock lock(mutex_); + static mutex mutex_; #endif + singleton() {} + public: + static T& instance() + { if (! pInstance_) { - if (destroyed_) +#ifdef MAPNIK_THREADSAFE + mutex::scoped_lock lock(mutex_); +#endif + if (! pInstance_) { - destroyed_ = false; - onDeadReference(); - } - else - { - pInstance_ = CreatePolicy::create(); + if (destroyed_) + { + destroyed_ = false; + onDeadReference(); + } + else + { + pInstance_ = CreatePolicy::create(); - // register destruction - std::atexit(&DestroySingleton); + // register destruction + std::atexit(&DestroySingleton); + } } } + return *pInstance_; } - return *pInstance_; - } -}; + }; #ifdef MAPNIK_THREADSAFE -template class CreatePolicy> mutex singleton::mutex_; + template class CreatePolicy> mutex singleton::mutex_; #endif -template class CreatePolicy> T* singleton::pInstance_=0; -template class CreatePolicy> bool singleton::destroyed_=false; + template class CreatePolicy> T* singleton::pInstance_=0; + template class CreatePolicy> bool singleton::destroyed_=false; + + +#ifdef _WINDOWS + +#include + + // UTF8 <--> UTF16 conversion routines + + std::string utf16_to_utf8(std::wstring const& wstr) + { + std::string str; + int size = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, 0, 0, 0, 0); + if(size > 0) + { + std::vector buffer(size); + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &buffer[0], size, 0, 0); + str.assign(buffer.begin(), buffer.end() - 1); + } + return str; + } + + std::wstring utf8_to_utf16 (std::string const& str) + { + std::wstring wstr; + int size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, 0, 0); + if (size > 0) + { + std::vector buffer(size); + MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &buffer[0], size); + wstr.assign(buffer.begin(), buffer.end() - 1); + } + return wstr; + } + +#endif // _WINDOWS }