diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-07-18 21:50:41 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-07-18 21:50:41 +0200 |
| commit | c28d3777bf5ee435e313e700bde2c0c84562b369 (patch) | |
| tree | ee4132b427074a31e121afbd234ff394bda2df41 /include | |
| parent | 41b3ee4c93182d10d717ab1d7e40c33034a53f7a (diff) | |
| download | Project-Tick-c28d3777bf5ee435e313e700bde2c0c84562b369.tar.gz Project-Tick-c28d3777bf5ee435e313e700bde2c0c84562b369.zip | |
Add value::as method for conversion
Make conversion operators explicit
Diffstat (limited to 'include')
| -rw-r--r-- | include/value.h | 35 |
1 files changed, 28 insertions, 7 deletions
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<class T> + T& as(); + template<class T> + 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> tag_; }; +template<class T> +T& value::as() +{ + return dynamic_cast<T&>(get()); +} + +template<class T> +const T& value::as() const +{ + return dynamic_cast<T&>(get()); +} + } #endif // TAG_REF_PROXY_H_INCLUDED |
