Commit 057c4ca3 authored by Ronan's avatar Ronan

feat(Object): provide a L_SYNC macro to synchronize Object's data

parent 09d29ea3
......@@ -30,8 +30,13 @@
LINPHONE_BEGIN_NAMESPACE
class ObjectPrivate : public BaseObjectPrivate {
protected:
constexpr const Object::Lock &getLock () const {
return lock;
}
private:
std::unordered_map<std::string, Variant> properties;
Object::Lock lock;
L_DECLARE_PUBLIC(Object);
};
......
......@@ -46,4 +46,9 @@ shared_ptr<const Object> Object::getSharedFromThis () const {
return nullptr;
}
inline const Object::Lock &Object::getLock () const {
L_D();
return d->getLock();
}
LINPHONE_END_NAMESPACE
......@@ -21,12 +21,21 @@
#define _OBJECT_H_
#include <memory>
#include <mutex>
#include "base-object.h"
#include "property-container.h"
// =============================================================================
// Must be used in Object or ObjectPrivate.
#define L_SYNC() \
static_assert( \
!std::is_base_of<Object, decltype(this)>::value && !std::is_base_of<ObjectPrivate, decltype(this)>::value, \
"Unable to lock. Instance is not an Object or ObjectPrivate." \
); \
const std::lock_guard<Object::Lock> synchronized(const_cast<Object::Lock &>(getLock()));
LINPHONE_BEGIN_NAMESPACE
/*
......@@ -38,14 +47,17 @@ class LINPHONE_PUBLIC Object :
public std::enable_shared_from_this<Object>,
public BaseObject,
public PropertyContainer {
public:
typedef std::recursive_mutex Lock;
std::shared_ptr<Object> getSharedFromThis ();
std::shared_ptr<const Object> getSharedFromThis () const;
protected:
explicit Object (ObjectPrivate &p);
const Lock &getLock () const;
private:
L_DECLARE_PRIVATE(Object);
L_DISABLE_COPY(Object);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment