From 7d0fad978e7f6026ea8bbf076df45442862e8086 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 16 Jun 2011 22:07:27 +0200 Subject: Add LocaleStringTraits resource class for resources which need to work with localised strings. --- src/FbTk/ResTraits.hh | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/FbTk/ResTraits.hh b/src/FbTk/ResTraits.hh index 2ea5986..3ee4464 100644 --- a/src/FbTk/ResTraits.hh +++ b/src/FbTk/ResTraits.hh @@ -25,6 +25,7 @@ #include #include +#include "FbString.hh" #include "Luamm.hh" #include "StringUtil.hh" @@ -65,14 +66,30 @@ struct IntTraits { struct StringTraits { typedef std::string Type; - static std::string toString(const std::string &x) { return x; } + static const std::string &toString(const std::string &x) { return x; } static void toLua(const std::string &x, lua::state &l) { l.pushstring(x); } - static std::string fromString(const std::string &x) { return x; } + static const std::string &fromString(const std::string &x) { return x; } static std::string fromLua(lua::state &l) { lua::stack_sentry s(l, -1); if(l.isstring(-1) || l.isnumber(-1)) + return l.tostring(-1); + throw ConversionError( std::string("Cannot convert to string from lua type ") + + l.type_name(l.type(-1)) ); + } +}; + +struct FbStringTraits { + typedef FbString Type; + static std::string toString(const FbString &x) { return FbStringUtil::FbStrToLocale(x); } + static void toLua(const FbString &x, lua::state &l) { l.pushstring(toString(x)); } + static FbString fromString(const std::string &x) { return FbStringUtil::LocaleStrToFb(x); } + + static FbString fromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + + if(l.isstring(-1) || l.isnumber(-1)) return fromString(l.tostring(-1)); throw ConversionError( std::string("Cannot convert to string from lua type ") + l.type_name(l.type(-1)) ); -- cgit v0.11.2