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
|
/*****************************************************************
|
| Platinum - Managed Helpers
|
| Copyright (c) 2004-2010, Plutinosoft, LLC.
| All rights reserved.
| http://www.plutinosoft.com
|
| This program is free software; you can redistribute it and/or
| modify it under the terms of the GNU General Public License
| as published by the Free Software Foundation; either version 2
| of the License, or (at your option) any later version.
|
| OEMs, ISVs, VARs and other distributors that combine and
| distribute commercially licensed software with Platinum software
| and do not wish to distribute the source code for the commercially
| licensed software under version 2, or (at your option) any later
| version, of the GNU General Public License (the "GPL") must enter
| into a commercial license agreement with Plutinosoft, LLC.
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; see the file LICENSE.txt. If not, write to
| the Free Software Foundation, Inc.,
| 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
| http://www.gnu.org/licenses/gpl-2.0.html
|
****************************************************************/
#pragma once
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "NeptuneException.h"
#include "Clix.h"
using namespace clix;
namespace Platinum
{
/*----------------------------------------------------------------------
| Helpers
+---------------------------------------------------------------------*/
private ref class Helpers
{
public:
static void ThrowOnError(NPT_Result r)
{
if (NPT_FAILED(r))
{
throw gcnew NeptuneException(r);
}
}
static void ThrowOnErrorButNoSuchItem(NPT_Result r)
{
if (NPT_FAILED(r) && (r != NPT_ERROR_NO_SUCH_ITEM))
{
throw gcnew NeptuneException(r);
}
}
// this code was part of RouterControl.IO (http://routercontrol.codeplex.com)
// for more details see:
// http://www.knopflerfish.org/releases/current/docs/jars/upnp/upnp_api-2.0.0/src/org/osgi/service/upnp/UPnPStateVariable.java
static Type^ ParseType(const NPT_String& upnpType)
{
NPT_String s (upnpType);
s.MakeLowercase();
if (s == "string")
return String::typeid;
if (s == "char")
return Char::typeid;
if (s == "boolean")
return Boolean::typeid;
if (s == "ui1")
return Byte::typeid;
if (s == "ui2")
return UInt16::typeid;
if (s == "ui4")
return UInt32::typeid;
if (s == "i1")
return SByte::typeid;
if (s == "i2")
return Int16::typeid;
if ((s == "i4") || (s == "int"))
return Int32::typeid;
if ((s == "r4") || (s == "float"))
return Single::typeid;
if ((s == "r8") || (s == "number") || (s == "fixed.14.4"))
return Double::typeid;
if ((s == "date") || (s == "datetime") || (s == "datetime.tz"))
return DateTime::typeid;
if ((s == "time") || (s == "time.tz")) // milliseconds since midnight
return UInt64::typeid;
if ((s == "bin.base64") || (s == "bin.hex"))
return array<Byte>::typeid;
if (s == "uri")
return Uri::typeid;
if (s == "uuid")
return Guid::typeid;
throw gcnew ArgumentException("unknown type", "upnpType");
}
static Object^ ConvertValue(const NPT_String& targetType, const NPT_String& val)
{
return ConvertValue(ParseType(targetType), val);
}
static Object^ ConvertValue(Type^ targetType, const NPT_String& val)
{
String^ strVal = gcnew String(val);
if (targetType == String::typeid)
return strVal;
if (targetType == Char::typeid)
{
if (val.IsEmpty())
throw gcnew ArgumentException("character value is empty", "val");
return Char(val[0]);
}
if (targetType == Boolean::typeid)
return Convert::ToBoolean(strVal);
if (targetType == Byte::typeid)
return Convert::ToByte(strVal);
if (targetType == SByte::typeid)
return Convert::ToSByte(strVal);
if (targetType == UInt16::typeid)
return Convert::ToUInt16(strVal);
if (targetType == UInt32::typeid)
return Convert::ToUInt32(strVal);
if (targetType == UInt64::typeid)
return Convert::ToUInt64(strVal);
if (targetType == Int16::typeid)
return Convert::ToInt16(strVal);
if (targetType == Int32::typeid)
return Convert::ToInt32(strVal);
if (targetType == Int64::typeid)
return Convert::ToInt64(strVal);
if (targetType == Single::typeid)
return Convert::ToSingle(strVal);
if (targetType == Double::typeid)
return Convert::ToDouble(strVal);
if (targetType == DateTime::typeid)
return Convert::ToDateTime(strVal);
if (targetType == array<Byte>::typeid)
return Convert::FromBase64String(strVal);
if (targetType == Uri::typeid)
return marshal_as<Uri^>(val);
if (targetType == Guid::typeid)
return gcnew Guid(strVal);
throw gcnew ArgumentException("unsupported type", "targetType");
}
};
/*----------------------------------------------------------------------
| StringConv
+---------------------------------------------------------------------*/
struct StringConv
{
gcroot<msclr::interop::marshal_context^> c;
//const char* szAnsi;
std::string szAnsi;
StringConv(System::String^ s) :
//c(gcnew msclr::interop::marshal_context),
//szAnsi(c->marshal_as<E_UTF8>(s))
szAnsi(marshalString<E_UTF8>(s))
{}
~StringConv()
{}
operator const char*() const
{
//return szAnsi;
return szAnsi.c_str();
}
};
/*----------------------------------------------------------------------
| StringConvA
+---------------------------------------------------------------------*/
struct StringConvA
{
char* szAnsi;
StringConvA(System::String^ s) :
szAnsi(static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
{}
~StringConvA()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};
/*----------------------------------------------------------------------
| StringConvW
+---------------------------------------------------------------------*/
struct StringConvW
{
wchar_t* szUnicode;
StringConvW(System::String^ s) :
szUnicode(static_cast<wchar_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer()))
{}
~StringConvW()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};
/*----------------------------------------------------------------------
| ManagedWrapper
+---------------------------------------------------------------------*/
template<typename T_NativeType>
public ref class ManagedWrapper
{
protected:
T_NativeType* m_pHandle;
bool m_Owned;
internal:
property T_NativeType& Handle
{
T_NativeType& get()
{
return *m_pHandle;
}
}
public:
virtual Boolean Equals(Object^ obj) override
{
if (obj == nullptr)
return false;
if (!this->GetType()->IsInstanceOfType(obj))
return false;
return (m_pHandle == ((ManagedWrapper^)obj)->m_pHandle);
}
internal:
ManagedWrapper() : m_Owned(true)
{
m_pHandle = new T_NativeType();
}
/*ManagedWrapper(ManagedWrapper^ obj)
{
m_pHandle = new T_NativeType(obj->Handle);
}
ManagedWrapper(T_NativeType object_class) : m_Owned(true)
{
m_pHandle = new T_NativeType(object_class.Handle);
}*/
ManagedWrapper(T_NativeType& object_class) : m_Owned(false)
{
// IMPORTANT: we're keeping a reference of the native pointer
// so passing a reference to a local variable allocated on the stack is not OK
m_pHandle = &object_class;
}
public:
~ManagedWrapper()
{
this->!ManagedWrapper();
}
!ManagedWrapper()
{
if (m_pHandle != 0 && m_Owned)
{
delete m_pHandle;
}
m_pHandle = 0;
}
};
}
#define PLATINUM_MANAGED_IMPLEMENT_PROPERTY(propertyType, propertyName, nativeVar, nativePtr) \
property propertyType propertyName { \
propertyType get() { \
return (nativePtr##->##nativeVar); \
} \
void set(propertyType var) { \
nativePtr##->##nativeVar = (var); \
} \
}
#define PLATINUM_MANAGED_IMPLEMENT_STRING_PROPERTY(propertyType, propertyName, nativeVar, nativePtr) \
property propertyType propertyName { \
propertyType get() { \
return marshal_as<propertyType>(nativePtr##->##nativeVar); \
} \
void set(propertyType var) { \
std::string s = marshalString<E_UTF8>(var); \
nativePtr##->##nativeVar = s.c_str(); \
} \
}
#define PLATINUM_MANAGED_IMPLEMENT_OBJECT_PROPERTY(propertyType, propertyName, nativeVar, nativePtr) \
property propertyType propertyName { \
propertyType get() { \
return marshal_as<propertyType>(nativePtr##->##nativeVar); \
} \
void set(propertyType var) { \
nativePtr##->##nativeVar = var->Handle; \
} \
}
|