#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
{
/**
* @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()
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.
*/
private:
const ContactArrayMetadata &getArrayMetadata() const;
-
- ContactFields m_Fields;
};
}
}
--- /dev/null
+/*
+ * 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:
* @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 */
#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
{
* @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()
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.
protected:
const ContactObjectMetadata &getObjectMetadata() const;
-
- private:
- ContactFields m_Fields;
};
}
}
*/
#include "Contacts/Model/ContactArray.h"
-#include "Contacts/Model/ContactFactory.h"
#include "Contacts/Model/ContactFieldMetadata.h"
-#include <algorithm>
-
using namespace Contacts::Model;
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()
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
--- /dev/null
+/*
+ * 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());
+}
--- /dev/null
+/*
+ * 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;
+}
*/
#include "Contacts/Model/ContactObject.h"
-#include "Contacts/Model/ContactFactory.h"
#include "Contacts/Model/ContactFieldMetadata.h"
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;
}
}