Česky
Kamil Dudka

GED 2006 (C++)

File detail

Name:Downloadpluginsupport.h [Download]
Location: ged2006 > src
Size:4.6 KB
Last modification:2007-08-29 02:16

Source code

/*
 * File: pluginsuppport.h - Maintain plugins
 * Project: GED - bitmap editor (ICP)
 * Author: Kamil Dudka, xdudka00
 * Team: xdudka00, xfilak01, xhefka00, xhradi08
 * Created: 2006-04-06
 */
 
#ifndef PLUGINSUPPORT_H
#define PLUGINSUPPORT_H
 
#include <string>
#include <map>
 
#include "framebuffer.h"
 
/**
 * Plugin list. Design pattern singleton.
 */
class PluginList {
public:
	~PluginList ();
 
	/**
	 * Access point to singleton.
	 * \return Return pointer to singleton object.
	 */
	static PluginList *ptr();
 
	/**
	 * Destroy plugin list singleton.
	 * This should be called on program exit.
	 */
	static void destroy();
 
	/**
	 * Lookup for plugins. Can be called repeatly to add plugins from different directories.
	 * This method throws PluginList::ErrPluginDir exception if directory can't be opened.
	 * \param szDirectory Directory to lookup for plugins.
	 */
	void pluginDirLookup (const char *szDirectory);
 
	/**
	 * Call plugin.
	 * This method throws PluginList::ErrPluginNotFound exception if pluginName is not valid.
	 * It can also throw PluginList::ErrPluginEC exception if plugin exits with an error.
	 * \param fb Frame buffer to work with.
	 * \param pluginName Name (identification) of plugin.
	 * \param args Argumets passed to plugin.
	 */
	void callByName (FrameBuffer &fb, std::string pluginName, std::string args);
 
	/**
	 * This is an exception thrown from PluginList::pluginDirLookup() method if directory can't be opened.
	 */
	struct ErrPluginDir { };
 
	/**
	 * This is an exception thrown from PluginList::callByName() if given name does not exist.
	 */
	struct ErrPluginNotFound { };
 
	/**
	 * This is an exception thrown from PluginList::callByName() method.
	 */
	struct ErrPluginEC {
		int iErrCode;			///< Plugin exit-code.
 
		/**
		 * Create exception.
		 * \param i Exit-code of plugin.
		 */
		ErrPluginEC (int i) { iErrCode = i; }
	};
 
	friend class DocMenu;			///< Needed for plugin names reading.
 
protected:
	/**
	 * Singleton PluginList can't be allocated static. Use PluginList::ptr() method to obtain singleton address.
	 */
	PluginList () { }
 
private:
	static PluginList *_instance;
	std::map<std::string, std::string> _pluginNameMap;
 
	/**
	 * Try to add plugin. It asks for plugin identification calling plugin with "--name" argument.
	 * It has no return value and throws no exception if failed.
	 * \param fileName File name of plugin executable.
	 */
	void addPluginFile (std::string &fileName);
 
	/**
	 * Call plugin.
	 * \param fb Frame buffer to work with.
	 * \param fileName File name of plugin executable.
	 * \param args Arguments passed to plugin.
	 */
	void invokePlugin (FrameBuffer &fb, std::string &fileName, std::string &args);
};
 
 
/**
 * IPC shared object
 */
class SharedObject {
public:
	/**
	 * \return Return pointer to frame buffer.
	 */
	FrameBuffer *pFB() { return &_fb; }
 
	/**
	 * \return Return shmID for image bitmap.
	 */
	int shmBitmap() { return _shmBitmap; }
 
	/**
	 * Create shared object from plain frame buffer.
	 * \param fb Source frame buffer.
	 * \return Return id of shared object.
	 */
	static int create (FrameBuffer &fb);
 
	/**
	 * Destroy shared object.
	 * \param shmID Id returned by SharedObject::create() method.
	 */
	static void destroy (int shmID);
 
	/**
	 * Attach process to shared object.
	 * \param shmID Id of shared object.
	 * \return Return address of shared object.
	 */
	static SharedObject *attach (int shmID);
 
	/**
	 * Detach process from shared object.
	 * \param addr Address of shared object returned by SharedObject::attach() method.
	 */
	static void detach (SharedObject *addr);
 
	/**
	 * Allocate shared bitmap.
	 * \param size Size of bitmap.
	 */
	void bitmapAlloc (BoxSize &size);
 
	/**
	 * Free shared memory allocated by SharedObject::bitmapAlloc() method.
	 */
	void bitmapFree ();
 
	/**
	 * Attach process to bitmap.
	 * \return Return address of attached bitmap.
	 */
	Pixel *bitmapAttach ();
 
	/**
	 * Detach process from bitmap.
	 * \param addr Address of shared object returned by SharedObject::bitmapAttach() method.
	 */
	void detach (Pixel *addr);
 
private:
	FrameBuffer _fb;	///< Use plain frame buffer to hold shared object data.
	int _shmBitmap;		///< shmID of allocated bitmap
 
	/**
	 * Use SharedObject::create() method to create shared object.
	 */
	SharedObject();
};
 
/**
 * FrameBuffer with IPC support
 */
class SharedFrameBuffer: public FrameBuffer {
public:
	/**
	 * Create frame buffer from shared object.
	 * \param shmID id of shared object.
	 */
	SharedFrameBuffer (int shmID);
 
	/**
	 * Commit data to shared object.
	 * \param shmID id of shared object.
	 */
	void commit (int shmID);
};
 
#endif // PLUGINSUPPORT_H