1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// (C) Copyright Gennadiy Rozental 2001-2006.
// 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/libs/test for the library home page.
// Boost.Runtime.Param
#include <boost/test/utils/runtime/cla/named_parameter.hpp>
#include <boost/test/utils/runtime/cla/parser.hpp>
namespace rt = boost::runtime;
namespace cla = boost::runtime::cla;
// STL
#include <iostream>
#include <iterator>
//_____________________________________________________________________//
struct Point : std::pair<int,int> {
bool parse( rt::cstring& in ) {
in.trim_left();
if( first_char( in ) != '(' )
return false;
in.trim_left( 1 );
rt::cstring::size_type pos = in.find( ")" );
if( pos == rt::cstring::npos )
return false;
rt::cstring ss( in.begin(), pos );
pos = ss.find( "," );
if( pos == rt::cstring::npos )
return false;
rt::cstring f( ss.begin(), pos );
rt::cstring s( ss.begin()+pos+1, ss.end() );
f.trim();
s.trim();
try {
first = boost::lexical_cast<int>( f );
second = boost::lexical_cast<int>( s );
}
catch( boost::bad_lexical_cast const& ) {
return false;
}
in.trim_left( ss.end()+1 );
return true;
}
};
std::ostream& operator<<( std::ostream& ostr, Point const& p )
{
ostr << '(' << p.first << ',' << p.second << ')';
return ostr;
}
struct Segment : std::pair<Point,Point> {
bool parse( rt::cstring& in ) {
in.trim_left();
if( first_char( in ) != '[' )
return false;
in.trim_left( 1 );
if( !first.parse( in ) )
return false;
in.trim_left();
if( first_char( in ) != ',' )
return false;
in.trim_left( 1 );
if( !second.parse( in ) )
return false;
in.trim_left();
if( first_char( in ) != ']' )
return false;
in.trim_left( 1 );
return true;
}
};
std::ostream& operator<<( std::ostream& ostr, Segment const& p )
{
ostr << '[' << p.first << ',' << p.second << ']';
return ostr;
}
struct Circle : std::pair<Point,int> {
bool parse( rt::cstring& in ) {
in.trim_left();
if( first_char( in ) != '[' )
return false;
in.trim_left( 1 );
if( !first.parse( in ) )
return false;
in.trim_left();
if( first_char( in ) != ',' )
return false;
in.trim_left( 1 );
rt::cstring::size_type pos = in.find( "]" );
if( pos == rt::cstring::npos )
return false;
rt::cstring ss( in.begin(), pos );
ss.trim();
try {
second = boost::lexical_cast<int>( ss );
}
catch( boost::bad_lexical_cast const& ) {
return false;
}
in.trim_left( pos+1 );
return true;
}
};
std::ostream& operator<<( std::ostream& ostr, Circle const& p )
{
ostr << '[' << p.first << ',' << p.second << ']';
return ostr;
}
//_____________________________________________________________________//
template<typename T>
class ShapeIdPolicy : public cla::identification_policy {
rt::cstring m_name;
rt::cstring m_usage_str;
public:
explicit ShapeIdPolicy( rt::cstring name )
: cla::identification_policy( boost::rtti::type_id<ShapeIdPolicy<T> >() )
, m_name( name ) {}
virtual bool responds_to( rt::cstring name ) const { return m_name == name; }
virtual bool conflict_with( cla::identification_policy const& ) const { return false; }
virtual rt::cstring id_2_report() const { return m_name; }
virtual void usage_info( rt::format_stream& fs ) const { fs << m_name; }
virtual bool matching( cla::parameter const& p, cla::argv_traverser& tr, bool primary ) const
{
T s;
rt::cstring in = tr.input();
return s.parse( in );
}
};
//_____________________________________________________________________//
template<typename T>
class ShapeArgumentFactory : public cla::argument_factory {
rt::cstring m_usage_str;
public:
explicit ShapeArgumentFactory( rt::cstring usage ) : m_usage_str( usage ) {}
// Argument factory interface
virtual rt::argument_ptr produce_using( cla::parameter& p, cla::argv_traverser& tr )
{
T s;
rt::cstring in = tr.input();
s.parse( in );
tr.trim( in.begin() - tr.input().begin() );
if( !p.actual_argument() ) {
rt::argument_ptr res;
rt::typed_argument<std::list<T> >* new_arg = new rt::typed_argument<std::list<T> >( p );
new_arg->p_value.value.push_back( s );
res.reset( new_arg );
return res;
}
else {
std::list<T>& arg_values = rt::arg_value<std::list<T> >( *p.actual_argument() );
arg_values.push_back( s );
return p.actual_argument();
}
}
virtual rt::argument_ptr produce_using( cla::parameter& p, cla::parser const& ) { return rt::argument_ptr(); }
virtual void argument_usage_info( rt::format_stream& fs ) { fs << m_usage_str; }
};
//_____________________________________________________________________//
struct SegmentParam : cla::parameter {
SegmentParam()
: cla::parameter( m_id_policy, m_arg_factory )
, m_id_policy( "segment" )
, m_arg_factory( ":((P1x,P1y), (P2x,P2y)) ... ((P1x,P1y), (P2x,P2y))" )
{}
ShapeIdPolicy<Segment> m_id_policy;
ShapeArgumentFactory<Segment> m_arg_factory;
};
inline boost::shared_ptr<SegmentParam>
segment_param() { return boost::shared_ptr<SegmentParam>( new SegmentParam ); }
//_____________________________________________________________________//
struct CircleParam : cla::parameter {
CircleParam()
: cla::parameter( m_id_policy, m_arg_factory )
, m_id_policy( "circle" )
, m_arg_factory( ":((P1x,P1y), R) ... ((P1x,P1y), R)" )
{}
ShapeIdPolicy<Circle> m_id_policy;
ShapeArgumentFactory<Circle> m_arg_factory;
};
inline boost::shared_ptr<CircleParam>
circle_param() { return boost::shared_ptr<CircleParam>( new CircleParam ); }
//_____________________________________________________________________//
int main() {
char* argv[] = { "basic", "[(1,", "1)", ",", "(7,", "-1", ")]", "[(", "1,1)", ",7", "]", "[(3,", "1", ")", ",", "2]",
"[", "(2,7", "),", "(5", ",1", ")]" };
int argc = sizeof(argv)/sizeof(char*);
try {
cla::parser P;
P << circle_param() - cla::optional
<< segment_param() - cla::optional;
P.parse( argc, argv );
boost::optional<std::list<Segment> > segments;
boost::optional<std::list<Circle> > circles;
P.get( "segment", segments );
if( segments ) {
std::cout << "segments : ";
std::copy( segments->begin(), segments->end(), std::ostream_iterator<Segment>( std::cout, "; " ) );
std::cout << std::endl;
}
P.get( "circle", circles );
if( circles ) {
std::cout << "circles : ";
std::copy( circles->begin(), circles->end(), std::ostream_iterator<Circle>( std::cout, "; " ) );
std::cout << std::endl;
}
}
catch( rt::logic_error const& ex ) {
std::cout << "Logic error: " << ex.msg() << std::endl;
return -1;
}
return 0;
}
// EOF
|