diff options
| author | Mike Buland <mbuland@penny-arcade.com> | 2019-04-18 19:09:04 -0700 |
|---|---|---|
| committer | Mike Buland <mbuland@penny-arcade.com> | 2019-04-18 19:09:04 -0700 |
| commit | 62753c815b5ec34ebfae37a3c89187a01cc17160 (patch) | |
| tree | d6f85d4af7d54c88a7486a3207c33a168ae09bd8 /src/stable/variant.h | |
| parent | d81e824732c33e2d5ce31fbc58571561a8d9b21f (diff) | |
| download | libbu++-62753c815b5ec34ebfae37a3c89187a01cc17160.tar.gz libbu++-62753c815b5ec34ebfae37a3c89187a01cc17160.tar.bz2 libbu++-62753c815b5ec34ebfae37a3c89187a01cc17160.tar.xz libbu++-62753c815b5ec34ebfae37a3c89187a01cc17160.zip | |
Minor quality of life additions to string and variant.
More coming to string. I need to figure some things out in variant.
Diffstat (limited to 'src/stable/variant.h')
| -rw-r--r-- | src/stable/variant.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/stable/variant.h b/src/stable/variant.h index af06815..7189d7a 100644 --- a/src/stable/variant.h +++ b/src/stable/variant.h | |||
| @@ -35,6 +35,7 @@ namespace Bu | |||
| 35 | virtual const std::type_info &getType() const=0; | 35 | virtual const std::type_info &getType() const=0; |
| 36 | virtual VariantTypeRoot *clone() const=0; | 36 | virtual VariantTypeRoot *clone() const=0; |
| 37 | virtual void format( Bu::Formatter &f ) const=0; | 37 | virtual void format( Bu::Formatter &f ) const=0; |
| 38 | virtual const void *rawData() const=0; | ||
| 38 | }; | 39 | }; |
| 39 | 40 | ||
| 40 | template<class t> | 41 | template<class t> |
| @@ -75,6 +76,11 @@ namespace Bu | |||
| 75 | { | 76 | { |
| 76 | f << data; | 77 | f << data; |
| 77 | } | 78 | } |
| 79 | |||
| 80 | virtual const void *rawData() const | ||
| 81 | { | ||
| 82 | return &data; | ||
| 83 | } | ||
| 78 | 84 | ||
| 79 | virtual const std::type_info &getType() const | 85 | virtual const std::type_info &getType() const |
| 80 | { | 86 | { |
| @@ -173,6 +179,21 @@ namespace Bu | |||
| 173 | } | 179 | } |
| 174 | return dynamic_cast<VariantType<t> *>(pCore)->getData(); | 180 | return dynamic_cast<VariantType<t> *>(pCore)->getData(); |
| 175 | } | 181 | } |
| 182 | |||
| 183 | template<class t> | ||
| 184 | t &cast() const | ||
| 185 | { | ||
| 186 | if( !pCore ) | ||
| 187 | { | ||
| 188 | throw Bu::ExceptionBase("No data!"); | ||
| 189 | } | ||
| 190 | |||
| 191 | t *p = dynamic_cast<t *>(pCore->rawData()); | ||
| 192 | if( p == NULL ) | ||
| 193 | throw Bu::ExceptionBase("Invalid type conversion."); | ||
| 194 | |||
| 195 | return *p; | ||
| 196 | } | ||
| 176 | 197 | ||
| 177 | template<class t> | 198 | template<class t> |
| 178 | void set( const t &val ) | 199 | void set( const t &val ) |
