summaryrefslogtreecommitdiff
path: root/src/bindings/eo_cxx/eo_base.hh
blob: 00b660efc1f194c8637779c2ccb9e840098ed2b6 (plain) (blame)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

///
/// @file eo_base.hh
///

#ifndef EFL_CXX_EO_BASE_HH
#define EFL_CXX_EO_BASE_HH

#include <cassert>
#include <stdexcept>
#include <cstddef>
#include <eina_optional.hh>

#include "eo_ops.hh"


namespace efl { namespace eo {

/// @addtogroup Efl_Cxx_API
/// @{

/// @brief A binding to the <em>EO Base Class</em>.
///
/// This class implements C++ wrappers to all the <em>EO Base</em>
/// operations.
///
struct base
{
   /// @brief Class constructor.
   ///
   /// @param eo The <em>EO Object</em>.
   ///
   /// efl::eo::base constructors semantics are that of stealing the
   /// <em>EO Object</em> lifecycle management. Its constructors do not
   /// increment the <em>EO</em> reference counter but the destructors
   /// do decrement.
   ///
   explicit base(Eo* eo) : _eo_raw(eo)
   {
      assert(eo != 0);
   }

   /// @brief Class destructor.
   ///
   ~base()
   {
      detail::unref(_eo_raw);
   }

   /// @brief Assignment operator.
   ///
   base& operator=(base const& other)
   {
      _eo_raw = detail::ref(other._eo_ptr());
      return *this;
   }

   /// @brief Return a pointer to the <em>EO Object</em> stored in this
   /// instance.
   ///
   /// @return A pointer to the opaque <em>EO Object</em>.
   ///
   Eo* _eo_ptr() const { return _eo_raw; }

   /// @brief Get the reference count of this object.
   ///
   /// @return The referencer count of this object.
   ///
   int ref_get() const { return detail::ref_get(_eo_raw); }

   /// @brief Set the parent of this object.
   ///
   /// @param parent The new parent.
   ///
   void parent_set(base parent)
   {
      detail::parent_set(_eo_raw, parent._eo_ptr());
   }

   /// @brief Get the parent of this object.
   ///
   /// @return An @ref efl::eo::base instance that binds the parent
   /// object. Returns NULL if there is no parent.
   ///
   eina::optional<base> parent_get()
   {
      Eo *r = detail::parent_get(_eo_raw);
      if(!r) return nullptr;
      else
        {
           detail::ref(r); // XXX eo_parent_get does not call eo_ref so we may.
           return base(r);
        }
   }

   /// @brief Set generic data to object.
   ///
   /// @param key The key associated with the data.
   /// @param data The data to set.
   /// @param free_func A pointer to the function that frees the
   /// data. @c (::eo_key_data_free_func*)0 is valid.
   ///
   void base_data_set(const char *key, const void *data, ::eo_key_data_free_func func)
   {
      detail::base_data_set(_eo_raw, key, data, func);
   }

   /// @brief Get generic data from object.
   ///
   /// @param key The key associated with desired data.
   /// @return A void pointer to the data.
   ///
   void* base_data_get(const char *key)
   {
      return detail::base_data_get(_eo_raw, key);
   }

   /// @brief Delete generic data from object.
   ///
   /// @param key The key associated with the data.
   ///
   void base_data_del(const char *key)
   {
      detail::base_data_del(_eo_raw, key);
   }

   /// @brief Freeze any event directed to this object.
   ///
   /// Prevents event callbacks from being called for this object.
   ///
   void event_freeze()
   {
      detail::event_freeze(_eo_raw);
   }

   /// @brief Thaw the events of this object.
   ///
   /// Let event callbacks be called for this object.
   ///
   void event_thaw()
   {
      detail::event_thaw(_eo_raw);
   }

   /// @brief Get the event freeze count for this object.
   ///
   /// @return The event freeze count for this object.
   ///
   int event_freeze_get()
   {
      return detail::event_freeze_get(_eo_raw);
   }

   /// @brief Get debug information of this object.
   ///
   /// @return The root node of the debug information tree.
   ///
   Eo_Dbg_Info dbg_info_get()
   {
      Eo_Dbg_Info info;
      detail::dbg_info_get(_eo_raw, &info);
      return info;
   }

 protected:
   Eo* _eo_raw; ///< The opaque <em>EO Object</em>.
};

/// @brief Downcast @p U to @p T.
///
/// @param T An <em>EO C++ Class</em>.
/// @param U An <em>EO C++ Class</em>.
///
/// @param object The target object.
/// @return This function returns a new instance of @p T if the
/// downcast is successful --- otherwise it raises a @c
/// std::runtime_error.
///
template <typename T, typename U>
T downcast(U object)
{
   Eo *eo = object._eo_ptr();

   if(detail::isa(eo, T::_eo_class()))
     {
        return T(detail::ref(eo));
     }
   else
     {
        throw std::runtime_error("Invalid cast");
     }
}

///
/// @brief Type used to hold the parent passed to base Eo C++
/// constructors.
///
struct parent_type
{
   Eo* _eo_raw;
};

///
/// @brief The expression type declaring the assignment operator used
/// in the parent argument of the base Eo C++ class.
///
struct parent_expr
{
   parent_type operator=(efl::eo::base const& parent)
   {
      return { parent._eo_ptr() };
   }

   parent_type operator=(std::nullptr_t)
   {
      return { NULL };
   }
};

///
/// @brief Placeholder for the parent argument.
///
parent_expr parent = {};

/// @}

} } // namespace efl { namespace eo {

#endif // EFL_CXX_EO_BASE_HH