From c28d3777bf5ee435e313e700bde2c0c84562b369 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Sat, 18 Jul 2015 21:50:41 +0200 Subject: Add value::as method for conversion Make conversion operators explicit --- include/value.h | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'include/value.h') diff --git a/include/value.h b/include/value.h index a03395c2d1..c82aeb9097 100644 --- a/include/value.h +++ b/include/value.h @@ -66,6 +66,15 @@ public: tag& get(); const tag& get() const; + /** + * @brief Returns the contained tag as an instance of T + * @throw std::bad_cast if the tag is not of type T + */ + template + T& as(); + template + const T& as() const; + //Assignment of primitives and string /** * @brief Assigns the given value to the tag if the type is compatible @@ -92,12 +101,12 @@ public: * @throw std::bad_cast if the tag type is not convertible to the desired * type via a widening conversion */ - operator int8_t() const; - operator int16_t() const; - operator int32_t() const; - operator int64_t() const; - operator float() const; - operator double() const; + explicit operator int8_t() const; + explicit operator int16_t() const; + explicit operator int32_t() const; + explicit operator int64_t() const; + explicit operator float() const; + explicit operator double() const; /** * @brief Returns the contained string if the type is tag_string @@ -105,7 +114,7 @@ public: * If the value is uninitialized, the behavior is undefined. * @throw std::bad_cast if the tag type is not tag_string */ - operator const std::string&() const; + explicit operator const std::string&() const; ///Returns true if the value is not uninitialized explicit operator bool() const; @@ -167,6 +176,18 @@ private: std::unique_ptr tag_; }; +template +T& value::as() +{ + return dynamic_cast(get()); +} + +template +const T& value::as() const +{ + return dynamic_cast(get()); +} + } #endif // TAG_REF_PROXY_H_INCLUDED -- cgit 0.0.5-2-1-g0f52