#include "StdAfx.h" #include "Wrapping/Value.h" #include "Wrapping/Frame.h" namespace Bblids { namespace Wrapping { // ----------------------------------------------------------------------------- // ***************************************************************************** // ----------------------------------------------------------------------------- CValueType::CValueType(const Bblids::Frames::TValueType& parNativeValueType) : FWrappedValueType (&parNativeValueType) { } // ----------------------------------------------------------------------------- int CValueType::CompareTo(CValueType^ parOther) { BBLIDS_ASSERT(parOther != nullptr); return ((*FWrappedValueType < *parOther->FWrappedValueType) ? -1 : ((*FWrappedValueType == *parOther->FWrappedValueType) ? 0 : 1)); } // ----------------------------------------------------------------------------- bool CValueType::Equals(CValueType^ parOther) { BBLIDS_ASSERT(parOther != nullptr); return (*FWrappedValueType == *parOther->FWrappedValueType); } // ----------------------------------------------------------------------------- CValueType^ CValueType::FromTypeId(const Bblids::Frames::EValueType parTypeId) { return (gcnew CValueType(Bblids::Frames::TValueType::FromTypeId(parTypeId))); } // ----------------------------------------------------------------------------- CValueType^ CValueType::FromTypeName(System::String^ parTypeName) { BBLIDS_ASSERT(parTypeName != nullptr); System::IntPtr clr_string = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(parTypeName); CValueType^ value_type = gcnew CValueType(Bblids::Frames::TValueType::FromTypeName(reinterpret_cast(clr_string.ToPointer()))); System::Runtime::InteropServices::Marshal::FreeHGlobal(clr_string); return (value_type); } // ----------------------------------------------------------------------------- CValueType^ CValueType::FromTypeName(CSymbol^ parTypeName) { BBLIDS_ASSERT(parTypeName != nullptr); return (gcnew CValueType(Bblids::Frames::TValueType::FromTypeName(parTypeName->GetWrappedSymbol()))); } // ----------------------------------------------------------------------------- CValueType^ CValueType::FromTypeId_Clr(const Bblids::Frames::EValueType parTypeId) { switch (parTypeId) { case Bblids::Frames::EVALUE_INTEGER : return (CValueType::Integer); case Bblids::Frames::EVALUE_REAL : return (CValueType::Real); case Bblids::Frames::EVALUE_BOOLEAN : return (CValueType::Boolean); case Bblids::Frames::EVALUE_STRING : return (CValueType::String); case Bblids::Frames::EVALUE_SYMBOL : return (CValueType::Symbol); case Bblids::Frames::EVALUE_DATETIME : return (CValueType::DateTime); case Bblids::Frames::EVALUE_LIST : return (CValueType::List); case Bblids::Frames::EVALUE_GUID : return (CValueType::Guid); case Bblids::Frames::EVALUE_FRAME : return (CValueType::Frame); #ifdef _BBLIDS_DEBUG default: ASSERT_UNREACHED; #endif } return (nullptr); } // ----------------------------------------------------------------------------- // ***************************************************************************** // ----------------------------------------------------------------------------- CValue::CValue(CValue::CInteger parValue) : FWrappedValue (new Bblids::Frames::TValue(parValue)) { } // ----------------------------------------------------------------------------- CValue::CValue(CReal parValue) : FWrappedValue (new Bblids::Frames::TValue(parValue)) { } // ----------------------------------------------------------------------------- CValue::CValue(CValue::CBoolean parValue) : FWrappedValue (new Bblids::Frames::TValue(parValue)) { } // ----------------------------------------------------------------------------- CValue::CValue(CValue::CString parValue) : FWrappedValue (nullptr) { BBLIDS_ASSERT(parValue != nullptr); typedef System::Runtime::InteropServices::Marshal marshall_namespace; System::IntPtr clr_string = marshall_namespace::StringToHGlobalAnsi(parValue); std::string native_string(reinterpret_cast(clr_string.ToPointer())); FWrappedValue.Reset(new Bblids::Frames::TValue(native_string)); marshall_namespace::FreeHGlobal(clr_string); } // ----------------------------------------------------------------------------- CValue::CValue(CValue::CSymbol parValue) : FWrappedValue (new Bblids::Frames::TValue(parValue->GetWrappedSymbol())) { } // ----------------------------------------------------------------------------- CValue::CValue(CValue::CDateTime parValue) : FWrappedValue (new Bblids::Frames::TValue(Bblids::TTimeStamp(parValue->Ticks))) { } // ----------------------------------------------------------------------------- CValue::CValue(CValue::CList parValue) : FWrappedValue (nullptr) { BBLIDS_ASSERT(parValue != nullptr); typedef Bblids::Frames::TListValue::value_type native_list_type; BBLIDS_ASSERT(parValue != nullptr); native_list_type native_list(new Bblids::Frames::TValueVariantList); for each (CValue^ item in parValue) native_list->push_back(item->GetWrappedValue()); FWrappedValue.Reset(new Bblids::Frames::TValue(native_list)); } // ----------------------------------------------------------------------------- CValue::CValue(CValue::CGuid parValue) : FWrappedValue (nullptr) { BBLIDS_ASSERT(parValue != nullptr); array^ bytes = parValue->ToByteArray(); Bblids::TGuid native_guid; for (int i = 0; i < 16; ++i) native_guid.FPack.v[i] = bytes[i]; FWrappedValue.Reset(new Bblids::Frames::TValue(native_guid)); } // ----------------------------------------------------------------------------- CValue::CValue(CValue::CFrame parValue) : FWrappedValue (new Bblids::Frames::TValue(parValue->GetWrappedFrame())) { } // ----------------------------------------------------------------------------- CValue::CValue(const Bblids::Frames::TValue& parWrappedValue) : FWrappedValue (new Bblids::Frames::TValue(parWrappedValue)) { } // ----------------------------------------------------------------------------- CValue::CValue(CValue% parOther) : FWrappedValue (parOther.FWrappedValue.Release()) { } // ----------------------------------------------------------------------------- System::Object^ CValue::Get() { switch (FWrappedValue->Type().Id()) { case Bblids::Frames::EVALUE_INTEGER : return (ConvertToInteger()); case Bblids::Frames::EVALUE_REAL : return (ConvertToReal()); case Bblids::Frames::EVALUE_BOOLEAN : return (ConvertToBoolean()); case Bblids::Frames::EVALUE_STRING : return (ConvertToString()); case Bblids::Frames::EVALUE_SYMBOL : return (ConvertToSymbol()); case Bblids::Frames::EVALUE_DATETIME : return (ConvertToDateTime()); case Bblids::Frames::EVALUE_LIST : return (ConvertToList()); case Bblids::Frames::EVALUE_GUID : return (ConvertToGuid()); case Bblids::Frames::EVALUE_FRAME : return (ConvertToFrame()); #ifdef _BBLIDS_DEBUG default: ASSERT_UNREACHED; #endif } return (nullptr); } // ----------------------------------------------------------------------------- void CValue::Set(System::Object^ parValue) { BBLIDS_ASSERT(parValue != nullptr); switch (Type->Id) { case Bblids::Frames::EVALUE_INTEGER : AssignFromInteger(safe_cast(parValue)); break; case Bblids::Frames::EVALUE_REAL : AssignFromReal(safe_cast(parValue)); break; case Bblids::Frames::EVALUE_BOOLEAN : AssignFromBoolean(safe_cast(parValue)); break; case Bblids::Frames::EVALUE_STRING : AssignFromString(safe_cast(parValue)); break; case Bblids::Frames::EVALUE_SYMBOL : AssignFromSymbol(safe_cast(parValue)); break; case Bblids::Frames::EVALUE_DATETIME : AssignFromDateTime(safe_cast(parValue)); break; case Bblids::Frames::EVALUE_LIST : AssignFromList(safe_cast(parValue)); break; case Bblids::Frames::EVALUE_GUID : AssignFromGuid(safe_cast(parValue)); break; case Bblids::Frames::EVALUE_FRAME : AssignFromFrame(safe_cast(parValue)); break; #ifdef _BBLIDS_DEBUG default: ASSERT_UNREACHED; #endif } } // ----------------------------------------------------------------------------- void CValue::NativeFromInteger(Bblids::Frames::TValue& outValue, CValue::CInteger parValue) { outValue = parValue; } // ----------------------------------------------------------------------------- void CValue::NativeFromReal(Bblids::Frames::TValue& outValue, CValue::CReal parValue) { outValue = parValue; } // ----------------------------------------------------------------------------- void CValue::NativeFromBoolean(Bblids::Frames::TValue& outValue, CValue::CBoolean parValue) { outValue = parValue; } // ----------------------------------------------------------------------------- void CValue::NativeFromString(Bblids::Frames::TValue& outValue, CValue::CString parValue) { BBLIDS_ASSERT(parValue != nullptr); typedef System::Runtime::InteropServices::Marshal marshall_namespace; System::IntPtr clr_string = marshall_namespace::StringToHGlobalAnsi(parValue); std::string native_string(reinterpret_cast(clr_string.ToPointer())); outValue = native_string; marshall_namespace::FreeHGlobal(clr_string); } // ----------------------------------------------------------------------------- void CValue::NativeFromSymbol(Bblids::Frames::TValue& outValue, CValue::CSymbol parValue) { BBLIDS_ASSERT(parValue != nullptr); outValue = parValue->GetWrappedSymbol(); } // ----------------------------------------------------------------------------- void CValue::NativeFromDateTime(Bblids::Frames::TValue& outValue, CValue::CDateTime parValue) { BBLIDS_ASSERT(parValue != nullptr); Bblids::Frames::TDateTimeValue::value_type native_datetime( Bblids::TTimeStamp::date_type( parValue->Year , parValue->Month , parValue->Day), Bblids::TTimeStamp::time_type( parValue->Hour , parValue->Minute , parValue->Second , parValue->Millisecond)); outValue = native_datetime; } // ----------------------------------------------------------------------------- void CValue::NativeFromList(Bblids::Frames::TValue& outValue, CValue::CList parValue) { typedef Bblids::Frames::TListValue::value_type native_list_type; BBLIDS_ASSERT(parValue != nullptr); native_list_type native_list(new Bblids::Frames::TValueVariantList); for each (CValue^ item in parValue) native_list->push_back(item->GetWrappedValue()); outValue = native_list; } // ----------------------------------------------------------------------------- void CValue::NativeFromGuid(Bblids::Frames::TValue& outValue, CValue::CGuid parValue) { typedef Bblids::Frames::TGuidValue::value_type native_guid_type; BBLIDS_ASSERT(parValue != nullptr); array^ clr_bytes = parValue->ToByteArray(); native_guid_type& native_guid = outValue.As(); for (int i = 0; i < 16; ++i) native_guid.FPack.v[i] = clr_bytes[i]; } // ----------------------------------------------------------------------------- void CValue::NativeFromFrame(Bblids::Frames::TValue& outValue, CValue::CFrame parValue) { BBLIDS_ASSERT(parValue != nullptr); outValue = parValue->GetWrappedFrame(); } // ----------------------------------------------------------------------------- CValue::CInteger CValue::NativeToInteger(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TIntegerValue::value_type native_integer_type; return (parValue.As()); } // ----------------------------------------------------------------------------- CValue::CReal CValue::NativeToReal(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TRealValue::value_type native_real_type; return (parValue.As()); } // ----------------------------------------------------------------------------- CValue::CBoolean CValue::NativeToBoolean(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TBooleanValue::value_type native_bool_type; return (parValue.As()); } // ----------------------------------------------------------------------------- CValue::CString CValue::NativeToString(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TStringValue::value_type native_string_type; const native_string_type& native_string = parValue.As(); return (gcnew System::String(native_string.c_str())); } // ----------------------------------------------------------------------------- CValue::CSymbol CValue::NativeToSymbol(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TSymbolValue::value_type native_symbol_type; const native_symbol_type& native_symbol = parValue.As(); return (gcnew Bblids::Wrapping::CSymbol(native_symbol)); } // ----------------------------------------------------------------------------- CValue::CDateTime CValue::NativeToDateTime(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TDateTimeValue::value_type native_datetime_type; const native_datetime_type& native_timestamp = parValue.As(); Bblids::TTimeStamp::date_type date = native_timestamp.ToDate(); Bblids::TTimeStamp::time_type time = native_timestamp.ToTime(); return (gcnew System::DateTime( static_cast(date.year()) , static_cast(date.month()) , static_cast(date.day()) , time.hours() , time.minutes() , time.seconds() , static_cast(time.fractional_seconds()))); } // ----------------------------------------------------------------------------- CValue::CList CValue::NativeToList(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TListValue::value_type native_list_type; const native_list_type& native_list = parValue.As(); CValue::CList managed_list = gcnew System::Collections::Generic::List(native_list->size()); foreach (Bblids::Frames::TValue& item, *native_list) managed_list->Add(gcnew CValue(item)); return (managed_list); } // ----------------------------------------------------------------------------- CValue::CGuid CValue::NativeToGuid(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TGuidValue::value_type native_guid_type; const native_guid_type& native_guid = parValue.As(); return (gcnew System::Guid( native_guid.FPack.clr.a , native_guid.FPack.clr.b , native_guid.FPack.clr.c , native_guid.FPack.clr.d , native_guid.FPack.clr.e , native_guid.FPack.clr.f , native_guid.FPack.clr.g , native_guid.FPack.clr.h , native_guid.FPack.clr.i , native_guid.FPack.clr.j , native_guid.FPack.clr.k)); } // ----------------------------------------------------------------------------- CValue::CFrame CValue::NativeToFrame(const Bblids::Frames::TValue& parValue) { typedef Bblids::Frames::TFrameValue::value_type native_frame_type; const native_frame_type& native_frame = parValue.As(); return (gcnew Bblids::Wrapping::CFrame(native_frame)); } // ----------------------------------------------------------------------------- // ***************************************************************************** // ----------------------------------------------------------------------------- }}