summaryrefslogtreecommitdiff
path: root/src/lib/efl_mono/efl_mono_accessors.c
blob: f798b14b11a27a6dcfc1227afd30ebdf849b471c (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
/*
 * Copyright 2019 by its authors. See AUTHORS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



#include "Eina.h"

#ifdef EAPI
# undef EAPI
#endif

#ifdef _WIN32
#  define EAPI __declspec(dllexport)
#else
# ifdef __GNUC__
#  if __GNUC__ >= 4
#   define EAPI __attribute__ ((visibility("default")))
#  else
#   define EAPI
#  endif
# else
#  define EAPI
# endif
#endif /* ! _WIN32 */

// This just a wrapper around carray acessors for pinned managed data
// It uses the free callback to unpin the managed data so it can be
// reclaimed by the GC back in C# world.
struct _Eina_Mono_Owned_Accessor
{
   Eina_Accessor accessor;

   Eina_Accessor *carray_acc;
   void *free_data;
   Eina_Free_Cb free_cb;
};

typedef struct _Eina_Mono_Owned_Accessor Eina_Mono_Owned_Accessor;

static Eina_Bool eina_mono_owned_carray_get_at(Eina_Mono_Owned_Accessor *accessor, unsigned int idx, void **data)
{
   return eina_accessor_data_get(accessor->carray_acc, idx, data);
}

static void** eina_mono_owned_carray_get_container(Eina_Mono_Owned_Accessor *accessor)
{
  // Is another accessor a valid container?
  return (void**)&accessor->carray_acc;
}

static void eina_mono_owned_carray_free_cb(Eina_Mono_Owned_Accessor* accessor)
{
   accessor->free_cb(accessor->free_data);

   free(accessor->carray_acc); // From Eina_CArray_Length_Accessor implementation...

   free(accessor);
}

EAPI Eina_Accessor *eina_mono_owned_carray_length_accessor_new(void** array, unsigned int step, unsigned int length, Eina_Free_Cb free_cb, void *handle)
{
   Eina_Mono_Owned_Accessor *accessor = calloc(1, sizeof(Eina_Mono_Owned_Accessor));
   if (!accessor) return NULL;

   EINA_MAGIC_SET(&accessor->accessor, EINA_MAGIC_ACCESSOR);

   accessor->carray_acc = eina_carray_length_accessor_new(array, step, length);

   accessor->accessor.version = EINA_ACCESSOR_VERSION;
   accessor->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_mono_owned_carray_get_at);
   accessor->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER(eina_mono_owned_carray_get_container);
   accessor->accessor.free = FUNC_ACCESSOR_FREE(eina_mono_owned_carray_free_cb);

   // The managed callback to be called with the pinned data.
   accessor->free_cb = free_cb;
   // The managed pinned data to be unpinned.
   accessor->free_data = handle;

   return &accessor->accessor;
}