2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
20 namespace Tizen.Content.MediaContent
22 internal static class InteropHelper
24 internal delegate MediaContentError GetStringFunc<T>(T handle, out IntPtr value);
26 internal static string GetString<T>(T handle, GetStringFunc<T> func, bool nullable = false)
28 IntPtr val = IntPtr.Zero;
31 func(handle, out val).ThrowIfError("Failed to get value");
33 if (val == IntPtr.Zero)
35 return nullable ? null : string.Empty;
38 return Marshal.PtrToStringAnsi(val);
42 Marshal.FreeHGlobal(val);
46 internal delegate MediaContentError GetValueFunc<T, TValue>(T handle, out TValue value);
48 internal static TValue GetValue<T, TValue>(T handle, GetValueFunc<T, TValue> func)
50 func(handle, out var val).ThrowIfError("Failed to get value");
55 internal static TValue GetValue<TValue>(IntPtr handle, GetValueFunc<IntPtr, TValue> func)
57 func(handle, out var val).ThrowIfError("Failed to get value");
62 internal static TValue GetValue<TValue>(Interop.MediaInfoHandle handle,
63 GetValueFunc<Interop.MediaInfoHandle, TValue> func)
65 func(handle, out var val).ThrowIfError("Failed to get value");
70 internal static DateTimeOffset GetDateTime<T>(T handle,
71 GetValueFunc<T, IntPtr> func)
73 IntPtr time = IntPtr.Zero;
75 func(handle, out time).ThrowIfError("Failed to get value");
77 return DateTimeOffset.FromUnixTimeSeconds(time.ToInt64());