-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.h
More file actions
98 lines (82 loc) · 2.7 KB
/
object.h
File metadata and controls
98 lines (82 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef OBJECT_H
#define OBJECT_H
#include <atomic>
#include <string>
#include <vector>
#include <iostream>
#include <functional>
#include <map>
#include "property.hpp"
#define PROP(cls, type, name) \
public: \
const type& name() const { return m_##name; } \
cls* set_##name(const type& v) { if(m_##name != v) { m_##name = v; emit(#name "_changed", &m_##name); } return this; }\
protected: type m_##name \
/*
#define PROPERTY_PTR(type, name) \
public: \
type name() { return m_##name; } \
void set_##name(type v) { m_##name = v; }\
protected: type m_##name \
*/
#define PROPERTY(type, name) Property<type> name
class Object {
public:
/// Default constructor
Object() { }
/// Copy constructor
Object(const Object &) : m_refCount(0) {}
/// Return the current reference count
int getRefCount() const { return m_refCount; }
/// Increase the object's reference count by one
void incRef() const { ++m_refCount; }
/** \brief Decrease the reference count of
* the object and possibly deallocate it.
*
* The object will automatically be deallocated once
* the reference count reaches zero.
*/
void decRef(bool dealloc = true) const noexcept;
/*template <typename T, typename ...Args>
int connect_member(std::string signal, T *inst, void (T::*func)(Args...)) {
return connect(signal, [=](Args... args) {
(inst->*func)(args...);
});
}
template <typename T, typename ...Args>
int connect_member(std::string signal, T *inst, void (T::*func)(Args...) const) {
return connect(signal, [=](Args... args) {
(inst->*func)(args...);
});
}*/
template <typename ...Args>
void connect(std::string signal, std::function<void(Args...)> const& slot) const {
if(slots_.find(signal) == slots_.end()) {
auto slots = std::vector<std::function<void(void*)>>();
slots.push_back(slot);
slots_[signal] = slots;
} else {
const auto &_slots = slots_.at(signal);
_slots.push_back(slot);
}
}
//template<typename arg>
void emit(std::string signal, void* p) {
//std::cout << signal << typeid(*p).name() << std::endl;
if(slots_.find(signal) == slots_.end())
return;
const auto &_slots = slots_.at(signal);
for(const auto &it : _slots) {
it(p);
}
}
protected:
/** \brief Virtual protected deconstructor.
* (Will only be called by \ref ref)
*/
virtual ~Object();
private:
mutable std::atomic<int> m_refCount { 0 };
mutable std::map<std::string, std::vector<std::function<void(void*)>> > slots_;
};
#endif // OBJECT_H