utils: c++11

This commit is contained in:
Evgeniy Dushistov
2013-07-06 22:38:56 +00:00
parent edfee97db3
commit 7274a61efa
2 changed files with 23 additions and 23 deletions

View File

@@ -32,16 +32,16 @@
std::string utf8_to_locale_ign_err(const std::string& utf8_str) std::string utf8_to_locale_ign_err(const std::string& utf8_str)
{ {
gsize bytes_read, bytes_written; gsize bytes_read, bytes_written;
GError *err=NULL; GError *err=nullptr;
std::string res; std::string res;
const char * charset; const char * charset;
if (g_get_charset(&charset)) if (g_get_charset(&charset))
res=utf8_str; res=utf8_str;
else { else {
gchar *tmp=g_convert_with_fallback(utf8_str.c_str(), -1, charset, "UTF-8", NULL, gchar *tmp=g_convert_with_fallback(utf8_str.c_str(), -1, charset, "UTF-8", nullptr,
&bytes_read, &bytes_written, &err); &bytes_read, &bytes_written, &err);
if (NULL==tmp){ if (nullptr==tmp){
fprintf(stderr, _("Can not convert %s to current locale.\n"), utf8_str.c_str()); fprintf(stderr, _("Can not convert %s to current locale.\n"), utf8_str.c_str());
fprintf(stderr, "%s\n", err->message); fprintf(stderr, "%s\n", err->message);
g_error_free(err); g_error_free(err);
@@ -56,14 +56,14 @@ std::string utf8_to_locale_ign_err(const std::string& utf8_str)
char *locale_to_utf8(const char *loc_str) char *locale_to_utf8(const char *loc_str)
{ {
if(NULL==loc_str) if(nullptr==loc_str)
return NULL; return nullptr;
gsize bytes_read; gsize bytes_read;
gsize bytes_written; gsize bytes_written;
GError *err=NULL; GError *err=nullptr;
gchar *str=NULL; gchar *str=nullptr;
str=g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err); str=g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err);
if(NULL==str){ if(nullptr==str){
fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str); fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str);
fprintf(stderr, "%s\n", err->message); fprintf(stderr, "%s\n", err->message);
g_error_free(err); g_error_free(err);

View File

@@ -1,5 +1,4 @@
#ifndef _UTILS_HPP_ #pragma once
#define _UTILS_HPP_
#include <glib.h> #include <glib.h>
#include <string> #include <string>
@@ -7,10 +6,12 @@
template <typename T, typename unref_res_t, void (*unref_res)(unref_res_t *)> template <typename T, typename unref_res_t, void (*unref_res)(unref_res_t *)>
class ResourceWrapper { class ResourceWrapper {
public: public:
ResourceWrapper(T *p = NULL) : p_(p) {} ResourceWrapper(T *p = nullptr) : p_(p) {}
~ResourceWrapper() { free_resource(); } ~ResourceWrapper() { free_resource(); }
ResourceWrapper(const ResourceWrapper&) = delete;
ResourceWrapper& operator=(const ResourceWrapper&) = delete;
T *operator->() const { return p_; } T *operator->() const { return p_; }
bool operator!() const { return p_ == NULL; } bool operator!() const { return p_ == nullptr; }
void reset(T *newp) { void reset(T *newp) {
if (p_ != newp) { if (p_ != newp) {
@@ -33,18 +34,18 @@ private:
// Helper for enabling 'if (sp)' // Helper for enabling 'if (sp)'
struct Tester { struct Tester {
Tester() {} Tester() {}
private: private:
void operator delete(void*); void operator delete(void*);
}; };
public: public:
// enable 'if (sp)' // enable 'if (sp)'
operator Tester*() const operator Tester*() const {
{ if (!*this)
if (!*this) return 0; return 0;
static Tester t; static Tester t;
return &t; return &t;
} }
}; };
namespace glib { namespace glib {
@@ -56,4 +57,3 @@ namespace glib {
extern char *locale_to_utf8(const char *locstr); extern char *locale_to_utf8(const char *locstr);
extern std::string utf8_to_locale_ign_err(const std::string& utf8_str); extern std::string utf8_to_locale_ign_err(const std::string& utf8_str);
#endif//!_UTILS_HPP_