mirror of
https://github.com/boostorg/convert.git
synced 2026-01-24 17:52:42 +00:00
95 lines
2.3 KiB
C++
95 lines
2.3 KiB
C++
// Copyright (c) 2009-2014 Vladimir Batov.
|
|
// Use, modification and distribution are subject to the Boost Software License,
|
|
// Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
|
|
|
|
//[stream_headers1
|
|
#include <boost/convert.hpp>
|
|
#include <boost/convert/stream.hpp>
|
|
#include <boost/detail/lightweight_test.hpp>
|
|
|
|
using std::string;
|
|
using boost::convert;
|
|
//]
|
|
//[stream_headers2
|
|
namespace cnv = boost::cnv;
|
|
namespace arg = boost::cnv::parameter;
|
|
//]
|
|
|
|
#include "../test/test.hpp"
|
|
|
|
static
|
|
void
|
|
example1()
|
|
{
|
|
//[stream_example1
|
|
boost::cnv::cstream cnv;
|
|
|
|
int i2 = convert<int>("123", cnv).value(); // Throws when fails.
|
|
int i3 = convert<int>("uhm", cnv).value_or(-1); // Returns -1 when fails.
|
|
string s2 = convert<string>(123, cnv).value();
|
|
|
|
BOOST_TEST(i2 == 123);
|
|
BOOST_TEST(i3 == -1);
|
|
BOOST_TEST(s2 == "123");
|
|
//]
|
|
}
|
|
|
|
static
|
|
void
|
|
example2()
|
|
{
|
|
//[stream_example2
|
|
boost::cnv::cstream ccnv;
|
|
boost::cnv::wstream wcnv;
|
|
|
|
int v01 = convert<int>(" FF", ccnv(std::hex)(std::skipws)).value_or(0);
|
|
int v02 = convert<int>(L" F", wcnv(std::hex)(std::skipws)).value_or(0);
|
|
int v03 = convert<int>(" FF", ccnv(std::dec)(std::skipws)).value_or(-5);
|
|
int v04 = convert<int>(L" F", wcnv(std::dec)(std::skipws)).value_or(-5);
|
|
|
|
BOOST_TEST(v01 == 255); // "FF"
|
|
BOOST_TEST(v02 == 15); // L"F"
|
|
BOOST_TEST(v03 == -5); // Failed to convert "FF" as decimal.
|
|
BOOST_TEST(v04 == -5); // Failed to convert L"F" as decimal.
|
|
//]
|
|
//[stream_example3
|
|
ccnv(std::showbase)(std::uppercase)(std::hex);
|
|
|
|
BOOST_TEST(convert<string>(255, ccnv).value() == "0XFF");
|
|
BOOST_TEST(convert<string>( 15, ccnv).value() == "0XF");
|
|
//]
|
|
//[stream_example4
|
|
ccnv(arg::base = cnv::base::dec)
|
|
(arg::uppercase = false)
|
|
(arg::notation = cnv::notation::scientific);
|
|
//]
|
|
//[stream_example5
|
|
ccnv(std::dec)(std::uppercase)(std::scientific);
|
|
//]
|
|
}
|
|
|
|
static
|
|
void
|
|
example6()
|
|
{
|
|
//[stream_example6
|
|
boost::cnv::cstream cnv;
|
|
|
|
change up = convert<change>("up", cnv).value();
|
|
string s1 = convert<string>(up, cnv).value();
|
|
string s2 = convert<string, change>(change::dn, cnv).value();
|
|
|
|
BOOST_TEST(up.value() == change::up);
|
|
BOOST_TEST(s1 == "up");
|
|
BOOST_TEST(s2 == "dn");
|
|
//]
|
|
}
|
|
|
|
int
|
|
main(int argc, char const* argv[])
|
|
{
|
|
example1();
|
|
example2();
|
|
example6();
|
|
}
|