Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73433 - sandbox/coerce/libs/coerce/example
From: vexocide_at_[hidden]
Date: 2011-07-29 13:55:21


Author: vexocide
Date: 2011-07-29 13:55:20 EDT (Fri, 29 Jul 2011)
New Revision: 73433
URL: http://svn.boost.org/trac/boost/changeset/73433

Log:
Added examples
Added:
   sandbox/coerce/libs/coerce/example/grammar.cpp (contents, props changed)
   sandbox/coerce/libs/coerce/example/interface.cpp (contents, props changed)
   sandbox/coerce/libs/coerce/example/rule.cpp (contents, props changed)
   sandbox/coerce/libs/coerce/example/tag.cpp (contents, props changed)
Text files modified:
   sandbox/coerce/libs/coerce/example/Jamfile.v2 | 12 ++++++++++++
   1 files changed, 12 insertions(+), 0 deletions(-)

Modified: sandbox/coerce/libs/coerce/example/Jamfile.v2
==============================================================================
--- sandbox/coerce/libs/coerce/example/Jamfile.v2 (original)
+++ sandbox/coerce/libs/coerce/example/Jamfile.v2 2011-07-29 13:55:20 EDT (Fri, 29 Jul 2011)
@@ -16,5 +16,17 @@
 exe coerce :
     coerce.cpp ;
 
+exe grammar :
+ grammar.cpp ;
+
+exe interface :
+ interface.cpp ;
+
 exe optional :
     optional.cpp ;
+
+exe "rule" :
+ rule.cpp ;
+
+exe tag :
+ tag.cpp ;

Added: sandbox/coerce/libs/coerce/example/grammar.cpp
==============================================================================
--- (empty file)
+++ sandbox/coerce/libs/coerce/example/grammar.cpp 2011-07-29 13:55:20 EDT (Fri, 29 Jul 2011)
@@ -0,0 +1,77 @@
+// Copyright Jeroen Habraken 2011.
+//
+// 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)
+
+#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
+
+#include <boost/coerce.hpp>
+#include <boost/spirit/include/karma_grammar.hpp>
+#include <boost/spirit/include/karma_uint.hpp>
+#include <boost/spirit/include/karma_sequence.hpp>
+#include <boost/spirit/include/karma_string.hpp>
+#include <boost/spirit/include/qi_grammar.hpp>
+#include <boost/spirit/include/qi_lit.hpp>
+#include <boost/spirit/include/qi_uint.hpp>
+#include <boost/spirit/include/qi_sequence.hpp>
+
+#include <iostream>
+#include <string>
+
+namespace tag {
+ using namespace boost;
+
+ struct hexadecimal {
+ template <typename Iterator, typename Target, typename Source>
+ struct parser
+ : spirit::qi::grammar<Iterator, Target()> {
+ parser(tag::hexadecimal const &)
+ : parser::base_type(start) {
+ start = spirit::qi::lit_type()("0x")
+ >> spirit::qi::hex_type();
+ }
+
+ spirit::qi::rule<Iterator, Target()> start;
+ };
+
+ template <typename Iterator, typename Target, typename Source>
+ struct generator
+ : spirit::karma::grammar<Iterator, Source()> {
+ generator(tag::hexadecimal const &)
+ : generator::base_type(start) {
+ start = spirit::karma::lit_type()("0x")
+ << spirit::karma::hex_type();
+ }
+
+ spirit::karma::rule<Iterator, Source()> start;
+ };
+ };
+
+} // namespace tag
+
+namespace boost { namespace coerce { namespace traits {
+
+ template <typename T>
+ struct reserve_size<T, ::tag::hexadecimal> {
+ typedef std::size_t type;
+
+ static inline type
+ call(T const &, ::tag::hexadecimal const &) {
+ return 2 + 8;
+ }
+ };
+
+} } } // namespace boost::coerce::traits
+
+int
+main() {
+ using namespace boost;
+
+ // A coerce from hexadecimal string to unsigned integer ..
+ std::cout
+ << coerce::as<unsigned int>("0x23", tag::hexadecimal()) << std::endl;
+
+ // .. and vice-versa
+ std::cout << coerce::as<std::string>(23u, tag::hexadecimal()) << std::endl;
+}

Added: sandbox/coerce/libs/coerce/example/interface.cpp
==============================================================================
--- (empty file)
+++ sandbox/coerce/libs/coerce/example/interface.cpp 2011-07-29 13:55:20 EDT (Fri, 29 Jul 2011)
@@ -0,0 +1,43 @@
+// Copyright Jeroen Habraken 2011.
+//
+// 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)
+
+#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
+
+#include <boost/coerce.hpp>
+
+#include <iostream>
+#include <string>
+#include <utility>
+
+template <typename Target, typename Source, typename Tag>
+inline std::pair<Target, bool>
+as_pair(Source const & source, Tag const & tag) {
+ Target target;
+
+ bool result = boost::coerce::traits::as<
+ Target, Source, Tag
+ >::template call<Target, Source, Tag>(
+ target, source, tag);
+
+ return std::make_pair(target, result);
+}
+
+template <typename Target, typename Source>
+inline std::pair<Target, bool>
+as_pair(Source const & source) {
+ return as_pair<Target, Source, boost::coerce::tag::none>(
+ source, boost::coerce::tag::none());
+}
+
+int
+main() {
+ // A coerce from string to integer
+ std::pair<int, bool> result = as_pair<int>("23");
+
+ if (result.second) {
+ std::cout << result.first << std::endl;
+ }
+}

Added: sandbox/coerce/libs/coerce/example/rule.cpp
==============================================================================
--- (empty file)
+++ sandbox/coerce/libs/coerce/example/rule.cpp 2011-07-29 13:55:20 EDT (Fri, 29 Jul 2011)
@@ -0,0 +1,73 @@
+// Copyright Jeroen Habraken 2011.
+//
+// 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)
+
+#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
+
+#include <boost/coerce.hpp>
+#include <boost/spirit/include/karma_uint.hpp>
+#include <boost/spirit/include/karma_sequence.hpp>
+#include <boost/spirit/include/karma_string.hpp>
+#include <boost/spirit/include/karma_rule.hpp>
+#include <boost/spirit/include/qi_lit.hpp>
+#include <boost/spirit/include/qi_uint.hpp>
+#include <boost/spirit/include/qi_sequence.hpp>
+#include <boost/spirit/include/qi_rule.hpp>
+
+#include <iostream>
+#include <string>
+
+namespace tag {
+ using namespace boost;
+
+ struct hexadecimal {
+ template <typename Iterator, typename Target, typename Source>
+ struct parser
+ : spirit::qi::rule<Iterator, Target()> {
+ parser(tag::hexadecimal const &)
+ : spirit::qi::rule<Iterator, Target()>(
+ spirit::qi::lit_type()("0x")
+ >> spirit::qi::hex_type()
+ ) { }
+ };
+
+ template <typename Iterator, typename Target, typename Source>
+ struct generator
+ : spirit::karma::rule<Iterator, Source()> {
+ generator(tag::hexadecimal const &)
+ : spirit::karma::rule<Iterator, Source()>(
+ spirit::karma::lit_type()("0x")
+ << spirit::karma::hex_type()
+ ) { }
+ };
+ };
+
+} // namespace tag
+
+namespace boost { namespace coerce { namespace traits {
+
+ template <typename T>
+ struct reserve_size<T, ::tag::hexadecimal> {
+ typedef std::size_t type;
+
+ static inline type
+ call(T const &, ::tag::hexadecimal const &) {
+ return 2 + 8;
+ }
+ };
+
+} } } // namespace boost::coerce::traits
+
+int
+main() {
+ using namespace boost;
+
+ // A coerce from hexadecimal string to unsigned integer ..
+ std::cout
+ << coerce::as<unsigned int>("0x23", tag::hexadecimal()) << std::endl;
+
+ // .. and vice-versa
+ std::cout << coerce::as<std::string>(23u, tag::hexadecimal()) << std::endl;
+}

Added: sandbox/coerce/libs/coerce/example/tag.cpp
==============================================================================
--- (empty file)
+++ sandbox/coerce/libs/coerce/example/tag.cpp 2011-07-29 13:55:20 EDT (Fri, 29 Jul 2011)
@@ -0,0 +1,25 @@
+// Copyright Jeroen Habraken 2011.
+//
+// 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)
+
+#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
+
+#include <boost/coerce.hpp>
+
+#include <iostream>
+#include <string>
+
+int
+main() {
+ using namespace boost;
+
+ // A coerce from hexadecimal string to integer -- "23", "0x23" as well as "0X23" are accepted ..
+ std::cout << coerce::as<int>("0x23", coerce::tag::hex()) << std::endl;
+
+ // .. and vice-versa
+ std::cout
+ << "0x"
+ << coerce::as<std::string>(23, coerce::tag::hex()) << std::endl;
+}


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