From efa5e425f9c7334b9eedbcdfb1928ed0b396ec97 Mon Sep 17 00:00:00 2001 From: Jonathan Robie Date: Mon, 1 Nov 2010 14:18:07 +0000 Subject: Changed Variant::fromString() to Variant::parse(). Also changed implementation. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1029673 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/include/qpid/types/Variant.h | 6 ++++- cpp/src/qpid/messaging/AddressParser.cpp | 3 +-- cpp/src/qpid/types/Variant.cpp | 46 +++++++++----------------------- 3 files changed, 19 insertions(+), 36 deletions(-) (limited to 'cpp') diff --git a/cpp/include/qpid/types/Variant.h b/cpp/include/qpid/types/Variant.h index 2b692f9bdf..9ae672b7c2 100644 --- a/cpp/include/qpid/types/Variant.h +++ b/cpp/include/qpid/types/Variant.h @@ -113,7 +113,11 @@ class Variant QPID_TYPES_EXTERN Variant& operator=(const Variant&); QPID_TYPES_EXTERN Variant& operator=(const Uuid&); - QPID_TYPES_EXTERN Variant& fromString(const std::string&); + /** + * Parses the argument and assigns itself the appropriate + * value. Recognises integers, doubles and booleans. + */ + QPID_TYPES_EXTERN Variant& parse(const std::string&); QPID_TYPES_EXTERN bool asBool() const; QPID_TYPES_EXTERN uint8_t asUint8() const; diff --git a/cpp/src/qpid/messaging/AddressParser.cpp b/cpp/src/qpid/messaging/AddressParser.cpp index d088b94f32..4c8f35fbc5 100644 --- a/cpp/src/qpid/messaging/AddressParser.cpp +++ b/cpp/src/qpid/messaging/AddressParser.cpp @@ -201,9 +201,8 @@ bool AddressParser::readSimpleValue(Variant& value) { std::string s; if (readWord(s)) { - value.fromString(s); + value.parse(s); return true; - } else { return false; } diff --git a/cpp/src/qpid/types/Variant.cpp b/cpp/src/qpid/types/Variant.cpp index ea4f5ffbbf..5d8878bdac 100644 --- a/cpp/src/qpid/types/Variant.cpp +++ b/cpp/src/qpid/types/Variant.cpp @@ -767,39 +767,19 @@ Variant& Variant::operator=(const Variant& v) return *this; } - -template -bool from_string(T& t, const std::string& s) -{ - char c; // Make sure there are no extra characters - - std::istringstream iss(s); - return !(iss >> t).fail() && (iss>>c).fail(); -} - -Variant& Variant::fromString(const std::string& s) -{ - double d; - int i; - - if (from_string(i, s)) { - return operator=(i); - } - else if (from_string(d, s)) { - return operator=(d); - } - else { - std::string upper(boost::to_upper_copy(s)); - if (upper == "TRUE") { - return operator=(true); - } - else if (upper == "FALSE") { - return operator=(false); - } - else { - return operator=(s); - } - } +Variant& Variant::parse(const std::string& s) +{ + operator=(s); + try { + return operator=(asInt64()); + } catch (const InvalidConversion&) {} + try { + return operator=(asDouble()); + } catch (const InvalidConversion&) {} + try { + return operator=(asBool()); + } catch (const InvalidConversion&) {} + return *this; } -- cgit v1.2.1