observers.remove(ob);
}
-bool DSObjectPrivate::setProperty(std::string key, DSProperty &&property)
-{
+void DSObjectPrivate::setProperty(std::string key, const DSPropertyVariant& data, DSPropertyType type)
+{
+ std::unordered_map<std::string, DSProperty>::const_iterator ci = properties.find(key);
+ if (ci == properties.end()){
+ properties.emplace(key, DSProperty(key, data, type));
+ }
+ else {
+ properties[key] = DSProperty(key, data, type);
+ }
}
-DSProperty &DSObjectPrivate::getProperty(std::string key)
+const DSPropertyVariant& DSObjectPrivate::getProperty(std::string key)
{
+ std::unordered_map<std::string, DSProperty>::const_iterator ci = properties.find(key);
+ if (ci == properties.end()){
+ throw DSPropertyException("NOT_EXIST");
+ }
+ return properties[key].getData();
}
void DSObjectPrivate::notifyDestroy(void)
d_func()->detachDestroyObserver(ob);
}
-bool DSObject::setProperty(std::string key, const DSPropertyVariant& data, DSPropertyType type)
+void DSObject::setProperty(std::string key, const DSPropertyVariant& data, DSPropertyType type)
{
+ d_func()->setProperty(key, data, type);
}
-DSPropertyVariant &DSObject::getProperty(std::string key)
+const DSPropertyVariant& DSObject::getProperty(std::string key)
{
+ return d_func()->getProperty(key);
}
std::string DSObject::getName(void)
void attachDestroyObserver(DSObjectObserverIface *ob);
void detachDestroyObserver(DSObjectObserverIface *ob);
- bool setProperty(std::string key, const DSPropertyVariant &data, DSPropertyType type);
- DSPropertyVariant &getProperty(std::string key);
+ void setProperty(std::string key, const DSPropertyVariant &data, DSPropertyType type);
+ const DSPropertyVariant& getProperty(std::string key);
std::string getName(void);
void detachDestroyObserver(DSObjectObserverIface *ob);
void notifyDestroy();
- bool setProperty(std::string key, DSProperty &&property);
- DSProperty &getProperty(std::string key);
+ void setProperty(std::string key, const DSPropertyVariant& data, DSPropertyType type);
+ const DSPropertyVariant& getProperty(std::string key);
std::string getName(void);
#include "DSProperty.h"
namespace display_server {
-DSProperty::DSProperty(std::string _key, DSPropertyVariant &_data, DSPropertyType _type) : key(_key), type(_type)
+DSProperty::DSProperty()
{
- switch (_type)
+}
+
+DSProperty::DSProperty(std::string key, const DSPropertyVariant &data, DSPropertyType type) : __key(key), __type(type)
+{
+ switch (__type)
+ {
+ //copy case
+ case DSPropertyType::INTEGER:
+ case DSPropertyType::DOUBLE:
+ case DSPropertyType::FLOAT:
+ case DSPropertyType::STRING:
+ __data = data; //value copy
+ break;
+ case DSPropertyType::POINTER:
+ __data = data; //address copy
+ break;
+ default:
+ break;
+ }
+}
+
+DSProperty::DSProperty(const DSProperty& property) //Copy Constructor
+{
+ __type = property.__type;
+ __key = property.__key;
+
+ switch (__type)
+ {
+ //copy case
+ case DSPropertyType::INTEGER:
+ case DSPropertyType::DOUBLE:
+ case DSPropertyType::FLOAT:
+ case DSPropertyType::STRING:
+ __data = property.__data; //value copy
+ break;
+ case DSPropertyType::POINTER:
+ __data = property.__data; //address copy
+ break;
+ default:
+ break;
+ }
+}
+
+DSProperty::DSProperty(DSProperty&& property) //Move Constructor
+{
+ __type = property.__type;
+ __key = property.__key;
+
+ switch (__type)
{
//copy case
case DSPropertyType::INTEGER:
case DSPropertyType::DOUBLE:
case DSPropertyType::FLOAT:
case DSPropertyType::STRING:
- data = _data; //copy
+ __data = property.__data; //value copy
+ property.__data = 0;
break;
- case DSPropertyType::OBJECT:
- data = (&_data); //address
+ case DSPropertyType::POINTER:
+ __data = property.__data; //address copy
+ property.__data = nullptr;
break;
default:
break;
}
+
+ property.__type = DSPropertyType::NONE;
+ property.__key.clear();
}
DSProperty::~DSProperty()
{
+ __key.clear();
+}
+
+DSProperty& DSProperty::operator=(DSProperty& property) //Copy Assign
+{
+ __type = property.__type;
+ __key = property.__key;
+
+ switch (__type)
+ {
+ //copy case
+ case DSPropertyType::INTEGER:
+ case DSPropertyType::DOUBLE:
+ case DSPropertyType::FLOAT:
+ case DSPropertyType::STRING:
+ __data = property.__data; //value copy
+ break;
+ case DSPropertyType::POINTER:
+ __data = property.__data; //address copy
+ break;
+ default:
+ break;
+ }
+
+ return *this;
+}
+
+DSProperty& DSProperty::operator=(const DSProperty& property) //Copy Assign
+{
+ __type = property.__type;
+ __key = property.__key;
+
+ switch (__type)
+ {
+ //copy case
+ case DSPropertyType::INTEGER:
+ case DSPropertyType::DOUBLE:
+ case DSPropertyType::FLOAT:
+ case DSPropertyType::STRING:
+ __data = property.__data; //value copy
+ break;
+ case DSPropertyType::POINTER:
+ __data = property.__data; //address copy
+ break;
+ default:
+ break;
+ }
+
+ return *this;
+}
+
+DSProperty& DSProperty::operator=(DSProperty&& property) //Move Assign
+{
+ __type = property.__type;
+ __key = property.__key;
+
+ switch (__type)
+ {
+ //copy case
+ case DSPropertyType::INTEGER:
+ case DSPropertyType::DOUBLE:
+ case DSPropertyType::FLOAT:
+ case DSPropertyType::STRING:
+ __data = property.__data; //value copy
+ property.__data = 0;
+ break;
+ case DSPropertyType::POINTER:
+ __data = property.__data; //address copy
+ property.__data = nullptr;
+ break;
+ default:
+ break;
+ }
+
+ property.__type = DSPropertyType::NONE;
+ property.__key.clear();
+
+ return *this;
+}
+
+const DSPropertyVariant& DSProperty::getData(void) const
+{
+ return __data;
+}
+
+const std::string DSProperty::getKey(void) const
+{
+ return __key;
+}
+
+const DSPropertyType DSProperty::getType(void) const
+{
+ return __type;
}
} // namespace display_server
#define __DSPROPERTY_H
#include <string>
#include <variant>
+#include <memory>
+#include <iostream>
+#include "DSPropertyPrivate.h"
namespace display_server {
using DSPropertyType = enum type {
NONE,
- INTEGER,
- FLOAT,
- DOUBLE,
- STRING,
- OBJECT,
+ INTEGER, //int
+ FLOAT, //float
+ DOUBLE, //double
+ STRING, //std::string
+ POINTER, //void*
};
using DSPropertyVariant = std::variant<int,
std::string,
void *>;
+struct DSPropertyException : public std::exception{
+ std::string msg;
+ public:
+ DSPropertyException(std::string str) : msg(str) {}
+ virtual const char* what() const noexcept override
+ {
+ return msg.c_str();
+ }
+};
+
class DSProperty {
public:
- DSProperty(std::string key, DSPropertyVariant& data, DSPropertyType type);
+ DSProperty();
+ DSProperty(std::string key, const DSPropertyVariant& data, DSPropertyType type);
+ DSProperty(const DSProperty& property);
+ DSProperty(DSProperty&& property);
virtual ~DSProperty();
+
+ const DSPropertyVariant& getData(void) const;
+ const std::string getKey(void) const;
+ const DSPropertyType getType(void) const;
+
+ DSProperty& operator=(DSProperty& property);
+ DSProperty& operator=(const DSProperty& property);
+ DSProperty& operator=(DSProperty&& property);
+
private:
- std::string key;
- DSPropertyVariant data;
- DSPropertyType type;
+ DSPropertyVariant __data;
+ std::string __key;
+ DSPropertyType __type;
+
+ friend class DSObject;
};
} // namespace display_server
#else
--- /dev/null
+#include "libds-tests.h"
+#include "DSProperty.h"
+
+class DSPropertyTest : public ::testing::Test
+{
+public:
+ void SetUp(void) override
+ {
+ std::cout << "setup DSPropertyTest\n";
+ }
+ void TearDown(void) override
+ {
+ std::cout << "teardown DSPropertyTest\n";
+ }
+};
+
+TEST_F(DSPropertyTest, CreateProperty)
+{
+ /* display_server::DSProperty::DSProperty()*/
+ display_server::DSProperty* pro = new display_server::DSProperty();
+ ASSERT_NE(pro, nullptr) << "Failed to DSProperty()";
+ delete pro;
+
+ /* display_server::DSProperty::DSProperty(std::__cxx11::string, const DSPropertyVariant&, display_server::DSPropertyType) */
+ pro = new display_server::DSProperty("testProperty", 2020, display_server::DSPropertyType::INTEGER);
+ ASSERT_NE(pro, nullptr) << "Failed to DSProperty(key, data, type)";
+ ASSERT_EQ(pro->getKey(), "testProperty") << "Key value mismatch!";
+ ASSERT_EQ(pro->getType(), display_server::DSPropertyType::INTEGER) << "Type mistmatch!!";
+ try {
+ int data = std::get<int>(pro->getData());
+ ASSERT_TRUE(data == 2020) << "data value mismatch!!";
+ } catch (std::bad_variant_access& e) {
+ ASSERT_TRUE(false) << e.what();
+ }
+ delete pro;
+
+ /* display_server::DSProperty::DSProperty(display_server::DSProperty&) */
+ display_server::DSProperty pro_s("testProperty2", 20.20, display_server::DSPropertyType::DOUBLE);
+ pro = new display_server::DSProperty(pro_s);
+ ASSERT_NE(pro, nullptr) << "Failed to DSProperty(const DSProperty&)";
+ ASSERT_EQ(pro->getKey(), "testProperty2") << "Key value mismatch!";
+ ASSERT_EQ(pro->getType(), display_server::DSPropertyType::DOUBLE) << "Type mistmatch!!";
+ try {
+ double data = std::get<double>(pro->getData());
+ ASSERT_TRUE(data == 20.20) << "data value mismatch!!";
+ } catch (std::bad_variant_access& e) {
+ ASSERT_TRUE(false) << e.what();
+ }
+ delete pro;
+}
int count = refBase->getref();
ASSERT_TRUE(count == 1);
-}
\ No newline at end of file
+}
incdir = include_directories('../src/lib')
libds_unittests_src = ['libds-tests.cpp',
- 'DSRefBase-test.cpp']
+ 'DSRefBase-test.cpp',
+ 'DSProperty-test.cpp']
gmock_dep = dependency('gmock', method : 'pkg-config')
executable('libds-unittests', libds_unittests_src,
- include_directories : incdir,
- dependencies : gmock_dep,
- install_dir : dir_bin,
- install : true
- )
+ include_directories : incdir,
+ link_with : lib_libds,
+ dependencies : gmock_dep,
+ install_dir : dir_bin,
+ install : true
+ )