Boost logo

Boost-Commit :

From: mariano.consoni_at_[hidden]
Date: 2007-08-10 14:30:57


Author: mconsoni
Date: 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
New Revision: 38582
URL: http://svn.boost.org/trac/boost/changeset/38582

Log:
- Initial commit of Boost.Reflection prototype.

Added:
   sandbox/libs/reflection/
   sandbox/libs/reflection/boost-build.jam (contents, props changed)
   sandbox/libs/reflection/boost.png (contents, props changed)
   sandbox/libs/reflection/doc/
   sandbox/libs/reflection/doc/Jamfile.v2 (contents, props changed)
   sandbox/libs/reflection/doc/introduction.qbk (contents, props changed)
   sandbox/libs/reflection/doc/reflection.qbk (contents, props changed)
   sandbox/libs/reflection/doc/tutorial1.qbk (contents, props changed)
   sandbox/libs/reflection/doc/tutorials.qbk (contents, props changed)
   sandbox/libs/reflection/examples/
   sandbox/libs/reflection/examples/Jamfile.v2 (contents, props changed)
   sandbox/libs/reflection/examples/car.hpp (contents, props changed)
   sandbox/libs/reflection/examples/extension/
   sandbox/libs/reflection/examples/extension/car_lib.cpp (contents, props changed)
   sandbox/libs/reflection/examples/extension/extension.cpp (contents, props changed)
   sandbox/libs/reflection/examples/interpreter/
   sandbox/libs/reflection/examples/interpreter/interpreter.cpp (contents, props changed)
   sandbox/libs/reflection/examples/main.cpp (contents, props changed)
   sandbox/libs/reflection/fake.cpp (contents, props changed)
   sandbox/libs/reflection/project-root.jam (contents, props changed)
   sandbox/libs/reflection/test/
   sandbox/libs/reflection/test/Jamfile.v2 (contents, props changed)
   sandbox/libs/reflection/test/basic_test.cpp (contents, props changed)
   sandbox/libs/reflection/test/hello_world_test.cpp (contents, props changed)

Added: sandbox/libs/reflection/boost-build.jam
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/boost-build.jam 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,43 @@
+# Copyright Rene Rivera 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)
+
+# For instructions see Jamfile.v2, or "bjam --help".
+
+local rule if-has-file ( file + : dir * )
+{
+ local result ;
+ if $(dir)
+ {
+ result = [ GLOB $(dir) : $(file) ] ;
+ }
+ return $(result[1]:P) ;
+}
+
+#~ Attempts to find the Boost source tree...
+
+local boost-src = [ if-has-file configure :
+ [ MATCH --boost=(.*) : $(ARGV) ]
+ $(BOOST)
+ $(.boost-build-file:D)/../boost
+ ] ;
+
+#~ Attempts to find the Boost.Build files...
+
+local boost-build-src = [ if-has-file bootstrap.jam :
+ [ MATCH --boost-build=(.*) : $(ARGV) ]
+ $(BOOST_BUILD_PATH)
+ $(BOOST_BUILD)
+ $(boost-src)/tools/build/v2
+ ] ;
+
+#~ Set some common vars to refer to the Boost sources...
+
+BOOST ?= $(boost-src) ;
+BOOST_ROOT ?= $(boost-src) ;
+
+#~ And load up Boost.Build...
+
+boost-build $(boost-build-src) ;

Added: sandbox/libs/reflection/boost.png
==============================================================================
Binary file. No diff available.

Added: sandbox/libs/reflection/doc/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/doc/Jamfile.v2 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,27 @@
+# Boost.Reflection - documentation Jamfile
+#
+# Copyright 2007 Mariano G. Consoni, Jeremy Pack
+# 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)
+#
+# See http://www.boost.org/ for latest version.
+#
+
+using quickbook ;
+xml reflection : reflection.qbk : : :
+ <xsl:param>toc.max.depth=5
+ <xsl:param>toc.section.depth=5
+ <xsl:param>chunk.section.depth=5 ;
+boostbook standalone
+ :
+ reflection
+ :
+ <xsl:param>generate.section.toc.level=3
+ <xsl:param>chunk.first.sections=1
+ <xsl:param>admon.graphics=1
+
+ ;
+
+install html : ../../../../src/sandbox/doc/html/boostbook.css ;
+install ../ : ../../../../src/sandbox/boost.png ;

Added: sandbox/libs/reflection/doc/introduction.qbk
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/doc/introduction.qbk 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,27 @@
+[/ Boost.Reflection - intro ]
+[/ Copyright 2007 Mariano G. Consoni, Jeremy Pack ]
+[/ 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) ]
+[/ See http://www.boost.org/ for latest version. ]
+
+[section:introduction Introduction]
+
+Reflection is the ability of a computer program to observe its structure and
+possibly modify it. Our library tries to achieve that objective, easing the
+development of this kind of programs and releasing the programmer of the
+task of constructing a meta-level layer describing the classes and methods.
+
+In short, Boost.Reflection has a set of classes that allows the programmer to
+store information about their C++ classes and methods, and use (call) them
+using run-time identifiers. For example, we can create a class, attach some
+meta-level information and then use run-time strings to select the method that
+we want to call.
+
+Unfortunately, the library is not fully developed yet. It only has the basic
+functionalities and it's not much than an interesting prototype on the subject.
+
+The documentation is, also, completely incomplete. Sorry. Look at tutorial #1 for
+examples of basic usage.
+
+[endsect]

Added: sandbox/libs/reflection/doc/reflection.qbk
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/doc/reflection.qbk 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,31 @@
+[/ Boost.Reflection - main doc ]
+[/ Copyright 2007 Mariano G. Consoni, Jeremy Pack ]
+[/ 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) ]
+[/ See http://www.boost.org/ for latest version. ]
+
+[library Boost.Reflection
+ [quickbook 1.3]
+ [version 1.0]
+ [copyright 2007 Mariano G. Consoni, Jeremy Pack]
+ [purpose Reflection techniques for C++, meta-classes]
+ [license
+ 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 http://www.boost.org/LICENSE_1_0.txt])
+ ]
+ [category Programming Interfaces]
+ [last-revision $Date: 2007/4/30 12:00:00 $]
+]
+
+
+
+[include introduction.qbk]
+[include tutorials.qbk]
+[/ include factories.qbk]
+[/ include shared_libraries.qbk]
+[/ include info.qbk]
+[/ include performance_analysis.qbk]
+[/ include appendices.qbk]
+

Added: sandbox/libs/reflection/doc/tutorial1.qbk
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/doc/tutorial1.qbk 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,132 @@
+[/ Boost.Reflection - first tutorial ]
+[/ Copyright 2007 Mariano G. Consoni ]
+[/ 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) ]
+[/ See http://www.boost.org/ for latest version. ]
+
+
+[section:tutorial01 Tutorial 1]
+
+Let's create a car class. This will later be our reflected class.
+
+``
+class car {
+private:
+ std::string name_;
+ bool started_;
+
+public:
+ car(std::string car_name) : name_(car_name), started_(false) {}
+
+ bool start(void) {
+ std::cout << name_ << " started." << std::endl;
+ started_ = true;
+ return true;
+ }
+
+ bool turn(float angle)
+ {
+ if(started_) {
+ std::cout << "Turning " << name_ << " "<< angle << " degrees."
+ << std::endl;
+ return true;
+ } else {
+ std::cout << "Cannot turn before starting the engine of "
+ << name_ << "." << std::endl;
+ return false;
+ }
+ }
+ virtual ~car(void) {}
+};
+``
+
+It has methods for starting the engine and turning the car. Note that you can't
+turn before starting the engine (it has state).
+
+Now we will create the reflection for this class. Let's note that we don't have
+to add any MACRO to the original header file.
+
+``
+ boost::extension::reflection<car, std::string> car_reflection("A Car!");
+``
+
+With this instruction we create a reflection. The template parameters describe
+the reflected class and the type that we will use to store additional data (the
+Info class). The parameter of the constructor is an instance of this class, in
+this case we use it to store a short description.
+
+Now that we have created the reflection (i.e. the place where we will store
+the meta-information of the class) we can start to add the methods.
+
+``
+ car_reflection.add<int, bool>(&car::start, 3);
+ car_reflection.add<std::string, bool, float,
+ std::string>(&car::turn, "turn", "turn_angle");
+``
+
+We are adding here the two methods. First we add the start method. The template
+parameters indicate that we will identify this method by an int (3 in this case) and
+that the return type is bool. We also have to provide a pointer to the method.
+
+Then we add the second method. It's described by an string ("turn" in this case),
+and it returns a bool. Also we say that it receives a float and it's described
+by a string ("turn_angle").
+
+Having the methods stored in our reflection we can now see how to call them. We have
+two ways.
+
+First, let's define one instance to use the reflection on:
+
+``
+ car porsche_911("Porsche 911");
+``
+
+The simplest one is the following:
+
+``
+ car_reflection.call<int, bool>(&porsche_911, 3);
+``
+
+We just invoke the "call" method of the reflection. We provide the instance
+and the description of the method. Let's see how to use it when the method has
+parameters:
+
+``
+ car_reflection.call<std::string, bool,
+ float, std::string>(&porsche_911, "turn", .5f);
+``
+
+Now we have to define with template arguments the way that we describe the method
+and the parameters. Also we have to provide the instance, the method description and
+the value of the parameter.
+
+Of course, this approach is a bit limited, because we have to provide a lot of
+information to call the actual method.
+
+Using meta-level programming we are trying to achieve a flexibility that
+could be mined with that requeriments. Simply we don't have such information
+most of the time. Because of that we implemented a second way to call methods.
+
+First we define a parameter_map. We will store there the possible parameters. Note
+that we're defining an unneeded parameter. It'll be simply ignored.
+
+``
+ boost::extension::parameter_map pm;
+ pm.add<float, std::string>(.15f, "turn_angle");
+ pm.add<int, std::string>(50, "aceleration");
+``
+
+Then we use this parameter_map to call the method and provide the needed
+parameters. In this way we just have to know the method ID.
+
+For example in the case of turn we could now call it with:
+
+``
+ car_reflection.call<std::string>("turn", &porsche_911, pm);
+``
+
+This are the basic functionalities of the library that allows the programmer
+to generate the meta-class layer and use them dynamically at run-time.
+
+[endsect]
\ No newline at end of file

Added: sandbox/libs/reflection/doc/tutorials.qbk
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/doc/tutorials.qbk 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,19 @@
+[/ Boost.Reflection - tutorials ]
+[/ Copyright 2007 Mariano G. Consoni and Jeremy Pack ]
+[/ 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) ]
+[/ See http://www.boost.org/ for latest version. ]
+
+[section Tutorials]
+[include tutorial1.qbk]
+[/ include tutorial2.qbk]
+[/ include tutorial3.qbk]
+[/ include tutorial4.qbk]
+[/ include tutorial5.qbk]
+[/ include tutorial6.qbk]
+
+We only have one basic tutorial for now. It shows the implemented functionalities.
+The tutorial code can be found in the examples folder.
+
+[endsect]
\ No newline at end of file

Added: sandbox/libs/reflection/examples/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/examples/Jamfile.v2 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,39 @@
+# Boost.Extension - examples Jamfile
+#
+# Copyright 2007 Jeremy Pack
+# 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)
+#
+# See http://www.boost.org/ for latest version.
+#
+
+import type : change-generated-target-suffix ;
+import type : change-generated-target-prefix ;
+type.change-generated-target-suffix SHARED_LIB : : extension ;
+type.change-generated-target-prefix SHARED_LIB : : lib ;
+import os ;
+
+local BOOST_ROOT = [ os.environ BOOST_ROOT ] ;
+project
+ : requirements
+ <include>../../../
+ <include>$(BOOST_ROOT)
+ <include>../../../../src/sandbox/
+ <toolset>gcc:<find-static-library>dl
+ <toolset>gcc:<find-static-library>readline
+ <toolset>gcc:<find-static-library>boost_regex-gcc41-mt
+ <toolset>gcc:<linkflags>"-Wl,-rpath,'$ORIGIN'"
+ <toolset>darwin:<define>DYLD_LIBRARY_PATH=./
+ :
+ ;
+
+# basic example
+exe reflection : main.cpp ;
+
+# interpreter prototype
+exe interpreter : interpreter/interpreter.cpp ;
+
+# extension integration example
+exe extension-reflection : extension/extension.cpp ;
+lib car_lib : extension/car_lib.cpp : <link>shared ;

Added: sandbox/libs/reflection/examples/car.hpp
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/examples/car.hpp 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,54 @@
+/*
+ * Boost.Reflection / prototype example (car int)
+ *
+ * (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)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+
+#include <iostream>
+
+class car {
+private:
+ std::string name_;
+ bool started_;
+
+public:
+ car(std::string car_name) : name_(car_name), started_(false) {}
+
+ bool start(void) {
+ std::cout << name_ << " started." << std::endl;
+ started_ = true;
+ return true;
+ }
+
+ bool turn(float angle)
+ {
+ if(started_) {
+ std::cout << "Turning " << name_ << " "<< angle << " degrees."
+ << std::endl;
+ return true;
+ } else {
+ std::cout << "Cannot turn before starting the engine of "
+ << name_ << "." << std::endl;
+ return false;
+ }
+ }
+
+ void accelerate(float qty, bool direction)
+ {
+ if(started_) {
+ std::cout << "Accelerating " << name_ << " "<< qty << " m/h."
+ << std::endl;
+ } else {
+ std::cout << "Cannot accelerate before starting the engine of "
+ << name_ << "." << std::endl;
+ }
+ }
+
+ virtual ~car(void) {}
+};

Added: sandbox/libs/reflection/examples/extension/car_lib.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/examples/extension/car_lib.cpp 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,38 @@
+/*
+ * Boost.Extension / hello world implementations
+ *
+ * (C) Copyright Jeremy Pack 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)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#include "../car.hpp"
+#include <boost/extension/factory_map.hpp>
+
+class suv : public car
+{
+public:
+ suv(const std::string name) : car(name) {}
+ virtual std::string get_type(void) { return "It's a SUV."; }
+ virtual ~suv(void) {}
+};
+
+class compact : public car
+{
+public:
+ compact(const std::string name) : car(name) {}
+ virtual std::string get_type(void) { return "It's a compact."; }
+ virtual ~compact(void) {}
+};
+
+
+extern "C"
+void BOOST_EXTENSION_EXPORT_DECL
+extension_export_car(boost::extensions::factory_map & fm)
+{
+ fm.add<suv, car, std::string, std::string>("SUV");
+ fm.add<compact, car, std::string, std::string>("Compact");
+}

Added: sandbox/libs/reflection/examples/extension/extension.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/examples/extension/extension.cpp 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,86 @@
+/*
+ * Boost.Reflection / extension integration example
+ *
+ * (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)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+
+#include <string>
+
+#include <boost/extension/factory_map.hpp>
+#include <boost/extension/shared_library.hpp>
+#include <boost/extension/convenience.hpp>
+
+#include <iostream>
+
+#define BOOST_EXTENSION_USE_PP 1
+
+#include "../car.hpp"
+#include <boost/reflection/reflection.hpp>
+
+
+int main(void)
+{
+
+ /* Here we declare that we are reflecting out a Car. The second
+ template parameter declares that we will describe this reflection
+ using a string. Any arbitrary type could be used here. */
+ boost::extension::reflection<car, std::string> car_reflection("A Car!");
+
+ /* Here we add two methods to the reflection. The library must
+ correctly parse and remember the parameters and return type of these
+ methods. Here, we elect to describe the first method using a number
+ and the second using a string. For the second method, the library
+ knows now that it is a function of Car called turn that takes a float
+ named "turn_angle". */
+ car_reflection.add<int, bool>(&car::start, 3);
+ car_reflection.add<std::string, bool, float,
+ std::string>(&car::turn, "turn", "turn_angle");
+
+ car porsche_911("Porsche 911");
+ car ferrari_f40("Ferrari F40");
+
+ car_reflection.call<int, bool>(&porsche_911, 3);
+ car_reflection.call<std::string, bool,
+ float, std::string>(&porsche_911, "turn", .5f);
+
+ car_reflection.call<std::string, bool,
+ float, std::string>(&ferrari_f40, "turn", .10f);
+
+ // prepare the parameter map for a call
+ boost::extension::parameter_map pm;
+ pm.add<float, std::string>(.15f, "turn_angle");
+ pm.add<int, std::string>(50, "aceleration");
+
+
+ // extension tests
+ using namespace boost::extensions;
+ factory_map fm;
+ load_single_library(fm, "libcar_lib.extension",
+ "extension_export_car");
+ std::list<factory<car, std::string, std::string> > & factory_list =
+ fm.get<car, std::string, std::string>();
+ if(factory_list.size() < 2) {
+ std::cout << "Error - the classes were not found ("
+ << factory_list.size() << ").\n";
+ return -1;
+ }
+ for (std::list<factory<car, std::string, std::string> >
+ ::iterator current_car = factory_list.begin();
+ current_car != factory_list.end();
+ ++current_car)
+ {
+ if(current_car->get_info() == "Compact") {
+ car *beetle(current_car->create("VW New Beetle"));
+ car_reflection.call<int, bool>(beetle, 3);
+ delete beetle;
+ }
+ }
+
+ return 0;
+}

Added: sandbox/libs/reflection/examples/interpreter/interpreter.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/examples/interpreter/interpreter.cpp 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,160 @@
+/*
+ * Boost.Reflection / intepreter prototype example
+ *
+ * (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)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+
+#include <string>
+
+#include <boost/extension/factory_map.hpp>
+#include <boost/extension/shared_library.hpp>
+#include <boost/extension/convenience.hpp>
+
+#include <iostream>
+
+#include "../car.hpp"
+#include <boost/reflection/reflection.hpp>
+
+#include <boost/regex.hpp>
+
+#ifdef USE_READLINE
+#include <readline/readline.h>
+#include <readline/history.h>
+#endif
+
+
+typedef std::list<boost::extensions::factory<car, std::string,
+ std::string> > factory_list;
+
+
+/// this map stores the instanced variables
+std::map<std::string, car *> context;
+
+/// parse and execute a command
+void parse_command(const std::string &line, factory_list &f,
+ boost::extension::reflection<car, std::string> &car_reflection)
+{
+ boost::regex re, call_re;
+ boost::cmatch matches, call_matches;
+
+ std::string creation_pattern("(\\w*)=(\\w*)\\((\\w*)\\)");
+ std::string call_pattern("(\\w*).(\\w*)\\(\\)");
+
+ try {
+ re = creation_pattern;
+ } catch (boost::regex_error& e) {
+ std::cout << creation_pattern << " is not a valid regular expression: \""
+ << e.what() << "\"" << std::endl;
+ return;
+ }
+
+ try {
+ call_re = call_pattern;
+ } catch (boost::regex_error& e) {
+ std::cout << call_pattern << " is not a valid regular expression: \""
+ << e.what() << "\"" << std::endl;
+ return;
+ }
+
+
+ if(boost::regex_match(line.c_str(), matches, re)) {
+
+ if(matches.size() == 4) {
+ std::string instance(matches[1].first, matches[1].second);
+ std::string method(matches[2].first, matches[2].second);
+ std::string parameter1(matches[3].first, matches[3].second);
+
+ for (std::list<boost::extensions::factory<car, std::string,
+ std::string> >::iterator current_car = f.begin();
+ current_car != f.end();
+ ++current_car) {
+ if(current_car->get_info() == method) {
+ // FIXME: free
+ car *created_car(current_car->create(parameter1));
+ context[instance] = created_car;
+
+ std::cout << "Instance [" << instance << "] created." << std::endl;
+ }
+ }
+ }
+ return;
+ }
+
+
+ if(boost::regex_match(line.c_str(), call_matches, call_re)) {
+
+ if(call_matches.size() == 3) {
+ std::string instance(call_matches[1].first, call_matches[1].second);
+ std::string method(call_matches[2].first, call_matches[2].second);
+
+ std::map<std::string, car *>::iterator m = context.find(instance);
+ if(m != context.end()) {
+ std::cout << " --> "
+ << car_reflection.call<std::string, bool>(m->second, method)
+ << std::endl;
+ } else {
+ std::cout << "Instance " << instance << " not found." << std::endl;
+ }
+ }
+ return;
+ }
+
+ std::cout << "The command \"" << line << "\" is invalid."
+ << std::endl;
+
+}
+
+
+int main(void)
+{
+ using namespace boost::extensions;
+
+ factory_map fm;
+ load_single_library(fm, "libcar_lib.extension",
+ "extension_export_car");
+ std::list<factory<car, std::string, std::string> > & car_factory_list =
+ fm.get<car, std::string, std::string>();
+ if(car_factory_list.size() < 2) {
+ std::cout << "Error - the classes were not found ("
+ << car_factory_list.size() << ").\n";
+ std::exit(-1);
+ }
+
+ std::cout << std::endl
+ << " Boost.Reflection example - Prototype C++ interpreter."
+ << std::endl << std::endl;
+
+ boost::extension::reflection<car, std::string> car_reflection("A Car!");
+ car_reflection.add<std::string, bool>(&car::start, "start");
+ car_reflection.add<std::string, bool, float,
+ std::string>(&car::turn, "turn", "turn_angle");
+
+
+ while(1) {
+ std::string line;
+ std::cout << "> ";
+ std::cin >> line;
+
+#ifdef USE_READLINE
+ char *line_chrptr = readline("> ");
+ std::string line(line_chrptr);
+ if(line.length() != 0) {
+ add_history(line.c_str());
+ }
+#endif
+
+ parse_command(line, car_factory_list, car_reflection);
+
+#ifdef USE_READLINE
+ free(line_chrptr);
+#endif
+ }
+
+ return 0;
+}

Added: sandbox/libs/reflection/examples/main.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/examples/main.cpp 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,63 @@
+/*
+ * Boost.Reflection / basic example
+ *
+ * (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)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+
+#include <string>
+
+#include <boost/extension/factory_map.hpp>
+#include <boost/extension/shared_library.hpp>
+#include <boost/extension/convenience.hpp>
+
+#include <iostream>
+
+#define BOOST_EXTENSION_USE_PP 1
+
+#include "car.hpp"
+#include <boost/reflection/reflection.hpp>
+
+
+int main(void)
+{
+
+ /* Here we declare that we are reflecting out a Car. The second
+ template parameter declares that we will describe this reflection
+ using a string. Any arbitrary type could be used here. */
+ boost::extension::reflection<car, std::string> car_reflection("A Car!");
+
+ /* Here we add two methods to the reflection. The library must
+ correctly parse and remember the parameters and return type of these
+ methods. Here, we elect to describe the first method using a number
+ and the second using a string. For the second method, the library
+ knows now that it is a function of Car called turn that takes a float
+ named "turn_angle". */
+ car_reflection.add<int, bool>(&car::start, 3);
+ car_reflection.add<std::string, bool, float,
+ std::string>(&car::turn, "turn", "turn_angle");
+
+ // create some instances to use our reflection
+ car porsche_911("Porsche 911");
+ car ferrari_f40("Ferrari F40");
+
+ // call methods
+ car_reflection.call<int, bool>(&porsche_911, 3);
+ car_reflection.call<std::string, bool,
+ float, std::string>(&porsche_911, "turn", .5f);
+
+ car_reflection.call<std::string, bool,
+ float, std::string>(&ferrari_f40, "turn", .10f);
+
+ // prepare the parameter map for a call
+ boost::extension::parameter_map pm;
+ pm.add<float, std::string>(.15f, "turn_angle");
+ pm.add<int, std::string>(50, "aceleration");
+
+ return 0;
+}

Added: sandbox/libs/reflection/fake.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/fake.cpp 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,12 @@
+/* This file is used to create empty library
+ * It is useful to test header files.
+ *
+ * (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)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#include <boost/reflection/reflection.hpp>

Added: sandbox/libs/reflection/project-root.jam
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/project-root.jam 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,7 @@
+# Copyright Rene Rivera 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)
+
+# For instructions see Jamfile.v2, or "bjam --help".

Added: sandbox/libs/reflection/test/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/test/Jamfile.v2 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,31 @@
+# Boost.Reflection - tests Jamfile
+#
+# Copyright 2007 Mariano G. Consoni
+# 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)
+#
+# See http://www.boost.org/ for latest version.
+#
+
+using testing ;
+import os ;
+local BOOST_ROOT = [ os.environ BOOST_ROOT ] ;
+project
+ : requirements
+ <library>$(BOOST_ROOT)libs/test/build//boost_unit_test_framework/<link>shared
+ <include>../../../
+ <include>$(BOOST_ROOT)
+ <define>BOOST_TEST_NO_AUTO_LINK=1
+ <toolset>gcc:<define>BOOST_TEST_DYN_LINK
+ <toolset>gcc:<find-static-library>boost_unit_test_framework
+ <toolset>gcc:<find-static-library>dl
+ <toolset>darwin:<define>DYLD_LIBRARY_PATH=../bin/
+ :
+ ;
+
+
+test-suite reflection_tests_all
+: [ run basic_test.cpp ]
+:
+;

Added: sandbox/libs/reflection/test/basic_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/test/basic_test.cpp 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,62 @@
+/*
+ * Boost.Reflection / basic unit test
+ *
+ * (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)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+
+#include <string>
+#include <iostream>
+
+#define BOOST_EXTENSION_USE_PP 1
+
+#define BOOST_TEST_MAIN
+#define BOOST_TEST_DYN_LINK 1
+#include <boost/test/unit_test.hpp>
+
+#include "../examples/car.hpp"
+#include <boost/reflection/reflection.hpp>
+
+
+BOOST_AUTO_TEST_CASE(basic_example)
+{
+
+ /* Here we declare that we are reflecting out a Car. The second
+ template parameter declares that we will describe this reflection
+ using a string. Any arbitrary type could be used here. */
+ boost::extension::reflection<car, std::string> car_reflection("A Car!");
+ std::string &s = car_reflection.get_info();
+ BOOST_CHECK_EQUAL( i, std::string("A Car!") );
+
+ /* Here we add two methods to the reflection. The library must
+ correctly parse and remember the parameters and return type of these
+ methods. Here, we elect to describe the first method using a number
+ and the second using a string. For the second method, the library
+ knows now that it is a function of Car called turn that takes a float
+ named "turn_angle". */
+ car_reflection.add<int, bool>(&car::start, 3);
+ car_reflection.add<std::string, bool, float,
+ std::string>(&car::turn, "turn", "turn_angle");
+
+ // create some instances to use our reflection
+ car porsche_911("Porsche 911");
+ car ferrari_f40("Ferrari F40");
+
+ // call methods
+ car_reflection.call<int, bool>(&porsche_911, 3);
+ car_reflection.call<std::string, bool,
+ float, std::string>(&porsche_911, "turn", .5f);
+
+ car_reflection.call<std::string, bool,
+ float, std::string>(&ferrari_f40, "turn", .10f);
+
+ // prepare the parameter map for a call
+ boost::extension::parameter_map pm;
+ pm.add<float, std::string>(.15f, "turn_angle");
+ pm.add<int, std::string>(50, "aceleration");
+}

Added: sandbox/libs/reflection/test/hello_world_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/reflection/test/hello_world_test.cpp 2007-08-10 14:30:56 EDT (Fri, 10 Aug 2007)
@@ -0,0 +1,60 @@
+/*
+ * Boost.Extension / hello world unit test
+ *
+ * (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)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#define BOOST_EXTENSION_USE_PP 1
+
+#include <boost/extension/factory_map.hpp>
+#include <boost/extension/factory.hpp>
+#include <boost/extension/shared_library.hpp>
+#define BOOST_TEST_MAIN
+#define BOOST_TEST_DYN_LINK 1
+#include <boost/test/unit_test.hpp>
+
+#include "../examples/word.hpp"
+
+
+using namespace boost::extensions;
+
+BOOST_AUTO_TEST_CASE(hello_world_example)
+{
+ // check if the library can be loaded
+ shared_library l((std::string("../bin/libHelloWorldLib") + ".extension").c_str());
+ BOOST_CHECK_EQUAL( l.open(), true );
+
+ // check if the factory can return the functor
+ factory_map fm;
+ functor<void, factory_map &> load_func =
+ l.get_functor<void, factory_map &>("extension_export_word");
+ BOOST_CHECK_EQUAL( load_func.is_valid(), true );
+
+ load_func(fm);
+
+ // check if we can get the word list
+ std::list<factory<word, int> > & factory_list = fm.get<word, int>();
+ BOOST_CHECK_EQUAL( factory_list.size(), 2U );
+
+ // iterate trough the classes and execute get_val method
+ // to obtain the correct words
+ std::list<factory<word, int> >::iterator current_word = factory_list.begin();
+
+ std::auto_ptr<word> hello_word_ptr(current_word->create());
+ BOOST_CHECK_EQUAL( !hello_word_ptr.get(), 0 );
+
+ BOOST_CHECK_EQUAL( hello_word_ptr->get_val(), "hello");
+
+ ++current_word;
+
+ std::auto_ptr<word> world_word_ptr(current_word->create());
+ BOOST_CHECK_EQUAL( !world_word_ptr.get(), 0 );
+
+ BOOST_CHECK_EQUAL( world_word_ptr->get_val(), "world!");
+}
+


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