kodi
Helpers.h
1 /*****************************************************************
2 |
3 | Platinum - Managed Helpers
4 |
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
13 |
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 |
21 | This program is distributed in the hope that it will be useful,
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | GNU General Public License for more details.
25 |
26 | You should have received a copy of the GNU General Public License
27 | along with this program; see the file LICENSE.txt. If not, write to
28 | the Free Software Foundation, Inc.,
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 | http://www.gnu.org/licenses/gpl-2.0.html
31 |
32 ****************************************************************/
33 #pragma once
34 
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NeptuneException.h"
39 #include "Clix.h"
40 
41 using namespace clix;
42 
43 namespace Platinum
44 {
45 
46 /*----------------------------------------------------------------------
47 | Helpers
48 +---------------------------------------------------------------------*/
49 private ref class Helpers
50 {
51 public:
52 
53  static void ThrowOnError(NPT_Result r)
54  {
55  if (NPT_FAILED(r))
56  {
57  throw gcnew NeptuneException(r);
58  }
59  }
60 
61  static void ThrowOnErrorButNoSuchItem(NPT_Result r)
62  {
63  if (NPT_FAILED(r) && (r != NPT_ERROR_NO_SUCH_ITEM))
64  {
65  throw gcnew NeptuneException(r);
66  }
67  }
68 
69  // this code was part of RouterControl.IO (http://routercontrol.codeplex.com)
70  // for more details see:
71  // http://www.knopflerfish.org/releases/current/docs/jars/upnp/upnp_api-2.0.0/src/org/osgi/service/upnp/UPnPStateVariable.java
72  static Type^ ParseType(const NPT_String& upnpType)
73  {
74  NPT_String s (upnpType);
75 
76  s.MakeLowercase();
77 
78  if (s == "string")
79  return String::typeid;
80 
81  if (s == "char")
82  return Char::typeid;
83 
84  if (s == "boolean")
85  return Boolean::typeid;
86 
87  if (s == "ui1")
88  return Byte::typeid;
89 
90  if (s == "ui2")
91  return UInt16::typeid;
92 
93  if (s == "ui4")
94  return UInt32::typeid;
95 
96  if (s == "i1")
97  return SByte::typeid;
98 
99  if (s == "i2")
100  return Int16::typeid;
101 
102  if ((s == "i4") || (s == "int"))
103  return Int32::typeid;
104 
105  if ((s == "r4") || (s == "float"))
106  return Single::typeid;
107 
108  if ((s == "r8") || (s == "number") || (s == "fixed.14.4"))
109  return Double::typeid;
110 
111  if ((s == "date") || (s == "datetime") || (s == "datetime.tz"))
112  return DateTime::typeid;
113 
114  if ((s == "time") || (s == "time.tz")) // milliseconds since midnight
115  return UInt64::typeid;
116 
117  if ((s == "bin.base64") || (s == "bin.hex"))
118  return array<Byte>::typeid;
119 
120  if (s == "uri")
121  return Uri::typeid;
122 
123  if (s == "uuid")
124  return Guid::typeid;
125 
126  throw gcnew ArgumentException("unknown type", "upnpType");
127  }
128 
129  static Object^ ConvertValue(const NPT_String& targetType, const NPT_String& val)
130  {
131  return ConvertValue(ParseType(targetType), val);
132  }
133 
134  static Object^ ConvertValue(Type^ targetType, const NPT_String& val)
135  {
136  String^ strVal = gcnew String(val);
137 
138  if (targetType == String::typeid)
139  return strVal;
140 
141  if (targetType == Char::typeid)
142  {
143  if (val.IsEmpty())
144  throw gcnew ArgumentException("character value is empty", "val");
145 
146  return Char(val[0]);
147  }
148 
149  if (targetType == Boolean::typeid)
150  return Convert::ToBoolean(strVal);
151 
152  if (targetType == Byte::typeid)
153  return Convert::ToByte(strVal);
154 
155  if (targetType == SByte::typeid)
156  return Convert::ToSByte(strVal);
157 
158  if (targetType == UInt16::typeid)
159  return Convert::ToUInt16(strVal);
160 
161  if (targetType == UInt32::typeid)
162  return Convert::ToUInt32(strVal);
163 
164  if (targetType == UInt64::typeid)
165  return Convert::ToUInt64(strVal);
166 
167  if (targetType == Int16::typeid)
168  return Convert::ToInt16(strVal);
169 
170  if (targetType == Int32::typeid)
171  return Convert::ToInt32(strVal);
172 
173  if (targetType == Int64::typeid)
174  return Convert::ToInt64(strVal);
175 
176  if (targetType == Single::typeid)
177  return Convert::ToSingle(strVal);
178 
179  if (targetType == Double::typeid)
180  return Convert::ToDouble(strVal);
181 
182  if (targetType == DateTime::typeid)
183  return Convert::ToDateTime(strVal);
184 
185  if (targetType == array<Byte>::typeid)
186  return Convert::FromBase64String(strVal);
187 
188  if (targetType == Uri::typeid)
189  return marshal_as<Uri^>(val);
190 
191  if (targetType == Guid::typeid)
192  return gcnew Guid(strVal);
193 
194  throw gcnew ArgumentException("unsupported type", "targetType");
195  }
196 
197 };
198 
199 /*----------------------------------------------------------------------
200 | StringConv
201 +---------------------------------------------------------------------*/
203 {
204  gcroot<msclr::interop::marshal_context^> c;
205  //const char* szAnsi;
206  std::string szAnsi;
207 
208  StringConv(System::String^ s) :
209  //c(gcnew msclr::interop::marshal_context),
210  //szAnsi(c->marshal_as<E_UTF8>(s))
211  szAnsi(marshalString<E_UTF8>(s))
212  {}
213 
214  ~StringConv()
215  {}
216 
217  operator const char*() const
218  {
219  //return szAnsi;
220  return szAnsi.c_str();
221  }
222 };
223 
224 /*----------------------------------------------------------------------
225 | StringConvA
226 +---------------------------------------------------------------------*/
228 {
229  char* szAnsi;
230 
231  StringConvA(System::String^ s) :
232  szAnsi(static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
233  {}
234 
235  ~StringConvA()
236  {
237 
238  System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
239  }
240 
241  operator LPCSTR() const
242  {
243  return szAnsi;
244  }
245 };
246 
247 /*----------------------------------------------------------------------
248 | StringConvW
249 +---------------------------------------------------------------------*/
251 {
252  wchar_t* szUnicode;
253 
254  StringConvW(System::String^ s) :
255  szUnicode(static_cast<wchar_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer()))
256  {}
257 
258  ~StringConvW()
259  {
260 
261  System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szUnicode));
262  }
263 
264  operator LPCWSTR() const
265  {
266  return szUnicode;
267  }
268 };
269 
270 /*----------------------------------------------------------------------
271 | ManagedWrapper
272 +---------------------------------------------------------------------*/
273 template<typename T_NativeType>
274 public ref class ManagedWrapper
275 {
276 protected:
277 
278  T_NativeType* m_pHandle;
279  bool m_Owned;
280 
281 internal:
282 
283  property T_NativeType& Handle
284  {
285  T_NativeType& get()
286  {
287  return *m_pHandle;
288  }
289  }
290 
291 public:
292 
293  virtual Boolean Equals(Object^ obj) override
294  {
295  if (obj == nullptr)
296  return false;
297 
298  if (!this->GetType()->IsInstanceOfType(obj))
299  return false;
300 
301  return (m_pHandle == ((ManagedWrapper^)obj)->m_pHandle);
302  }
303 
304 internal:
305 
306  ManagedWrapper() : m_Owned(true)
307  {
308  m_pHandle = new T_NativeType();
309  }
310 
311  /*ManagedWrapper(ManagedWrapper^ obj)
312  {
313  m_pHandle = new T_NativeType(obj->Handle);
314  }
315 
316  ManagedWrapper(T_NativeType object_class) : m_Owned(true)
317  {
318  m_pHandle = new T_NativeType(object_class.Handle);
319  }*/
320 
321  ManagedWrapper(T_NativeType& object_class) : m_Owned(false)
322  {
323  // IMPORTANT: we're keeping a reference of the native pointer
324  // so passing a reference to a local variable allocated on the stack is not OK
325  m_pHandle = &object_class;
326  }
327 
328 public:
329 
330  ~ManagedWrapper()
331  {
332  this->!ManagedWrapper();
333  }
334 
335  !ManagedWrapper()
336  {
337  if (m_pHandle != 0 && m_Owned)
338  {
339  delete m_pHandle;
340  }
341 
342  m_pHandle = 0;
343  }
344 };
345 
346 }
347 
348 #define PLATINUM_MANAGED_IMPLEMENT_PROPERTY(propertyType, propertyName, nativeVar, nativePtr) \
349 property propertyType propertyName { \
350  propertyType get() { \
351  return (nativePtr##->##nativeVar); \
352  } \
353  void set(propertyType var) { \
354  nativePtr##->##nativeVar = (var); \
355  } \
356 }
357 
358 #define PLATINUM_MANAGED_IMPLEMENT_STRING_PROPERTY(propertyType, propertyName, nativeVar, nativePtr) \
359 property propertyType propertyName { \
360  propertyType get() { \
361  return marshal_as<propertyType>(nativePtr##->##nativeVar); \
362  } \
363  void set(propertyType var) { \
364  std::string s = marshalString<E_UTF8>(var); \
365  nativePtr##->##nativeVar = s.c_str(); \
366  } \
367 }
368 
369 #define PLATINUM_MANAGED_IMPLEMENT_OBJECT_PROPERTY(propertyType, propertyName, nativeVar, nativePtr) \
370 property propertyType propertyName { \
371  propertyType get() { \
372  return marshal_as<propertyType>(nativePtr##->##nativeVar); \
373  } \
374  void set(propertyType var) { \
375  nativePtr##->##nativeVar = var->Handle; \
376  } \
377 }
378 
Definition: Helpers.h:227
Definition: NeptuneException.h:42
Definition: Helpers.h:49
Definition: Helpers.h:202
Definition: Clix.h:19
Definition: Helpers.h:274
Definition: Helpers.h:250
Definition: NptStrings.h:57