File: fallbacks.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (79 lines) | stat: -rw-r--r-- 2,961 bytes parent folder | download | duplicates (5)
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
// Boost.Convert test and usage example
// Copyright (c) 2009-2020 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.

#include "./test.hpp"

#if !defined(BOOST_CONVERT_CXX14)
int main(int, char const* []) { return 0; }
#else

#include <boost/convert.hpp>
#include <boost/convert/stream.hpp>
#include <functional>

namespace { namespace local
{
    bool    called_functor_int;
    bool called_functor_double;
    bool    called_functor_foo;
    bool   called_function_int;
    bool  called_function_long;
}}

struct    functor_int { int    operator() () const { local::   called_functor_int = true; return INT_MAX; }};
struct functor_double { double operator() () const { local::called_functor_double = true; return INT_MAX; }};
struct    functor_foo { int       func (int) const { local::   called_functor_foo = true; return INT_MAX; }};

int   function_int () { local:: called_function_int = true; return INT_MAX; }
long function_long () { local::called_function_long = true; return INT_MAX; }

int
main(int, char const* [])
{
    auto cnv = boost::cnv::cstream();
    auto foo = functor_foo();

    int i01 = boost::convert<int>("uhm", cnv).value_or_eval(functor_int());
    int i02 = boost::convert<int>("uhm", cnv).value_or_eval(functor_double());
    int i03 = boost::convert<int>("uhm", cnv).value_or_eval(std::bind(&functor_foo::func, foo, 0));
    int i04 = boost::convert<int>("uhm", cnv).value_or_eval(function_int);
    int i05 = boost::convert<int>("uhm", cnv).value_or_eval(function_long);

    BOOST_TEST(local::   called_functor_int && i01 == INT_MAX);
    BOOST_TEST(local::called_functor_double && i02 == INT_MAX);
    BOOST_TEST(local::   called_functor_foo && i03 == INT_MAX);
    BOOST_TEST(local::  called_function_int && i04 == INT_MAX);
    BOOST_TEST(local:: called_function_long && i05 == INT_MAX);

    local::   called_functor_int = false;
    local::called_functor_double = false;
    local::   called_functor_foo = false;
    local::  called_function_int = false;
    local:: called_function_long = false;

    boost::convert<int>("uhm", cnv, functor_int());
    boost::convert<int>("uhm", cnv, functor_double());
    boost::convert<int>("uhm", cnv, std::bind(&functor_foo::func, foo, 0));
    boost::convert<int>("uhm", cnv, function_int);
    boost::convert<int>("uhm", cnv, function_long);

    BOOST_TEST(local::   called_functor_int && i01 == INT_MAX);
    BOOST_TEST(local::called_functor_double && i02 == INT_MAX);
    BOOST_TEST(local::   called_functor_foo && i03 == INT_MAX);
    BOOST_TEST(local::  called_function_int && i04 == INT_MAX);
    BOOST_TEST(local:: called_function_long && i05 == INT_MAX);

    try
    {
        boost::convert<int>("uhm", cnv, boost::throw_on_failure);
        BOOST_TEST(0);
    }
    catch (boost::bad_optional_access const&)
    {
    }
    return boost::report_errors();
}

#endif