From d4d2794cd3c6571e74bd9bdcff5a07535a91eb9d Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 14 Sep 2017 15:21:29 +0200 Subject: [PATCH] feat(Object): add a PropertyContainer class --- src/CMakeLists.txt | 2 + src/object/clonable-object-p.h | 4 -- src/object/clonable-object.cpp | 16 -------- src/object/clonable-object.h | 10 ++--- src/object/object.cpp | 16 -------- src/object/object.h | 12 ++---- src/object/property-container.cpp | 61 +++++++++++++++++++++++++++++++ src/object/property-container.h | 54 +++++++++++++++++++++++++++ 8 files changed, 123 insertions(+), 52 deletions(-) create mode 100644 src/object/property-container.cpp create mode 100644 src/object/property-container.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 773f308f8..4c2fe44a2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -89,6 +89,7 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES object/clonable-object.h object/object-p.h object/object.h + object/property-container.h object/singleton.h utils/payload-type-handler.h variant/variant.h @@ -140,6 +141,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES nat/stun-client.cpp object/clonable-object.cpp object/object.cpp + object/property-container.cpp utils/general.cpp utils/payload-type-handler.cpp utils/utils.cpp diff --git a/src/object/clonable-object-p.h b/src/object/clonable-object-p.h index 87266a2c5..7d507599f 100644 --- a/src/object/clonable-object-p.h +++ b/src/object/clonable-object-p.h @@ -23,8 +23,6 @@ #include "linphone/utils/general.h" -#include "variant/variant.h" - // ============================================================================= LINPHONE_BEGIN_NAMESPACE @@ -43,8 +41,6 @@ private: int nRefs = 0; - std::unordered_map properties; - L_DECLARE_PUBLIC(ClonableObject); // It's forbidden to copy directly one Clonable object private. diff --git a/src/object/clonable-object.cpp b/src/object/clonable-object.cpp index 72a9f290d..45412dc88 100644 --- a/src/object/clonable-object.cpp +++ b/src/object/clonable-object.cpp @@ -82,20 +82,4 @@ void ClonableObject::setRef (const ClonableObjectPrivate &p) { mPrivate->ref(); } -Variant ClonableObject::getProperty (const string &name) const { - L_D(const ClonableObject); - auto it = d->properties.find(name); - return it == d->properties.cend() ? Variant() : it->second; -} - -void ClonableObject::setProperty (const string &name, const Variant &value) { - L_D(ClonableObject); - d->properties[name] = value; -} - -void ClonableObject::setProperty (const string &name, Variant &&value) { - L_D(ClonableObject); - d->properties[name] = move(value); -} - LINPHONE_END_NAMESPACE diff --git a/src/object/clonable-object.h b/src/object/clonable-object.h index e36c5add2..89fe66b3f 100644 --- a/src/object/clonable-object.h +++ b/src/object/clonable-object.h @@ -23,20 +23,16 @@ #include "linphone/utils/general.h" +#include "property-container.h" + // ============================================================================= LINPHONE_BEGIN_NAMESPACE -class Variant; - -class LINPHONE_PUBLIC ClonableObject { +class LINPHONE_PUBLIC ClonableObject : public PropertyContainer { public: virtual ~ClonableObject (); - Variant getProperty (const std::string &name) const; - void setProperty (const std::string &name, const Variant &value); - void setProperty (const std::string &name, Variant &&value); - protected: // Use a new ClonableObjectPrivate without owner. explicit ClonableObject (ClonableObjectPrivate &p); diff --git a/src/object/object.cpp b/src/object/object.cpp index b201df143..01457e82d 100644 --- a/src/object/object.cpp +++ b/src/object/object.cpp @@ -34,20 +34,4 @@ Object::Object (ObjectPrivate &p) : mPrivate(&p) { mPrivate->mPublic = this; } -Variant Object::getProperty (const string &name) const { - L_D(const Object); - auto it = d->properties.find(name); - return it == d->properties.cend() ? Variant() : it->second; -} - -void Object::setProperty (const string &name, const Variant &value) { - L_D(Object); - d->properties[name] = value; -} - -void Object::setProperty (const string &name, Variant &&value) { - L_D(Object); - d->properties[name] = move(value); -} - LINPHONE_END_NAMESPACE diff --git a/src/object/object.h b/src/object/object.h index 0941664d9..7dae752a6 100644 --- a/src/object/object.h +++ b/src/object/object.h @@ -19,24 +19,18 @@ #ifndef _OBJECT_H_ #define _OBJECT_H_ -#include - #include "linphone/utils/general.h" +#include "property-container.h" + // ============================================================================= LINPHONE_BEGIN_NAMESPACE -class Variant; - -class LINPHONE_PUBLIC Object { +class LINPHONE_PUBLIC Object : public PropertyContainer { public: virtual ~Object (); - Variant getProperty (const std::string &name) const; - void setProperty (const std::string &name, const Variant &value); - void setProperty (const std::string &name, Variant &&value); - protected: explicit Object (ObjectPrivate &p); diff --git a/src/object/property-container.cpp b/src/object/property-container.cpp new file mode 100644 index 000000000..0cbc8f712 --- /dev/null +++ b/src/object/property-container.cpp @@ -0,0 +1,61 @@ +/* + * property-container.cpp + * Copyright (C) 2017 Belledonne Communications SARL + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "property-container.h" + +// ============================================================================= + +using namespace std; + +LINPHONE_BEGIN_NAMESPACE + +class PropertyContainerPrivate { +public: + unordered_map properties; +}; + +// ----------------------------------------------------------------------------- + +// Empty copy constructor. Don't change this pattern. +// PropertyContainer is an Entity component, not a simple structure. +// An Entity is UNIQUE. +PropertyContainer::PropertyContainer (const PropertyContainer &) {} + +PropertyContainer &PropertyContainer::operator= (const PropertyContainer &) { + return *this; +} + +Variant PropertyContainer::getProperty (const string &name) const { + L_D(const PropertyContainer); + auto it = d->properties.find(name); + return it == d->properties.cend() ? Variant() : it->second; +} + +void PropertyContainer::setProperty (const string &name, const Variant &value) { + L_D(PropertyContainer); + d->properties[name] = value; +} + +void PropertyContainer::setProperty (const string &name, Variant &&value) { + L_D(PropertyContainer); + d->properties[name] = move(value); +} + +LINPHONE_END_NAMESPACE diff --git a/src/object/property-container.h b/src/object/property-container.h new file mode 100644 index 000000000..c8cf7724d --- /dev/null +++ b/src/object/property-container.h @@ -0,0 +1,54 @@ +/* + * property-container.h + * Copyright (C) 2017 Belledonne Communications SARL + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _PROPERTY_CONTAINER_H_ +#define _PROPERTY_CONTAINER_H_ + +#include + +#include "linphone/utils/general.h" + +#include "variant/variant.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class PropertyContainerPrivate; + +class LINPHONE_PUBLIC PropertyContainer { +public: + PropertyContainer () = default; + PropertyContainer (const PropertyContainer &src); + virtual ~PropertyContainer () = default; + + PropertyContainer &operator= (const PropertyContainer &src); + + Variant getProperty (const std::string &name) const; + void setProperty (const std::string &name, const Variant &value); + void setProperty (const std::string &name, Variant &&value); + +private: + PropertyContainerPrivate *mPrivate = nullptr; + + L_DECLARE_PRIVATE(PropertyContainer); +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _PROPERTY_CONTAINER_H_