From 62753c815b5ec34ebfae37a3c89187a01cc17160 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 18 Apr 2019 19:09:04 -0700 Subject: Minor quality of life additions to string and variant. More coming to string. I need to figure some things out in variant. --- src/stable/string.cpp | 19 +++++++++++++++++++ src/stable/string.h | 4 ++++ src/stable/variant.h | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/stable/string.cpp b/src/stable/string.cpp index ce679fe..3029584 100644 --- a/src/stable/string.cpp +++ b/src/stable/string.cpp @@ -964,6 +964,25 @@ Bu::String Bu::String::toUpper() const return sRet; } +int16_t Bu::String::toInt16( int iRadix ) const +{ + flatten(); + return strtol( core->pFirst->pData, NULL, iRadix ); +} + +int32_t Bu::String::toInt32( int iRadix ) const +{ + flatten(); + return strtol( core->pFirst->pData, NULL, iRadix ); +} + +int64_t Bu::String::toInt64( int iRadix ) const +{ + flatten(); + return strtoll( core->pFirst->pData, NULL, iRadix ); +} + + Bu::String::const_iterator Bu::String::find( const char cChar, Bu::String::const_iterator iStart ) const { diff --git a/src/stable/string.h b/src/stable/string.h index 131f621..af147a7 100644 --- a/src/stable/string.h +++ b/src/stable/string.h @@ -879,6 +879,10 @@ namespace Bu */ String toUpper() const; + int16_t toInt16( int iRadix=10 ) const; + int32_t toInt32( int iRadix=10 ) const; + int64_t toInt64( int iRadix=10 ) const; + const_iterator find( const char cChar, const_iterator iStart=const_iterator() ) const; 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 virtual const std::type_info &getType() const=0; virtual VariantTypeRoot *clone() const=0; virtual void format( Bu::Formatter &f ) const=0; + virtual const void *rawData() const=0; }; template @@ -75,6 +76,11 @@ namespace Bu { f << data; } + + virtual const void *rawData() const + { + return &data; + } virtual const std::type_info &getType() const { @@ -173,6 +179,21 @@ namespace Bu } return dynamic_cast *>(pCore)->getData(); } + + template + t &cast() const + { + if( !pCore ) + { + throw Bu::ExceptionBase("No data!"); + } + + t *p = dynamic_cast(pCore->rawData()); + if( p == NULL ) + throw Bu::ExceptionBase("Invalid type conversion."); + + return *p; + } template void set( const t &val ) -- cgit v1.2.3