Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
Factory.cpp
Go to the documentation of this file.
1 /**
2  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
3  * storing and accessing finite element mesh data.
4  *
5  * Copyright 2004 Sandia Corporation. Under the terms of Contract
6  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
7  * retains certain rights in this software.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  */
15 
16 #ifdef WIN32
17 #ifdef _DEBUG
18 // turn off warnings that say they debugging identifier has been truncated
19 // this warning comes up when using some STL containers
20 #pragma warning( disable : 4786 )
21 #endif
22 #endif
23 
24 #include "moab/Core.hpp"
25 
26 #ifdef XPCOM_MB
27 
28 #include "nsIGenericFactory.h"
29 
30 // define constructor function for Core
31 NS_GENERIC_FACTORY_CONSTRUCTOR( moab::Core )
32 
33 // support for nsIClassInfo
34 NS_DECL_CLASSINFO( moab::Core )
35 
36 MB_EXPORT const char* MoabVersion();
37 MB_EXPORT void GetInterface( MBuuid& interface_requested, UnknownInterface** iface );
38 MB_EXPORT void DeInitialize();
39 MB_EXPORT void ReleaseInterface( UnknownInterface* iface );
40 
41 static const nsModuleComponentInfo components[] = {
42  { "MOAB Interface", CORE_CID, CORE_CONTRACTID, CoreConstructor, NULL /* NULL if you dont need one */,
43  NULL /* NULL if you dont need one */, NULL /* no factory destructor */, NS_CI_INTERFACE_GETTER_NAME( moab::Core ),
44  NULL /* no language helper */, &NS_CLASSINFO_NAME( moab::Core ), 0 } };
45 
46 // implement NSGetModule()
47 NS_IMPL_NSGETMODULE( moab::Core, components );
48 
49 #endif
50 
51 #ifndef WIN32
52 #define MB_EXPORT extern "C"
53 #else
54 #define MB_EXPORT extern "C" __declspec( dllexport )
55 #endif
56 
57 #include <list>
58 
59 namespace moab
60 {
61 
63 {
64  public:
66  virtual ~ComponentFactory() {}
67  // returns the interface requested from an object
68  virtual int QueryInterface( const MBuuid&, UnknownInterface** );
69  // keep track of the objects this component factory creates
70  static std::list< UnknownInterface* > objects_in_use;
71 };
72 
73 // the list that keeps track of all the interfaces generated by this server
74 std::list< UnknownInterface* > ComponentFactory::objects_in_use;
75 
76 // this QueryInterface function is supposed to create an instance of the object
77 // that contains the interface requested
78 //
79 // note: the object is not the same as the interface, therefore
80 // we ask the object for the interface that was requested
81 //
82 
84 {
85  // this is an unknown interface that was requested
86  // if wanted, we could provide a default interface
87  // if IDD_MBUnknown is specified
88  if( uuid == IDD_MBUnknown ) return 0;
89  // IDD_MBVerde interface was requested
90  // create an Verde object and have it return the interface
91  // requested
92  else if( uuid == IDD_MBCore )
93  {
94  Core* mdb = new Core;
95  // if the object does not contain the interface requested, delete the object
96  if( !mdb->QueryInterface( uuid, iface ) )
97  {
98  delete mdb;
99  return 0;
100  }
101  return 1;
102  }
103  else
104  return 0;
105 }
106 
107 // returns the interface version
108 MB_EXPORT const char* MoabVersion()
109 {
110  return MB_INTERFACE_VERSION;
111 }
112 
113 // Initialize function is accessed by the MBClient when asking for interfaces
114 MB_EXPORT void GetInterface( MBuuid& interface_requested, UnknownInterface** iface )
115 {
116  // create an instance of our component factory
117  ComponentFactory server;
118  // ask the component factory to give us the interface we want
119  server.QueryInterface( interface_requested, iface );
120  // if the interface existed, put it on our list
121  if( iface && *iface ) ComponentFactory::objects_in_use.push_front( *iface );
122 }
123 
124 // DeInitialize function is accessed by the MBClient when disconnecting from this library
125 // this will clean everything up prior to a disconnection
126 // from this library
128 {
129  // delete all instances of objects
131  {
134  if( iface ) delete iface;
135  }
136 }
137 
138 // ReleaseInterface function is accessed by the MBClient when deleting an interface
139 
140 // ReleaseInterface will delete this interface
142 {
143  if( !iface ) return;
144  // remove this object from our list and delete it
146  delete iface;
147 }
148 
149 } // namespace moab