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 namespace casa { 00036 00037 //# Forward Declarations 00038 class PlotOperationWatcher; 00039 00040 00041 // Abstract class, for a synchronization mutex specific to the threading 00042 // library used by the plotting implementations. 00043 class PlotMutex { 00044 public: 00045 // Constructor. 00046 PlotMutex() { } 00047 00048 // Destructor. 00049 virtual ~PlotMutex() { } 00050 00051 00052 // ABSTRACT METHODS // 00053 00054 // Locks the mutex. 00055 virtual void lock() = 0; 00056 00057 // Unlocks the mutex. 00058 virtual void unlock() = 0; 00059 00060 // Tries to lock the mutex, and returns immediately with either true for 00061 // success (mutex is now locked), or false (mutex is unavailable). 00062 virtual bool tryLock() = 0; 00063 }; 00064 typedef CountedPtr<PlotMutex> PlotMutexPtr; 00065 00066 00067 // Simple object to synchronize operation progress information across threads. 00068 class PlotOperation { 00069 public: 00070 // Constructor which takes the operation name and a synchronization mutex. 00071 PlotOperation(const String& name, PlotMutexPtr mutex); 00072 00073 // Destructor. 00074 ~PlotOperation(); 00075 00076 00077 // Accessors (synchronized). 00078 // <group> 00079 String name() const; 00080 bool inProgress() const; 00081 bool isFinished() const; 00082 unsigned int currentProgress() const; 00083 String currentStatus() const; 00084 bool cancelRequested() const; 00085 // </group> 00086 00087 // Mutators (synchronized). Any mutator will notify registered watchers of 00088 // a change. 00089 // <group> 00090 void setInProgress(bool inProgress); 00091 void setIsFinished(bool isFinished); 00092 void setCurrentProgress(unsigned int currentProgress); 00093 void setCurrentStatus(const String& currentStatus); 00094 void setCancelRequested(bool cancel); 00095 // </group> 00096 00097 // Sets the operation's mutex to the given. 00098 void setMutex(PlotMutexPtr mutex); 00099 00100 // Adds the given watcher for this object. 00101 void addWatcher(PlotOperationWatcher* watcher); 00102 00103 // Removes the given watcher for this object. 00104 void removeWatcher(PlotOperationWatcher* watcher); 00105 00106 // Resets the in progress, is finished, current progress, and current 00107 // status members. Will notify registered watchers of a change. 00108 void reset(); 00109 00110 // Updates in progress, is finished, current progress, and current status 00111 // members to reflect a "finished" state. Will notify registered watchers 00112 // of a change. 00113 void finish(); 00114 00115 private: 00116 // Name. 00117 String m_name; 00118 00119 // Flags. 00120 bool m_inProgress, m_isFinished; 00121 00122 // Current progress (0 - 100)%. 00123 unsigned int m_currentProgress; 00124 00125 // Current status message. 00126 String m_currentStatus; 00127 00128 // Cancel requested flag. 00129 bool m_cancelRequested; 00130 00131 // Synchronization mutex. 00132 PlotMutexPtr m_mutex; 00133 00134 // Watchers. 00135 std::vector<PlotOperationWatcher*> m_watchers; 00136 00137 00138 // Notifies any registered watchers that the operation has changed. 00139 void notifyWatchers() const; 00140 }; 00141 typedef CountedPtr<PlotOperation> PlotOperationPtr; 00142 00143 00144 // Abstract interface for any object that wants to watch a PlotOperation object 00145 // for changes. 00146 class PlotOperationWatcher { 00147 public: 00148 // Constructor. 00149 PlotOperationWatcher() { } 00150 00151 // Destructor. 00152 virtual ~PlotOperationWatcher() { } 00153 00154 00155 // This method is called to notify the watcher that the given PlotOperation 00156 // object has changed. 00157 virtual void operationChanged(const PlotOperation& operation) = 0; 00158 }; 00159 00160 } 00161 00162 #endif /* PLOTOPERATION_H_ */