summaryrefslogtreecommitdiff
path: root/src/bindings/cxx/eo_cxx/eo_wref.hh
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-05-23 12:01:30 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-05-23 15:05:50 -0300
commite3c8b280c639058c54f5576f189d0dc288105fc7 (patch)
treee313fa05bd9d2b2cb88623f6adea2c3b396b66e8 /src/bindings/cxx/eo_cxx/eo_wref.hh
parent930f39b91b12b311f40cd96b4cf2e85085b01832 (diff)
c++: Reorganized C++ binding generation
Moved all generation to a single Makefile_Cxx.am file instead of throughout the whole project. And fixed C++ generation on Elementary after elm merge.
Diffstat (limited to 'src/bindings/cxx/eo_cxx/eo_wref.hh')
-rw-r--r--src/bindings/cxx/eo_cxx/eo_wref.hh136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/bindings/cxx/eo_cxx/eo_wref.hh b/src/bindings/cxx/eo_cxx/eo_wref.hh
new file mode 100644
index 0000000000..fc32e47616
--- /dev/null
+++ b/src/bindings/cxx/eo_cxx/eo_wref.hh
@@ -0,0 +1,136 @@
1
2///
3/// @file eo_wref.hh
4///
5
6#ifndef EFL_CXX_WREF_HH
7#define EFL_CXX_WREF_HH
8
9#include <eina_optional.hh>
10
11#include "eo_ops.hh"
12
13namespace efl { namespace eo {
14
15/// @addtogroup Efl_Cxx_API
16/// @{
17
18/// @brief Weak references to an <em>EO Object</em>.
19///
20template<typename T>
21struct wref
22{
23 /// @brief Default constructor.
24 ///
25 /// Create a empty weak reference.
26 ///
27 wref() : _eo_wref(nullptr)
28 {
29 }
30
31 /// @brief Class constructor.
32 ///
33 /// @param obj The <em>EO Object</em> to be referenced.
34 ///
35 /// Create a weak reference to @p obj.
36 ///
37 explicit wref(Eo* obj) : _eo_wref(obj)
38 {
39 _add();
40 }
41
42 /// @brief Class constructor.
43 ///
44 /// @param obj The <em>EO C++ Object</em> to be referenced.
45 ///
46 /// Create a weak reference to @p obj.
47 ///
48 wref(T obj) : _eo_wref(obj._eo_ptr())
49 {
50 _add();
51 }
52
53 /// @brief Class destructor.
54 ///
55 ~wref()
56 {
57 if(_eo_wref)
58 if(eina::optional<T> p = lock())
59 _del();
60 }
61
62 /// @brief Try to acquire a strong reference to the underlying
63 /// <em>EO Object</em>.
64 ///
65 /// This function checks whether the weak reference still points to
66 /// a valid <em>EO Object</em>. If the reference is still valid it
67 /// increments the reference counter of the object and returns a
68 /// pointer to it.
69 ///
70 /// @return If the lock was successfully acquired it returns a
71 /// strong reference to the <em>EO Object</em>. Otherwise it returns
72 /// an empty eina::optional.
73 ///
74 eina::optional<T> lock()
75 {
76 if(_eo_wref) // XXX eo_ref() should work on multi-threaded environments
77 {
78 detail::ref(_eo_wref);
79 }
80 else
81 {
82 return nullptr;
83 }
84 return T(_eo_wref);
85 }
86
87 /// @brief Copy constructor.
88 ///
89 wref(wref const& other)
90 : _eo_wref(other._eo_wref)
91 {
92 if(eina::optional<T> p = lock())
93 {
94 _add();
95 }
96 else
97 {
98 _eo_wref = 0;
99 }
100 }
101
102 /// @brief Assignment operator.
103 ///
104 wref& operator=(wref const& other)
105 {
106 _eo_wref = other._eo_wref;
107 if(eina::optional<T> p = lock())
108 {
109 _add();
110 }
111 else
112 {
113 _eo_wref = 0;
114 }
115 return *this;
116 }
117
118private:
119 void _add()
120 {
121 detail::wref_add(_eo_wref, &_eo_wref);
122 }
123
124 void _del()
125 {
126 detail::wref_del(_eo_wref, &_eo_wref);
127 }
128
129 Eo* _eo_wref; ///< The weak reference.
130};
131
132/// @}
133
134} } // namespace efl { namespace eo {
135
136#endif // EFL_CXX_WREF_HH