summaryrefslogtreecommitdiff
path: root/NSCmpts/ActionEngine.h
diff options
context:
space:
mode:
Diffstat (limited to 'NSCmpts/ActionEngine.h')
-rw-r--r--NSCmpts/ActionEngine.h157
1 files changed, 157 insertions, 0 deletions
diff --git a/NSCmpts/ActionEngine.h b/NSCmpts/ActionEngine.h
new file mode 100644
index 0000000..301ed81
--- /dev/null
+++ b/NSCmpts/ActionEngine.h
@@ -0,0 +1,157 @@
+// ActionEngine.h: interface for the CActionEngine class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#if !defined(AFX_ACTIONENGINE_H__00EC1855_A1A5_11D3_82DB_0020182B97FC__INCLUDED_)
+#define AFX_ACTIONENGINE_H__00EC1855_A1A5_11D3_82DB_0020182B97FC__INCLUDED_
+
+#if _MSC_VER > 1000
+#pragma once
+#endif // _MSC_VER > 1000
+
+#include <path.h>
+#include "NSMessages.h"
+
+////////////////////////////////////////////////////////////////////////
+// Hooks onto any object to provide reference count services
+
+template<class TBase>
+class counted_obj
+{
+public:
+ counted_obj()
+ { m_lCount = 0; }
+ long addref()
+ { return ++m_lCount; }
+ long release()
+ {
+ long lCount = --m_lCount;
+
+ if(lCount <= 0)
+ delete (TBase*)this;
+
+ return lCount;
+ }
+protected:
+ long m_lCount;
+};
+
+////////////////////////////////////////////////////////////////////////
+// Forward Declarations
+
+class CAction; // Action processed by the Action Engine
+class CActionError; // Error thrown by an Action
+class CActionEngine; // The Engine
+
+/////////////////////////////////////////////////////////////////////////
+// Abstract class: An Action for the Action Engine
+
+class CAction
+ : public counted_obj<CAction>
+{
+public:
+ CAction() {};
+ virtual ~CAction() {};
+
+ // Does the actual action
+ virtual bool Do(CActionEngine* pEngine, HWND hwndUpdates)
+ throw(CActionError) = 0;
+ // Retry after an error
+ virtual bool IsRetryable() const throw() = 0;
+
+ // Try and fix after there's an error
+ virtual void Fix(HRESULT hr) throw(CActionError) = 0;
+ virtual bool IsFixable(HRESULT hr) const throw() = 0;
+
+protected:
+ // Helper function for implementations
+ // Updates the dialog
+ void Update(HWND hwndUpdates, LPCTSTR szDesc);
+ void Update(HWND hwndUpdates, LPCTSTR szStatus, LPCTSTR szDesc);
+};
+
+
+
+//////////////////////////////////////////////////////////////////////
+// An Error thrown by an Action Object and caught by Action Engine
+// With built in formatting
+//
+// %e Error Text
+// %f File Name
+// %F Full File Path
+
+class CActionError
+{
+// Constructors
+public:
+ // These two get the error message from the HRESULT
+ CActionError(HRESULT hr, const string& sMessage,
+ const file_path& path = file_path());
+ CActionError(HRESULT hr, UINT nID,
+ const file_path& path = file_path());
+
+ // These two ask you to supply the error message
+ CActionError(const string& sError, HRESULT hr, UINT nID,
+ const file_path& path = file_path());
+ CActionError(const string& sError, HRESULT hr, const string& sMessage,
+ const file_path& path = file_path());
+
+ string m_sDesc; // Formatted Error Text
+ HRESULT m_hRes; // Original HRESULT
+
+// Helper Functions
+protected:
+ void Construct(HRESULT hr, string sMessage, const file_path& path);
+ void Construct(const string& sError, HRESULT hr, string sMessage,
+ const file_path& path);
+};
+
+/////////////////////////////////////////////////////////////////////////
+// The Engine that processes CAction objects
+// Derives from a vector of Action pointers
+
+typedef std::vector<CAction*> action_array;
+
+class CActionEngine
+ : protected action_array
+{
+// Construction
+public:
+ CActionEngine() {};
+ CActionEngine(CAction* pFirstAction)
+ { push_back(pFirstAction); }
+ virtual ~CActionEngine()
+ { RemoveAll(); };
+
+// Actions
+public:
+ // These start engine in current thread
+ HRESULT Start(CAction* pFirstAction);
+ HRESULT Start();
+
+ // Start engine in another thread and return immediately
+ HANDLE StartThread();
+
+// Set Properties
+public:
+ void SetUpdates(HWND hwndUpdates = NULL, HWND hwndErrors = NULL);
+
+// A few array management functions for the outside world
+public:
+ UINT Add(CAction* pAction);
+ UINT Insert(CAction* pAction, UINT nIndex);
+ void Remove(UINT nIndex);
+ void RemoveAll(CAction* pKeep = NULL);
+ CAction* At(UINT nIndex);
+
+protected:
+ HWND m_hwndUpdates; // Window to Send Updates to
+ HWND m_hwndErrors; // Window to Send Errors to
+
+ // Used to start in a separate thread
+ static DWORD CALLBACK ThreadProc(LPVOID lpParam);
+};
+
+void Lower(string& s);
+#endif // !defined(AFX_ACTIONENGINE_H__00EC1855_A1A5_11D3_82DB_0020182B97FC__INCLUDED_)
+