Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r79690 - in sandbox/type_erasure/libs/type_erasure: doc example
From: steven_at_[hidden]
Date: 2012-07-22 23:18:08


Author: steven_watanabe
Date: 2012-07-22 23:18:07 EDT (Sun, 22 Jul 2012)
New Revision: 79690
URL: http://svn.boost.org/trac/boost/changeset/79690

Log:
Add an example to the introduction.
Added:
   sandbox/type_erasure/libs/type_erasure/example/intro.cpp (contents, props changed)
Text files modified:
   sandbox/type_erasure/libs/type_erasure/doc/type_erasure.qbk | 3 +++
   sandbox/type_erasure/libs/type_erasure/example/Jamfile.jam | 1 +
   2 files changed, 4 insertions(+), 0 deletions(-)

Modified: sandbox/type_erasure/libs/type_erasure/doc/type_erasure.qbk
==============================================================================
--- sandbox/type_erasure/libs/type_erasure/doc/type_erasure.qbk (original)
+++ sandbox/type_erasure/libs/type_erasure/doc/type_erasure.qbk 2012-07-22 23:18:07 EDT (Sun, 22 Jul 2012)
@@ -88,6 +88,9 @@
 
 [note TypeErasure is not an official Boost library.]
 
+[import ../example/intro.cpp]
+[intro]
+
 [endsect]
 
 [section:reading How to read this documentation]

Modified: sandbox/type_erasure/libs/type_erasure/example/Jamfile.jam
==============================================================================
--- sandbox/type_erasure/libs/type_erasure/example/Jamfile.jam (original)
+++ sandbox/type_erasure/libs/type_erasure/example/Jamfile.jam 2012-07-22 23:18:07 EDT (Sun, 22 Jul 2012)
@@ -17,5 +17,6 @@
 compile compose.cpp ;
 compile overload.cpp ;
 compile associated.cpp ;
+compile intro.cpp ;
 
 run print_sequence.cpp ;

Added: sandbox/type_erasure/libs/type_erasure/example/intro.cpp
==============================================================================
--- (empty file)
+++ sandbox/type_erasure/libs/type_erasure/example/intro.cpp 2012-07-22 23:18:07 EDT (Sun, 22 Jul 2012)
@@ -0,0 +1,96 @@
+// Boost.TypeErasure library
+//
+// Copyright 2011 Steven Watanabe
+//
+// 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)
+//
+// $Id$
+
+#include <boost/type_erasure/builtin.hpp>
+#include <boost/type_erasure/operators.hpp>
+#include <boost/type_erasure/iterator.hpp>
+#include <boost/type_erasure/callable.hpp>
+#include <boost/type_erasure/any.hpp>
+#include <boost/type_erasure/relaxed_match.hpp>
+#include <boost/mpl/vector.hpp>
+#include <iostream>
+
+namespace mpl = boost::mpl;
+using namespace boost::type_erasure;
+
+//[intro
+/*`
+ Here is a simple example using the library to implement
+ a type safe printf.
+ */
+typedef any<
+ mpl::vector<
+ copy_constructible<>,
+ ostreamable<>
+ >
+> any_printable;
+
+typedef std::vector<any_printable> print_storage;
+
+void print_impl(const char * format, const print_storage& args) {
+ int idx = 0;
+ while(char ch = *format++) {
+ if (ch == '%') {
+ switch(*format++) {
+ case '%': std::cout << '%'; break;
+ case 'd': std::cout << std::dec << args.at(idx++); break;
+ case 'x': std::cout << std::hex << args.at(idx++); break;
+ case '\0': return;
+ }
+ } else {
+ std::cout << ch;
+ }
+ }
+}
+
+template<class... T>
+void print(const char * format, const T&... t)
+{
+ print_storage args = { any_printable(t)... };
+ print_impl(format, args);
+}
+/*`
+ The top level function just stores all the arguments
+ in a vector and forwards to the real implementation.
+ `any_printable<>`, as its name suggests, is like
+ Boost.Any, but also supports streaming to `std::cout`.
+ */
+/*`
+ Boost.TypeErasure generalizes a technique used by
+ several other Boost libraries.
+ */
+typedef any<
+ mpl::vector<
+ copy_constructible<>,
+ typeid_<>,
+ relaxed_match
+ >
+> boost_any; // equivalent to boost::any
+
+typedef any<
+ mpl::vector<
+ copy_constructible<>,
+ callable<void(int)>,
+ typeid_<>,
+ relaxed_match
+ >
+> boost_function; // equivalent to boost::function<void(int)>
+
+typedef any<
+ mpl::vector<
+ forward_iterator<>,
+ same_type<forward_iterator<>::value_type, int>,
+ relaxed_match
+ >
+> any_iterator;
+// equivalent to
+// boost::any_range<int, boost::forward_traversal_tag,
+// int&, std::ptrdiff_t>::iterator
+//]


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