Commit cd10ea5d authored by Ronan's avatar Ronan

feat(Object): provide a way to deal with user data (properties/variant)

parent 615174f4
......@@ -46,11 +46,11 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES
chat/cpim/parser/cpim-parser.h
chat/imdn.h
chat/is-composing.h
chat/real-time-text-chat-room-p.h
chat/real-time-text-chat-room.h
chat/modifier/chat-message-modifier.h
chat/modifier/multipart-chat-message-modifier.h
chat/modifier/cpim-chat-message-modifier.h
chat/modifier/multipart-chat-message-modifier.h
chat/real-time-text-chat-room-p.h
chat/real-time-text-chat-room.h
conference/conference-listener.h
conference/conference-p.h
conference/conference.h
......@@ -93,6 +93,7 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES
object/object.h
object/singleton.h
utils/payload-type-handler.h
variant/variant.h
)
set(LINPHONE_CXX_OBJECTS_SOURCE_FILES
......@@ -112,9 +113,9 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES
chat/cpim/parser/cpim-parser.cpp
chat/imdn.cpp
chat/is-composing.cpp
chat/real-time-text-chat-room.cpp
chat/modifier/multipart-chat-message-modifier.cpp
chat/modifier/cpim-chat-message-modifier.cpp
chat/modifier/multipart-chat-message-modifier.cpp
chat/real-time-text-chat-room.cpp
conference/conference.cpp
conference/local-conference.cpp
conference/params/call-session-params.cpp
......@@ -143,6 +144,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES
utils/general.cpp
utils/payload-type-handler.cpp
utils/utils.cpp
variant/variant.cpp
)
set(LINPHONE_CXX_OBJECTS_DEFINITIONS "-DLIBLINPHONE_EXPORTS")
......
......@@ -21,7 +21,6 @@
#include <list>
#include <memory>
#include <string>
// From coreapi.
#include "private.h"
......
......@@ -19,8 +19,6 @@
#ifndef _CALL_LISTENER_H_
#define _CALL_LISTENER_H_
#include <string>
#include "linphone/types.h"
// =============================================================================
......
......@@ -19,8 +19,6 @@
#ifndef _CPIM_HEADER_H_
#define _CPIM_HEADER_H_
#include <string>
#include "object/object.h"
// =============================================================================
......
......@@ -19,8 +19,6 @@
#ifndef _IMDN_H_
#define _IMDN_H_
#include <string>
#include "linphone/utils/general.h"
#include "chat-room.h"
......
......@@ -19,8 +19,6 @@
#ifndef _IS_COMPOSING_H_
#define _IS_COMPOSING_H_
#include <string>
#include "linphone/utils/general.h"
#include "is-composing-listener.h"
......
......@@ -27,8 +27,6 @@
#include "conference/participant.h"
#include "conference/session/call-session-listener.h"
#include <string>
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
......
......@@ -19,8 +19,6 @@
#ifndef _CALL_SESSION_PARAMS_H_
#define _CALL_SESSION_PARAMS_H_
#include <string>
#include "object/clonable-object.h"
#include "linphone/types.h"
......
......@@ -20,7 +20,6 @@
#define _CALL_SESSION_P_H_
#include <memory>
#include <string>
#include "object/object-p.h"
......
......@@ -19,7 +19,6 @@
#ifndef _MEDIA_SESSION_P_H_
#define _MEDIA_SESSION_P_H_
#include <string>
#include <utility>
#include "call-session-p.h"
......
......@@ -61,11 +61,8 @@ Content &Content::operator= (const Content &src) {
Content &Content::operator= (Content &&src) {
L_D(Content);
if (this != &src) {
d->body = move(src.getPrivate()->body);
d->contentType = move(src.getPrivate()->contentType);
}
d->body = move(src.getPrivate()->body);
d->contentType = move(src.getPrivate()->contentType);
return *this;
}
......
......@@ -19,8 +19,6 @@
#ifndef _ABSTRACT_DB_H_
#define _ABSTRACT_DB_H_
#include <string>
#include "object/object.h"
// =============================================================================
......
......@@ -19,8 +19,6 @@
#ifndef _DB_SESSION_PROVIDER_H_
#define _DB_SESSION_PROVIDER_H_
#include <string>
#include "db-session.h"
#include "object/singleton.h"
......
......@@ -19,8 +19,6 @@
#ifndef _DB_SESSION_H_
#define _DB_SESSION_H_
#include <string>
#include "object/clonable-object.h"
// =============================================================================
......
......@@ -19,8 +19,12 @@
#ifndef _OBJECT_P_H_
#define _OBJECT_P_H_
#include <unordered_map>
#include "linphone/utils/general.h"
#include "variant/variant.h"
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
......@@ -33,6 +37,8 @@ protected:
Object *mPublic = nullptr;
private:
std::unordered_map<std::string, Variant> properties;
L_DECLARE_PUBLIC(Object);
};
......
......@@ -22,6 +22,8 @@
// =============================================================================
using namespace std;
LINPHONE_BEGIN_NAMESPACE
Object::~Object () {
......@@ -32,4 +34,20 @@ 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
......@@ -19,16 +19,24 @@
#ifndef _OBJECT_H_
#define _OBJECT_H_
#include <string>
#include "linphone/utils/general.h"
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
class Variant;
class LINPHONE_PUBLIC Object {
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);
......
/*
* variant.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 <http://www.gnu.org/licenses/>.
*/
#include "variant.h"
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
class VariantPrivate {
public:
union Value {
int i;
unsigned int ui;
long l;
unsigned long ul;
long long ll;
unsigned long long ull;
bool b;
double d;
float f;
};
Variant::Type type = Variant::Invalid;
Value value = {};
};
Variant::Variant () {
// Nothing. Construct an invalid invariant.
}
Variant::Variant (Type type) {
// TODO.
}
Variant::Variant (const Variant &src) {
// TODO.
}
Variant::Variant (Variant &&src) {
// TODO.
}
Variant::Variant (int value) : Variant(Int) {
L_D(Variant);
d->value.i = value;
}
Variant::Variant (unsigned int value) : Variant(Int) {
L_D(Variant);
d->value.ui = value;
}
Variant::Variant (long value) : Variant(Int) {
L_D(Variant);
d->value.l = value;
}
Variant::Variant (unsigned long value) : Variant(Int) {
L_D(Variant);
d->value.ul = value;
}
Variant::Variant (long long value) : Variant(Int) {
L_D(Variant);
d->value.ll = value;
}
Variant::Variant (unsigned long long value) : Variant(Int) {
L_D(Variant);
d->value.ull = value;
}
Variant::Variant (bool value) : Variant(Int) {
L_D(Variant);
d->value.b = value;
}
Variant::Variant (double value) : Variant(Int) {
L_D(Variant);
d->value.d = value;
}
Variant::Variant (float value) : Variant(Int) {
L_D(Variant);
d->value.f = value;
}
Variant::Variant (const std::string &value) {
// TODO.
}
Variant::~Variant () {
// TODO.
}
bool Variant::operator!= (const Variant &variant) const {
// TODO.
return false;
}
bool Variant::operator< (const Variant &variant) const {
// TODO.
return false;
}
bool Variant::operator<= (const Variant &variant) const {
// TODO.
return false;
}
Variant &Variant::operator= (const Variant &variant) {
// TODO.
return *this;
}
Variant &Variant::operator= (Variant &&variant) {
// TODO.
return *this;
}
bool Variant::operator== (const Variant &variant) const {
// TODO.
return false;
}
bool Variant::operator> (const Variant &variant) const {
// TODO.
return false;
}
bool Variant::operator>= (const Variant &variant) const {
// TODO.
return false;
}
bool Variant::isValid () const {
// TODO.
return false;
}
void Variant::clear () {
// TODO.
}
void Variant::swap (const Variant &variant) {
// TODO.
}
// -----------------------------------------------------------------------------
void Variant::getValue (int type, void *value, bool *soFarSoGood) {
if (type <= 0 || type >= MaxDefaultTypes)
return; // Unable to get value.
// TODO.
}
LINPHONE_END_NAMESPACE
/*
* variant.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 <http://www.gnu.org/licenses/>.
*/
#ifndef _VARIANT_H_
#define _VARIANT_H_
#include <string>
#include "linphone/utils/general.h"
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
#define L_DECLARE_VARIANT_TYPES(FUNC) \
FUNC(int, Int, 1) \
FUNC(unsigned int, UnsignedInt, 2) \
FUNC(long, Long, 3) \
FUNC(unsigned long, UnsignedLong, 4) \
FUNC(long long, LongLong, 5) \
FUNC(unsigned long long, UnsignedLongLong, 6) \
FUNC(bool, Bool, 7) \
FUNC(double, Double, 8) \
FUNC(float, Float, 9) \
FUNC(std::string, String, 10) \
FUNC(void *, Generic, 11)
#define L_DECLARE_VARIANT_ENUM_TYPE(TYPE, NAME, ID) NAME = ID,
#define L_DECLARE_VARIANT_TRAIT_TYPE(TYPE, NAME, ID) \
template<> \
struct Variant::IdOfType<TYPE> { \
static const int id = ID; \
};
class VariantPrivate;
class Variant {
public:
enum Type {
Invalid = 0,
L_DECLARE_VARIANT_TYPES(L_DECLARE_VARIANT_ENUM_TYPE)
MaxDefaultTypes
};
Variant ();
Variant (Type type);
Variant (const Variant &src);
Variant (Variant &&src);
Variant (int value);
Variant (unsigned int value);
Variant (long value);
Variant (unsigned long value);
Variant (long long value);
Variant (unsigned long long value);
Variant (bool value);
Variant (double value);
Variant (float value);
Variant (const std::string &value);
template<typename T, typename = typename std::enable_if<std::is_same<T, void *>::value> >
// void* constructor. Must be explicitly called.
Variant (T value);
~Variant ();
bool operator!= (const Variant &variant) const;
bool operator< (const Variant &variant) const;
bool operator<= (const Variant &variant) const;
Variant &operator= (const Variant &variant);
Variant &operator= (Variant &&variant);
bool operator== (const Variant &variant) const;
bool operator> (const Variant &variant) const;
bool operator>= (const Variant &variant) const;
template<typename T>
void setValue (const T &value) {
// TODO.
}
template<typename T>
T getValue (bool *soFarSoGood = nullptr) const {
constexpr int id = IdOfType<T>::id;
static_assert(id != Invalid, "Unable to get value of unsupported type.");
T value;
getValue(id, &value, &soFarSoGood);
return value;
}
bool isValid () const;
void clear ();
void swap (const Variant &variant);
private:
template<typename T>
struct IdOfType {
static const int id = Invalid;
};
void getValue (int type, void *value, bool *soFarSoGood);
VariantPrivate *mPrivate = nullptr;
L_DECLARE_PRIVATE(Variant);
};
L_DECLARE_VARIANT_TYPES(L_DECLARE_VARIANT_TRAIT_TYPE);
#undef L_DECLARE_VARIANT_TYPES
#undef L_DECLARE_VARIANT_ENUM_TYPE
#undef L_DECLARE_VARIANT_TRAIT_TYPE
LINPHONE_END_NAMESPACE
#endif // ifndef _VARIANT_H_
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