00001 #ifndef SimTK_SimTKCOMMON_STUDY_GUTS_H_ 00002 #define SimTK_SimTKCOMMON_STUDY_GUTS_H_ 00003 00004 /* -------------------------------------------------------------------------- * 00005 * SimTK Core: SimTKcommon * 00006 * -------------------------------------------------------------------------- * 00007 * This is part of the SimTK Core biosimulation toolkit originating from * 00008 * Simbios, the NIH National Center for Physics-Based Simulation of * 00009 * Biological Structures at Stanford, funded under the NIH Roadmap for * 00010 * Medical Research, grant U54 GM072970. See https://simtk.org. * 00011 * * 00012 * Portions copyright (c) 2007 Stanford University and the Authors. * 00013 * Authors: Michael Sherman * 00014 * Contributors: * 00015 * * 00016 * Permission is hereby granted, free of charge, to any person obtaining a * 00017 * copy of this software and associated documentation files (the "Software"), * 00018 * to deal in the Software without restriction, including without limitation * 00019 * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 00020 * and/or sell copies of the Software, and to permit persons to whom the * 00021 * Software is furnished to do so, subject to the following conditions: * 00022 * * 00023 * The above copyright notice and this permission notice shall be included in * 00024 * all copies or substantial portions of the Software. * 00025 * * 00026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 00027 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 00028 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 00029 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 00030 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 00031 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 00032 * USE OR OTHER DEALINGS IN THE SOFTWARE. * 00033 * -------------------------------------------------------------------------- */ 00034 00035 #include "SimTKcommon/basics.h" 00036 #include "SimTKcommon/Simmatrix.h" 00037 #include "SimTKcommon/internal/State.h" 00038 00039 namespace SimTK { 00040 00041 // See below for definitions. 00042 static void systemDestructImplLocator(Study::Guts*); 00043 static Study::Guts* systemCloneImplLocator(const Study::Guts&); 00044 00045 // TODO: more to come. 00046 00084 class SimTK_SimTKCOMMON_EXPORT Study::Guts { 00085 class GutsRep; 00086 friend class GutsRep; 00087 00088 // This is the only data member in this class. 00089 GutsRep* rep; // opaque implementation of Study::Guts base class. 00090 public: 00091 // Constructor must be inline for binary compatibility. Note that this 00092 // serves as a default constructor since both arguments have defaults. 00093 inline explicit Guts(const String& name="<UNNAMED STUDY>", 00094 const String& version="0.0.0"); 00095 00096 // This won't be called directly from library-side code. Instead, 00097 // a method from the explicit virtual function table will be invoked 00098 // which will know where to find this on in the C++ VFT on the client side. 00099 virtual ~Guts() {librarySideDestruction();} 00100 00101 const String& getName() const; 00102 const String& getVersion() const; 00103 00104 // Obtain the owner handle for this Study::Guts object. 00105 const Study& getStudy() const; 00106 Study& updStudy(); 00107 00108 void setOwnerHandle(Study&); 00109 bool hasOwnerHandle() const; 00110 00111 explicit Guts(class GutsRep* r) : rep(r) { } 00112 bool hasRep() const {return rep!=0;} 00113 const GutsRep& getRep() const {assert(rep); return *rep;} 00114 GutsRep& updRep() const {assert(rep); return *rep;} 00115 00116 // Call this routine to invoke the client-side virtual destructor, 00117 // by going through the library-side explicit virtual function table. 00118 static void destruct(Study::Guts*); 00119 00120 // Wrap the cloneImpl virtual method. 00121 Study::Guts* clone() const; 00122 00123 protected: 00124 Guts(const Guts&); // copies the base class; for use from derived class copy constructors 00125 00126 // The destructor is already virtual; see above. 00127 00128 virtual Study::Guts* cloneImpl() const = 0; 00129 00130 private: 00131 Guts& operator=(const Guts&); // suppress default copy assignment operator 00132 00133 // These typedefs are used internally to manage the binary-compatible 00134 // handling of the virtual function table. 00135 00136 // This first entry calls the virtual destructor above to delete the 00137 // heap-allocated object pointed to by the passed-in pointer. 00138 typedef void (*DestructImplLocator)(Study::Guts*); 00139 typedef Study::Guts* (*CloneImplLocator)(const Study::Guts&); 00140 00141 void librarySideConstruction(const String& name, const String& version); 00142 void librarySideDestruction(); 00143 00144 void registerDestructImpl(DestructImplLocator); 00145 void registerCloneImpl(CloneImplLocator); 00146 00147 // We want the locator functions to have access to the protected "Impl" 00148 // virtual methods, so we make them friends. 00149 00150 friend void systemDestructImplLocator(Study::Guts*); 00151 friend Study::Guts* systemCloneImplLocator(const Study::Guts&); 00152 }; 00153 00154 00155 // These are used to supply the client-side virtual function to the library, without 00156 // the client and library having to agree on the layout of the virtual function tables. 00157 00158 static void systemDestructImplLocator(Study::Guts* sysp) 00159 { delete sysp; } // invokes virtual destructor 00160 static Study::Guts* systemCloneImplLocator(const Study::Guts& sys) 00161 { return sys.cloneImpl(); } 00162 00163 // Constructor must be inline so that it has access to the above static 00164 // functions which are private to the client-side compilation unit in which the 00165 // client-side virtual function table is understood. 00166 inline Study::Guts::Guts(const String& name, const String& version) : rep(0) 00167 { 00168 librarySideConstruction(name, version); 00169 00170 // Teach the library code how to call client side virtual functions by 00171 // calling through the client side compilation unit's private static 00172 // locator functions. 00173 registerDestructImpl(systemDestructImplLocator); 00174 registerCloneImpl(systemCloneImplLocator); 00175 } 00176 00177 } // namespace SimTK 00178 00179 #endif // SimTK_SimTKCOMMON_STUDY_GUTS_H_