PLearn 0.1
OptionBase.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 // Copyright (C) 2007 Xavier Saint-Mleux, ApSTAT Technologies inc.
00008 
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 // 
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 // 
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 // 
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 // 
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 // 
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036 
00037 
00038 
00039 /* *******************************************************      
00040  * $Id: OptionBase.h 8318 2007-11-28 21:15:36Z nouiz $
00041  * This file is part of the PLearn library.
00042  ******************************************************* */
00043 
00044 
00047 #ifndef OptionBase_INC
00048 #define OptionBase_INC
00049 
00050 #ifdef PL_PYTHON_VERSION 
00051 #include <plearn/python/PythonObjectWrapper.h>
00052 #endif //def PL_PYTHON_VERSION 
00053 
00054 #include <plearn/io/pl_io.h>
00055 #include "PP.h"
00056 #include <plearn/io/PStream.h>        
00057 #include <plearn/io/PStream_util.h>   
00058 #include <vector>
00059 
00060 namespace PLearn {
00061 using std::string;
00062 
00064 class Object;
00065 class PLearnDiff;
00066 
00068 class OptionBase: public PPointable
00069 {
00070 public:
00071     //#####  Option Flags  ####################################################
00072 
00073     // === IMPORTANT NOTICE ===
00074     // If you add new option flags to the following list, don't forget to add
00075     // a human-readable interpretation within the flagStrings() function.
00076     // ========================
00077 
00080     typedef unsigned int flag_t;
00081 
00087     static const flag_t buildoption;       
00088 
00095     static const flag_t learntoption;
00096 
00101     static const flag_t tuningoption;
00102 
00109     static const flag_t nosave;
00110 
00117     static const flag_t nonparentable;
00118 
00124     static const flag_t nontraversable;
00125 
00131     static const flag_t remotetransmit;
00132 
00138     typedef unsigned int OptionLevel;
00139     static const OptionLevel basic_level; 
00140     static const OptionLevel advanced_level; 
00141     static const OptionLevel expert_level; 
00142     static const OptionLevel experimental_level; 
00143     static const OptionLevel deprecated_level; 
00144     static const OptionLevel default_level; 
00145 
00146     typedef map<string, OptionLevel> StrToLevelMap;
00147     typedef map<OptionLevel, string> LevelToStrMap;
00148     typedef map<string, flag_t> StrToFlagMap;
00149     typedef map<flag_t, string> FlagToStrMap;
00150 protected:
00151     static OptionLevel current_option_level_;
00152     static StrToLevelMap str_to_level;
00153     static LevelToStrMap level_to_str;
00154 
00156     static flag_t current_flags_;
00157     static StrToFlagMap str_to_flag;
00158     static FlagToStrMap flag_to_str;
00159 
00160 protected:
00161     string optionname_;  
00162     flag_t flags_; 
00163     string optiontype_;  
00164     string defaultval_;  
00165     string description_; 
00166     OptionLevel level_;  
00167 
00168 public:
00169 
00172     OptionBase(const string& optionname, flag_t flags,
00173                const string& optiontype, const string& defaultval, 
00174                const string& description, const OptionLevel& level);
00175 
00176     //#####  Stream Read-Write  ###############################################
00177 
00179     virtual void read(Object* o, PStream& in) const = 0;
00180 
00182     virtual void write(const Object* o, PStream& out) const = 0;
00183 
00186     string writeIntoString(const Object* o) const;
00187 
00190     virtual void readIntoIndex(Object* o, PStream& in, const string& index);
00191 
00194     virtual void writeAtIndex(const Object* o, PStream& out,
00195                               const string& index) const;
00196 
00197 
00198     //#####  Access and Indexing  #############################################
00199 
00203     virtual Object* getAsObject(Object* o) const = 0;
00204 
00208     virtual const Object* getAsObject(const Object* o) const = 0;
00209 
00213     virtual Object *getIndexedObject(Object *o, int i) const = 0;
00214 
00218     virtual const Object *getIndexedObject(const Object *o, int i) const = 0;
00219 
00223     virtual void* getAsVoidPtr(Object* o) const = 0;
00224 
00228     virtual const void* getAsVoidPtr(const Object* o) const = 0;
00229 
00230 #ifdef PL_PYTHON_VERSION 
00231 
00232     virtual PythonObjectWrapper getAsPythonObject(Object* o) const = 0;
00233     virtual PythonObjectWrapper getAsPythonObject(const Object* o) const = 0;
00234     virtual void setFromPythonObject(Object* o, 
00235                                      const PythonObjectWrapper& v) const= 0;
00236 #endif //def PL_PYTHON_VERSION 
00237 
00238     //#####  Option Information  ##############################################
00239 
00242     virtual bool shouldBeSkipped() const; 
00243 
00245     virtual string optionHolderClassName(const Object* o) const = 0;
00246 
00248     inline const string& optionname() const { return optionname_; }
00249 
00252     inline bool isOptionNamed(string name) const { return name == optionname(); }
00253 
00255     inline void setOptionName(const string& name) { optionname_ = name; }
00256 
00258     inline const string& optiontype() const { return optiontype_; }
00259 
00261     inline const string& defaultval() const { return defaultval_; }
00262 
00264     inline const OptionLevel& level() const { return level_; }
00266     inline string levelString() const { return optionLevelToString(level()); }
00267 
00269     inline void setDefaultVal(const string& newdefaultval)
00270     { defaultval_ = newdefaultval; }
00271 
00273     inline const string& description() const { return description_; }
00274 
00276     inline flag_t flags() const { return flags_; }
00277 
00279     vector<string> flagStrings() const;
00280     
00284     virtual bool isAccessibleAsObject() const = 0;
00285 
00296     virtual int indexableSize(const Object* o) const = 0;
00297     
00298 
00299     //#####  Miscellaneous  ###################################################
00300 
00302     virtual int diff(const string& refer, const string& other, PLearnDiff* diffs) const = 0;
00303 
00305     static const OptionLevel& getCurrentOptionLevel() { return current_option_level_; }
00307     static void setCurrentOptionLevel(const OptionLevel& l) { current_option_level_= l; }
00309     static const StrToLevelMap& getStrToLevelMap();
00311     static const LevelToStrMap& getLevelToStrMap();
00313     static OptionLevel optionLevelFromString(const string& s);
00315     static string optionLevelToString(const OptionLevel& l);
00316 
00318     static const flag_t& getCurrentFlags() { return current_flags_; }
00320     static void setCurrentFlags(const flag_t& f) { current_flags_= f; }
00322     static const StrToFlagMap& getStrToFlagMap();
00324     static const FlagToStrMap& getFlagToStrMap();
00325 
00326 };
00327 
00328 typedef std::vector< PP<OptionBase> > OptionList;
00329 typedef std::map<std::string, PP<OptionBase> > OptionMap;
00330 
00331 
00332 
00333 } // end of namespace PLearn
00334 
00335 #endif //!<  Option_INC
00336 
00337 
00338 /*
00339   Local Variables:
00340   mode:c++
00341   c-basic-offset:4
00342   c-file-style:"stroustrup"
00343   c-file-offsets:((innamespace . 0)(inline-open . 0))
00344   indent-tabs-mode:nil
00345   fill-column:79
00346   End:
00347 */
00348 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines