DSProperty: property system prototype added with testcase 87/241487/1
authorMinJeong Kim <minjjj.kim@samsung.com>
Tue, 25 Feb 2020 06:50:09 +0000 (15:50 +0900)
committerSung-Jin Park <sj76.park@samsung.com>
Thu, 20 Aug 2020 09:43:29 +0000 (18:43 +0900)
Change-Id: I8d74c9bd93644f4529ae5a0fb4215eb0e6c67b59
Signed-off-by: MinJeong Kim <minjjj.kim@samsung.com>
src/lib/DSObject.cpp
src/lib/DSObject.h
src/lib/DSObjectPrivate.h
src/lib/DSProperty.cpp
src/lib/DSProperty.h
tests/DSProperty-test.cpp [new file with mode: 0644]
tests/DSRefBase-test.cpp
tests/meson.build

index 2d13870..f0ee030 100644 (file)
@@ -23,12 +23,24 @@ void DSObjectPrivate::detachDestroyObserver(DSObjectObserverIface *ob)
        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)
@@ -64,12 +76,14 @@ void DSObject::detachDestroyObserver(DSObjectObserverIface *ob)
        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)
index 48b2696..1dce237 100644 (file)
@@ -21,8 +21,8 @@ class DSObject {
        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);
 
index 6af36df..94c5f6c 100644 (file)
@@ -24,8 +24,8 @@ public:
        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);
 
index 5510add..c3869bc 100644 (file)
 #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
index 2270c70..96768bd 100644 (file)
@@ -2,15 +2,18 @@
 #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,
@@ -19,14 +22,38 @@ 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
diff --git a/tests/DSProperty-test.cpp b/tests/DSProperty-test.cpp
new file mode 100644 (file)
index 0000000..9c27f31
--- /dev/null
@@ -0,0 +1,50 @@
+#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;
+}
index 57ab821..9fa6a68 100644 (file)
@@ -18,4 +18,4 @@ TEST_F(DSRefBaseTest, CreateRefBase)
 
        int count = refBase->getref();
        ASSERT_TRUE(count == 1);
-}
\ No newline at end of file
+}
index 663d13e..03ef65f 100644 (file)
@@ -1,12 +1,14 @@
 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
+           )