Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <fir/str/str.h>
- #include <map>
- #include <vector>
- #include <string>
- #include <string.h>
- #include <stdexcept>
- namespace fir {
- namespace ds {
- class Dmap {
- enum Type {
- TYPE_NULL,
- TYPE_LONG,
- TYPE_ULONG,
- TYPE_STRING_POINTER,
- TYPE_STRING,
- TYPE_MAP,
- TYPE_VECTOR
- };
- public:
- typedef std::map<std::string, Dmap> MapType;
- typedef std::vector<Dmap> VectorType;
- Type type_ = TYPE_NULL;
- union Value {
- long val_long;
- unsigned long val_ulong;
- str::BuffPtr val_string;
- MapType *val_map;
- VectorType *val_vector;
- Value() {
- }
- };
- Value m_value;
- Dmap() = default;
- Dmap(const Dmap& _rhs)
- : type_( TYPE_NULL )
- //, m_value{0L}
- {
- this->operator=( _rhs );
- }
- explicit Dmap(const std::string &_str)
- : type_( TYPE_NULL )
- {
- this->operator=(_str);
- }
- virtual ~Dmap()
- {
- forget();
- }
- operator bool() const { return TYPE_NULL != type_; }
- bool isLong() { return TYPE_LONG == type_; }
- bool isULong() { return TYPE_ULONG == type_; }
- bool isNumeric() { return (TYPE_LONG == type_) || (TYPE_ULONG == type_); }
- bool isString() { return ((TYPE_STRING_POINTER == type_) || (TYPE_STRING == type_)); }
- bool isVector() { return TYPE_VECTOR == type_; }
- bool isMap() { return TYPE_MAP == type_; }
- long getLong() {
- if ( TYPE_LONG == type_ ) {
- return m_value.val_long;
- }
- if ( TYPE_ULONG == type_ ) {
- return m_value.val_ulong;
- }
- return 0;
- }
- unsigned long getULong() {
- if ( TYPE_LONG == type_ ) {
- return m_value.val_long;
- }
- if ( TYPE_ULONG == type_ ) {
- return m_value.val_ulong;
- }
- return 0;
- }
- str::BuffPtr getString() {
- if ( ! ((TYPE_STRING_POINTER == type_) || (TYPE_STRING == type_)) ) {
- return str::BuffPtr();
- }
- return m_value.val_string;
- }
- VectorType& get_vector()
- {
- if ( TYPE_VECTOR != type_ )
- throw std::runtime_error("not a vector");
- return *(m_value.val_vector);
- }
- Dmap& operator=(const Dmap& _rhs) {
- forget();
- type_ = _rhs.type_;
- switch (type_) {
- default: {
- m_value.val_long = _rhs.m_value.val_long;
- m_value.val_ulong = _rhs.m_value.val_ulong;
- break;
- }
- case TYPE_STRING: {
- m_value.val_string.ptr = new char[_rhs.m_value.val_string.size];
- m_value.val_string.size = _rhs.m_value.val_string.size;
- m_value.val_string.clone(_rhs.m_value.val_string);
- break;
- }
- case TYPE_STRING_POINTER: {
- m_value.val_string = _rhs.m_value.val_string;
- break;
- }
- case TYPE_VECTOR: {
- m_value.val_vector = new VectorType;
- *(m_value.val_vector) = *(_rhs.m_value.val_vector);
- break;
- }
- case TYPE_MAP: {
- m_value.val_map = new MapType;
- *(m_value.val_map) = *(_rhs.m_value.val_map);
- break;
- }
- }
- return *this;
- }
- Dmap& operator=(int _val) {
- forget();
- type_ = TYPE_LONG;
- m_value.val_long = _val;
- return *this;
- }
- Dmap& operator=(unsigned int _val) {
- forget();
- type_ = TYPE_ULONG;
- m_value.val_long = _val;
- return *this;
- }
- Dmap& operator=(long _val) {
- forget();
- type_ = TYPE_LONG;
- m_value.val_long = _val;
- return *this;
- }
- Dmap& operator=(unsigned long _val) {
- forget();
- type_ = TYPE_ULONG;
- m_value.val_ulong = _val;
- return *this;
- }
- Dmap& operator=(const str::BuffPtr& _buffptr) {
- forget();
- type_ = TYPE_STRING;
- char *s = new char[_buffptr.size];
- memcpy(s, _buffptr.ptr, _buffptr.size);
- m_value.val_string.ptr = s;
- m_value.val_string.size = _buffptr.size;
- return *this;
- }
- Dmap& operator=(const std::string &_str) {
- return this->operator=(str::BuffPtr(_str));
- }
- Dmap& operator=(const char *_str) {
- if ( ! _str ) {
- this->operator=( (int) 0);
- return *this;
- }
- return this->operator=(str::BuffPtr(_str));
- }
- Dmap& set_ref(const char *_str) {
- return set_ref( str::BuffPtr(_str) );
- }
- Dmap& set_ref(const std::string &_str) {
- return set_ref(str::BuffPtr(_str));
- }
- Dmap& set_ref(const str::BuffPtr& _buffptr) {
- forget();
- type_ = TYPE_STRING_POINTER;
- m_value.val_string = _buffptr;
- return *this;
- }
- Dmap& make_vector() {
- forget();
- m_value.val_vector = new VectorType;
- type_ = TYPE_VECTOR;
- return *this;
- }
- void push_back( const Dmap &_rhs) {
- if ( TYPE_VECTOR != type_ ) {
- return;
- }
- m_value.val_vector->push_back(_rhs);
- }
- Dmap& operator[](const char *_key) {
- if ( TYPE_MAP != type_ ) {
- forget();
- type_ = TYPE_MAP;
- m_value.val_map = new MapType;
- }
- return (*(m_value.val_map))[ _key ];
- }
- Dmap& operator[](const std::string &_key) {
- return operator[]( _key.c_str() );
- }
- Dmap& operator[](int _index) {
- return operator[]((size_t)_index);
- }
- Dmap& operator[](size_t _index) {
- if ( TYPE_VECTOR != type_ ) {
- make_vector();
- }
- if ( _index >= m_value.val_vector->size() ) {
- m_value.val_vector->resize( _index + 1 );
- }
- return (*(m_value.val_vector))[ _index ];
- }
- size_t size() {
- if ( TYPE_VECTOR == type_ ) {
- return m_value.val_vector->size();
- }
- return 0;
- }
- protected:
- void forget() {
- switch(type_) {
- case TYPE_STRING:
- if (m_value.val_string.size) {
- delete[] m_value.val_string.ptr;
- m_value.val_string.size = 0;
- m_value.val_string.ptr = nullptr;
- }
- break;
- case TYPE_MAP:
- delete m_value.val_map;
- break;
- case TYPE_VECTOR:
- delete m_value.val_vector;
- break;
- default:
- break;
- }
- type_ = TYPE_NULL;
- }
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement