Date: Monday, December 19, 2005 @ 15:06:49
Author: marc
Path: /cvsroot/carob/carob
Modified: include/Common.hpp (1.21 -> 1.22) src/Common.cpp (1.15 -> 1.16)
test/01-Unit/TestStringCodecs.cpp (1.3 -> 1.4)
Moved static codecs inside a class to forbid direct access to them.
-----------------------------------+
include/Common.hpp | 36 ++++++++++++++++++++++++++++++++----
src/Common.cpp | 7 ++-----
test/01-Unit/TestStringCodecs.cpp | 4 ++--
3 files changed, 36 insertions(+), 11 deletions(-)
Index: carob/include/Common.hpp
diff -u carob/include/Common.hpp:1.21 carob/include/Common.hpp:1.22
--- carob/include/Common.hpp:1.21 Mon Dec 19 12:43:58 2005
+++ carob/include/Common.hpp Mon Dec 19 15:06:49 2005
@@ -24,7 +24,6 @@
#include <string>
#include "StringCodecs.hpp"
-namespace CarobNS {
// Uncomment to get definition of uint8_t (needed with gcc<3.4 for instance)
// #include <stdint.h>
@@ -37,6 +36,9 @@
#define WARNING_PRE L"WARNING:\t"
#define ERROR_PRE L"ERROR:\t"
+
+namespace CarobNS {
+
/** Java byte (signed) */
typedef int8_t java_byte;
@@ -63,9 +65,6 @@
#endif
;
-// user-defined locale (typically set using LANG, LC_etc)
-extern const MBSCodec user_codec;
-extern const MBSCodec utf8_codec;
/** The current log level (set by setLogLevel) */
@@ -119,6 +118,24 @@
*/
std::wstring trim(const std::wstring& source,
const wchar_t* delims = L" \t\r\n");
+
+/**
+ * The only purpose of this class is to prevent direct access to the
+ * private codecs fields, while still be able to inline the calls.
+ */
+class StaticCodecs
+{
+public:
+ static std::wstring fromUTF8(const std::string& in)
+ { return utf8_codec.decode(in); }
+ static std::string toUTF8(const std::wstring& in)
+ { return utf8_codec.encode(in); }
+private:
+ static const CarobNS::MBSCodec utf8_codec;
+ // user-defined locale (typically set using LANG, LC_etc)
+ static const CarobNS::MBSCodec user_codec;
+};
+
/**
* Default (locale) String to Wide-String conversion.
* FIXME: throw exception on error
@@ -133,6 +150,17 @@
* @return converted string
*/
std::string toString(const std::wstring& in);
+
+inline std::wstring fromUTF8(const std::string& in)
+{
+ return StaticCodecs::fromUTF8(in);
+}
+
+inline std::string toUTF8(const std::wstring& in)
+{
+ return StaticCodecs::toUTF8(in);
+}
+
/**
* Converts integers to wstring
* @param i integer to convert
Index: carob/src/Common.cpp
diff -u carob/src/Common.cpp:1.15 carob/src/Common.cpp:1.16
--- carob/src/Common.cpp:1.15 Mon Dec 19 12:43:58 2005
+++ carob/src/Common.cpp Mon Dec 19 15:06:49 2005
@@ -38,11 +38,6 @@
// On linux try "locale -a"
#define NAME_OF_ANY_UTF8_LOCALE_AVAILABLE "en_US.utf8"
-const CarobNS::MBSCodec CarobNS::user_codec;
-
-const CarobNS::MBSCodec
CarobNS::utf8_codec(CarobNS::trylocale(NAME_OF_ANY_UTF8_LOCALE_AVAILABLE));
-
-
void CarobNS::setLogLevel(const LogLevel l)
{
CarobNS::currentLogLevel = l;
@@ -114,6 +109,8 @@
return result;
}
+const CarobNS::MBSCodec
CarobNS::StaticCodecs::utf8_codec(trylocale(NAME_OF_ANY_UTF8_LOCALE_AVAILABLE));
+
wstring CarobNS::fromString(const string& in)
{
bool ret = true;
Index: carob/test/01-Unit/TestStringCodecs.cpp
diff -u carob/test/01-Unit/TestStringCodecs.cpp:1.3
carob/test/01-Unit/TestStringCodecs.cpp:1.4
--- carob/test/01-Unit/TestStringCodecs.cpp:1.3 Mon Dec 19 12:43:58 2005
+++ carob/test/01-Unit/TestStringCodecs.cpp Mon Dec 19 15:06:49 2005
@@ -73,7 +73,7 @@
// ENCODE
- string utf8s(utf8_codec.encode(wide_cae));
+ string utf8s(toUTF8(wide_cae));
string latin9s(latin9_codec.encode(wide_cae));
string iso7s;
@@ -96,7 +96,7 @@
// DECODE BACK AND COMPARE
- CPPUNIT_ASSERT(0 == wide_cae.compare(utf8_codec.decode(utf8s)));
+ CPPUNIT_ASSERT(0 == wide_cae.compare(fromUTF8(utf8s)));
CPPUNIT_ASSERT(0 == wide_cae.compare(latin9_codec.decode(latin9s)));
CPPUNIT_ASSERT(0 == wide_pbk.compare(iso7_codec.decode(iso7s)));
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits