PLearn 0.1
ProgressBar.h
Go to the documentation of this file.
00001 // -*- C++ -*-
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) 2007 Xavier Saint-Mleux, ApSTAT Technologies inc.
00007 //
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 /* *******************************************************      
00041  * $Id: ProgressBar.h 9789 2008-12-16 21:35:14Z nouiz $
00042  * AUTHORS: Pascal Vincent
00043  * This file is part of the PLearn library.
00044  ******************************************************* */
00045 
00046 // This file contains useful functions for string manipulation
00047 // that are used in the PLearn Library
00048 
00049 
00052 #ifndef ProgressBar_INC
00053 #define ProgressBar_INC
00054 
00055 #include <string>
00056 //#include <iostream>
00057 #include "PP.h"
00058 #include <plearn/io/PStream.h>
00059 
00060 
00061 namespace PLearn {
00062 using namespace std;
00063 
00064 class ProgressBar;
00065 
00067 class ProgressBarPlugin : public PPointable
00068 {
00069 public:
00070     ProgressBarPlugin() {}
00071     virtual ~ProgressBarPlugin() {}
00072     virtual void addProgressBar(ProgressBar * pb){};
00073     virtual void killProgressBar(ProgressBar * pb){};
00074     virtual void update(ProgressBar * pb, uint32_t newpos){};
00075 };
00076 
00077 
00079 class TextProgressBarPlugin : public ProgressBarPlugin
00080 {
00081 protected:
00082     PStream out;
00083 public:
00084     virtual void addProgressBar(ProgressBar * pb);
00085     virtual void update(ProgressBar * pb, uint32_t newpos);
00086 
00087     TextProgressBarPlugin(ostream& _out);
00088     TextProgressBarPlugin(PStream& _out);
00089 
00091     static int width;
00092 };
00093 
00096 class RemoteProgressBarPlugin : public TextProgressBarPlugin
00097 {
00098 public:
00099     virtual void addProgressBar(ProgressBar* pb);
00100     virtual void update(ProgressBar* pb, uint32_t newpos);
00101 
00102     RemoteProgressBarPlugin(ostream& _out, unsigned int nticks_= 20);
00103     RemoteProgressBarPlugin(PStream& _out, unsigned int nticks_= 20);
00104 
00105     virtual void killProgressBar(ProgressBar* pb);
00106 
00107 protected:
00108     static map<ProgressBar*, unsigned int> pb_ids;
00109     static unsigned int next_pb_id;
00110     static unsigned int getPBarID(ProgressBar* pb);
00111     void printTitle(ProgressBar* pb);
00112     unsigned int nticks;
00113 };
00114 
00118 class LineOutputProgressBarPlugin : public TextProgressBarPlugin
00119 {
00120 public:
00121     virtual void addProgressBar(ProgressBar* pb);
00122     virtual void update(ProgressBar* pb, uint32_t newpos);
00123 
00124     LineOutputProgressBarPlugin(ostream& _out, unsigned int nticks_= 100);
00125     LineOutputProgressBarPlugin(PStream& _out, unsigned int nticks_= 100);
00126 
00127     virtual void killProgressBar(ProgressBar* pb);
00128 
00129 protected:
00130     static string pbInfo(ProgressBar* pb);
00131     unsigned int nticks;
00132 };
00133 
00141 struct NullProgressBarPlugin : public ProgressBarPlugin
00142 { /* all inherited methods are fine... :-) */ };
00143 
00144 
00153 class ProgressBar : public PPointable
00154 {
00155 public:
00156     string title;
00157     uint32_t currentpos; // current position
00158     uint32_t maxpos;
00159 
00160     // creates a new progressbar with the given title and maxpos
00161     // *** Note, for now, ignore the stream (someday, remove this argument for 
00162     // every progressBar creation in PLearn)
00163     ProgressBar(string _title, uint32_t the_maxpos);
00164     ProgressBar(ostream& _out,string _title, uint32_t the_maxpos);
00165     ProgressBar(PStream& _out,string _title, uint32_t the_maxpos);
00166 
00167     // moves the progressbar up to position newpos
00168     void operator()(uint32_t newpos){plugin->update(this,newpos);}
00169 
00170     void update(uint32_t newpos){plugin->update(this,newpos);}
00171     // this function assumes plugin is always a valid object (it is created statically in the .cc)
00172     static void setPlugin(PP<ProgressBarPlugin> plugin_) { plugin = plugin_; }
00173     static PP<ProgressBarPlugin> getCurrentPlugin();
00174 
00175     // Completes and removes the progressBar 
00176     void close();
00177 
00178     // calls close() if not already done
00179     ~ProgressBar();
00180 private:
00181     bool closed;
00182     static PP<ProgressBarPlugin> plugin;
00183 };
00184 
00185 
00186 void setProgressBarPlugin(string pb_type);
00187 
00188 
00189 } // end of namespace PLearn
00190 
00191 #endif
00192 
00193 
00194 /*
00195   Local Variables:
00196   mode:c++
00197   c-basic-offset:4
00198   c-file-style:"stroustrup"
00199   c-file-offsets:((innamespace . 0)(inline-open . 0))
00200   indent-tabs-mode:nil
00201   fill-column:79
00202   End:
00203 */
00204 // 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