mirror of
https://github.com/boostorg/bimap.git
synced 2026-01-19 04:02:10 +00:00
xpressive & typeof
[SVN r37892]
This commit is contained in:
@@ -34,6 +34,8 @@ test-suite "bimap_and_boost"
|
||||
[ run bimap_and_boost/foreach.cpp ]
|
||||
[ run bimap_and_boost/lambda.cpp ]
|
||||
[ run bimap_and_boost/assign.cpp ]
|
||||
[ run bimap_and_boost/xpressive.cpp ]
|
||||
[ run bimap_and_boost/typeof.cpp ]
|
||||
[ run bimap_and_boost/serialization.cpp
|
||||
/boost/serialization//boost_serialization ]
|
||||
;
|
||||
|
||||
86
example/bimap_and_boost/typeof.cpp
Executable file
86
example/bimap_and_boost/typeof.cpp
Executable file
@@ -0,0 +1,86 @@
|
||||
// Boost.Bimap
|
||||
//
|
||||
// Copyright (c) 2006-2007 Matias Capeletto
|
||||
//
|
||||
// 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)
|
||||
|
||||
// VC++ 8.0 warns on usage of certain Standard Library and API functions that
|
||||
// can be cause buffer overruns or other possible security issues if misused.
|
||||
// See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
|
||||
// But the wording of the warning is misleading and unsettling, there are no
|
||||
// portable alternative functions, and VC++ 8.0's own libraries use the
|
||||
// functions in question. So turn off the warnings.
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
#define _SCL_SECURE_NO_DEPRECATE
|
||||
|
||||
// Boost.Bimap Example
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <boost/bimap/bimap.hpp>
|
||||
|
||||
#include <boost/typeof/typeof.hpp>
|
||||
|
||||
using namespace boost::bimaps;
|
||||
|
||||
struct name {};
|
||||
struct number {};
|
||||
|
||||
void using_auto()
|
||||
{
|
||||
//[ code_bimap_and_boost_typeof_first
|
||||
|
||||
typedef bimap< tagged<std::string,name>, tagged<int,number> > bm_type;
|
||||
bm_type bm;
|
||||
bm.insert( bm_type::value_type("one" ,1) );
|
||||
bm.insert( bm_type::value_type("two" ,2) );
|
||||
//]
|
||||
|
||||
//[ code_bimap_and_boost_typeof_using_auto
|
||||
|
||||
for( BOOST_AUTO(iter, bm.by<name>().begin()); iter!=bm.by<name>().end(); ++iter)
|
||||
{
|
||||
std::cout << iter->first << " --> " << iter->second << std::endl;
|
||||
}
|
||||
|
||||
BOOST_AUTO( iter, bm.by<number>().find(2) );
|
||||
std::cout << "2: " << iter->get<name>();
|
||||
//]
|
||||
}
|
||||
|
||||
void not_using_auto()
|
||||
{
|
||||
typedef bimap< tagged<std::string,name>, tagged<int,number> > bm_type;
|
||||
bm_type bm;
|
||||
bm.insert( bm_type::value_type("one" ,1) );
|
||||
bm.insert( bm_type::value_type("two" ,2) );
|
||||
|
||||
//[ code_bimap_and_boost_typeof_not_using_auto
|
||||
|
||||
for( bm_type::map_by<name>::iterator iter = bm.by<name>().begin();
|
||||
iter!=bm.by<name>().end(); ++iter)
|
||||
{
|
||||
std::cout << iter->first << " --> " << iter->second << std::endl;
|
||||
}
|
||||
|
||||
bm_type::map_by<number>::iterator iter = bm.by<number>().find(2);
|
||||
std::cout << "2: " << iter->get<name>();
|
||||
//]
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
using_auto();
|
||||
not_using_auto();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
57
example/bimap_and_boost/xpressive.cpp
Executable file
57
example/bimap_and_boost/xpressive.cpp
Executable file
@@ -0,0 +1,57 @@
|
||||
// Boost.Bimap
|
||||
//
|
||||
// Copyright (c) 2006-2007 Matias Capeletto
|
||||
//
|
||||
// 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)
|
||||
|
||||
// VC++ 8.0 warns on usage of certain Standard Library and API functions that
|
||||
// can be cause buffer overruns or other possible security issues if misused.
|
||||
// See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
|
||||
// But the wording of the warning is misleading and unsettling, there are no
|
||||
// portable alternative functions, and VC++ 8.0's own libraries use the
|
||||
// functions in question. So turn off the warnings.
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
#define _SCL_SECURE_NO_DEPRECATE
|
||||
|
||||
// Boost.Bimap Example
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/bimap/bimap.hpp>
|
||||
|
||||
#include <boost/xpressive/xpressive.hpp>
|
||||
#include <boost/xpressive/regex_actions.hpp>
|
||||
|
||||
using namespace boost::bimaps;
|
||||
using namespace boost::xpressive;
|
||||
namespace xp = boost::xpressive;
|
||||
|
||||
int main()
|
||||
{
|
||||
//[ code_bimap_and_boost_xpressive
|
||||
|
||||
typedef bimap< std::string, int > bm_type;
|
||||
bm_type bm;
|
||||
|
||||
std::string rel_str("one <--> 1 two <--> 2 three <--> 3");
|
||||
|
||||
sregex rel = ( (s1= +_w) >> " <--> " >> (s2= +_d) )
|
||||
[
|
||||
xp::ref(bm)->*insert( construct<bm_type::value_type>(s1, as<int>(s2)) )
|
||||
];
|
||||
|
||||
sregex relations = rel >> *(+_s >> rel);
|
||||
|
||||
regex_match(rel_str, relations);
|
||||
|
||||
assert( bm.size() == 3 );
|
||||
//]
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -27,10 +27,56 @@
|
||||
#include <boost/bimap/unordered_set_of.hpp>
|
||||
#include <boost/bimap/multiset_of.hpp>
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/none.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/assign/list_inserter.hpp>
|
||||
|
||||
using namespace boost::bimaps;
|
||||
using namespace boost;
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
|
||||
typedef bimap<
|
||||
|
||||
string,
|
||||
multiset_of< optional<string> >
|
||||
|
||||
> bm_type;
|
||||
|
||||
bm_type bm;
|
||||
|
||||
assign::insert( bm )
|
||||
|
||||
( "John" , string("lazarus" ) )
|
||||
( "Peter", string("vinicius") )
|
||||
( "Simon", string("vinicius") )
|
||||
( "Brian", none )
|
||||
;
|
||||
|
||||
cout << "John is working in "
|
||||
<< bm.left.at( "John" ).get_value_or( "no project" )
|
||||
<< endl;
|
||||
|
||||
cout << "Project vinicius is being developed by " << endl;
|
||||
BOOST_FOREACH( bm_type::right_reference rp,
|
||||
bm.right.equal_range( std::string("vinicius") ) )
|
||||
{
|
||||
cout << rp.second << endl;
|
||||
}
|
||||
|
||||
cout << "This workers need a project " << endl;
|
||||
BOOST_FOREACH( bm_type::right_reference rp,
|
||||
bm.right.equal_range(none) )
|
||||
{
|
||||
cout << rp.second << endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//[ code_population_bimap
|
||||
|
||||
typedef bimap<
|
||||
|
||||
@@ -36,9 +36,9 @@ void tutorial_about_info_hook()
|
||||
typedef bimap<
|
||||
|
||||
multiset_of< std::string >, // author
|
||||
set_of< std::string >, // book name
|
||||
set_of< std::string >, // title
|
||||
|
||||
info_hook< std::string > // abstract
|
||||
with_info< std::string > // abstract
|
||||
|
||||
> bm_type;
|
||||
typedef bm_type::value_type book;
|
||||
@@ -54,7 +54,7 @@ void tutorial_about_info_hook()
|
||||
);
|
||||
|
||||
|
||||
// Print the abstract of the bible
|
||||
// Print the author of the bible
|
||||
std::cout << bm.right.at("The C++ Programming Language");
|
||||
|
||||
// Print the abstract of this book
|
||||
@@ -81,6 +81,51 @@ void tutorial_about_info_hook()
|
||||
//]
|
||||
}
|
||||
|
||||
struct author {};
|
||||
struct title {};
|
||||
struct abstract {};
|
||||
|
||||
void tutorial_about_tagged_info_hook()
|
||||
{
|
||||
//[ code_tutorial_info_hook_tagged_info
|
||||
|
||||
typedef bimap<
|
||||
|
||||
multiset_of< tagged< std::string, author > >,
|
||||
set_of< tagged< std::string, title > >,
|
||||
|
||||
with_info< tagged< std::string, abstract > >
|
||||
|
||||
> bm_type;
|
||||
typedef bm_type::value_type book;
|
||||
|
||||
bm_type bm;
|
||||
|
||||
bm.insert(
|
||||
|
||||
book( "Bjarne Stroustrup" , "The C++ Programming Language",
|
||||
|
||||
"For C++ old-timers, the first edition of this book is"
|
||||
"the one that started it all—the font of our knowledge." )
|
||||
);
|
||||
|
||||
// Print the author of the bible
|
||||
std::cout << bm.by<title>().at("The C++ Programming Language");
|
||||
|
||||
// Print the abstract of this book
|
||||
bm_type::map_by<author>::iterator i = bm.by<author>().find("Bjarne Stroustrup");
|
||||
std::cout << i->get<abstract>();
|
||||
|
||||
// Contrary to the two key types, the information will be mutable
|
||||
// using iterators.
|
||||
|
||||
i->get<abstract>() += "More details about this book";
|
||||
|
||||
// Print the new abstract
|
||||
std::cout << bm.by<title>().info_at("The C++ Programming Language");
|
||||
//]
|
||||
}
|
||||
|
||||
|
||||
void bimap_without_an_info_hook()
|
||||
{
|
||||
@@ -89,7 +134,7 @@ void bimap_without_an_info_hook()
|
||||
typedef bimap<
|
||||
|
||||
multiset_of< std::string >, // author
|
||||
set_of< std::string > // book name
|
||||
set_of< std::string > // title
|
||||
|
||||
> bm_type;
|
||||
typedef bm_type::value_type book;
|
||||
@@ -109,6 +154,7 @@ void bimap_without_an_info_hook()
|
||||
int main()
|
||||
{
|
||||
tutorial_about_info_hook();
|
||||
tutorial_about_tagged_info_hook();
|
||||
bimap_without_an_info_hook();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user