summaryrefslogtreecommitdiff
path: root/src/bindings/mono/eina_mono/eina_hash.cs
blob: c254d58ae58bb454890e3b29f2a7247ddeabfebe (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
 * 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.
 */
#pragma warning disable 1591

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;

using static Eina.TraitFunctions;
using static Eina.IteratorNativeFunctions;
using static Eina.HashNativeFunctions;
using Eina.Callbacks;

namespace Eina
{

[StructLayout(LayoutKind.Sequential)]
[EditorBrowsable(EditorBrowsableState.Never)]    
public struct HashTupleNative : IEquatable<HashTupleNative>
{
    public IntPtr key;
    public IntPtr data;
    public uint   key_length;

    /// <summary>
    ///   Gets a hash for <see cref="HashTupleNative" />.
    /// <para>Since EFL 1.24.</para>
    /// </summary>
    /// <returns>A hash code.</returns>
    public override int GetHashCode()
        => key.GetHashCode() ^ data.GetHashCode() ^ key_length.GetHashCode();

    /// <summary>Returns whether this <see cref="HashTupleNative" />
    /// is equal to the given <see cref="object" />.
    /// <para>Since EFL 1.24.</para>
    /// </summary>
    /// <param name="other">The <see cref="object" /> to be compared to.</param>
    /// <returns><c>true</c> if is equal to <c>other</c>.</returns>
    public override bool Equals(object other)
        => (!(other is HashTupleNative)) ? false : Equals((HashTupleNative)other);

    /// <summary>Returns whether this <see cref="HashTupleNative" /> is equal
    /// to the given <see cref="HashTupleNative" />.
    /// <para>Since EFL 1.24.</para>
    /// </summary>
    /// <param name="other">The <see cref="HashTupleNative" /> to be compared to.</param>
    /// <returns><c>true</c> if is equal to <c>other</c>.</returns>
    public bool Equals(HashTupleNative other)
        => (key == other.key) && (data == other.data)
        && (key_length == other.key_length);

    /// <summary>Returns whether <c>lhs</c> is equal to <c>rhs</c>.
    /// <para>Since EFL 1.24.</para>
    /// </summary>
    /// <param name="lhs">The left hand side of the operator.</param>
    /// <param name="rhs">The right hand side of the operator.</param>
    /// <returns><c>true</c> if <c>lhs</c> is equal
    /// to <c>rhs</c>.</returns>
    public static bool operator==(HashTupleNative lhs, HashTupleNative rhs)
        => lhs.Equals(rhs);

    /// <summary>Returns whether <c>lhs</c> is not equal to <c>rhs</c>.
    /// <para>Since EFL 1.24.</para>
    /// </summary>
    /// <param name="lhs">The left hand side of the operator.</param>
    /// <param name="rhs">The right hand side of the operator.</param>
    /// <returns><c>true</c> if <c>lhs</c> is not equal
    /// to <c>rhs</c>.</returns>
    public static bool operator!=(HashTupleNative lhs, HashTupleNative rhs) => !(lhs == rhs);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public static class HashNativeFunctions
{
    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_new(IntPtr key_length_cb, IntPtr key_cmp_cb, IntPtr key_hash_cb, IntPtr data_free_cb, int buckets_power_size);

    [DllImport(efl.Libs.Eina)] internal static extern void
        eina_hash_free_cb_set(IntPtr hash, IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_string_djb2_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_string_superfast_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_string_small_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_int32_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_int64_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_pointer_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_stringshared_new(IntPtr data_free_cb);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_add(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_direct_add(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_del(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_find(IntPtr hash, IntPtr key);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_modify(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_set(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_move(IntPtr hash, IntPtr old_key, IntPtr new_key);

    [DllImport(efl.Libs.Eina)] internal static extern void
        eina_hash_free(IntPtr hash);

    [DllImport(efl.Libs.Eina)] internal static extern void
        eina_hash_free_buckets(IntPtr hash);

    [DllImport(efl.Libs.Eina)] internal static extern int
        eina_hash_population(IntPtr hash);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_add_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_direct_add_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_del_by_key_hash(IntPtr hash, IntPtr key, int key_length, int key_hash);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_del_by_key(IntPtr hash, IntPtr key);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_del_by_data(IntPtr hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool
        eina_hash_del_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_find_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_modify_by_hash(IntPtr hash, IntPtr key, int key_length, int key_hash, IntPtr data);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_iterator_key_new(IntPtr hash);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_iterator_data_new(IntPtr hash);

    [DllImport(efl.Libs.Eina)] internal static extern IntPtr
        eina_hash_iterator_tuple_new(IntPtr hash);

    [DllImport(efl.Libs.Eina)] internal static extern void
        eina_hash_foreach(IntPtr hash, IntPtr func, IntPtr fdata);


    [DllImport(efl.Libs.Eina)] internal static extern void
        eina_hash_list_append(IntPtr hash, IntPtr key, IntPtr data);
    [DllImport(efl.Libs.Eina)] internal static extern void
        eina_hash_list_prepend(IntPtr hash, IntPtr key, IntPtr data);
    [DllImport(efl.Libs.Eina)] internal static extern void
        eina_hash_list_remove(IntPtr hash, IntPtr key, IntPtr data);

    [DllImport(efl.Libs.Eina)] internal static extern int
        eina_hash_superfast(string key, int len);

    [DllImport(efl.Libs.CustomExports)] internal static extern IntPtr
        eina_hash_iterator_ptr_key_wrapper_new_custom_export_mono(IntPtr hash);
}

/// <summary>Wrapper around native dictionary mapping keys to values.
///
/// Since EFL 1.23.
/// </summary>
public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDisposable
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public IntPtr Handle {get; set;} = IntPtr.Zero;
    /// <summary>Whether this wrapper owns the native hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public bool Own {get; set;}
    /// <summary>Whether this wrapper owns the key.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public bool OwnKey {get; set;}
    /// <summary>Whether this wrapper owns the value.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public bool OwnValue {get; set;}

    /// <summary>Quantity of elements in the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public int Count
    {
        get
        {
            return Population();
        }
    }


    private void InitNew()
    {
        Handle = EinaHashNew<TKey>();
        SetOwn(true);
        SetOwnKey(true);
        SetOwnValue(true);
    }

    /// <summary>Default constructor.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public Hash()
    {
        InitNew();
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    public Hash(IntPtr handle, bool own)
    {
        Handle = handle;
        SetOwnership(own);
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    public Hash(IntPtr handle, bool own, bool ownKey, bool ownValue)
    {
        Handle = handle;
        SetOwnership(own, ownKey, ownValue);
    }

    /// <summary>Default destructor.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    ~Hash()
    {
        Dispose(false);
    }

    /// <summary>Disposes of this wrapper, releasing the native accessor if
    /// owned.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="disposing">True if this was called from <see cref="Dispose()"/> public method. False if
    /// called from the C# finalizer.</param>
    protected virtual void Dispose(bool disposing)
    {
        IntPtr h = Handle;
        Handle = IntPtr.Zero;
        if (h == IntPtr.Zero)
        {
            return;
        }

        if (Own)
        {
            if (disposing)
            {
                eina_hash_free(h);
            }
            else
            {
                Efl.Eo.Globals.ThreadSafeFreeCbExec(eina_hash_free, h);
            }
        }
    }

    /// <summary>Release the native resources held by this instance.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    /// <summary>Release the native resources held by this instance.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public void Free()
    {
        Dispose();
    }

    /// <summary>Release the pointer.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <returns>The instance.</returns>
    public IntPtr Release()
    {
        IntPtr h = Handle;
        Handle = IntPtr.Zero;
        return h;
    }

    /// <summary>Sets ownership.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="own">If the hash own the object.</param>
    public void SetOwn(bool own)
    {
        Own = own;
    }

    /// <summary>Sets key's ownership.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="ownKey">If the hash own the key's object.</param>
    public void SetOwnKey(bool ownKey)
    {
        OwnKey = ownKey;
    }

    /// <summary>Sets value's ownership.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="ownValue">If the hash own the value's object.</param>
    public void SetOwnValue(bool ownValue)
    {
        OwnValue = ownValue;

        if (ownValue)
        {
            eina_hash_free_cb_set(Handle, EinaFreeCb<TValue>());
        }
    }

    /// <summary>Sets all ownership.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="ownAll">If the hash own for all ownerships.</param>
    public void SetOwnership(bool ownAll)
    {
        SetOwn(ownAll);
        SetOwnKey(ownAll);
        SetOwnValue(ownAll);
    }

    /// <summary>Sets own individually.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="own">If the hash own the object.</param>
    /// <param name="ownKey">If the hash own the key's object.</param>
    /// <param name="ownValue">If the hash own the value's object.</param>
    public void SetOwnership(bool own, bool ownKey, bool ownValue)
    {
        SetOwn(own);
        SetOwnKey(ownKey);
        SetOwnValue(ownValue);
    }

    /// <summary>
    ///   Cleanup for the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public void UnSetFreeCb()
    {
        eina_hash_free_cb_set(Handle, IntPtr.Zero);
    }

    /// <summary>
    ///   Adds an entry to the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">A unique key.</param>
    /// <param name="val">The value to associate with the key.</param>
    /// <returns> false if an error occurred, true otherwise.</returns>
    public bool AddNew(TKey key, TValue val)
    {
        IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
        IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        IntPtr gchnv = CopyNativeObject(val, false);
        IntPtr nv = GetNativePtr<TValue>(gchnv, false);
        var r = eina_hash_add(Handle, nk, nv);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        FreeNativeIndirection<TValue>(gchnv, false);
        return r;
    }

    /// <summary>
    ///   Modifies the entry at the specified key.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">The key of the entry to modify.</param>
    /// <param name="val">The value to replace the previous entry.</param>
    public void Add(TKey key, TValue val)
    {
        Set(key, val);
    }

    /// <summary>
    ///   Removes the entry identified by a key from the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">The key.</param>
    public bool DelByKey(TKey key)
    {
        IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
        IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        var r = eina_hash_del_by_key(Handle, nk);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        return r;
    }

    /// <summary>Searches this hash for <c>val</c> and deletes it from the hash,
    /// also deleting it.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="val">The value to be deleted.</param>
    /// <returns><c>true</c> if the value was found and deleted, false if it was <c>null</c> or not found.</returns>
    public bool DelByValue(TValue val)
    {
        // We don't use the C version of `eina_hash_del_by_data` because it requires the exact pointer
        // we passed to add(). As our hashes store the data by pointer, this makes it harder to pass the
        // same value.
        if (val == null)
        {
            return false;
        }

        foreach (var pair in this)
        {
            if (pair.Value != null && val.Equals(pair.Value))
            {
                return this.DelByKey(pair.Key);
            }
        }

        return false;

    }

    /// <summary>
    ///   Removes the entry identified by a key from the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">The key.</param>
    public void Remove(TKey key)
    {
        DelByKey(key);
    }

    /// <summary>
    ///   Retrieves a specific entry in the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">The key of the entry to find.</param>
    /// <returns>The value of the entry.</returns>
    public TValue Find(TKey key)
    {
        var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
        var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        var found = eina_hash_find(Handle, nk);
        //NativeFreeRef<TKey>(nk);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        if (found == IntPtr.Zero)
        {
            throw new KeyNotFoundException();
        }

        return NativeToManaged<TValue>(IndirectNative<TValue>(found, false));
    }

    /// <summary>
    ///   Check if key is present. if not, a default value is setted.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">The key to be checked.</param>
    /// <param name="val">[out] The value of the entry.</param>
    /// <returns>true if key exists, false otherwise.</returns>
    public bool TryGetValue(TKey key, out TValue val)
    {
        var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
        var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        var found = eina_hash_find(Handle, nk);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        if (found == IntPtr.Zero)
        {
            val = default(TValue);
            return false;
        }

        val = NativeToManaged<TValue>(IndirectNative<TValue>(found, false));
        return true;
    }

    /// <summary>
    ///   Check if key is present.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">The key to be checked.</param>
    /// <returns>true if key exists, false otherwise.</returns>
    public bool ContainsKey(TKey key)
    {
        var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
        var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        // var nk = ManagedToNativeAllocRef(key);
        var found = eina_hash_find(Handle, nk);
        // NativeFreeRef<TKey>(nk);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        return found != IntPtr.Zero;
    }

    /// <summary>
    ///   Modifies the speficied key if exists.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">The key to modify.</param>
    /// <param name="val"> The new value.</param>
    /// <returns>False if key do not exists, true otherwise.</returns>
    public bool Modify(TKey key, TValue val)
    {
        var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
        var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        var gchnv = CopyNativeObject<TValue>(val, false);
        var nv = GetNativePtr<TValue>(gchnv, false);
        var old = eina_hash_modify(Handle, nk, nv);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        // NativeFreeRef<TKey>(nk);
        if (old == IntPtr.Zero)
        {
            NativeFree<TValue>(nv);
            return false;
        }

        if (OwnValue)
        {
            NativeFree<TValue>(old);
        }

        return true;
    }

    private static bool ForceRefKey<T>()
    {
        return (!typeof(T).IsValueType)
               && (typeof(T) != typeof(string))
               && (typeof(T) != typeof(Eina.Stringshare));
    }

    private static IntPtr CopyNativeObject<T>(T value, bool forceRef)
    {
        if (!IsEflObject(typeof(T)) && forceRef)
        {
            GCHandle gch = GCHandle.Alloc(new byte[Marshal.SizeOf<T>()], GCHandleType.Pinned);
            IntPtr pin = gch.AddrOfPinnedObject();

            ManagedToNativeCopyTo(value, pin);

            return GCHandle.ToIntPtr(gch);
        }
        else if (IsEflObject(typeof(T)) && forceRef)
        {
            GCHandle gch = GCHandle.Alloc(new byte[Marshal.SizeOf<IntPtr>()], GCHandleType.Pinned);
            IntPtr pin = gch.AddrOfPinnedObject();

            ManagedToNativeCopyTo(value, pin);

            return GCHandle.ToIntPtr(gch);
        }
        else
        {
            return ManagedToNativeAlloc(value);
        }
    }

    private static IntPtr GetNativePtr<T>(IntPtr gchptr, bool forceRef)
    {
        if (forceRef)
        {
            GCHandle gch = GCHandle.FromIntPtr(gchptr);
            IntPtr pin = gch.AddrOfPinnedObject();

            return pin;
        }
        else
        {
            return gchptr;
        }
    }

    private static void FreeNativeIndirection<T>(IntPtr gchptr, bool forceRef)
    {
        if (forceRef)
        {
            GCHandle gch = GCHandle.FromIntPtr(gchptr);
            gch.Free();
        }
    }

    private static IntPtr IndirectNative<T>(IntPtr ptr, bool forceRef)
    {
        if (forceRef)
        {
            IntPtr val = Marshal.ReadIntPtr(ptr);
            return val;
        }
        else
        {
            return ptr;
        }
    }


    /// <summary>
    ///   Modifies the entry at the specified key. Adds if key is not found.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key">The key to modify.</param>
    /// <param name="val">The value to replace the previous entry.</param>
    public void Set(TKey key, TValue val)
    {
        IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
        IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());

        IntPtr gchnv = CopyNativeObject(val, false);
        IntPtr nv = GetNativePtr<TValue>(gchnv, false);
        IntPtr old = eina_hash_set(Handle, nk, nv);
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        FreeNativeIndirection<TValue>(gchnv, false);
        if (OwnValue && old != IntPtr.Zero)
        {
            NativeFree<TValue>(old);
        }
    }

    /// <summary>
    ///   Accessor by key to the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public TValue this[TKey key]
    {
        get
        {
            return Find(key);
        }
        set
        {
            Set(key, value);
        }
    }

    /// <summary>
    ///   Changes the keys of an entry in the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="key_old">The current key with data.</param>
    /// <param name="key_new">The new key with data.</param>
    /// <returns>false in any case but success, true on success.</returns>
    public bool Move(TKey key_old, TKey key_new)
    {
        IntPtr gchnko = CopyNativeObject(key_old, ForceRefKey<TKey>());
        IntPtr nko = GetNativePtr<TKey>(gchnko, ForceRefKey<TKey>());
        IntPtr gchnk = CopyNativeObject(key_new, ForceRefKey<TKey>());
        IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
        // var nk_old = ManagedToNativeAllocRef(key_old);
        // var nk_new = ManagedToNativeAllocRef(key_new, true);
        var r = eina_hash_move(Handle, nko, nk);
        FreeNativeIndirection<TKey>(gchnko, ForceRefKey<TKey>());
        FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
        // NativeFreeRef<TKey>(nk_old, OwnKey && r);
        // NativeFreeRef<TKey>(nk_new, !r);
        return r;
    }

    /// <summary>
    ///   Frees the hash buckets.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public void FreeBuckets()
    {
        eina_hash_free_buckets(Handle);
    }

    /// <summary>
    ///   Returns the number of entries in the hash.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <returns>The number of entries, 0 on error.</returns>
    public int Population()
    {
        return eina_hash_population(Handle);
    }

    /// <summary>
    ///   Gets an Iterator for keys.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public Eina.Iterator<TKey> Keys()
    {
        return new Eina.Iterator<TKey>(EinaHashIteratorKeyNew<TKey>(Handle), true);
    }

    /// <summary>
    ///   Gets An Iterator for values.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public Eina.Iterator<TValue> Values()
    {
        return new Eina.Iterator<TValue>(eina_hash_iterator_data_new(Handle), true);
    }

    /// <summary>
    ///   Gets an Iterator for hask.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        IntPtr itr = eina_hash_iterator_tuple_new(Handle);
        try
        {
            for (IntPtr tuplePtr; eina_iterator_next(itr, out tuplePtr);)
            {
                var tuple = Marshal.PtrToStructure<Eina.HashTupleNative>(tuplePtr);
                IntPtr ikey = IndirectNative<TKey>(tuple.key, ForceRefKey<TKey>());
                var key = NativeToManaged<TKey>(ikey);
                var val = NativeToManaged<TValue>(tuple.data);
                yield return new KeyValuePair<TKey, TValue>(key, val);
            }
        }
        finally
        {
            eina_iterator_free(itr);
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

}