TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/ofx/OfxhBinary.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License :
00003  *
00004  * Copyright (c) 2007-2009, The Open Effects Association Ltd. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * Redistributions of source code must retain the above copyright notice,
00010  * this list of conditions and the following disclaimer.
00011  * Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  * Neither the name The Open Effects Association Ltd, nor the names of its
00015  * contributors may be used to endorse or promote products derived from this
00016  * software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00022  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00025  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 #ifndef OFXH_BINARY_H
00030 #define OFXH_BINARY_H
00031 
00032 #include "OfxhCore.hpp"
00033 
00034 #include <boost/serialization/export.hpp>
00035 #include <boost/serialization/string.hpp>
00036 #include <boost/serialization/vector.hpp>
00037 
00038 #include <string>
00039 #include <iostream>
00040 
00041 #ifndef WINDOWS
00042  #if ( defined( WIN32 ) || defined( WIN64 ) || defined( _WIN32 ) || defined( _WIN64 ) || defined( __WINDOWS__ ) || defined( __TOS_WIN__ ) || defined( __WIN32__ ) )
00043   #define WINDOWS
00044  #endif
00045 #endif
00046 
00047 #if defined( _WIN32 ) || defined( _WIN64 )
00048  #define I386
00049 #elif defined( __linux__ )
00050  #ifndef UNIX
00051   #define UNIX
00052  #endif
00053  #ifdef __i386__
00054   #define I386
00055  #elif defined( __amd64__ )
00056   #define AMD64
00057  #else
00058   #error cannot detect architecture
00059  #endif
00060 #elif defined( __APPLE__ )
00061  #ifndef UNIX
00062   #define UNIX
00063  #endif
00064 #else
00065  #error cannot detect operating system
00066 #endif
00067 
00068 #if defined( UNIX )
00069  #include <dlfcn.h>
00070 #elif defined ( WINDOWS )
00071  #include "windows.h"
00072  #include <cassert>
00073 #endif
00074 
00075 #include <sys/stat.h>
00076 
00077 namespace tuttle {
00078 namespace host {
00079 namespace ofx {
00080 
00081 /// class representing a DLL/Shared Object/etc
00082 class OfxhBinary
00083 {
00084 public:
00085         typedef OfxhBinary This;
00086         /// destruction will close the library and invalidate
00087         /// any function pointers returned by lookupSymbol()
00088 
00089 protected:
00090         std::string _binaryPath;
00091         bool _invalid;
00092         #if defined( UNIX )
00093         void* _dlHandle;
00094         #elif defined ( WINDOWS )
00095         HINSTANCE _dlHandle;
00096         #endif
00097         bool _exists;
00098         time_t _time;
00099         size_t _size;
00100         int _users;
00101 
00102 public:
00103         OfxhBinary();
00104 
00105         void init( const std::string& binaryPath );
00106 
00107         /// create object representing the binary.  will stat() it,
00108         /// and this fails, will set binary to be invalid.
00109         OfxhBinary( const std::string& binaryPath );
00110 
00111         ~OfxhBinary() { unload(); }
00112 
00113         bool operator==( const This& other ) const
00114         {
00115                 if( _binaryPath != other._binaryPath ||
00116                     _invalid != other._invalid ||
00117                     _exists != other._exists ||
00118                     _time != other._time ||
00119                     _size != other._size )
00120                         return false;
00121                 return true;
00122         }
00123 
00124         bool operator!=( const This& other ) const { return !This::operator==( other ); }
00125 
00126         bool isLoaded() const { return _dlHandle != 0; }
00127 
00128         /// is this binary invalid? (did the a stat() or load() on the file fail,
00129         /// or are we missing a some of the symbols?
00130         bool isInvalid() const { return _invalid; }
00131 
00132         /// set invalid status (e.g. called by user if a mandatory symbol was missing)
00133         void setInvalid( bool invalid ) { _invalid = invalid; }
00134 
00135         /// Last modification time of the file.
00136         time_t getTime() const { return _time; }
00137 
00138         /// Current size of the file.
00139         size_t getSize() const { return _size; }
00140 
00141         /// Path to the file.
00142         const std::string& getBinaryPath() const { return _binaryPath; }
00143 
00144         void ref();
00145         void unref();
00146 
00147         /// open the binary.
00148         void load();
00149 
00150         /// close the binary
00151         void unload();
00152 
00153         /// look up a symbol in the binary file and return it as a pointer.
00154         /// returns null pointer if not found, or if the library is not loaded.
00155         void* findSymbol( const std::string& symbol );
00156 
00157 private:
00158         friend class boost::serialization::access;
00159         template<class Archive>
00160         void serialize( Archive& ar, const unsigned int version )
00161         {
00162                 ar& BOOST_SERIALIZATION_NVP( _binaryPath );
00163                 ar& BOOST_SERIALIZATION_NVP( _invalid );
00164                 ar& BOOST_SERIALIZATION_NVP( _exists );
00165                 ar& BOOST_SERIALIZATION_NVP( _size );
00166                 ar& BOOST_SERIALIZATION_NVP( _time );
00167 
00168                 //              ar & BOOST_SERIALIZATION_NVP(_users);
00169 
00170                 if( typename Archive::is_loading() )
00171                 {
00172                         _users = 0;
00173                         init( _binaryPath );
00174                 }
00175         }
00176 
00177 };
00178 
00179 }
00180 }
00181 }
00182 
00183 #endif