Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Interop / Interop.CoordinatesList.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19
20 internal static partial class Interop
21 {
22     [DllImport(Libraries.MapService, EntryPoint = "maps_coordinates_list_append")]
23     internal static extern ErrorCode Append(this CoordinatesListHandle /* maps_coordinates_list_h */ coordinatesList, IntPtr /* maps_coordinates_h */ coordinates);
24
25     [DllImport(Libraries.MapService, EntryPoint = "maps_coordinates_list_remove")]
26     internal static extern ErrorCode Remove(this CoordinatesListHandle /* maps_coordinates_list_h */ coordinatesList, CoordinatesHandle /* maps_coordinates_h */ coordinates);
27
28
29     internal class CoordinatesListHandle : SafeMapsHandle
30     {
31         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
32         internal delegate bool CoordinatesCallback(int index, IntPtr /* maps_coordinates_h */ coordinates, IntPtr /* void */ userData);
33
34         [DllImport(Libraries.MapService, EntryPoint = "maps_coordinates_list_create")]
35         internal static extern ErrorCode Create(out IntPtr /* maps_coordinates_list_h */ coordinatesList);
36
37         [DllImport(Libraries.MapService, EntryPoint = "maps_coordinates_list_destroy")]
38         internal static extern ErrorCode Destroy(IntPtr /* maps_coordinates_list_h */ coordinatesList);
39
40         [DllImport(Libraries.MapService, EntryPoint = "maps_coordinates_list_foreach")]
41         internal static extern ErrorCode Foreach(IntPtr /* maps_coordinates_list_h */ coordinatesList, CoordinatesCallback callback, IntPtr /* void */ userData);
42
43         internal CoordinatesListHandle(IntPtr handle, bool needToRelease) : base(handle, needToRelease, Destroy) { }
44
45         internal CoordinatesListHandle(bool needToRelease = true) : this(IntPtr.Zero, needToRelease)
46         {
47             Create(out handle).ThrowIfFailed("Failed to create native handle");
48         }
49
50         internal void Add(CoordinatesHandle handleToAdd)
51         {
52             using (var clonedHandle = CoordinatesHandle.CloneFrom(handleToAdd))
53             {
54                 if (this.Append(clonedHandle).WarnIfFailed("Failed to add coordinates to the list"))
55                 {
56                     clonedHandle.HasOwnership = false;
57                 }
58             }
59         }
60
61         internal void ForEach(Action<CoordinatesHandle> action)
62         {
63             CoordinatesCallback callback = (index, handle, userData) =>
64             {
65                 action(CoordinatesHandle.CloneFrom(handle));
66                 return true;
67             };
68
69             Foreach(handle, callback, IntPtr.Zero).WarnIfFailed("Failed to get coordinates list from native handle");
70         }
71     }
72 }