00001 /*****************************************************************************/ 00002 /*! 00003 * \file cdo.h 00004 * 00005 * Author: Clark Barrett 00006 * 00007 * Created: Wed Feb 12 17:27:43 2003 00008 * 00009 * <hr> 00010 * Copyright (C) 2003 by the Board of Trustees of Leland Stanford 00011 * Junior University and by New York University. 00012 * 00013 * License to use, copy, modify, sell and/or distribute this software 00014 * and its documentation for any purpose is hereby granted without 00015 * royalty, subject to the terms and conditions defined in the \ref 00016 * LICENSE file provided with this distribution. In particular: 00017 * 00018 * - The above copyright notice and this permission notice must appear 00019 * in all copies of the software and related documentation. 00020 * 00021 * - THE SOFTWARE IS PROVIDED "AS-IS", WITHOUT ANY WARRANTIES, 00022 * EXPRESSED OR IMPLIED. USE IT AT YOUR OWN RISK. 00023 * 00024 * <hr> 00025 * 00026 */ 00027 /*****************************************************************************/ 00028 00029 #ifndef _cvcl__include__cdo_h_ 00030 #define _cvcl__include__cdo_h_ 00031 00032 #include "context.h" 00033 00034 namespace CVCL { 00035 00036 /////////////////////////////////////////////////////////////////////////////// 00037 // // 00038 // Class: CDO (Context Dependent Object) // 00039 // Author: Clark Barrett // 00040 // Created: Wed Feb 12 17:28:25 2003 // 00041 // Description: Generic templated class for an object which must be saved // 00042 // and restored as contexts are pushed and popped. Requires // 00043 // that operator= be defined for the data class. // 00044 // // 00045 /////////////////////////////////////////////////////////////////////////////// 00046 template <class T> 00047 class CDO :public ContextObj { 00048 T d_data; 00049 00050 virtual ContextObj* makeCopy(void) { return new CDO<T>(*this); } 00051 virtual void restoreData(ContextObj* data) { 00052 d_data = ((CDO<T>*)data)->d_data; 00053 } 00054 virtual void setNull(void) { d_data = T(); } 00055 00056 // Disable copy constructor and operator= 00057 // If you need these, use smartcdo instead 00058 CDO(const CDO<T>& cdo): ContextObj(cdo), d_data(cdo.d_data) { } 00059 CDO<T>& operator=(const CDO<T>& cdo) {} 00060 00061 public: 00062 CDO(Context* context) : ContextObj(context) 00063 { IF_DEBUG(setName("CDO")); } 00064 CDO(Context* context, const T& data, int scope = -1) 00065 : ContextObj(context) { 00066 IF_DEBUG(setName("CDO")); 00067 set(data, scope); 00068 } 00069 ~CDO() { 00070 TRACE("context verbose", "~CDO[", this, "]"); 00071 } 00072 void set(const T& data, int scope=-1) { makeCurrent(scope); d_data = data; } 00073 const T& get() const { return d_data; } 00074 operator T() { return get(); } 00075 CDO<T>& operator=(const T& data) { set(data); return *this; } 00076 00077 }; 00078 00079 } 00080 00081 #endif