| 1 | #ifndef GI_WRAP_HPP |
| 2 | #define GI_WRAP_HPP |
| 3 | |
| 4 | #include "base.hpp" |
| 5 | #include "string.hpp" |
| 6 | |
| 7 | GI_MODULE_EXPORT |
| 8 | namespace gi |
| 9 | { |
| 10 | // object/wrapper conversion |
| 11 | template<typename CType, typename TransferType, |
| 12 | typename CppType = typename traits::cpptype<CType *>::type, |
| 13 | typename Enable = |
| 14 | typename std::enable_if<traits::is_wrapper<CppType>::value>::type> |
| 15 | inline typename std::remove_const<CppType>::type |
| 16 | wrap(CType *v, const TransferType &t) |
| 17 | { |
| 18 | // should be called with a concrete transfer subtype |
| 19 | static_assert(!std::is_same<TransferType, transfer_t>::value, ""); |
| 20 | // the class wrap only has to deal with non-const class type |
| 21 | typedef typename std::remove_const<CppType>::type TNC; |
| 22 | return CppType::template wrap<TNC>(v, t.value); |
| 23 | } |
| 24 | |
| 25 | // special case; wrap an owned box with full transfer |
| 26 | template<typename CType, |
| 27 | typename CppType = typename traits::cpptype<CType *>::type, |
| 28 | typename Enable = |
| 29 | typename std::enable_if<traits::is_boxed<CppType>::value>::type, |
| 30 | typename TNC = typename std::remove_const<CppType>::type> |
| 31 | inline TNC |
| 32 | wrap(CType *v, const transfer_full_t &) |
| 33 | { |
| 34 | return CppType::template wrap<TNC>(v); |
| 35 | } |
| 36 | |
| 37 | // special case; wrap a unowned box (that is, transfer none) to the Ref type |
| 38 | template<typename CType, |
| 39 | typename CppType = typename traits::cpptype<CType *>::type, |
| 40 | typename Enable = |
| 41 | typename std::enable_if<traits::is_boxed<CppType>::value>::type, |
| 42 | typename RefType = typename traits::reftype< |
| 43 | typename std::remove_const<CppType>::type>::type> |
| 44 | inline RefType |
| 45 | wrap(CType *v, const transfer_none_t &) |
| 46 | { |
| 47 | // unowned and no copy in all cases |
| 48 | return RefType::template wrap<RefType>(v); |
| 49 | } |
| 50 | |
| 51 | template<typename T, |
| 52 | typename std::remove_reference<T>::type::BaseObjectType * = nullptr> |
| 53 | inline typename traits::ctype<T>::type |
| 54 | unwrap(T &&v, const transfer_none_t &) |
| 55 | { |
| 56 | using DT = typename std::decay<T>::type; |
| 57 | // test convenience |
| 58 | #ifndef GI_TEST |
| 59 | static constexpr bool ALLOW_ALL = false; |
| 60 | #else |
| 61 | static constexpr bool ALLOW_ALL = true; |
| 62 | #endif |
| 63 | static_assert(ALLOW_ALL || traits::is_wrapper<DT>::value || |
| 64 | traits::is_reftype<DT>::value, |
| 65 | "transfer none expects refcnt wrapper or reftype (not owning box)"); |
| 66 | return v.gobj_(); |
| 67 | } |
| 68 | |
| 69 | namespace detail |
| 70 | { |
| 71 | // lvalue |
| 72 | template<typename T, |
| 73 | typename std::enable_if<!traits::is_boxed<T>::value>::type * = nullptr> |
| 74 | inline typename traits::ctype<T>::type |
| 75 | unwrap(const T &v, const transfer_full_t &, std::true_type) |
| 76 | { |
| 77 | // no implicit copy for boxed; should end up in other case |
| 78 | static_assert(!traits::is_boxed<T>::value, "boxed copy"); |
| 79 | return v.gobj_copy_(); |
| 80 | } |
| 81 | |
| 82 | // rvalue |
| 83 | template<typename T, |
| 84 | typename std::enable_if<!traits::is_reftype<T>::value>::type * = nullptr> |
| 85 | inline typename traits::ctype<T>::type |
| 86 | unwrap(T &&v, const transfer_full_t &, std::false_type) |
| 87 | { |
| 88 | // in case of wrapper/object; |
| 89 | // release only provided on base case with void* return |
| 90 | return (typename traits::ctype<T>::type)v.release_(); |
| 91 | } |
| 92 | |
| 93 | } // namespace detail |
| 94 | |
| 95 | template<typename T, typename std::decay<T>::type::BaseObjectType * = nullptr> |
| 96 | inline typename traits::ctype<T>::type |
| 97 | unwrap(T &&v, const transfer_full_t &t) |
| 98 | { |
| 99 | // universal reference dispatch |
| 100 | return detail::unwrap(std::forward<T>(v), t, std::is_lvalue_reference<T>()); |
| 101 | } |
| 102 | |
| 103 | // container types |
| 104 | template<typename T, typename Transfer, |
| 105 | typename std::decay<T>::type::_detail::DataType * = nullptr> |
| 106 | inline typename std::decay<T>::type::_detail::DataType |
| 107 | unwrap(T &&v, const Transfer &t) |
| 108 | { |
| 109 | // universal reference dispatch |
| 110 | return std::forward<T>(v)._unwrap(t); |
| 111 | } |
| 112 | |
| 113 | // to wrap a container, the target wrapped type needs to be explicitly specified |
| 114 | // (in particular the contained element type) |
| 115 | // (target type should be decay'ed type) |
| 116 | |
| 117 | // generic case, let wrap take care of it (usually no target type is needed) |
| 118 | template<typename TargetType, typename CType, typename Transfer, |
| 119 | decltype(wrap(std::declval<typename std::decay<CType>::type>(), |
| 120 | Transfer())) * = nullptr> |
| 121 | TargetType |
| 122 | wrap_to(CType v, const Transfer &t) |
| 123 | { |
| 124 | static_assert(traits::is_decayed<TargetType>::value, ""); |
| 125 | return wrap(v, t); |
| 126 | } |
| 127 | |
| 128 | // container case |
| 129 | template<typename TargetType, typename CType, typename Transfer, |
| 130 | typename TargetType::_detail::DataType * = nullptr> |
| 131 | TargetType |
| 132 | wrap_to(CType v, const Transfer &t) |
| 133 | { |
| 134 | static_assert(traits::is_decayed<TargetType>::value, ""); |
| 135 | return TargetType::template _wrap<TargetType>(v, t); |
| 136 | } |
| 137 | |
| 138 | // container size case |
| 139 | template<typename TargetType, typename CType, typename Transfer, |
| 140 | typename TargetType::_detail::DataType * = nullptr> |
| 141 | TargetType |
| 142 | wrap_to(CType v, int s, const Transfer &t) |
| 143 | { |
| 144 | static_assert(traits::is_decayed<TargetType>::value, ""); |
| 145 | return TargetType::template _wrap<TargetType>(v, s, t); |
| 146 | } |
| 147 | |
| 148 | #if 0 |
| 149 | // string conversion |
| 150 | inline std::string |
| 151 | wrap(const char *v, const transfer_none_t &, |
| 152 | const direction_t & = direction_dummy) |
| 153 | { |
| 154 | return detail::make_string(v); |
| 155 | } |
| 156 | |
| 157 | // actually should not accept const input (as it makes no sense for full |
| 158 | // transfer) but let's go the runtime way and not mind that too much (code |
| 159 | // generation will warn though) |
| 160 | inline std::string |
| 161 | wrap(const char *v, const transfer_full_t &, |
| 162 | const direction_t & = direction_dummy) |
| 163 | { |
| 164 | // a custom type that would allow direct mem transfer might be nice |
| 165 | // but that might be too nifty and create yet-another-string-type |
| 166 | std::string s; |
| 167 | if (v) { |
| 168 | s = v; |
| 169 | g_free((char *)v); |
| 170 | } |
| 171 | return s; |
| 172 | } |
| 173 | #else |
| 174 | // string conversion |
| 175 | inline gi::cstring_v |
| 176 | wrap(const char *v, const transfer_none_t &) |
| 177 | { |
| 178 | return cstring_v(v); |
| 179 | } |
| 180 | |
| 181 | // actually should not accept const input (as it makes no sense for full |
| 182 | // transfer) but let's go the runtime way and not mind that too much (code |
| 183 | // generation will warn though) |
| 184 | inline gi::cstring |
| 185 | wrap(const char *v, const transfer_full_t &) |
| 186 | { |
| 187 | // as said, never mind const |
| 188 | return cstring{(char *)v, transfer_full}; |
| 189 | } |
| 190 | #endif |
| 191 | |
| 192 | // return const here, as somewhat customary, also |
| 193 | // wrapped function call is force-casted anyway (to const char* parameter) |
| 194 | // FIXME ?? though const is generally rare and it breaks consistency that way |
| 195 | inline const gchar * |
| 196 | unwrap(const std::string &v, const transfer_none_t &) |
| 197 | { |
| 198 | return v.c_str(); |
| 199 | } |
| 200 | |
| 201 | inline const gchar * |
| 202 | unwrap(const detail::optional_string &v, const transfer_none_t &) |
| 203 | { |
| 204 | return v.empty() ? nullptr : v.c_str(); |
| 205 | } |
| 206 | |
| 207 | template<typename Transfer> |
| 208 | inline const gchar * |
| 209 | unwrap(const detail::cstr<Transfer> &v, const transfer_none_t &) |
| 210 | { |
| 211 | return v.c_str(); |
| 212 | } |
| 213 | |
| 214 | // R-value variants of the above, akin to transfer_none from an owning R-value |
| 215 | // bad dangling things would happen |
| 216 | inline const gchar *unwrap(std::string &&v, const transfer_none_t &) = delete; |
| 217 | |
| 218 | inline const gchar *unwrap( |
| 219 | detail::optional_string &&v, const transfer_none_t &) = delete; |
| 220 | |
| 221 | template<typename Transfer> |
| 222 | inline const gchar * |
| 223 | unwrap(detail::cstr<Transfer> &&v, const transfer_none_t &) |
| 224 | { |
| 225 | static_assert(std::is_same<Transfer, transfer_none_t>::value, |
| 226 | "transfer none expects non-owning type"); |
| 227 | return v.c_str(); |
| 228 | } |
| 229 | |
| 230 | inline gchar * |
| 231 | unwrap(const std::string &v, const transfer_full_t &) |
| 232 | { |
| 233 | return g_strdup(v.c_str()); |
| 234 | } |
| 235 | |
| 236 | inline gchar * |
| 237 | unwrap(const detail::optional_string &v, const transfer_full_t &) |
| 238 | { |
| 239 | return v.empty() ? nullptr : g_strdup(v.c_str()); |
| 240 | } |
| 241 | |
| 242 | template<typename Transfer> |
| 243 | inline gchar * |
| 244 | unwrap(const gi::detail::cstr<Transfer> &v, const transfer_full_t &) |
| 245 | { |
| 246 | return g_strdup(v.c_str()); |
| 247 | } |
| 248 | |
| 249 | inline gchar * |
| 250 | unwrap(gi::cstring &&v, const transfer_full_t &) |
| 251 | { |
| 252 | return v.release_(); |
| 253 | } |
| 254 | |
| 255 | // enum conversion |
| 256 | template<typename T, |
| 257 | typename std::enable_if<std::is_enum<T>::value>::type * = nullptr> |
| 258 | inline typename traits::cpptype<T>::type |
| 259 | wrap(T v, const transfer_t & = transfer_dummy) |
| 260 | { |
| 261 | return (typename traits::cpptype<T>::type)v; |
| 262 | } |
| 263 | |
| 264 | template<typename T, |
| 265 | typename std::enable_if<std::is_enum<T>::value>::type * = nullptr> |
| 266 | inline typename traits::ctype<T>::type |
| 267 | unwrap(T v, const transfer_t & = transfer_dummy) |
| 268 | { |
| 269 | return (typename traits::ctype<T>::type)v; |
| 270 | } |
| 271 | |
| 272 | // plain basic pass along |
| 273 | template<typename T, |
| 274 | typename std::enable_if<traits::is_basic<T>::value>::type * = nullptr> |
| 275 | inline T |
| 276 | wrap(T v, const transfer_t & = transfer_dummy) |
| 277 | { |
| 278 | return v; |
| 279 | } |
| 280 | |
| 281 | template<typename T, |
| 282 | typename std::enable_if<traits::is_basic<T>::value>::type * = nullptr> |
| 283 | inline T |
| 284 | unwrap(T v, const transfer_t & = transfer_dummy) |
| 285 | { |
| 286 | return v; |
| 287 | } |
| 288 | |
| 289 | // callback conversion |
| 290 | // async or destroy-notify; |
| 291 | // signature forces copy, and std::move is used in unwrap call |
| 292 | template<typename T, |
| 293 | typename std::remove_reference<T>::type::CallbackWrapperType * = nullptr> |
| 294 | inline typename std::remove_reference<T>::type::CallbackWrapperType |
| 295 | unwrap(T &&v, const transfer_t & = transfer_dummy) |
| 296 | { |
| 297 | return typename std::remove_reference<T>::type::CallbackWrapperType( |
| 298 | std::forward<T>(v)); |
| 299 | } |
| 300 | |
| 301 | // call or destroy-notify scope |
| 302 | template<typename T> |
| 303 | inline typename std::remove_reference<T>::type::template wrapper_type<false> * |
| 304 | unwrap(T &&v, const scope_t &) |
| 305 | { |
| 306 | return new |
| 307 | typename std::remove_reference<T>::type::template wrapper_type<false>( |
| 308 | std::forward<T>(v)); |
| 309 | } |
| 310 | |
| 311 | // async scope |
| 312 | template<typename T> |
| 313 | inline typename std::remove_reference<T>::type::template wrapper_type<true> * |
| 314 | unwrap(T &&v, const scope_async_t &) |
| 315 | { |
| 316 | return new |
| 317 | typename std::remove_reference<T>::type::template wrapper_type<true>( |
| 318 | std::forward<T>(v)); |
| 319 | } |
| 320 | |
| 321 | // dynamic GType casting within GObject/interface hierarchy |
| 322 | template<typename T, typename I, |
| 323 | typename std::enable_if< |
| 324 | traits::is_object<T>::value && |
| 325 | traits::is_object<typename std::decay<I>::type>::value>::type * = |
| 326 | nullptr> |
| 327 | inline T |
| 328 | object_cast(I &&t) |
| 329 | { |
| 330 | if (!t || !g_type_is_a(t.gobj_type_(), T::get_type_())) { |
| 331 | return T(); |
| 332 | } else { |
| 333 | return wrap((typename T::BaseObjectType *)t.gobj_copy_(), transfer_full); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // this utility can be used to arrange for pointer-like const-ness |
| 338 | // that is, a const shared_ptr<T> is still usable like (non-const) T* |
| 339 | // in a way, any wrapper object T acts much like a smart-pointer, |
| 340 | // but as the (code generated) methods are non-const, they are not usable |
| 341 | // if the wrapper object is const (e.g. captured in a lambda) |
| 342 | // this helper object/class can be wrapped around the wrapper (phew) |
| 343 | // to absorb/shield the (outer) `const` (as it behaves as other smart pointers) |
| 344 | template<typename T> |
| 345 | class cs_ptr |
| 346 | { |
| 347 | T t; |
| 348 | |
| 349 | public: |
| 350 | // rough check; T is expected to be a pointer wrapper |
| 351 | static_assert(sizeof(T) == sizeof(void *), ""); |
| 352 | |
| 353 | // if T not copy-able, argument may need to be move'd |
| 354 | cs_ptr(T _t) : t(std::move(_t)) {} |
| 355 | |
| 356 | operator T() const & { return t; } |
| 357 | operator T() && { return std::move(t); } |
| 358 | |
| 359 | T *get() const { return &t; } |
| 360 | |
| 361 | // C++ sacrilege, |
| 362 | // but the const of pointer in T does not extend to pointee anyway |
| 363 | T *operator*() const { return const_cast<T *>(&t); } |
| 364 | T *operator->() const { return const_cast<T *>(&t); } |
| 365 | }; |
| 366 | |
| 367 | } // namespace gi |
| 368 | |
| 369 | #endif // GI_WRAP_HPP |