Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50299 - in sandbox/mirror: boost/mirror libs/mirror/example libs/mirror/example/factories
From: chochlik_at_[hidden]
Date: 2008-12-16 15:53:28


Author: matus.chochlik
Date: 2008-12-16 15:53:28 EST (Tue, 16 Dec 2008)
New Revision: 50299
URL: http://svn.boost.org/trac/boost/changeset/50299

Log:
[mirror 0.3.x]
- update of the factory/tetrahedon example
- added a new example showing how the generic function caller works

Added:
   sandbox/mirror/libs/mirror/example/factories/inserter.cpp (contents, props changed)
Text files modified:
   sandbox/mirror/boost/mirror/functor_call.hpp | 11 +++++++++++
   sandbox/mirror/libs/mirror/example/Jamfile.v2 | 1 +
   sandbox/mirror/libs/mirror/example/factories/input_ui.hpp | 37 +++++++++++++++++++++++++------------
   sandbox/mirror/libs/mirror/example/factories/tetrahedron.cpp | 1 -
   4 files changed, 37 insertions(+), 13 deletions(-)

Modified: sandbox/mirror/boost/mirror/functor_call.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/functor_call.hpp (original)
+++ sandbox/mirror/boost/mirror/functor_call.hpp 2008-12-16 15:53:28 EST (Tue, 16 Dec 2008)
@@ -56,6 +56,17 @@
                 param \
         ) { } \
  \
+ template <class Functor> \
+ void operator()(Functor func) \
+ { \
+ func( \
+ BOOST_PP_ENUM( \
+ PARAM_COUNT, \
+ BOOST_MIRROR_BASE_FUNCTION_CALLER_CALL_FACTORY, \
+ 0 \
+ ) \
+ ); \
+ } \
         template <class Functor, class Consumer> \
         Consumer& operator()(Functor func, Consumer& consumer) \
         { \

Modified: sandbox/mirror/libs/mirror/example/Jamfile.v2
==============================================================================
--- sandbox/mirror/libs/mirror/example/Jamfile.v2 (original)
+++ sandbox/mirror/libs/mirror/example/Jamfile.v2 2008-12-16 15:53:28 EST (Tue, 16 Dec 2008)
@@ -38,6 +38,7 @@
 # configurable factories
 #
 exe fctry_tetrahedron : factories/tetrahedron.cpp ;
+exe fn_call_inserter : factories/inserter.cpp ;
 #
 # cooperation with Boost.Serialization
 #

Modified: sandbox/mirror/libs/mirror/example/factories/input_ui.hpp
==============================================================================
--- sandbox/mirror/libs/mirror/example/factories/input_ui.hpp (original)
+++ sandbox/mirror/libs/mirror/example/factories/input_ui.hpp 2008-12-16 15:53:28 EST (Tue, 16 Dec 2008)
@@ -32,7 +32,7 @@
                 template <class Context, class ConstrIndex, class ParamIndex>
                 banner(int tabs, Context* pc, ConstrIndex ci, ParamIndex pi)
                 {
- ::std::cout <<
+ ::boost::cts::bcout() <<
                                 ::boost::cts::bstring(tabs, BOOST_CTS_LIT('\t')) <<
                                 BOOST_CTS_LIT("Construct ") <<
                                 BOOST_MIRRORED_TYPE(Product)::full_name() <<
@@ -58,37 +58,50 @@
         }
 };
 
-/** Specialization of the input interface, used to produce
- * doubles by prompting the user to enter a value on the
+/** Implementation of the input interface, used to produce
+ * values by prompting the user to enter a value on the
  * console.
  */
-template <>
-struct input_ui<double>
+template <class Product>
+struct console_input_ui
 {
- double x;
+ Product x;
 
         template <class Context, class ConstrIndex, class ParamIndex>
- input_ui(int tabs, Context* pc, ConstrIndex ci, ParamIndex pi)
+ console_input_ui(int tabs, Context* pc, ConstrIndex ci, ParamIndex pi)
         {
- ::std::cout <<
+ ::boost::cts::bcout() <<
                         ::boost::cts::bstring(tabs, BOOST_CTS_LIT('\t')) <<
                         BOOST_CTS_LIT("Enter ") <<
- BOOST_MIRRORED_TYPE(double)::full_name() <<
+ BOOST_MIRRORED_TYPE(Product)::full_name() <<
                         BOOST_CTS_LIT(" ") <<
                         boost::mirror::meta_constructors<
                                 Context
>::base_param_name(ci, pi) <<
                         BOOST_CTS_LIT(" = ") <<
                         ::std::flush;
- ::std::cin >> x;
+ ::boost::cts::bcin() >> x;
         }
 
- inline double operator()(void)
+ inline Product operator()(void)
         {
                 return x;
         }
 };
 
+#define BOOST_MIRROR_EXAMPLES_SPECIALIZE_CONSOLE_INPUT_UI(TYPE) \
+template <> \
+struct input_ui< TYPE > : console_input_ui< TYPE > \
+{ \
+ template <class Context, class ConstrIndex, class ParamIndex> \
+ input_ui(int tabs, Context* pc, ConstrIndex ci, ParamIndex pi) \
+ : console_input_ui< TYPE >(tabs, pc, ci, pi) \
+ { } \
+};
+
+BOOST_MIRROR_EXAMPLES_SPECIALIZE_CONSOLE_INPUT_UI(double)
+BOOST_MIRROR_EXAMPLES_SPECIALIZE_CONSOLE_INPUT_UI(::std::string)
+
 /** A manager of this input user interface, which picks the
  * constructor that will be used by the means of the result
  * of the index member function (i.e. the zero-th registered
@@ -106,7 +119,7 @@
         input_ui(const ::boost::cts::bchar* names[], int factory_index)
          : tabs(0)
         {
- ::std::cout <<
+ ::boost::cts::bcout() <<
                         BOOST_CTS_LIT("Create ") <<
                         names[factory_index] << ::std::endl;
         }

Added: sandbox/mirror/libs/mirror/example/factories/inserter.cpp
==============================================================================
--- (empty file)
+++ sandbox/mirror/libs/mirror/example/factories/inserter.cpp 2008-12-16 15:53:28 EST (Tue, 16 Dec 2008)
@@ -0,0 +1,196 @@
+/**
+ * \file examples/factories/inserter.cpp
+ *
+ * This example shows how to use the generic function caller.
+ *
+ * Copyright 2008 Matus Chochlik. 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 <math.h>
+#include <list>
+#include <algorithm>
+
+#include <boost/char_type_switch/iostream.hpp>
+#include <boost/mirror/functor_call.hpp>
+#include <boost/mirror/meta_class.hpp>
+#include <boost/mirror/algorithm/for_each.hpp>
+
+#include "./input_ui.hpp"
+
+namespace test {
+
+struct person
+{
+ ::std::string first_name;
+ ::std::string family_name;
+ ::std::string street;
+ ::std::string number;
+ ::std::string city;
+ ::std::string postal_code;
+
+ person(
+ const ::std::string& _first_name,
+ const ::std::string& _family_name,
+ const ::std::string& _street,
+ const ::std::string& _number,
+ const ::std::string& _city,
+ const ::std::string& _postal_code
+ ): first_name(_first_name)
+ , family_name(_family_name)
+ , street(_street)
+ , number(_number)
+ , city(_city)
+ , postal_code(_postal_code)
+ { }
+};
+
+} // namespace test
+
+
+namespace boost {
+namespace mirror {
+
+// register the ::test namespace
+BOOST_MIRROR_REG_NAMESPACE( (test) )
+
+// register the person type
+BOOST_MIRROR_REG_TYPE( ::test, person)
+
+// register the constructors of ::test::person
+BOOST_MIRROR_REG_CONSTRUCTORS_BEGIN( ::test::person )
+ BOOST_MIRROR_REG_CONSTRUCTOR(0,
+ ((::std::string)(first_name))
+ ((::std::string)(family_name))
+ ((::std::string)(street))
+ ((::std::string)(number))
+ ((::std::string)(city))
+ ((::std::string)(postal_code))
+ )
+BOOST_MIRROR_REG_CONSTRUCTORS_END
+
+// register the attributes of person
+BOOST_MIRROR_REG_CLASS_ATTRIBS_BEGIN(::test::person)
+ BOOST_MIRROR_REG_AUTO_CLASS_ATTRIB(_, first_name)
+ BOOST_MIRROR_REG_AUTO_CLASS_ATTRIB(_, family_name)
+ BOOST_MIRROR_REG_AUTO_CLASS_ATTRIB(_, street)
+ BOOST_MIRROR_REG_AUTO_CLASS_ATTRIB(_, number)
+ BOOST_MIRROR_REG_AUTO_CLASS_ATTRIB(_, city)
+ BOOST_MIRROR_REG_AUTO_CLASS_ATTRIB(_, postal_code)
+BOOST_MIRROR_REG_CLASS_ATTRIBS_END
+
+
+} // namespace mirror
+} // namespace boost
+
+/** Prints the types, names and values of
+ * class' attribute
+ */
+template <class Class>
+struct attrib_printer
+{
+ const Class& inst;
+
+ attrib_printer(const Class& _inst)
+ : inst(_inst)
+ { }
+
+ template <class MetaAttribute>
+ void operator()(MetaAttribute ma) const
+ {
+ ::boost::cts::bcout() <<
+ BOOST_CTS_LIT("\t") <<
+ MetaAttribute::type::base_name() <<
+ BOOST_CTS_LIT(" ") <<
+ MetaAttribute::base_name() <<
+ BOOST_CTS_LIT(": '") <<
+ MetaAttribute::get(inst) <<
+ BOOST_CTS_LIT("'") <<
+ ::std::endl;
+ }
+};
+
+/** Prints the type name of a class and
+ * it's attributes
+ */
+struct class_printer
+{
+ template <class Class>
+ void operator()(const Class& inst)
+ {
+ ::boost::cts::bcout() <<
+ BOOST_MIRRORED_CLASS(Class)::base_name() <<
+ ::std::endl;
+
+ ::boost::mirror::for_each<
+ typename BOOST_MIRRORED_CLASS(Class)::all_attributes
+ >(attrib_printer<Class>(inst));
+ }
+};
+
+
+/** A functor with one parameter. This functor is called
+ * by the mirror::function_caller, which also constructs
+ * and supplies all the necessary arguments (the single
+ * one in this case).
+ */
+template <class Class>
+struct back_inserter
+{
+ ::std::list< Class >& container;
+
+ back_inserter(::std::list< Class >& _container)
+ : container(_container)
+ { }
+
+ void operator()(const Class& inst)
+ {
+ container.push_back(inst);
+ }
+};
+
+
+int main(void)
+{
+ using namespace ::boost;
+ using namespace ::boost::mirror;
+ using namespace ::test;
+ //
+ // a list of persons
+ ::std::list< ::test::person > persons;
+ //
+ // keep inserting while insert_more == 'y'
+ char insert_more = 'y';
+ while(insert_more == 'y')
+ {
+ // the name of the function argument.
+ // there is only one in this case
+ const cts::bchar* param_names[] = {BOOST_CTS_LIT("Person")};
+ // create a functor caller, plug the input ui in
+ // and supply the parameter types and parameter names
+ functor_caller<
+ input_ui,
+ mpl::vector1< person>
+ > caller(param_names);
+ // call the back inserter by the means of the caller
+ caller(back_inserter<person>(persons));
+ // check whether to insert more persons
+ do
+ {
+ cts::bcout() << "Insert more ? (y/n) " << ::std::flush;
+ cts::bcin() >> insert_more;
+ } while(insert_more != 'y' && insert_more != 'n');
+ }
+ //
+ // print out all the persons in the list
+ cts::bcout() << "Persons:" << ::std::endl;
+ ::std::for_each(
+ persons.begin(),
+ persons.end(),
+ class_printer()
+ );
+ //
+ return 0;
+}
+

Modified: sandbox/mirror/libs/mirror/example/factories/tetrahedron.cpp
==============================================================================
--- sandbox/mirror/libs/mirror/example/factories/tetrahedron.cpp (original)
+++ sandbox/mirror/libs/mirror/example/factories/tetrahedron.cpp 2008-12-16 15:53:28 EST (Tue, 16 Dec 2008)
@@ -11,7 +11,6 @@
  */
 
 #include <math.h>
-#include <cstdlib>
 
 #include <boost/char_type_switch/iostream.hpp>
 #include <boost/mirror/factory.hpp>


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