Commit a01a0641 authored by gladish's avatar gladish

Added some support for Variant arrays.

parent 66cd7a9b
......@@ -298,7 +298,7 @@ std::string MockAdapterValue::GetName()
return m_name;
}
common::Variant const& MockAdapterValue::GetData()
common::Variant const& MockAdapterValue::GetData() const
{
return m_data;
}
......
......@@ -94,7 +94,7 @@ namespace mock
MockAdapterValue(std::string const& name);
virtual std::string GetName();
virtual common::Variant const& GetData();
virtual common::Variant const& GetData() const;
virtual void SetData(common::Variant const& v);
private:
......
......@@ -3,9 +3,139 @@
QStatus
bridge::AllJoynHelper::SetMsgArg(IAdapterValue const& adapterValue, ajn::MsgArg& msgArg)
bridge::AllJoynHelper::SetMsgArg(IAdapterValue const& adapterValue, ajn::MsgArg& m)
{
return ER_NOT_IMPLEMENTED;
QStatus st = ER_OK;
std::string sig;
common::Variant const& val = adapterValue.GetData();
st = GetSignature(val.GetType(), sig);
if (st != ER_OK)
return st;
switch (val.GetType())
{
case common::Variant::DataType::Boolean:
{
bool b = val.ToBoolean();
st = m.Set(sig.c_str(), &b);
}
break;
case common::Variant::DataType::UInt8:
{
uint8_t i = val.ToUInt8();
st = m.Set(sig.c_str(), &i);
}
break;
case common::Variant::DataType::Int16:
{
int16_t i = val.ToInt16();
st = m.Set(sig.c_str(), &i);
}
break;
case common::Variant::DataType::UInt16:
{
uint16_t u = val.ToUInt16();
st = m.Set(sig.c_str(), &u);
}
break;
case common::Variant::DataType::Int32:
{
int32_t i = val.ToInt32();
st = m.Set(sig.c_str(), &i);
}
break;
case common::Variant::DataType::UInt32:
{
uint32_t u = val.ToUInt32();
st = m.Set(sig.c_str(), &u);
}
break;
case common::Variant::DataType::Int64:
{
int64_t l = val.ToInt64();
st = m.Set(sig.c_str(), &l);
}
break;
case common::Variant::DataType::UInt64:
{
uint64_t l = val.ToUInt64();
st = m.Set(sig.c_str(), &l);
}
break;
case common::Variant::DataType::Double:
{
double d = val.ToDouble();
st = m.Set(sig.c_str(), &d);
}
break;
case common::Variant::DataType::String:
{
std::string s = val.ToString();
if (!s.empty())
{
st = m.Set(sig.c_str(), s.c_str());
if (st == ER_OK)
m.Stabilize();
}
else
{
st = m.Set(sig.c_str(), "");
}
}
break;
case common::Variant::DataType::BooleanArray:
st = SetMsgArg<bool>(m, sig.c_str(), val.ToBooleanArray());
break;
case common::Variant::DataType::UInt8Array:
st = SetMsgArg<uint8_t>(m, sig.c_str(), val.ToUInt8Array());
break;
case common::Variant::DataType::Int16Array:
st = SetMsgArg<int16_t>(m, sig.c_str(), val.ToInt16Array());
break;
case common::Variant::DataType::UInt16Array:
st = SetMsgArg<uint16_t>(m, sig.c_str(), val.ToUInt16Array());
break;
case common::Variant::DataType::Int32Array:
st = SetMsgArg<int32_t>(m, sig.c_str(), val.ToInt32Array());
break;
case common::Variant::DataType::UInt32Array:
st = SetMsgArg<uint32_t>(m, sig.c_str(), val.ToUInt32Array());
break;
case common::Variant::DataType::Int64Array:
st = SetMsgArg<int64_t>(m, sig.c_str(), val.ToInt64Array());
break;
case common::Variant::DataType::UInt64Array:
st = SetMsgArg<uint64_t>(m, sig.c_str(), val.ToUInt64Array());
break;
case common::Variant::DataType::DoubleArray:
st = SetMsgArg<double>(m, sig.c_str(), val.ToDoubleArray());
break;
case common::Variant::DataType::StringArray:
st = SetMsgArg<std::string>(m, sig.c_str(), val.ToStringArray());
break;
}
return st;
}
QStatus
......@@ -139,7 +269,6 @@ bridge::AllJoynHelper::EncodePropertyOrMethodOrSignalName(std::string const& s,
upperCaseNextChar = true;
}
}
}
void
......
......@@ -16,10 +16,10 @@ namespace bridge
static QStatus SetMsgArg(IAdapterValue const& adapterValue, ajn::MsgArg& msgArg);
template<class T>
static QStatus SetMsgArg(ajn::MsgArg& msgArg, std::string const& signature, std::vector<T> const& arr);
static QStatus SetMsgArg(ajn::MsgArg& msgArg, std::string const& sig, std::vector<T> const& arr);
template<class T>
static QStatus GetArrayFromMsgArg(ajn::MsgArg& msgArg, std::string const& signature, std::vector<T>& arr);
static QStatus GetArrayFromMsgArg(ajn::MsgArg& msgArg, std::string const& sig, std::vector<T>& arr);
static QStatus SetMsgArgFromAdapterObject(IAdapterValue const& adapterValue, ajn::MsgArg& msg, DeviceMain* deviceMain);
......@@ -38,5 +38,12 @@ namespace bridge
static void EncodeStringForAppName(std::string const& s, std::string &encodeString);
static std::string TrimChar(std::string const& s, char c);
};
template<class T>
QStatus AllJoynHelper::SetMsgArg(ajn::MsgArg& msgArg, std::string const& sig, std::vector<T> const& arr)
{
QStatus st = ER_OK;
return st;
}
}
......@@ -37,7 +37,7 @@ namespace bridge
public:
virtual ~IAdapterValue() { }
virtual std::string GetName() = 0;
virtual common::Variant const& GetData() = 0;
virtual common::Variant const& GetData() const = 0;
virtual void SetData(common::Variant const& msg) = 0;
};
......
#include "Variant.h"
common::Variant::Variant()
: m_type(DataType::Invalid)
{
memset(&m_data, 0, sizeof(m_data));
}
common::Variant::Variant(bool b)
: m_type(DataType::Boolean)
{
m_data.v_bool = b;
}
common::Variant::Variant(uint8_t y)
: m_type(DataType::UInt8)
{
m_data.v_uint8 = y;
}
common::Variant::Variant(int16_t n)
: m_type(DataType::Int16)
{
m_data.v_int16 = n;
}
common::Variant::Variant(uint16_t q)
: m_type(DataType::UInt16)
{
m_data.v_uint16 = q;
}
common::Variant::Variant(int32_t i)
: m_type(DataType::Int32)
{
m_data.v_int32 = i;
}
common::Variant::Variant(uint32_t u)
: m_type(DataType::UInt32)
{
m_data.v_uint32 = u;
}
common::Variant::Variant(int64_t x)
: m_type(DataType::Int64)
{
m_data.v_int64 = x;
}
common::Variant::Variant(uint64_t t)
: m_type(DataType::UInt64)
{
m_data.v_uint64 = t;
}
common::Variant::Variant(double d)
: m_type(DataType::Double)
{
m_data.v_double = d;
}
common::Variant::Variant(DataType t)
: m_type(t)
{
memset(&m_data, 0, sizeof(m_data));
}
common::Variant::Variant(std::string const& s)
: m_type(DataType::String)
{
if (!m_data.v_string)
m_data.v_string = new std::string();
*m_data.v_string = s;
}
common::Variant::Variant(char const* s)
: m_type(DataType::String)
{
if (s)
{
if (!m_data.v_string)
m_data.v_string = new std::string();
*m_data.v_string = s;
}
}
common::Variant::Variant(Variant const& other)
{
AssignFrom(other);
}
common::Variant::~Variant()
{
if (m_type == DataType::String && m_data.v_string)
delete m_data.v_string;
}
common::Variant&
common::Variant::operator=(Variant const& other)
{
if (&other != this)
{
AssignFrom(other);
}
return *this;
}
std::vector<bool>
common::Variant::ToBooleanArray(bool* ok) const
{
return std::vector<bool>();
}
std::vector<uint8_t>
common::Variant::ToUInt8Array(bool* ok) const
{
return std::vector<uint8_t>();
}
std::vector<int16_t>
common::Variant::ToInt16Array(bool* ok) const
{
return std::vector<int16_t>();
}
std::vector<uint16_t>
common::Variant::ToUInt16Array(bool* ok) const
{
return std::vector<uint16_t>();
}
std::vector<int32_t>
common::Variant::ToInt32Array(bool* ok) const
{
return std::vector<int32_t>();
}
std::vector<uint32_t>
common::Variant::ToUInt32Array(bool* ok) const
{
return std::vector<uint32_t>();
}
std::vector<int64_t>
common::Variant::ToInt64Array(bool* ok) const
{
return std::vector<int64_t>();
}
std::vector<uint64_t>
common::Variant::ToUInt64Array(bool* ok) const
{
return std::vector<uint64_t>();
}
std::vector<double>
common::Variant::ToDoubleArray(bool* ok) const
{
return std::vector<double>();
}
std::vector<std::string>
common::Variant::ToStringArray(bool* ok) const
{
return std::vector<std::string>();
}
void
common::Variant::AssignFrom(Variant const& other)
{
switch (other.m_type)
{
case DataType::Invalid: break;
case DataType::Boolean: m_data.v_bool = other.m_data.v_bool; break;
case DataType::UInt8: m_data.v_uint8 = other.m_data.v_uint8; break;
case DataType::Int16: m_data.v_int16 = other.m_data.v_int16; break;
case DataType::UInt16: m_data.v_uint16 = other.m_data.v_uint16; break;
case DataType::Int32: m_data.v_int32 = other.m_data.v_int32; break;
case DataType::UInt32: m_data.v_uint32 = other.m_data.v_uint32; break;
case DataType::Int64: m_data.v_int64 = other.m_data.v_int64; break;
case DataType::UInt64: m_data.v_uint64 = other.m_data.v_uint64; break;
case DataType::Double: m_data.v_double = other.m_data.v_double; break;
case DataType::String:
{
if (other.m_data.v_string)
{
if (!m_data.v_string)
m_data.v_string = new std::string();
*m_data.v_string = *other.m_data.v_string;
}
}
break;
}
m_type = other.m_type;
}
bool
common::Variant::CanConvert(DataType t) const
{
return m_type == t;
}
std::string
common::Variant::ToString(bool* ok) const
{
// string handled special to allow for other data types
// to be converted later.
if (CanConvert(DataType::String))
{
if (ok) *ok = true;
if (m_data.v_string) return *m_data.v_string;
return std::string();
}
if (ok) *ok = false;
return std::string();
}
......@@ -3,7 +3,8 @@
#include <stdint.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <vector>
#include "defines.h"
namespace common
{
......@@ -11,7 +12,7 @@ namespace common
{
public:
enum class DataType
{
{
Invalid,
Boolean,
UInt8,
......@@ -37,60 +38,29 @@ namespace common
StringArray,
};
Variant() : m_type(DataType::Invalid) { m_data.v_uint64 = 0; }
Variant(bool b) : m_type(DataType::Boolean) { m_data.v_bool = b; }
Variant(uint8_t y) : m_type(DataType::UInt8) { m_data.v_uint8 = y; }
Variant(int16_t n) : m_type(DataType::Int16) { m_data.v_int16 = n; }
Variant(uint16_t q) : m_type(DataType::UInt16) { m_data.v_uint16 = q; }
Variant(int32_t i) : m_type(DataType::Int32) { m_data.v_int32 = i; }
Variant(uint32_t u) : m_type(DataType::UInt32) { m_data.v_uint32 = u; }
Variant(int64_t x) : m_type(DataType::Int64) { m_data.v_int64 = x; }
Variant(uint64_t t) : m_type(DataType::UInt64) { m_data.v_uint64 = t; }
Variant(double d) : m_type(DataType::Double) { m_data.v_double = d; }
Variant(DataType t) : m_type(t)
{
memset(&m_data, 0, sizeof(m_data));
}
Variant(std::string const& s) : m_type(DataType::String)
{
if (s.size())
m_data.v_string = strdup(s.c_str());
else
m_data.v_string = NULL;
}
Variant(char const* s) : m_type(DataType::String)
{
if (s)
m_data.v_string = strdup(s);
}
Variant(Variant const& other)
{
AssignFrom(other);
}
Variant& operator=(Variant const& other)
{
if (&other != this)
{
AssignFrom(other);
}
return *this;
}
~Variant()
{
if (m_type == DataType::String && m_data.v_string)
free(m_data.v_string);
}
Variant();
Variant(bool b);
Variant(uint8_t y);
Variant(int16_t n);
Variant(uint16_t q);
Variant(int32_t i);
Variant(uint32_t u);
Variant(int64_t x);
Variant(uint64_t t);
Variant(double d);
Variant(DataType t);
Variant(char const* s);
Variant(std::string const& s);
Variant(Variant const& other);
Variant& operator=(Variant const& other);
~Variant();
inline DataType GetType() const
{ return m_type; }
inline bool ToBool(bool* ok = NULL) const
inline bool ToBoolean(bool* ok = NULL) const
{ return Get<bool>(DataType::Boolean, ok); }
inline uint8_t ToUInt8(bool* ok = NULL) const
......@@ -117,29 +87,21 @@ namespace common
inline double ToDouble(bool* ok = NULL) const
{ return Get<uint16_t>(DataType::Double, ok); }
inline std::string ToString(bool* ok = NULL) const
{
// string handled special to allow for other data types
// to be converted later.
if (CanConvert(DataType::String))
{
if (ok)
*ok = true;
if (m_data.v_string)
return std::string(m_data.v_string);
return std::string();
}
if (ok)
*ok = false;
return std::string();
}
std::vector<bool> ToBooleanArray(bool* ok = NULL) const;
std::vector<uint8_t> ToUInt8Array(bool* ok = NULL) const;
std::vector<int16_t> ToInt16Array(bool* ok = NULL) const;
std::vector<uint16_t> ToUInt16Array(bool* ok = NULL) const;
std::vector<int32_t> ToInt32Array(bool* ok = NULL) const;
std::vector<uint32_t> ToUInt32Array(bool* ok = NULL) const;
std::vector<int64_t> ToInt64Array(bool* ok = NULL) const;
std::vector<uint64_t> ToUInt64Array(bool* ok = NULL) const;
std::vector<double> ToDoubleArray(bool* ok = NULL) const;
std::vector<std::string> ToStringArray(bool* ok = NULL) const;
std::string ToString(bool* ok = NULL) const;
private:
inline bool CanConvert(DataType t) const
{
// no conversion for now
return m_type == t;
}
bool CanConvert(DataType t) const;
template<class T>
T Convert(DataType t) const
......@@ -179,45 +141,18 @@ namespace common
DataType m_type;
union {
bool v_bool;
uint8_t v_uint8;
int16_t v_int16;
uint16_t v_uint16;
int32_t v_int32;
uint32_t v_uint32;
int64_t v_int64;
uint64_t v_uint64;
double v_double;
char* v_string;
bool v_bool;
uint8_t v_uint8;
int16_t v_int16;
uint16_t v_uint16;
int32_t v_int32;
uint32_t v_uint32;
int64_t v_int64;
uint64_t v_uint64;
double v_double;
std::string* v_string;
} m_data;
void AssignFrom(Variant const& other)
{
switch (other.m_type)
{
case DataType::Invalid: break;
case DataType::Boolean: m_data.v_bool = other.m_data.v_bool; break;
case DataType::UInt8: m_data.v_uint8 = other.m_data.v_uint8; break;
case DataType::Int16: m_data.v_int16 = other.m_data.v_int16; break;
case DataType::UInt16: m_data.v_uint16 = other.m_data.v_uint16; break;
case DataType::Int32: m_data.v_int32 = other.m_data.v_int32; break;
case DataType::UInt32: m_data.v_uint32 = other.m_data.v_uint32; break;
case DataType::Int64: m_data.v_int64 = other.m_data.v_int64; break;
case DataType::UInt64: m_data.v_uint64 = other.m_data.v_uint64; break;
case DataType::Double: m_data.v_double = other.m_data.v_double; break;
case DataType::String:
{
if (other.m_data.v_string)
{
if (m_data.v_string)
free(m_data.v_string);
m_data.v_string = strdup(other.m_data.v_string);
}
}
break;
}
m_type = other.m_type;
}
void AssignFrom(Variant const& other);
};
}
......@@ -7,6 +7,7 @@ SRCS=DeviceProviders/AllJoynProvider.cpp \
Bridge/BridgeDevice.cpp \
Bridge/ConfigManager.cpp \
Common/Log.cpp \
Common/Variant.cpp \
Adapters/MockAdapter/MockAdapter.cpp \
Adapters/MockAdapter/MockDevices.cpp \
Adapters/MockAdapter/MockAdapterDevice.cpp \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment