PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // ParentableObject.h 00004 // 00005 // Copyright (C) 2005-2006 Nicolas Chapados 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Nicolas Chapados 00040 00044 #ifndef ParentableObject_INC 00045 #define ParentableObject_INC 00046 00047 #include <plearn/base/Object.h> 00048 00049 namespace PLearn { 00050 00051 //##### ParentableObject #################################################### 00052 00093 class ParentableObject : public Object 00094 { 00095 typedef Object inherited; 00096 00097 public: 00099 enum ParentKind { 00100 AnyParent, 00101 WeakParent, 00102 UniqueParent 00103 }; 00104 00105 public: 00106 //##### Public Member Functions ######################################### 00107 00109 ParentableObject(bool adoptive_parent = false, ParentKind = WeakParent); 00110 00112 Object* parent() { return m_parent; } 00113 const Object* parent() const { return m_parent; } 00114 00118 virtual void updateChildrensParent(Object* parent); 00119 00122 virtual void setParent(Object* parent); 00123 00127 virtual void checkParent() const; 00128 00129 00130 //##### PLearn::Object Protocol ######################################### 00131 00132 // Declares other standard object methods. 00133 PLEARN_DECLARE_ABSTRACT_OBJECT(ParentableObject); 00134 00135 // Simply calls inherited::build() then build_() 00136 virtual void build(); 00137 00139 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00140 00142 static void declareMethods(RemoteMethodMap& rmm); 00143 00144 protected: 00146 Object* m_parent; 00147 00149 bool m_adoptive_parent; 00150 00152 ParentKind m_parent_kind; 00153 00154 private: 00156 void build_(); 00157 }; 00158 00159 // Declares a few other classes and functions related to this class 00160 DECLARE_OBJECT_PTR(ParentableObject); 00161 00162 00163 //##### TypedParentableObject ############################################### 00164 00170 #define TEMPLATE_DEF_TypedParentableObject class ParentT 00171 #define TEMPLATE_ARGS_TypedParentableObject ParentT 00172 #define TEMPLATE_NAME_TypedParentableObject \ 00173 string("TypedParentableObject< ") + TypeTraits<ParentT>::name() + " >" 00174 00175 template <class ParentT> 00176 class TypedParentableObject : public ParentableObject 00177 { 00178 typedef ParentableObject inherited; 00179 00180 public: 00182 TypedParentableObject(bool adoptive_parent = false, ParentKind = WeakParent); 00183 00185 ParentT* parent() 00186 { 00187 return static_cast<ParentT*>(m_parent); 00188 } 00189 00190 const ParentT* parent() const 00191 { 00192 return static_cast<const ParentT*>(m_parent); 00193 } 00194 00196 virtual void checkParent() const; 00197 00198 00199 //##### PLearn::Object Protocol ######################################### 00200 00201 // Declares other standard object methods. 00202 PLEARN_DECLARE_TEMPLATE_OBJECT(TypedParentableObject) 00203 00204 // Simply calls inherited::build() then build_() 00205 virtual void build(); 00206 00207 private: 00209 void build_(); 00210 }; 00211 00212 // Declares a few other classes and functions related to this class 00213 DECLARE_TEMPLATE_OBJECT_PTR(TypedParentableObject); 00214 00215 PLEARN_IMPLEMENT_TEMPLATE_OBJECT( 00216 TypedParentableObject, 00217 "Injects type information about the parent", 00218 "This is a simple subclass of ParentableObject that injects type information\n" 00219 "about the parent. Its build function also dynamically ensures that the\n" 00220 "parent is of the right type." 00221 ) 00222 00223 template <class T> 00224 TypedParentableObject<T>::TypedParentableObject(bool adoptive_parent, ParentKind pk) 00225 : inherited(adoptive_parent, pk) 00226 { } 00227 00228 template <class T> 00229 void TypedParentableObject<T>::build() 00230 { 00231 inherited::build(); 00232 build_(); 00233 } 00234 00235 template <class T> 00236 void TypedParentableObject<T>::build_() 00237 { 00238 // We simply dynamically check that the parent, if any, makes sense 00239 if (m_parent) 00240 checkParent(); 00241 } 00242 00243 template <class T> 00244 void TypedParentableObject<T>::checkParent() const 00245 { 00246 // We simply dynamically check that the parent, makes sense 00247 PLASSERT( m_parent ); 00248 if (! dynamic_cast<T*>(m_parent)) 00249 PLERROR("TypedParentableObject::checkParent: Expected a parent of type %s\n" 00250 "but got one of type %s", T::_classname_().c_str(), 00251 m_parent->classname().c_str()); 00252 } 00253 00254 00255 //##### TransparentParentable ############################################### 00256 00275 class TransparentParentable : public ParentableObject 00276 { 00277 typedef ParentableObject inherited; 00278 00279 public: 00280 //##### Public Member Functions ######################################### 00281 00283 TransparentParentable(bool adoptive_parent = false, ParentKind = WeakParent); 00284 00287 virtual void updateChildrensParent(Object* the_parent); 00288 00291 virtual void setParent(Object* parent); 00292 00294 virtual void checkParent() const; 00295 00296 00297 //##### PLearn::Object Protocol ######################################### 00298 00299 // Declares other standard object methods. 00300 PLEARN_DECLARE_ABSTRACT_OBJECT(TransparentParentable); 00301 00302 // Simply calls inherited::build() then build_() 00303 virtual void build(); 00304 00306 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00307 00308 private: 00311 void build_(); 00312 }; 00313 00314 00315 // Declares a few other classes and functions related to this class 00316 DECLARE_OBJECT_PTR(TransparentParentable); 00317 00318 } // end of namespace PLearn 00319 00320 #endif 00321 00322 00323 /* 00324 Local Variables: 00325 mode:c++ 00326 c-basic-offset:4 00327 c-file-style:"stroustrup" 00328 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00329 indent-tabs-mode:nil 00330 fill-column:79 00331 End: 00332 */ 00333 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :