casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PlotOperation.h
Go to the documentation of this file.
00001 //# PlotOperation.h: Classes for managing large, threaded plot operations.
00002 //# Copyright (C) 2009
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: $
00027 #ifndef PLOTOPERATION_H_
00028 #define PLOTOPERATION_H_
00029 
00030 #include <casa/BasicSL/String.h>
00031 #include <casa/Utilities/CountedPtr.h>
00032 
00033 #include <vector>
00034 
00035 #include <casa/namespace.h>
00036 using namespace std;
00037 
00038 namespace casa {
00039 
00040 //# Forward Declarations
00041 class PlotOperationWatcher;
00042 
00043 
00044 // Abstract class, for a synchronization mutex specific to the threading
00045 // library used by the plotting implementations.
00046 class PlotMutex {
00047 public:
00048     // Constructor.
00049     PlotMutex() { }
00050     
00051     // Destructor.
00052     virtual ~PlotMutex() { }
00053     
00054     
00055     // ABSTRACT METHODS //
00056     
00057     // Locks the mutex.
00058     virtual void lock() = 0;
00059     
00060     // Unlocks the mutex.
00061     virtual void unlock() = 0;
00062     
00063     // Tries to lock the mutex, and returns immediately with either true for
00064     // success (mutex is now locked), or false (mutex is unavailable).
00065     virtual bool tryLock() = 0;
00066 };
00067 typedef CountedPtr<PlotMutex> PlotMutexPtr;
00068 
00069 
00070 // Simple object to synchronize operation progress information across threads.
00071 class PlotOperation {    
00072 public:
00073     // Constructor which takes the operation name and a synchronization mutex.
00074     PlotOperation(const String& name, PlotMutexPtr mutex);
00075     
00076     // Destructor.
00077     ~PlotOperation();
00078     
00079     
00080     // Accessors (synchronized).
00081     // <group>
00082     String name() const;    
00083     bool inProgress() const;    
00084     bool isFinished() const;    
00085     unsigned int currentProgress() const;    
00086     String currentStatus() const;
00087     bool cancelRequested() const;
00088     // </group>
00089     
00090     // Mutators (synchronized).  Any mutator will notify registered watchers of
00091     // a change.
00092     // <group>
00093     void setInProgress(bool inProgress);
00094     void setIsFinished(bool isFinished);
00095     void setCurrentProgress(unsigned int currentProgress);
00096     void setCurrentStatus(const String& currentStatus);
00097     void setCancelRequested(bool cancel);
00098     // </group>
00099     
00100     // Sets the operation's mutex to the given.
00101     void setMutex(PlotMutexPtr mutex);
00102     
00103     // Adds the given watcher for this object.
00104     void addWatcher(PlotOperationWatcher* watcher);
00105     
00106     // Removes the given watcher for this object.
00107     void removeWatcher(PlotOperationWatcher* watcher);
00108     
00109     // Resets the in progress, is finished, current progress, and current
00110     // status members.  Will notify registered watchers of a change.
00111     void reset();
00112     
00113     // Updates in progress, is finished, current progress, and current status
00114     // members to reflect a "finished" state.  Will notify registered watchers
00115     // of a change.
00116     void finish();
00117     
00118 private:    
00119     // Name.
00120     String m_name;
00121     
00122     // Flags.
00123     bool m_inProgress, m_isFinished;
00124     
00125     // Current progress (0 - 100)%.
00126     unsigned int m_currentProgress;
00127     
00128     // Current status message.
00129     String m_currentStatus;
00130     
00131     // Cancel requested flag.
00132     bool m_cancelRequested;
00133     
00134     // Synchronization mutex.
00135     PlotMutexPtr m_mutex;
00136     
00137     // Watchers.
00138     vector<PlotOperationWatcher*> m_watchers;
00139     
00140     
00141     // Notifies any registered watchers that the operation has changed.
00142     void notifyWatchers() const;
00143 };
00144 typedef CountedPtr<PlotOperation> PlotOperationPtr;
00145 
00146 
00147 // Abstract interface for any object that wants to watch a PlotOperation object
00148 // for changes.
00149 class PlotOperationWatcher {
00150 public:
00151     // Constructor.
00152     PlotOperationWatcher() { }
00153     
00154     // Destructor.
00155     virtual ~PlotOperationWatcher() { }
00156     
00157     
00158     // This method is called to notify the watcher that the given PlotOperation
00159     // object has changed.
00160     virtual void operationChanged(const PlotOperation& operation) = 0;
00161 };
00162 
00163 }
00164 
00165 #endif /* PLOTOPERATION_H_ */