Boost logo

Boost-Commit :

From: mconsoni_at_[hidden]
Date: 2007-06-08 14:33:07


Author: mconsoni
Date: 2007-06-08 14:33:07 EDT (Fri, 08 Jun 2007)
New Revision: 4499
URL: http://svn.boost.org/trac/boost/changeset/4499

Log:
Some examples about versioning and multiple library loading:

a) Loading two different libraries with the same interface and the same class names. The result was OK, both implementations loaded and you could differentiate them by its Info (and I changed a returned std::string in one of them to see which was loaded).

b) Loading two times the same library. Both (equal) implementations loaded fine.

c) Loading two libraries, both with the same implementation (class) name but with different interface. The result was OK too, the factory_map loaded both factories and you could access to both classes.

        . examples/versioning: main directory for the new example.
                _ hello_world_versions.cpp: library to show (a), we define a new hello world library as it will be version 2.
                _ salute.hpp/salute.cpp: different interface implementing a hello class to test (c)
                _ main_versions.cpp: main function showing the examples.
        . examples/Jamfile.v2: added new example.

Added:
   sandbox/libs/extension/examples/versioning/
   sandbox/libs/extension/examples/versioning/hello_world_versions.cpp
   sandbox/libs/extension/examples/versioning/main_versions.cpp
   sandbox/libs/extension/examples/versioning/salute.cpp
   sandbox/libs/extension/examples/versioning/salute.hpp
Text files modified:
   sandbox/libs/extension/examples/Jamfile.v2 | 4 ++++
   1 files changed, 4 insertions(+), 0 deletions(-)

Modified: sandbox/libs/extension/examples/Jamfile.v2
==============================================================================
--- sandbox/libs/extension/examples/Jamfile.v2 (original)
+++ sandbox/libs/extension/examples/Jamfile.v2 2007-06-08 14:33:07 EDT (Fri, 08 Jun 2007)
@@ -16,6 +16,10 @@
 exe HelloWorld : main.cpp ;
 lib HelloWorldLib : hello_world.cpp : <link>shared ;
 
+exe HelloWorldVersions : versioning/main_versions.cpp ;
+lib HelloWorldLibv2 : versioning/hello_world_versions.cpp : <link>shared ;
+lib SaluteLib : versioning/salute.cpp : <link>shared ;
+
 exe Parameters : parameters/main_lp.cpp ;
 lib ParametersLib : parameters/lots_of_parameters.cpp : <link>shared ;
 

Added: sandbox/libs/extension/examples/versioning/hello_world_versions.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/versioning/hello_world_versions.cpp 2007-06-08 14:33:07 EDT (Fri, 08 Jun 2007)
@@ -0,0 +1,24 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include "../word.hpp"
+#include <boost/extension/factory_map.hpp>
+
+class world : public word
+{
+public:
+ virtual const char * get_val(){return "world! v2";}
+};
+class hello : public word
+{
+public:
+ virtual const char * get_val(){return "| v2 hello";}
+};
+extern "C" void BOOST_EXTENSION_EXPORT_DECL extension_export_word(boost::extensions::factory_map & fm)
+{
+ fm.add<hello, word, int>(21); // int could be used as version (v2 word 1)
+ fm.add<world, word, int>(22); // int could be used as version (v2 word 2)
+}

Added: sandbox/libs/extension/examples/versioning/main_versions.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/versioning/main_versions.cpp 2007-06-08 14:33:07 EDT (Fri, 08 Jun 2007)
@@ -0,0 +1,78 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include <boost/extension/factory_map.hpp>
+#include <boost/extension/shared_library.hpp>
+#include <boost/extension/convenience.hpp>
+
+#include <iostream>
+
+
+#include "../word.hpp"
+#include "salute.hpp"
+
+int main(void)
+{
+ using namespace boost::extensions;
+
+ // Create the factory_map object - it will hold all of the available
+ // constructors. Multiple factory_maps can be constructed.
+ factory_map fm;
+
+ // load hello world first version
+ load_single_library(fm, "libHelloWorldLib.extension", "extension_export_word");
+
+ // load hello world second version
+ load_single_library(fm, "libHelloWorldLibv2.extension", "extension_export_word");
+
+ // load hello world second version again
+ load_single_library(fm, "libHelloWorldLibv2.extension", "extension_export_word");
+
+ // load salute library (with hello included)
+ load_single_library(fm, "libSaluteLib.extension", "extension_export_salute");
+
+ // Get a reference to the list of constructors for words.
+ std::list<factory<word, int> > & factory_list = fm.get<word, int>();
+
+ if (factory_list.size() < 6) {
+ std::cout << "Error - the classes were not found (" << factory_list.size() << " classes)" << std::endl;
+ return 1;
+ }
+
+ std::cout << "words: " << std::endl;
+ for (std::list<factory<word, int> >::iterator current_word = factory_list.begin();
+ current_word != factory_list.end(); ++current_word)
+ {
+ // Using auto_ptr to avoid needing delete. Using smart_ptrs is recommended.
+ // Note that this has a zero argument constructor - currently constructors
+ // with up to six arguments can be used.
+ std::auto_ptr<word> word_ptr(current_word->create());
+ std::cout << word_ptr->get_val() << " ";
+ }
+ std::cout << std::endl << std::endl;
+
+ // Get a reference to the list of constructors for salutes.
+ std::list<factory<salute, int> > & salute_factory_list = fm.get<salute, int>();
+
+ if (salute_factory_list.size() < 2) {
+ std::cout << "Error - the classes were not found (" << salute_factory_list.size() << " classes)" << std::endl;
+ return 1;
+ }
+
+ std::cout << "salutes: " << std::endl;
+ for (std::list<factory<salute, int> >::iterator current_salute = salute_factory_list.begin();
+ current_salute != salute_factory_list.end(); ++current_salute)
+ {
+ // Using auto_ptr to avoid needing delete. Using smart_ptrs is recommended.
+ // Note that this has a zero argument constructor - currently constructors
+ // with up to six arguments can be used.
+ std::auto_ptr<salute> salute_ptr(current_salute->create());
+ std::cout << salute_ptr->say() << " ";
+ }
+ std::cout << std::endl;
+
+ return 0;
+}

Added: sandbox/libs/extension/examples/versioning/salute.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/versioning/salute.cpp 2007-06-08 14:33:07 EDT (Fri, 08 Jun 2007)
@@ -0,0 +1,27 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include "salute.hpp"
+#include <boost/extension/factory_map.hpp>
+
+class hello : public salute
+{
+public:
+ virtual const char *say(void) {return "hello";}
+};
+
+class bye : public salute
+{
+public:
+ virtual const char *say(void) {return "bye!";}
+};
+
+
+extern "C" void BOOST_EXTENSION_EXPORT_DECL extension_export_salute(boost::extensions::factory_map & fm)
+{
+ fm.add<hello, salute, int>(1);
+ fm.add<bye, salute, int>(2);
+}

Added: sandbox/libs/extension/examples/versioning/salute.hpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/versioning/salute.hpp 2007-06-08 14:33:07 EDT (Fri, 08 Jun 2007)
@@ -0,0 +1,12 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+class salute
+{
+public:
+ virtual ~salute(){}
+ virtual const char * say(){return "";}
+};


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk