Created
December 18, 2019 16:34
-
-
Save tyhenry/19c88fd0edd9b2f0b30fbd7ee13fc79c to your computer and use it in GitHub Desktop.
C++ "dictionary" - mixed-type map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C++11 "any" class for multi-type storage | |
// https://stackoverflow.com/a/24702400/5195277 | |
// (better to use C++17 std::any or std::variant) | |
class AnyBase | |
{ | |
public: | |
virtual ~AnyBase() = 0; | |
}; | |
inline AnyBase::~AnyBase() {} | |
template<class T> | |
class Any : public AnyBase | |
{ | |
public: | |
typedef T Type; | |
explicit Any(const Type& data) : data(data) {} | |
Any() {} | |
Type data; | |
}; | |
template <class T> | |
using AnyMap = std::map<T, std::unique_ptr<AnyBase>>; | |
using AnyDict = AnyMap<std::string>; | |
// usage: | |
/* | |
AnyDict anydict; | |
anydict["number"].reset(new Any<int>(5)); // create / update | |
anydict["text"].reset(new Any<std::string>("5")); | |
// throws std::bad_cast if not really Any<int> | |
int value = dynamic_cast<Any<int>&>(*anydict["number"]).data; | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Any.h" | |
class Dictionary | |
{ | |
public: | |
bool has( const std::string& key ) | |
{ | |
return std::find( dict.begin(), dict.end(), key ) != dict.end(); | |
} | |
template <class T> | |
bool find( const std::string& key, T& result ) const | |
{ | |
if (!has(key)){ return false; } | |
try { | |
// throws std::bad_cast if wrong type | |
result = dynamic_cast<Any<T>&>( *dict.at(key) ).data; | |
return true; | |
} catch ( std::bad_cast& e ) { | |
std::cerr << "Dictionary ['" << key << "'] found, but wrong type - exception: " << e.what() << std::endl; | |
// todo: typeid | |
} // todo: catch others | |
return false; | |
} | |
template <class T> | |
void set( const std::string& key, const T& value ) | |
{ | |
dict[key].reset( new Any<T>( value ) ); // create / update | |
} | |
template <class T> | |
bool create( const std::string& key, const T& value ) | |
{ | |
if ( !has( key ) ) { // not found? | |
set( key, value ); | |
return true; | |
} | |
return false; | |
} | |
protected: | |
AnyDict dict; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment