Skip to content

File ConversionHelper.h

File List > GenApiNet > ConversionHelper.h

Go to the documentation of this file

//-----------------------------------------------------------------------------
//  (c) 2007 by STEMMER IMAGING
//  Author:  Sascha Dorenbeck
//  $Header:
//
//  License: This file is published under the license of the EMVA GENICAM_NAMESPACE  Standard Group.
//  A text file describing the legal terms is included in  your installation as 'GENICAM_NAMESPACE_license.pdf'.
//  If for some reason you are missing  this file please contact the EMVA or visit the website
//  (http://www.genicam.org) for a full copy.
//
//  THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
//  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD  GROUP
//  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL,
//  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  LIMITED TO,
//  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  DATA, OR PROFITS;
//  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  THEORY OF LIABILITY,
//  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)
//  ARISING IN ANY WAY OUT OF THE USE  OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
//-----------------------------------------------------------------------------

#pragma managed ( push , off)
#include "SpinGenApi/INode.h"
#include "SpinGenApi/GCString.h"
#pragma managed ( pop )
#include "Node.h"
#include "Exceptions.h"
#include "../ManagedLogger.h"

namespace SpinnakerNET
{
    using namespace ::System;

#pragma region Type Conversion / Handling
    // ---------------------------------------------------------------------------
    // Type conversion helper template for unmanaged GenApi types.
    // ---------------------------------------------------------------------------
    template <typename T>
        inline T* node_cast(void *pType) { return dynamic_cast<T*>(reinterpret_cast<::Spinnaker::GenApi::INode*>(pType)); }

    // ---------------------------------------------------------------------------
    // Type conversion helper template for unmanaged GenApi types.
    // ---------------------------------------------------------------------------
    template <typename T>
        inline T* node_cast(IntPtr type) { return node_cast<T>(type.ToPointer()); }

    // ---------------------------------------------------------------------------
    // Type check helper template for unmanaged GenApi types.
    // ---------------------------------------------------------------------------
    template <typename T>
        inline bool is_node(void *pType) { return node_cast<T>(pType) != NULL; }

    // ---------------------------------------------------------------------------
    // Type check helper template for unmanaged GenApi types.
    // ---------------------------------------------------------------------------
    template <typename T>
        inline bool is_node(IntPtr type) { return is_node<T>(type.ToPointer()); }

    // ---------------------------------------------------------------------------
    // Type conversion helper template for unmanaged types (downcast).
    // ---------------------------------------------------------------------------
    template <typename T>
        inline T* type_cast(void *pType) { return dynamic_cast<T*>(static_cast<T*>(pType)); }

    // ---------------------------------------------------------------------------
    // Type conversion helper template for unmanaged types (downcast).
    // ---------------------------------------------------------------------------
    template <typename T>
        inline T* type_cast(IntPtr type) { return type_cast<T>(type.ToPointer()); }

    namespace GenApi
    {
        ref class NodeMap;

        // -------------------------------------------------------------------------
        // Converts the given native node into a managed node.
        // -------------------------------------------------------------------------
        SpinnakerNET::GenApi::INode ^ ConvertNode(NodeMap ^map, IntPtr node);

        // -------------------------------------------------------------------------
        // Converts the given native node list into a managed one.
        // -------------------------------------------------------------------------
        inline array<SpinnakerNET::GenApi::INode^> ^ ConvertList(NodeMap ^map, ::Spinnaker::GenApi::NodeList_t &list)
        {
            System::Collections::Generic::List<SpinnakerNET::GenApi::INode^> ^nodeList = gcnew System::Collections::Generic::List<SpinnakerNET::GenApi::INode^>();
            for(::Spinnaker::GenApi::NodeList_t::iterator entry = list.begin(); entry != list.end(); ++entry)
            {
                try
                {
                    SpinnakerNET::GenApi::INode ^ node = ConvertNode(map, IntPtr(*entry));
                    if(node != nullptr)
                    {
                        nodeList->Add(node);
                    }
                }
                catch(Exception ^e) // ignore unsupported nodes
                {
                    System::Diagnostics::Trace::WriteLine(e->Message);
                }
            }
            return nodeList->Count > 0 ? nodeList->ToArray() : nullptr;
        }
    }
#pragma endregion

#pragma region System::String Conversion
    // -------------------------------------------------------------------------
    // Creates a gcstring object from a CLR System::String str.
    // -------------------------------------------------------------------------
    Spinnaker::GenICam::gcstring ToNative(::System::String ^str);

    // -------------------------------------------------------------------------
    // Creates a CLR System::String object from a const char str.
    // -------------------------------------------------------------------------
    inline ::System::String^ ToManaged(const char *str) { return gcnew ::System::String(str); }

    // -------------------------------------------------------------------------
    // Creates a CLR System::String object from a const char str.
    // -------------------------------------------------------------------------
    inline ::System::String^ ToManaged(const char *str, size_t len) { return gcnew ::System::String(str, 0, static_cast<int>(len)); }

    // -------------------------------------------------------------------------
    // Creates a CLR System::String object from a gcstring.
    // -------------------------------------------------------------------------
    inline ::System::String^ ToManaged(const Spinnaker::GenICam::gcstring &str) { return gcnew ::System::String(str.c_str(), 0, static_cast<int>(str.length())); }
#pragma endregion

#pragma region Exception Handling

    // -------------------------------------------------------------------------
    // Start of a native->managed exception conversion.
    // -------------------------------------------------------------------------
#define NATIVE_EXCEPTION_START try {

    // -------------------------------------------------------------------------
    // Type conversion for native->managed of exceptions and its data
    // -------------------------------------------------------------------------
#define NATIVE_TO_MANAGED_EXCEPTION_TYPE(ERRCODE) throw gcnew SpinnakerNET::SpinnakerException(Spinnaker::Exception((int)ex.GetSourceLine(), ex.GetSourceFileName(), "", "", "", ex.what(), ERRCODE))

    // -------------------------------------------------------------------------
    // End of a native->managed exception conversion.
    // -------------------------------------------------------------------------
#define NATIVE_EXCEPTION_END } \
    catch(Spinnaker::Exception &ex) { throw gcnew SpinnakerNET::SpinnakerException(ex); } \
    catch(std::exception &ex) { throw gcnew SpinnakerNET::SpinnakerException(SpinnakerNET::ToManaged(ex.what())); } \
    catch(...) { throw gcnew SpinnakerNET::SpinnakerException("Unknown error in native code"); }

    // -------------------------------------------------------------------------
    // Type conversion for managed->native of exceptions and its data
    // -------------------------------------------------------------------------
#define MANAGED_TO_NATIVE_EXCEPTION_TYPE(EXC) throw EXC(SpinnakerNET::ToNative(ex->Message).c_str(), \
        ex->Data->Contains("Native.Filename") ? SpinnakerNET::ToNative(safe_cast<System::String^>(ex->Data["Native.Filename"])) : "", \
        ex->Data->Contains("Native.FileLine") ? safe_cast<int>(ex->Data["Native.FileLine"]) : 0)

    // -------------------------------------------------------------------------
    // Start of a managed->native exception conversion.
    // -------------------------------------------------------------------------
#define MANAGED_EXCEPTION_START try {

    // -------------------------------------------------------------------------
    // End of a native->managed exception conversion.
    // -------------------------------------------------------------------------
#define MANAGED_EXCEPTION_END } \
    catch(SpinnakerNET::SpinnakerException ^ex) { throw Spinnaker::Exception(__LINE__, __FILE__, "", "", "", SpinnakerNET::ToNative(ex->Message).c_str(), (Spinnaker::Error)ex->ErrorCode);} \
    catch(...) { throw Spinnaker::Exception(__LINE__, __FILE__, "", "", "", "Unknown error in managed code", Spinnaker::SPINNAKER_ERR_ERROR);}

#pragma endregion
}