Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Option.h

Go to the documentation of this file.
00001 // -*- C++ -*-4 1999/10/29 20:41:34 dugas 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // Copyright (C) 2002 Frederic Morin 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 /* ******************************************************* 00039 * $Id: Option.h,v 1.5 2004/06/26 00:24:12 plearner Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 00046 #ifndef Option_INC 00047 #define Option_INC 00048 00049 #include "OptionBase.h" 00050 00051 namespace PLearn { 00052 using namespace std; 00053 00055 template<class ObjectType, class OptionType, class ConstOptionType> 00056 class Option: public OptionBase 00057 { 00058 protected: 00059 OptionType ObjectType::*ptr; 00060 00061 public: 00062 00065 Option(const string& optionname, OptionType ObjectType::* member_ptr, 00066 flag_t flags, const string& optiontype, const string& defaultval, 00067 const string& description) 00068 :OptionBase(optionname, flags, optiontype, defaultval, description), 00069 ptr(member_ptr) {} 00070 virtual void read(Object* o, PStream& in) const 00071 { in >> dynamic_cast<ObjectType*>(o)->*ptr; } 00072 virtual void read_and_discard(PStream& in) const 00073 { 00074 string dummy; 00075 in.smartReadUntilNext(";)", dummy); 00076 in.unget(); 00077 //OptionType op; //dummy object that will be destroyed after read 00078 //in >> op; //read dummy object 00079 } 00080 virtual void write(const Object* o, PStream& out) const 00081 { out << static_cast<ConstOptionType>(dynamic_cast<const ObjectType *>(o)->*ptr); } 00082 00083 virtual Object* getAsObject(Object* o) const 00084 { return toObjectPtr(dynamic_cast<ObjectType*>(o)->*ptr); } 00085 00086 virtual const Object* getAsObject(const Object* o) const 00087 { return toObjectPtr(dynamic_cast<const ObjectType*>(o)->*ptr); } 00088 00089 virtual Object *getIndexedObject(Object *o, int i) const 00090 { return toIndexedObjectPtr(dynamic_cast<ObjectType*>(o)->*ptr, i); }; 00091 00092 virtual const Object* getIndexedObject(const Object *o, int i) const 00093 { return toIndexedObjectPtr(dynamic_cast<const ObjectType*>(o)->*ptr, i); }; 00094 00095 virtual string optionHolderClassName(const Object* o) const 00096 { return dynamic_cast<const ObjectType*>(o)->ObjectType::classname(); } 00097 }; 00098 00099 00104 00110 template<class ObjectType, class OptionType> 00111 inline void declareOption(OptionList& ol, 00112 const string& optionname, 00113 OptionType ObjectType::* member_ptr, 00114 OptionBase::flag_t flags, 00115 const string& description, 00116 const string & defaultval="") 00117 { ol.push_back(new Option<ObjectType, OptionType, const OptionType>(optionname, member_ptr, flags, 00118 TypeTraits<OptionType>::name(), 00119 defaultval, description)); } 00120 template<class ObjectType, class OptionType> 00121 inline void declareOption(OptionList& ol, const string& optionname, OptionType *ObjectType::* member_ptr, OptionBase::flag_t flags, 00122 const string& description, const string & defaultval="") 00123 { ol.push_back(new Option<ObjectType, OptionType *, const OptionType *>(optionname, member_ptr, flags, TypeTraits<OptionType *>::name(), 00124 defaultval, description)); } 00125 00128 template<class ObjectType, class OptionType> 00129 inline void redeclareOption(OptionList& ol, 00130 const string& optionname, 00131 OptionType ObjectType::* member_ptr, 00132 OptionBase::flag_t flags, 00133 const string& description, 00134 const string & defaultval="") 00135 { 00136 bool found = false; 00137 for (OptionList::iterator it = ol.begin(); !found && it != ol.end(); it++) { 00138 if ((*it)->optionname() == optionname) { 00139 // We found the option to redeclare. 00140 found = true; 00141 (*it) = new Option<ObjectType, OptionType, const OptionType> 00142 (optionname, member_ptr, flags, TypeTraits<OptionType>::name(), defaultval, description); 00143 } 00144 } 00145 if (!found) { 00146 // We tried to redeclare an option that wasn't declared previously. 00147 PLERROR("In Option::redeclareOption - The option you are trying to redeclare has not been declared yet."); 00148 } 00149 } 00150 00151 00152 } // end of namespace PLearn 00153 00154 #endif

Generated on Tue Aug 17 16:00:36 2004 for PLearn by doxygen 1.3.7