Author: tgoettlicher
Date: Wed May 16 16:32:15 2007
New Revision: 5559
URL: http://svn.opensuse.org/viewcvs/zypp?rev=5559&view=rev
Log:
first steps in using plugins
Added:
branches/tmp/tgoettlicher/
branches/tmp/tgoettlicher/updater-gnome-kde/
branches/tmp/tgoettlicher/updater-gnome-kde/Makefile
branches/tmp/tgoettlicher/updater-gnome-kde/PlugInFun.cc
branches/tmp/tgoettlicher/updater-gnome-kde/PlugInHappy.cc
branches/tmp/tgoettlicher/updater-gnome-kde/dll.cc
branches/tmp/tgoettlicher/updater-gnome-kde/dll.h
branches/tmp/tgoettlicher/updater-gnome-kde/plugin.cc
branches/tmp/tgoettlicher/updater-gnome-kde/plugin.h
branches/tmp/tgoettlicher/updater-gnome-kde/plugtest.cc
Added: branches/tmp/tgoettlicher/updater-gnome-kde/Makefile
URL: http://svn.opensuse.org/viewcvs/zypp/branches/tmp/tgoettlicher/updater-gnome-kde/Makefile?rev=5559&view=auto
==============================================================================
--- branches/tmp/tgoettlicher/updater-gnome-kde/Makefile (added)
+++ branches/tmp/tgoettlicher/updater-gnome-kde/Makefile Wed May 16 16:32:15 2007
@@ -0,0 +1,40 @@
+
+# The flags needed to compile all
+CXXFLAGS = -fPIC -frtti -O
+
+# we need to link in the dll handling library
+
+LDLIBS = -ldl
+
+
+all : plugtest PlugInFun.so PlugInHappy.so
+
+# the main executable has to be linked with the -rdynamic flag
+# so the plug in libraries can call inherited methods and
+# access vtables in the main executable.
+
+plugtest : plugtest.o dll.o plugin.o
+ $(CXX) -rdynamic $^ -o $@ $(LDLIBS)
+
+# link the shared libraries with the -shared flag.
+
+PlugInFun.so : PlugInFun.o
+ $(CXX) $(CXXFLAGS) -shared $^ -o $@
+
+PlugInHappy.so : PlugInHappy.o
+ $(CXX) $(CXXFLAGS) -shared $^ -o $@
+
+
+clean :
+ -rm *.o
+
+realclean : clean
+ -rm plugtest *.so
+
+
+
+
+
+
+
+
Added: branches/tmp/tgoettlicher/updater-gnome-kde/PlugInFun.cc
URL: http://svn.opensuse.org/viewcvs/zypp/branches/tmp/tgoettlicher/updater-gnome-kde/PlugInFun.cc?rev=5559&view=auto
==============================================================================
--- branches/tmp/tgoettlicher/updater-gnome-kde/PlugInFun.cc (added)
+++ branches/tmp/tgoettlicher/updater-gnome-kde/PlugInFun.cc Wed May 16 16:32:15 2007
@@ -0,0 +1,65 @@
+#include <typeinfo>
+#include <iostream>
+#include "plugin.h"
+
+// Class PlugInFun inherits from PlugIn
+// and shows the world when one is created/destroyed
+// and supplies a Show() method that announces its name
+
+
+class PlugInFun : public PlugIn
+{
+ public:
+ PlugInFun()
+ {
+ cout << "PlugInFun created" << endl;
+ }
+
+ virtual ~PlugInFun()
+ {
+ cout << "PlugInFun destroyed" << endl;
+ }
+
+ virtual void Show()
+ {
+ cout << "I am a Fun PlugIn" << endl;
+ }
+};
+
+//
+// The PlugInFunFactory class inherits from PlugInFactory
+// and creates a PlugInFun object when requested.
+//
+
+
+class PlugInFunFactory : public PlugInFactory
+{
+ public:
+ PlugInFunFactory()
+ {
+ }
+
+ ~PlugInFunFactory()
+ {
+ }
+
+ virtual PlugIn * CreatePlugIn()
+ {
+ return new PlugInFun;
+ }
+
+};
+
+
+//
+// The "C" linkage factory0() function creates the PlugInFunFactory
+// class for this library
+//
+
+
+extern "C" void * factory0( void )
+{
+ return new PlugInFunFactory;
+}
+
+
Added: branches/tmp/tgoettlicher/updater-gnome-kde/PlugInHappy.cc
URL: http://svn.opensuse.org/viewcvs/zypp/branches/tmp/tgoettlicher/updater-gnome-kde/PlugInHappy.cc?rev=5559&view=auto
==============================================================================
--- branches/tmp/tgoettlicher/updater-gnome-kde/PlugInHappy.cc (added)
+++ branches/tmp/tgoettlicher/updater-gnome-kde/PlugInHappy.cc Wed May 16 16:32:15 2007
@@ -0,0 +1,64 @@
+#include <typeinfo>
+#include <iostream>
+#include <string>
+#include "plugin.h"
+
+
+// Class PlugInHappy inherits from PlugIn
+// and shows the world when one is created/destroyed
+// and supplies a Show() method that announces its name
+
+class PlugInHappy : public PlugIn
+{
+ public:
+ PlugInHappy()
+ {
+ cout << "PlugInHappy() Created" << endl;
+ }
+
+ virtual ~PlugInHappy()
+ {
+ cout << "PlugInHappy() destroyed" << endl;
+ }
+
+ virtual void Show()
+ {
+ cout << "I am a New Happy PlugIn" << endl;
+ }
+};
+
+
+//
+// The PlugInHappyFactory class inherits from PlugInFactory
+// and creates a PlugInHappy object when requested.
+//
+
+class PlugInHappyFactory : public PlugInFactory
+{
+ public:
+ PlugInHappyFactory()
+ {
+ }
+
+ ~PlugInHappyFactory()
+ {
+ }
+
+ virtual PlugIn * CreatePlugIn()
+ {
+ return new PlugInHappy;
+ }
+
+};
+
+
+//
+// The "C" linkage factory0() function creates the PlugInHappyFactory
+// class for this library
+//
+
+extern "C" void * factory0( void )
+{
+ return new PlugInHappyFactory;
+}
+
Added: branches/tmp/tgoettlicher/updater-gnome-kde/dll.cc
URL: http://svn.opensuse.org/viewcvs/zypp/branches/tmp/tgoettlicher/updater-gnome-kde/dll.cc?rev=5559&view=auto
==============================================================================
--- branches/tmp/tgoettlicher/updater-gnome-kde/dll.cc (added)
+++ branches/tmp/tgoettlicher/updater-gnome-kde/dll.cc Wed May 16 16:32:15 2007
@@ -0,0 +1,73 @@
+#include <typeinfo>
+#include <iostream>
+#include <string>
+#include
+#include "dll.h"
+
+
+
+DLLManager::DLLManager( const char *fname )
+{
+ // Try to open the library now and get any error message.
+
+ h=dlopen( fname, RTLD_NOW );
+ err=dlerror();
+}
+
+DLLManager::~DLLManager()
+{
+ // close the library if it isn't null
+ if( h!=0 )
+ dlclose(h);
+}
+
+
+bool DLLManager::GetSymbol(
+ void **v,
+ const char *sym_name
+ )
+{
+ // try extract a symbol from the library
+ // get any error message is there is any
+
+ if( h!=0 )
+ {
+ *v = dlsym( h, sym_name );
+ err=dlerror();
+ if( err==0 )
+ return true;
+ else
+ return false;
+ }
+ else
+ {
+ return false;
+ }
+
+}
+
+
+DLLFactoryBase::DLLFactoryBase(
+ const char *fname,
+// const char *factory=0
+ const char *factory
+ ) : DLLManager(fname)
+{
+ // try get the factory function if there is no error yet
+
+ factory_func=0;
+
+ if( LastError()==0 )
+ {
+ GetSymbol( (void **)&factory_func, factory ? factory : "factory0" );
+ }
+
+}
+
+
+DLLFactoryBase::~DLLFactoryBase()
+{
+}
+
+
+
Added: branches/tmp/tgoettlicher/updater-gnome-kde/dll.h
URL: http://svn.opensuse.org/viewcvs/zypp/branches/tmp/tgoettlicher/updater-gnome-kde/dll.h?rev=5559&view=auto
==============================================================================
--- branches/tmp/tgoettlicher/updater-gnome-kde/dll.h (added)
+++ branches/tmp/tgoettlicher/updater-gnome-kde/dll.h Wed May 16 16:32:15 2007
@@ -0,0 +1,115 @@
+
+#ifndef __DLL_H
+#define __DLL_H
+
+//
+// class DLLManager is the simple ELF C++ Library manager.
+//
+// It tries to dynamically load the specified shared library
+// when it is construted.
+//
+// You should call LastError() before doing anything. If it
+// returns NULL there is no error.
+//
+
+
+class DLLManager
+{
+ public:
+ DLLManager( const char *fname );
+ virtual ~DLLManager();
+
+
+ bool GetSymbol( void **, const char *sym_name );
+
+ const char *LastError()
+ {
+ return err;
+ }
+
+ protected:
+ void *h;
+ const char *err;
+};
+
+
+//
+// class DLLFactoryBase is the base class used for the DLLFactory
+// template class.
+//
+// It inherits from the DLLManager class and must be constructed with
+// the file name of the shared library and the function name within that
+// library which will create the desired C++ factory class.
+// If you do not provide func_name to the constructor, it defaults to
+// the undecorated "C" symbol "factory0"
+//
+// factory_func will be set to a pointer to the requested factory creator
+// function. If there was an error linking to the shared library,
+// factory_func will be 0.
+//
+// You can call 'LastError()' to find the error message that occurred.
+//
+//
+
+class DLLFactoryBase : public DLLManager
+{
+ public:
+ DLLFactoryBase(
+ const char *fname,
+ const char *factory=0
+ );
+
+ virtual ~DLLFactoryBase();
+
+ void * (*factory_func)(void);
+};
+
+
+//
+// The DLLFactory template class inherits from DLLFactoryBase.
+// The constructor takes the file name of the shared library
+// and the undecorated "C" symbol name of the factory creator
+// function. The factory creator function in your shared library
+// MUST either return a pointer to an object that is a subclass
+// of 'T' or it must return 0.
+//
+// If everything is cool, then 'factory' will point to the
+// requested factory class. If not, it will be 0.
+//
+// Since the DLLFactory template ultimately inherits DLLManager,
+// you can call LastError() to get any error code information
+//
+// The created factory is OWNED by the DLLFactory class.
+// The created factory will get deleted when the DLLFactory class
+// is deleted, because the DLL will get unloaded as well.
+//
+
+template <class T>
+class DLLFactory : public DLLFactoryBase
+{
+ public:
+ DLLFactory(
+ const char *fname,
+ const char *func_name=0
+ ) : DLLFactoryBase( fname, func_name )
+ {
+ if( factory_func )
+ factory = (T *)factory_func();
+ else
+ factory = 0;
+ }
+
+ ~DLLFactory()
+ {
+ delete factory;
+ }
+
+ T *factory;
+};
+
+
+
+
+
+
+#endif
Added: branches/tmp/tgoettlicher/updater-gnome-kde/plugin.cc
URL: http://svn.opensuse.org/viewcvs/zypp/branches/tmp/tgoettlicher/updater-gnome-kde/plugin.cc?rev=5559&view=auto
==============================================================================
--- branches/tmp/tgoettlicher/updater-gnome-kde/plugin.cc (added)
+++ branches/tmp/tgoettlicher/updater-gnome-kde/plugin.cc Wed May 16 16:32:15 2007
@@ -0,0 +1,19 @@
+#include <typeinfo>
+#include <iostream>
+#include "plugin.h"
+
+//
+// Announce to the world that the PlugIn base
+// class has been created or destroyed
+//
+
+PlugIn::PlugIn()
+{
+ cout << "PlugIn Created" << endl;
+}
+
+PlugIn::~PlugIn()
+{
+ cout << "PlugIn Destroyed" << endl;
+}
+
Added: branches/tmp/tgoettlicher/updater-gnome-kde/plugin.h
URL: http://svn.opensuse.org/viewcvs/zypp/branches/tmp/tgoettlicher/updater-gnome-kde/plugin.h?rev=5559&view=auto
==============================================================================
--- branches/tmp/tgoettlicher/updater-gnome-kde/plugin.h (added)
+++ branches/tmp/tgoettlicher/updater-gnome-kde/plugin.h Wed May 16 16:32:15 2007
@@ -0,0 +1,65 @@
+#ifndef __PLUGIN_H
+#define __PLUGIN_H
+
+
+#include "dll.h"
+
+using namespace std;
+
+
+//
+// PlugIn is an abstract class.
+//
+// This is an example plug in. This plug in only has one method, Show(),
+// which we will use to show its name.
+//
+//
+
+class PlugIn
+{
+ public:
+ PlugIn();
+
+ virtual ~PlugIn();
+
+ virtual void Show() = 0;
+};
+
+
+//
+// The is an example factory for plug ins.
+//
+// This example factory only announces when it is created/destroyed and
+// has the single abstract method CreatePlugIn() which returns a type
+// of plug in.
+//
+// In the real world, you may have multiple different classes in each
+// shared library that are made to work together. All these classes
+// must be created by the Factory class.
+//
+// You may find it useful to have the objects that you create with
+// the factory class be given a pointer to the factory class so
+// they can create their own objects that they need, using the same
+// factory class. Compiler support of covariant return types is
+// real useful here.
+//
+
+
+class PlugInFactory
+{
+ public:
+ PlugInFactory()
+ {
+ cout << "PlugInFactory Created" << endl;
+ }
+
+ virtual ~PlugInFactory()
+ {
+ cout << "PlugInFactory Destroy" << endl;
+ }
+
+ virtual PlugIn * CreatePlugIn() = 0;
+
+};
+
+#endif
Added: branches/tmp/tgoettlicher/updater-gnome-kde/plugtest.cc
URL: http://svn.opensuse.org/viewcvs/zypp/branches/tmp/tgoettlicher/updater-gnome-kde/plugtest.cc?rev=5559&view=auto
==============================================================================
--- branches/tmp/tgoettlicher/updater-gnome-kde/plugtest.cc (added)
+++ branches/tmp/tgoettlicher/updater-gnome-kde/plugtest.cc Wed May 16 16:32:15 2007
@@ -0,0 +1,71 @@
+#include <typeinfo>
+#include <iostream>
+#include <string>
+#include "dll.h"
+#include "plugin.h"
+
+using namespace std;
+
+main()
+{
+
+ // Ask the user for the name of the DLL
+
+ string fname, fname2;
+ cout << "Type in a full pathname of a dll:";
+ cin >> fname;
+ cin >> fname2;
+
+
+ // show it
+
+ cout << endl << "Opening '" << fname << "'" << endl;
+
+ // Now create a DLLFactory for our PlugInFactory attached
+ // to the requested file
+
+ DLLFactory<PlugInFactory> dll( fname.c_str() );
+ DLLFactory<PlugInFactory> dll2( fname2.c_str() );
+
+ //
+ // If it worked we should have dll.factory pointing
+ // to a subclass of PlugInFactory
+ //
+
+ if( dll.factory )
+ {
+ //
+ // yes, we have a factory. Ask the factory to create a
+ // PlugIn object for us.
+ //
+
+ PlugIn *c=dll.factory->CreatePlugIn();
+ PlugIn *d=dll2.factory->CreatePlugIn();
+
+ // tell it to show itself
+
+ c->Show();
+ d->Show();
+
+ // show the C++ RTTI typeid name to the user
+
+ cout << "type name = " << typeid(*c).name() << endl;
+
+ // kill the plug in.
+
+ delete c;
+ }
+ else
+ {
+ cout << "Error opening dll!" << endl;
+
+ // show the error message if any
+
+ if( dll.LastError() )
+ {
+ cout << "DLL Error: " << dll.LastError() << endl;
+ }
+
+ }
+
+}
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org