Tanoda
BitConverterTemplate.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Runtime.InteropServices;
11
12namespace Leap.Unity.Generation {
13
14 public static class BitConverterNonAlloc_Template_ {
15
16#if !ENABLE_IL2CPP
17 [ThreadStatic]
18 private static ConversionStruct _c;
19#endif
20
21 //BEGIN TO
27 public static Single ToSingle(byte[] bytes, int offset = 0) {
28#if ENABLE_IL2CPP
29 unsafe {
30 fixed (void* ptr = &bytes[offset]) {
31 return *(Single*)ptr;
32 }
33 }
34#else
35 //FILL BYTES
36 return _c.Single;
37#endif
38 }
39 //END
40 //BEGIN TO
41
50 public static Single ToSingle(byte[] bytes, ref int offset) {
51#if ENABLE_IL2CPP
52 unsafe {
53 fixed (void* ptr = &bytes[offset]) {
54 offset += sizeof(Single);
55 return *(Single*)ptr;
56 }
57 }
58#else
59 //FILL BYTES
60 return _c.Single;
61#endif
62 }
63 //END
64 //BEGIN GET
65
71 public static void GetBytes(Single value, byte[] bytes, int offset = 0) {
72#if ENABLE_IL2CPP
73 unsafe {
74 fixed (void* ptr = &bytes[offset]) {
75 *(Single*)ptr = value;
76 }
77 }
78#else
79 _c.Single = value;
80 //FILL BYTES
81#endif
82 }
83 //END
84 //BEGIN GET
85
94 public static void GetBytes(Single value, byte[] bytes, ref int offset) {
95#if ENABLE_IL2CPP
96 unsafe {
97 fixed (void* ptr = &bytes[offset]) {
98 offset += sizeof(Single);
99 *(Single*)ptr = value;
100 }
101 }
102#else
103 _c.Single = value;
104 //FILL BYTES
105#endif
106 }
107 //END
108
109#if !ENABLE_IL2CPP
110 [StructLayout(LayoutKind.Explicit)]
111 private struct ConversionStruct {
112 [FieldOffset(0)]
113 public byte Byte0;
114 [FieldOffset(1)]
115 public byte Byte1;
116 [FieldOffset(2)]
117 public byte Byte2;
118 [FieldOffset(3)]
119 public byte Byte3;
120 [FieldOffset(4)]
121 public byte Byte4;
122 [FieldOffset(5)]
123 public byte Byte5;
124 [FieldOffset(6)]
125 public byte Byte6;
126 [FieldOffset(7)]
127 public byte Byte7;
128
129 [FieldOffset(0)]
130 public UInt16 UInt16;
131 [FieldOffset(0)]
132 public Int16 Int16;
133 [FieldOffset(0)]
134 public UInt32 UInt32;
135 [FieldOffset(0)]
136 public Int32 Int32;
137 [FieldOffset(0)]
138 public UInt64 UInt64;
139 [FieldOffset(0)]
140 public Int64 Int64;
141 [FieldOffset(0)]
142 public Single Single;
143 [FieldOffset(0)]
144 public Double Double;
145 }
146#endif
147 }
148}