cast to array and cast to object implemented, refactored shared-ptr and clone() methods to utilize std::make_shared (see issue #7)
This commit is contained in:
parent
bcc6eaff82
commit
b8d4581569
|
|
@ -62,9 +62,9 @@ public:
|
||||||
* Create a new instance of this object
|
* Create a new instance of this object
|
||||||
* @return Field*
|
* @return Field*
|
||||||
*/
|
*/
|
||||||
virtual Field *clone() const override
|
virtual std::shared_ptr<Field> clone() const override
|
||||||
{
|
{
|
||||||
return new Array(*this);
|
return std::make_shared<Array>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -84,7 +84,7 @@ public:
|
||||||
Array set(uint8_t index, const Field &value)
|
Array set(uint8_t index, const Field &value)
|
||||||
{
|
{
|
||||||
// construct a shared pointer
|
// construct a shared pointer
|
||||||
auto ptr = std::shared_ptr<Field>(value.clone());
|
auto ptr = value.clone();
|
||||||
|
|
||||||
// should we overwrite an existing record?
|
// should we overwrite an existing record?
|
||||||
if (index >= _fields.size())
|
if (index >= _fields.size())
|
||||||
|
|
@ -181,7 +181,7 @@ public:
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
||||||
// loop through all members
|
// loop through all members
|
||||||
for (auto iter : _fields)
|
for (auto &iter : _fields)
|
||||||
{
|
{
|
||||||
// split with comma
|
// split with comma
|
||||||
if (!first) stream << ",";
|
if (!first) stream << ",";
|
||||||
|
|
@ -196,6 +196,16 @@ public:
|
||||||
// postfix
|
// postfix
|
||||||
stream << ")";
|
stream << ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast to array
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
virtual operator const Array& () const override
|
||||||
|
{
|
||||||
|
// this already is an array, so no cast is necessary
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -78,9 +78,9 @@ public:
|
||||||
* Extending from field forces us to implement a clone function.
|
* Extending from field forces us to implement a clone function.
|
||||||
* @return shared_ptr
|
* @return shared_ptr
|
||||||
*/
|
*/
|
||||||
virtual Field *clone() const override
|
virtual std::shared_ptr<Field> clone() const override
|
||||||
{
|
{
|
||||||
return new BooleanSet(*this);
|
return std::make_shared<BooleanSet>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -83,9 +83,9 @@ public:
|
||||||
* Create a new identical instance of this object
|
* Create a new identical instance of this object
|
||||||
* @return Field*
|
* @return Field*
|
||||||
*/
|
*/
|
||||||
virtual Field *clone() const override
|
virtual std::shared_ptr<Field> clone() const override
|
||||||
{
|
{
|
||||||
return new DecimalField(_places, _number);
|
return std::make_shared<DecimalField>(_places, _number);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ public:
|
||||||
* Create a new instance on the heap of this object, identical to the object passed
|
* Create a new instance on the heap of this object, identical to the object passed
|
||||||
* @return Field*
|
* @return Field*
|
||||||
*/
|
*/
|
||||||
virtual Field *clone() const = 0;
|
virtual std::shared_ptr<Field> clone() const = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the size this field will take when
|
* Get the size this field will take when
|
||||||
|
|
@ -64,6 +64,25 @@ public:
|
||||||
* @param std::ostream
|
* @param std::ostream
|
||||||
*/
|
*/
|
||||||
virtual void output(std::ostream &stream) const = 0;
|
virtual void output(std::ostream &stream) const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Casting operators
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
virtual operator const std::string& () const;
|
||||||
|
virtual operator const char * () const { return nullptr; }
|
||||||
|
virtual operator uint8_t () const { return 0; }
|
||||||
|
virtual operator uint16_t () const { return 0; }
|
||||||
|
virtual operator uint32_t () const { return 0; }
|
||||||
|
virtual operator uint64_t () const { return 0; }
|
||||||
|
virtual operator int8_t () const { return 0; }
|
||||||
|
virtual operator int16_t () const { return 0; }
|
||||||
|
virtual operator int32_t () const { return 0; }
|
||||||
|
virtual operator int64_t () const { return 0; }
|
||||||
|
virtual operator float () const { return 0; }
|
||||||
|
virtual operator double () const { return 0; }
|
||||||
|
virtual operator const Array& () const;
|
||||||
|
virtual operator const Table& () const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -197,6 +197,30 @@ public:
|
||||||
return operator=(std::string(value));
|
return operator=(std::string(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign an array value
|
||||||
|
* @param value
|
||||||
|
* @return FieldProxy
|
||||||
|
*/
|
||||||
|
FieldProxy &operator=(const Array &value)
|
||||||
|
{
|
||||||
|
// assign value and allow chaining
|
||||||
|
_source->set(_index, value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign a table value
|
||||||
|
* @param value
|
||||||
|
* @return FieldProxy
|
||||||
|
*/
|
||||||
|
FieldProxy &operator=(const Table &value)
|
||||||
|
{
|
||||||
|
// assign value and allow chaining
|
||||||
|
_source->set(_index, value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the underlying field
|
* Get the underlying field
|
||||||
* @return Field
|
* @return Field
|
||||||
|
|
@ -210,97 +234,8 @@ public:
|
||||||
* Get a boolean
|
* Get a boolean
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
operator bool ()
|
template <typename TARGET>
|
||||||
{
|
operator TARGET () const
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get numeric value
|
|
||||||
* @return int8_t
|
|
||||||
*/
|
|
||||||
operator int8_t ()
|
|
||||||
{
|
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get numeric value
|
|
||||||
* @return uint8_t
|
|
||||||
*/
|
|
||||||
operator uint8_t ()
|
|
||||||
{
|
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get numeric value
|
|
||||||
* @return int16_t
|
|
||||||
*/
|
|
||||||
operator int16_t ()
|
|
||||||
{
|
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get numeric value
|
|
||||||
* @return uint16_t
|
|
||||||
*/
|
|
||||||
operator uint16_t ()
|
|
||||||
{
|
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get numeric value
|
|
||||||
* @return int32_t
|
|
||||||
*/
|
|
||||||
operator int32_t ()
|
|
||||||
{
|
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get numeric value
|
|
||||||
* @return uint32_t
|
|
||||||
*/
|
|
||||||
operator uint32_t ()
|
|
||||||
{
|
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get numeric value
|
|
||||||
* @return int64_t
|
|
||||||
*/
|
|
||||||
operator int64_t ()
|
|
||||||
{
|
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get numeric value
|
|
||||||
* @return uint64_t
|
|
||||||
*/
|
|
||||||
operator uint64_t ()
|
|
||||||
{
|
|
||||||
// retrieve the value
|
|
||||||
return _source->get(_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get string value
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
operator std::string ()
|
|
||||||
{
|
{
|
||||||
// retrieve the value
|
// retrieve the value
|
||||||
return _source->get(_index);
|
return _source->get(_index);
|
||||||
|
|
|
||||||
|
|
@ -78,10 +78,10 @@ public:
|
||||||
* Create a new instance of this object
|
* Create a new instance of this object
|
||||||
* @return Field*
|
* @return Field*
|
||||||
*/
|
*/
|
||||||
virtual Field *clone() const override
|
virtual std::shared_ptr<Field> clone() const override
|
||||||
{
|
{
|
||||||
// create a new copy of ourselves and return it
|
// create a new copy of ourselves and return it
|
||||||
return new NumericField(_value);
|
return std::make_shared<NumericField>(_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -100,7 +100,7 @@ public:
|
||||||
* Get the value
|
* Get the value
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
operator T () const
|
virtual operator T () const override
|
||||||
{
|
{
|
||||||
return _value;
|
return _value;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,10 +58,10 @@ public:
|
||||||
* Create a new instance of this object
|
* Create a new instance of this object
|
||||||
* @return Field*
|
* @return Field*
|
||||||
*/
|
*/
|
||||||
virtual Field *clone() const override
|
virtual std::shared_ptr<Field> clone() const override
|
||||||
{
|
{
|
||||||
// create a new copy of ourselves and return it
|
// create a new copy of ourselves and return it
|
||||||
return new StringField(_data);
|
return std::make_shared<StringField>(_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -96,7 +96,7 @@ public:
|
||||||
* Get the value
|
* Get the value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
operator const std::string& () const
|
virtual operator const std::string& () const override
|
||||||
{
|
{
|
||||||
return _data;
|
return _data;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,9 +76,9 @@ public:
|
||||||
* Create a new instance on the heap of this object, identical to the object passed
|
* Create a new instance on the heap of this object, identical to the object passed
|
||||||
* @return Field*
|
* @return Field*
|
||||||
*/
|
*/
|
||||||
virtual Field *clone() const override
|
virtual std::shared_ptr<Field> clone() const override
|
||||||
{
|
{
|
||||||
return new Table(*this);
|
return std::make_shared<Table>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -96,7 +96,7 @@ public:
|
||||||
Table set(const std::string& name, const Field &value)
|
Table set(const std::string& name, const Field &value)
|
||||||
{
|
{
|
||||||
// copy to a new pointer and store it
|
// copy to a new pointer and store it
|
||||||
_fields[name] = std::shared_ptr<Field>(value.clone());
|
_fields[name] = value.clone();
|
||||||
|
|
||||||
// allow chaining
|
// allow chaining
|
||||||
return *this;
|
return *this;
|
||||||
|
|
@ -122,6 +122,16 @@ public:
|
||||||
return AssociativeFieldProxy(this, name);
|
return AssociativeFieldProxy(this, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a field
|
||||||
|
*
|
||||||
|
* @param name field name
|
||||||
|
*/
|
||||||
|
AssociativeFieldProxy operator[](const char *name)
|
||||||
|
{
|
||||||
|
return AssociativeFieldProxy(this, name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a const field
|
* Get a const field
|
||||||
*
|
*
|
||||||
|
|
@ -132,6 +142,16 @@ public:
|
||||||
return get(name);
|
return get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a const field
|
||||||
|
*
|
||||||
|
* @param name field name
|
||||||
|
*/
|
||||||
|
const Field &operator[](const char *name) const
|
||||||
|
{
|
||||||
|
return get(name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write encoded payload to the given buffer.
|
* Write encoded payload to the given buffer.
|
||||||
* @param buffer
|
* @param buffer
|
||||||
|
|
@ -160,7 +180,7 @@ public:
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
||||||
// loop through all members
|
// loop through all members
|
||||||
for (auto iter : _fields)
|
for (auto &iter : _fields)
|
||||||
{
|
{
|
||||||
// split with comma
|
// split with comma
|
||||||
if (!first) stream << ",";
|
if (!first) stream << ",";
|
||||||
|
|
@ -175,6 +195,17 @@ public:
|
||||||
// postfix
|
// postfix
|
||||||
stream << ")";
|
stream << ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast to table
|
||||||
|
* @return Table
|
||||||
|
*/
|
||||||
|
virtual operator const Table& () const override
|
||||||
|
{
|
||||||
|
// this already is an array, so no cast is necessary
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,45 @@ Field *Field::decode(ReceivedFrame &frame)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast to string
|
||||||
|
* @return std::string
|
||||||
|
*/
|
||||||
|
Field::operator const std::string& () const
|
||||||
|
{
|
||||||
|
// static empty string
|
||||||
|
static std::string empty;
|
||||||
|
|
||||||
|
// return it
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast to array
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
Field::operator const Array& () const
|
||||||
|
{
|
||||||
|
// static empty array
|
||||||
|
static Array empty;
|
||||||
|
|
||||||
|
// return it
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast to object
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
Field::operator const Table& () const
|
||||||
|
{
|
||||||
|
// static empty table
|
||||||
|
static Table empty;
|
||||||
|
|
||||||
|
// return it
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* End of namespace
|
* End of namespace
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue