Moved common functionality of ContactObject and ContactArray into separate class. 56/57456/1
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Wed, 20 Jan 2016 07:57:11 +0000 (09:57 +0200)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Wed, 20 Jan 2016 08:24:54 +0000 (10:24 +0200)
Change-Id: I65d53fd1cfdda23c225a8328a9576943df2d0166
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
lib-contact/inc/Contacts/Model/ContactArray.h
lib-contact/inc/Contacts/Model/ContactFieldContainer.h [new file with mode: 0644]
lib-contact/inc/Contacts/Model/ContactFieldIterator.h [moved from lib-contact/inc/Contacts/Model/ContactIterator.h with 57% similarity]
lib-contact/inc/Contacts/Model/ContactObject.h
lib-contact/src/Contacts/Model/ContactArray.cpp
lib-contact/src/Contacts/Model/ContactFieldContainer.cpp [new file with mode: 0644]
lib-contact/src/Contacts/Model/ContactFieldIterator.cpp [new file with mode: 0644]
lib-contact/src/Contacts/Model/ContactObject.cpp

index cbf66f0..b2790c9 100644 (file)
@@ -18,8 +18,7 @@
 #ifndef CONTACTS_MODEL_CONTACT_ARRAY_H
 #define CONTACTS_MODEL_CONTACT_ARRAY_H
 
-#include "Contacts/Model/ContactField.h"
-#include "Contacts/Model/ContactIterator.h"
+#include "Contacts/Model/ContactFieldContainer.h"
 
 namespace Contacts
 {
@@ -30,10 +29,10 @@ namespace Contacts
                /**
                 * @brief Adaptor for property containing multiple records.
                 */
-               class ContactArray : public ContactField
+               class ContactArray : public ContactFieldContainer
                {
                public:
-                       using ContactField::ContactField;
+                       using ContactFieldContainer::ContactFieldContainer;
 
                        /**
                         * @see ContactField::initialize()
@@ -41,28 +40,6 @@ namespace Contacts
                        virtual void initialize() override;
 
                        /**
-                        * @see ContactField::isEmpty()
-                        */
-                       virtual bool isEmpty() const override;
-
-                       /**
-                        * @return Begin iterator.
-                        */
-                       ContactArrayIterator begin() const;
-
-                       /**
-                        * @return End iterator.
-                        */
-                       ContactArrayIterator end() const;
-
-                       /**
-                        * @brief Get child field by index.
-                        * @param[in]   index   Child field index
-                        * @return Child field.
-                        */
-                       ContactField *getField(unsigned index) const;
-
-                       /**
                         * @brief Add new child field.
                         * @return New child field.
                         */
@@ -76,8 +53,6 @@ namespace Contacts
 
                private:
                        const ContactArrayMetadata &getArrayMetadata() const;
-
-                       ContactFields m_Fields;
                };
        }
 }
diff --git a/lib-contact/inc/Contacts/Model/ContactFieldContainer.h b/lib-contact/inc/Contacts/Model/ContactFieldContainer.h
new file mode 100644 (file)
index 0000000..8175e8c
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef CONTACTS_MODEL_CONTACT_FIELD_CONTAINER_H
+#define CONTACTS_MODEL_CONTACT_FIELD_CONTAINER_H
+
+#include "Contacts/Model/ContactField.h"
+#include "Contacts/Model/ContactFieldIterator.h"
+
+namespace Contacts
+{
+       namespace Model
+       {
+               /**
+                * @brief Base class for a field containing other fields.
+                */
+               class ContactFieldContainer : public ContactField
+               {
+               public:
+                       using ContactField::ContactField;
+
+                       /**
+                        * @see ContactField::isEmpty()
+                        */
+                       virtual bool isEmpty() const override;
+
+                       /**
+                        * @see ContactField::reset()
+                        */
+                       virtual void reset() override;
+
+                       /**
+                        * @return Begin iterator.
+                        */
+                       ContactFieldIterator begin() const;
+
+                       /**
+                        * @return End iterator.
+                        */
+                       ContactFieldIterator end() const;
+
+                       /**
+                        * @brief Get child field by index.
+                        * @param[in]   index   Child field index
+                        * @return Child field.
+                        */
+                       ContactField *getField(unsigned index) const;
+
+               protected:
+                       ContactField &addField(contacts_record_h record,
+                                       const ContactFieldMetadata &metadata);
+                       void removeField(ContactField &field);
+
+               private:
+                       ContactFields m_Fields;
+               };
+       }
+}
+
+#endif /* CONTACTS_MODEL_CONTACT_FIELD_CONTAINER_H */
  *
  */
 
-#ifndef CONTACTS_MODEL_CONTACT_ITERATOR_H
-#define CONTACTS_MODEL_CONTACT_ITERATOR_H
+#ifndef CONTACTS_MODEL_CONTACT_FIELD_ITERATOR_H
+#define CONTACTS_MODEL_CONTACT_FIELD_ITERATOR_H
 
-#include "Contacts/Model/ContactField.h"
 #include <iterator>
 
 namespace Contacts
 {
        namespace Model
        {
-               class ContactArray;
-               class ContactObject;
+               class ContactField;
+               class ContactFieldContainer;
 
                /**
-                * @brief Index-based iterator template for ContactArray and ContactObject.
+                * @brief Index-based iterator for ContactFieldContainer.
                 */
-               template <typename FieldContainer>
-               class ContactIterator :
+               class ContactFieldIterator :
                        public std::iterator<std::input_iterator_tag, ContactField>
                {
                public:
@@ -41,32 +39,28 @@ namespace Contacts
                         * @param[in]   container   Container with getField() method
                         * @param[in]   index       Index of the field pointed by iterator
                         */
-                       ContactIterator(const FieldContainer &container, int index)
-                               : m_Container(container), m_Index(index) { }
+                       ContactFieldIterator(const ContactFieldContainer &container, int index);
 
                        /**
                         * @brief Increment iterator.
                         */
-                       ContactIterator &operator++() { ++m_Index; return *this; }
+                       ContactFieldIterator &operator++();
 
                        /**
                         * @brief Get field pointed by iterator.
                         */
-                       ContactField &operator*() const { return *m_Container.getField(m_Index); }
+                       ContactField &operator*() const;
 
                        /**
                         * @brief Compare iterators for inequality.
                         */
-                       bool operator!=(const ContactIterator &that) const { return m_Index != that.m_Index;}
+                       bool operator!=(const ContactFieldIterator &that) const;
 
                protected:
-                       const FieldContainer &m_Container;
+                       const ContactFieldContainer &m_Container;
                        size_t m_Index;
                };
-
-               typedef ContactIterator<ContactArray> ContactArrayIterator;
-               typedef ContactIterator<ContactObject> ContactObjectIterator;
        }
 }
 
-#endif /* CONTACTS_MODEL_CONTACT_ITERATOR_H */
+#endif /* CONTACTS_MODEL_CONTACT_FIELD_ITERATOR_H */
index bc3b3ad..c64254e 100644 (file)
@@ -18,8 +18,7 @@
 #ifndef CONTACTS_MODEL_CONTACT_OBJECT_H
 #define CONTACTS_MODEL_CONTACT_OBJECT_H
 
-#include "Contacts/Model/ContactField.h"
-#include "Contacts/Model/ContactIterator.h"
+#include "Contacts/Model/ContactFieldContainer.h"
 
 namespace Contacts
 {
@@ -31,10 +30,10 @@ namespace Contacts
                 * @brief Adapter for contacts_record_h object which may itself be
                 *        a field of another object.
                 */
-               class ContactObject : public ContactField
+               class ContactObject : public ContactFieldContainer
                {
                public:
-                       using ContactField::ContactField;
+                       using ContactFieldContainer::ContactFieldContainer;
 
                        /**
                         * @see ContactField::initialize()
@@ -42,33 +41,6 @@ namespace Contacts
                        virtual void initialize() override;
 
                        /**
-                        * @see ContactField::isEmpty()
-                        */
-                       virtual bool isEmpty() const override;
-
-                       /**
-                        * @see ContactField::reset()
-                        */
-                       virtual void reset() override;
-
-                       /**
-                        * @return Begin iterator.
-                        */
-                       ContactObjectIterator begin() const;
-
-                       /**
-                        * @return End iterator.
-                        */
-                       ContactObjectIterator end() const;
-
-                       /**
-                        * @brief Get child field by index.
-                        * @param[in]   index   Child field index
-                        * @return Child field.
-                        */
-                       ContactField *getField(unsigned index) const;
-
-                       /**
                         * @brief Get child field by id.
                         * @param[in]   id      Child field id
                         * @return Child field.
@@ -77,9 +49,6 @@ namespace Contacts
 
                protected:
                        const ContactObjectMetadata &getObjectMetadata() const;
-
-               private:
-                       ContactFields m_Fields;
                };
        }
 }
index 4e13875..314cfcd 100644 (file)
  */
 
 #include "Contacts/Model/ContactArray.h"
-#include "Contacts/Model/ContactFactory.h"
 #include "Contacts/Model/ContactFieldMetadata.h"
 
-#include <algorithm>
-
 using namespace Contacts::Model;
 
 void ContactArray::initialize()
@@ -31,40 +28,8 @@ void ContactArray::initialize()
        for (int i = 0; i < count; ++i) {
                contacts_record_h record = nullptr;
                contacts_record_get_child_record_at_p(getRecord(), getPropertyId(), i, &record);
-               m_Fields.push_back(ContactFactory::createField(record, getArrayMetadata().element));
-       }
-}
-
-bool ContactArray::isEmpty() const
-{
-       bool isEmpty = true;
-       for (auto &&field : *this) {
-               if (!field.isEmpty()) {
-                       isEmpty = false;
-                       break;
-               }
-       }
-
-       return isEmpty;
-}
-
-ContactArrayIterator ContactArray::begin() const
-{
-       return ContactArrayIterator(*this, 0);
-}
-
-ContactArrayIterator ContactArray::end() const
-{
-       return ContactArrayIterator(*this, m_Fields.size());
-}
-
-ContactField *ContactArray::getField(unsigned index) const
-{
-       if (index < m_Fields.size()) {
-               return m_Fields[index].get();
+               ContactFieldContainer::addField(record, getArrayMetadata().element);
        }
-
-       return nullptr;
 }
 
 ContactField &ContactArray::addField()
@@ -76,23 +41,16 @@ ContactField &ContactArray::addField()
        contacts_record_create(uri, &record);
        contacts_record_add_child_record(getRecord(), getPropertyId(), record);
 
-       ContactFieldPtr field = ContactFactory::createField(record, getArrayMetadata().element);
-       field->reset();
-
-       m_Fields.push_back(std::move(field));
-       return *m_Fields.back().get();
+       ContactField &field = ContactFieldContainer::addField(record, getArrayMetadata().element);
+       field.reset();
+       return field;
 }
 
 void ContactArray::removeField(ContactField &field)
 {
-       auto comp = [&field](ContactFieldPtr &ptr) {
-               return ptr.get() == &field;
-       };
-
-       m_Fields.erase(std::remove_if(m_Fields.begin(), m_Fields.end(), comp), m_Fields.end());
-
        contacts_record_remove_child_record(getRecord(), getPropertyId(), field.getRecord());
        contacts_record_destroy(field.getRecord(), true);
+       ContactFieldContainer::removeField(field);
 }
 
 const ContactArrayMetadata &ContactArray::getArrayMetadata() const
diff --git a/lib-contact/src/Contacts/Model/ContactFieldContainer.cpp b/lib-contact/src/Contacts/Model/ContactFieldContainer.cpp
new file mode 100644 (file)
index 0000000..ccfefae
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "Contacts/Model/ContactFieldContainer.h"
+#include "Contacts/Model/ContactFactory.h"
+
+#include <algorithm>
+
+using namespace Contacts::Model;
+
+bool ContactFieldContainer::isEmpty() const
+{
+       bool isEmpty = true;
+       for (auto &&field : *this) {
+               if (!field.isEmpty()) {
+                       isEmpty = false;
+                       break;
+               }
+       }
+
+       return isEmpty;
+}
+
+void ContactFieldContainer::reset()
+{
+       for (auto &&field : *this) {
+               field.reset();
+       }
+}
+
+ContactFieldIterator ContactFieldContainer::begin() const
+{
+       return ContactFieldIterator(*this, 0);
+}
+
+ContactFieldIterator ContactFieldContainer::end() const
+{
+       return ContactFieldIterator(*this, m_Fields.size());
+}
+
+ContactField *ContactFieldContainer::getField(unsigned index) const
+{
+       if (index < m_Fields.size()) {
+               return m_Fields[index].get();
+       }
+
+       return nullptr;
+}
+
+ContactField &ContactFieldContainer::addField(contacts_record_h record,
+               const ContactFieldMetadata &metadata)
+{
+       ContactFieldPtr field = ContactFactory::createField(record, metadata);
+       m_Fields.push_back(std::move(field));
+       return *m_Fields.back();
+}
+
+void ContactFieldContainer::removeField(ContactField &field)
+{
+       auto comp = [&field](ContactFieldPtr &ptr) {
+               return ptr.get() == &field;
+       };
+
+       m_Fields.erase(std::remove_if(m_Fields.begin(), m_Fields.end(), comp), m_Fields.end());
+}
diff --git a/lib-contact/src/Contacts/Model/ContactFieldIterator.cpp b/lib-contact/src/Contacts/Model/ContactFieldIterator.cpp
new file mode 100644 (file)
index 0000000..f983d64
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "Contacts/Model/ContactFieldIterator.h"
+#include "Contacts/Model/ContactFieldContainer.h"
+
+using namespace Contacts::Model;
+
+ContactFieldIterator::ContactFieldIterator(const ContactFieldContainer &container, int index)
+       : m_Container(container), m_Index(index)
+{
+}
+
+ContactFieldIterator &ContactFieldIterator::operator++()
+{
+       ++m_Index;
+       return *this;
+}
+
+ContactField &ContactFieldIterator::operator*() const
+{
+       return *m_Container.getField(m_Index);
+}
+
+bool ContactFieldIterator::operator!=(const ContactFieldIterator &that) const
+{
+       return m_Index != that.m_Index;
+}
index 0fd6606..1226d07 100644 (file)
@@ -16,7 +16,6 @@
  */
 
 #include "Contacts/Model/ContactObject.h"
-#include "Contacts/Model/ContactFactory.h"
 #include "Contacts/Model/ContactFieldMetadata.h"
 
 using namespace Contacts::Model;
@@ -24,54 +23,15 @@ using namespace Contacts::Model;
 void ContactObject::initialize()
 {
        for (auto &&field : getObjectMetadata().fields) {
-               m_Fields.push_back(ContactFactory::createField(getRecord(), field));
+               addField(getRecord(), field);
        }
 }
 
-bool ContactObject::isEmpty() const
-{
-       bool isEmpty = true;
-       for (auto &&field : *this) {
-               if (!field.isEmpty()) {
-                       isEmpty = false;
-                       break;
-               }
-       }
-
-       return isEmpty;
-}
-
-void ContactObject::reset()
-{
-       for (auto &&field : *this) {
-               field.reset();
-       }
-}
-
-ContactObjectIterator ContactObject::begin() const
-{
-       return ContactObjectIterator(*this, 0);
-}
-
-ContactObjectIterator ContactObject::end() const
-{
-       return ContactObjectIterator(*this, m_Fields.size());
-}
-
-ContactField *ContactObject::getField(unsigned index) const
-{
-       if (index < m_Fields.size()) {
-               return m_Fields[index].get();
-       }
-
-       return nullptr;
-}
-
 ContactField *ContactObject::getFieldById(unsigned id) const
 {
-       for (auto &&field : m_Fields) {
-               if (field->getId() == id) {
-                       return field.get();
+       for (auto &&field : *this) {
+               if (field.getId() == id) {
+                       return &field;
                }
        }