eudora-mac/Include/MachOWrapper.h

1 line
4.7 KiB
C
Raw Permalink Normal View History

2018-05-23 09:59:15 +00:00
/* Copyright (c) 2017, Computer History Museum All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Computer History Museum nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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. */ /* A template class that holds a pointer to a Mach-O framework entry point. Author: Marshall Clow Change Log: 13-Aug-2002 mc Initial Version */ #ifndef __MACHOWRAPPER__ #define __MACHOWRAPPER__ #include <MacTypes.h> #include <CFBundle.h> #if !TARGET_RT_MAC_CFM #error "This code only works for CFM Targets!" #endif OSStatus LoadFrameworkBundle ( CFStringRef framework, CFBundleRef *bundlePtr ); template <typename Proc> class MachOWrapper { private: CFBundleRef fBundle; // The reference to the framework pointer Proc fProcPtr; // The actual procedure pointer public: MachOWrapper ( CFStringRef framework, CFStringRef routine ) : fBundle ( NULL ), fProcPtr ( NULL ) { // Get the framework bundle OSStatus err = ::LoadFrameworkBundle ( framework, &fBundle ); // Don't call "CFBundleGetFunctionPointerForName" with an invalid bundle - it will crash. if ( err != noErr ) fBundle = NULL; else { fProcPtr = (Proc) CFBundleGetFunctionPointerForName ( fBundle, routine ); if ( fProcPtr == nil ) err = cfragNoSymbolErr; } // Have to throw something if we don't link up! } MachOWrapper ( CFBundleRef aBundle, CFStringRef routine ) : fBundle ( aBundle ), fProcPtr ( NULL ) { OSStatus err = noErr; CFRetain ( fBundle ); fProcPtr = (Proc) CFBundleGetFunctionPointerForName ( fBundle, routine ); if ( fProcPtr == nil ) err = cfragNoSymbolErr; // Have to throw something if we don't link up! } ~MachOWrapper () { if ( fBundle != NULL ) CFRelease ( fBundle ); } Boolean IsValid () const { return fProcPtr != NULL; } // Marshall dreams: // A whole set of templates for different numbers of arguments // The beauty of this is that only one will be instantiated by the compiler // for each specialization of this template. #if 0 template <typename R> R operator () () const { return fProcPtr (); } template <typename R, typename A1> R operator () (A1 arg1) const { return fProcPtr (arg1); } template <typename R, typename A1, typename A2> R operator () (A1 arg1, A2 arg2) const { return fProcPtr (arg1, arg2); } template <typename R, typename A1, typename A2, typename A3> R operator () (A1 arg1, A2 arg2, A3 arg3) const { return fProcPtr (arg1, arg2, arg3); } template <typename R, typename A1, typename A2, typename A3, typename A4> R operator () (A1 arg1, A2 arg2, A3 arg3, A4 arg4) const { return fProcPtr (arg1, arg2, arg3, arg4); } template