MOAB: Mesh Oriented datABase  (version 5.2.1)
Factory.cpp
Go to the documentation of this file.
00001 /**
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 #ifdef WIN32
00017 #ifdef _DEBUG
00018 // turn off warnings that say they debugging identifier has been truncated
00019 // this warning comes up when using some STL containers
00020 #pragma warning( disable : 4786 )
00021 #endif
00022 #endif
00023 
00024 #include "moab/Core.hpp"
00025 
00026 #ifdef XPCOM_MB
00027 
00028 #include "nsIGenericFactory.h"
00029 
00030 // define constructor function for Core
00031 NS_GENERIC_FACTORY_CONSTRUCTOR( moab::Core )
00032 
00033 // support for nsIClassInfo
00034 NS_DECL_CLASSINFO( moab::Core )
00035 
00036 MB_EXPORT const char* MoabVersion();
00037 MB_EXPORT void GetInterface( MBuuid& interface_requested, UnknownInterface** iface );
00038 MB_EXPORT void DeInitialize();
00039 MB_EXPORT void ReleaseInterface( UnknownInterface* iface );
00040 
00041 static const nsModuleComponentInfo components[] = {
00042     { "MOAB Interface", CORE_CID, CORE_CONTRACTID, CoreConstructor, NULL /* NULL if you dont need one */,
00043       NULL /* NULL if you dont need one */, NULL /* no factory destructor */, NS_CI_INTERFACE_GETTER_NAME( moab::Core ),
00044       NULL /* no language helper */, &NS_CLASSINFO_NAME( moab::Core ), 0 }
00045 };
00046 
00047 // implement NSGetModule()
00048 NS_IMPL_NSGETMODULE( moab::Core, components );
00049 
00050 #endif
00051 
00052 #ifndef WIN32
00053 #define MB_EXPORT extern "C"
00054 #else
00055 #define MB_EXPORT extern "C" __declspec( dllexport )
00056 #endif
00057 
00058 #include <list>
00059 
00060 namespace moab
00061 {
00062 
00063 class ComponentFactory : public UnknownInterface
00064 {
00065   public:
00066     ComponentFactory() {}
00067     virtual ~ComponentFactory() {}
00068     // returns the interface requested from an object
00069     virtual int QueryInterface( const MBuuid&, UnknownInterface** );
00070     // keep track of the objects this component factory creates
00071     static std::list< UnknownInterface* > objects_in_use;
00072 };
00073 
00074 // the list that keeps track of all the interfaces generated by this server
00075 std::list< UnknownInterface* > ComponentFactory::objects_in_use;
00076 
00077 // this QueryInterface function is supposed to create an instance of the object
00078 // that contains the interface requested
00079 //
00080 // note: the object is not the same as the interface, therefore
00081 // we ask the object for the interface that was requested
00082 //
00083 
00084 int ComponentFactory::QueryInterface( const MBuuid& uuid, UnknownInterface** iface )
00085 {
00086     // this is an unknown interface that was requested
00087     // if wanted, we could provide a default interface
00088     // if IDD_MBUnknown is specified
00089     if( uuid == IDD_MBUnknown ) return 0;
00090     // IDD_MBVerde interface was requested
00091     // create an Verde object and have it return the interface
00092     // requested
00093     else if( uuid == IDD_MBCore )
00094     {
00095         Core* mdb = new Core;
00096         // if the object does not contain the interface requested, delete the object
00097         if( !mdb->QueryInterface( uuid, iface ) )
00098         {
00099             delete mdb;
00100             return 0;
00101         }
00102         return 1;
00103     }
00104     else
00105         return 0;
00106 }
00107 
00108 // returns the interface version
00109 MB_EXPORT const char* MoabVersion()
00110 {
00111     return MB_INTERFACE_VERSION;
00112 }
00113 
00114 // Initialize function is accessed by the MBClient when asking for interfaces
00115 MB_EXPORT void GetInterface( MBuuid& interface_requested, UnknownInterface** iface )
00116 {
00117     // create an instance of our component factory
00118     ComponentFactory server;
00119     // ask the component factory to give us the interface we want
00120     server.QueryInterface( interface_requested, iface );
00121     // if the interface existed, put it on our list
00122     if( iface && *iface ) ComponentFactory::objects_in_use.push_front( *iface );
00123 }
00124 
00125 // DeInitialize function is accessed by the MBClient when disconnecting from this library
00126 // this will clean everything up prior to a disconnection
00127 // from this library
00128 MB_EXPORT void DeInitialize()
00129 {
00130     // delete all instances of objects
00131     while( ComponentFactory::objects_in_use.size() )
00132     {
00133         UnknownInterface* iface = ComponentFactory::objects_in_use.front();
00134         ComponentFactory::objects_in_use.pop_front();
00135         if( iface ) delete iface;
00136     }
00137 }
00138 
00139 // ReleaseInterface function is accessed by the MBClient when deleting an interface
00140 
00141 // ReleaseInterface will delete this interface
00142 MB_EXPORT void ReleaseInterface( UnknownInterface* iface )
00143 {
00144     if( !iface ) return;
00145     // remove this object from our list and delete it
00146     ComponentFactory::objects_in_use.remove( iface );
00147     delete iface;
00148 }
00149 
00150 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines