Concretize.h

Go to the documentation of this file.
00001 #ifndef SimTK_SimTKCOMMON_CONCRETIZE_H_
00002 #define SimTK_SimTKCOMMON_CONCRETIZE_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) 2005-6 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 namespace SimTK {
00036 
00048 template <class T> class Concretize {
00049 public:
00050     Concretize() : p(0) { }
00051     explicit Concretize(T*  obj) : p(obj) { }           // steals object            
00052     explicit Concretize(T** obj) : p(*obj) { *obj=0; }  // steal & tidy up
00053     explicit Concretize(const T* obj) : p(obj?obj->clone():0) { }
00054     explicit Concretize(const T& obj) : p(&obj?obj.clone():0) { }
00055 
00056     Concretize(const Concretize& c) : p(c.p?c.p->clone():0) { }
00057     Concretize& operator=(const Concretize& c) { replace(c.p?c.p->clone():0); return *this; }
00058     Concretize& operator=(const T& t)          { replace(&t ? t.clone()  :0); return *this; }
00059     Concretize& operator=(T* tp)               { replace(tp); return *this; }
00060        
00061     ~Concretize() { delete p; }
00062 
00063     bool operator==(const Concretize& c) const {return getRef()==c.getRef();}
00064     bool operator!=(const Concretize& c) const {return !((*this)==c);}
00065     
00066     const T* operator->() const { return p; }
00067     T*       operator->()       { return p; }
00068     
00070     operator const T&() const { return *p; }
00071     operator T&()             { return *p; }    
00072        
00073     T&       updRef()         { return *p; }
00074     const T& getRef()  const  { return *p; }    
00075     bool     isEmpty() const  { return p==0; }
00076     void     clear()          { replace(0); }
00077     T*       extract()        { T* x=p; p=0; return x; }
00078     void     replace(T* tp)   { delete p; p=tp; }
00079     void     replace(T** tpp) { delete p; p=*tpp; *tpp=0; }
00080      
00081 private:
00082     // Warning: Concretize must be exactly the same size as type T*. That way
00083     // one can reinterpret_cast a T* to a Concretize<T> when needed.
00084     T*  p;  
00085 };  
00086     
00087 } // namespace SimTK
00088 #endif // SimTK_SimTKCOMMON_CONCRETIZE_H_

Generated on Wed Dec 30 11:04:32 2009 for SimTKcore by  doxygen 1.6.1