File: value_from.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (696 lines) | stat: -rw-r--r-- 17,420 bytes parent folder | download | duplicates (3)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
//
// 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)
//
// Official repository: https://github.com/boostorg/json
//

// Test that header file is self-contained.
#include <boost/json/value_from.hpp>

#include <boost/json/value.hpp> // prevent intellisense bugs
#include <boost/json/serialize.hpp>
#include <boost/core/detail/static_assert.hpp>
#include <boost/core/ignore_unused.hpp>
#include <boost/describe/class.hpp>
#include <boost/describe/enum.hpp>

#include "test_suite.hpp"

#include <array>
#include <string>
#include <vector>
#include <tuple>
#include <map>
#include <unordered_map>

#ifndef BOOST_NO_CXX17_HDR_FILESYSTEM
# include <filesystem>
#endif // BOOST_NO_CXX17_HDR_FILESYSTEM

//----------------------------------------------------------

namespace value_from_test_ns
{

//----------------------------------------------------------

struct T1
{
    int i = 42;
};

void
tag_invoke(
    ::boost::json::value_from_tag,
    ::boost::json::value& jv,
    T1 const& t)
{
    jv = t.i;
}

//----------------------------------------------------------

// member function
// uses generic algorithms
struct T2
{
    std::vector<int> v;
    std::string s;

    T2()
        : v({1,2,3, 4})
        , s("test")
    {
    }
};

void
tag_invoke(
    ::boost::json::value_from_tag,
    ::boost::json::value& jv,
    T2 const& t)
{
    jv = { boost::json::value_from(t.v), boost::json::value_from(t.s) };
}

struct T3
{
};

BOOST_CORE_STATIC_ASSERT( ! ::boost::json::has_value_from<T3>::value );

struct T4
{
    operator ::boost::json::value()
    {
        return nullptr;
    }
};

BOOST_CORE_STATIC_ASSERT( ! ::boost::json::has_value_from<T4>::value );

//----------------------------------------------------------

struct T5 : std::vector<int>
{
    using std::vector<int>::vector;
};

void
tag_invoke(
    ::boost::json::value_from_tag,
    ::boost::json::value& jv,
    T5 const&)
{
    jv = "T5";
}


//----------------------------------------------------------

struct T6
{
};

inline
std::size_t
size(T6 const&)
{
    return 3;
}

struct T7 { };

//----------------------------------------------------------

struct T8
{
    bool error;
};

void
tag_invoke(
    ::boost::json::value_from_tag,
    ::boost::json::value& jv,
    ::boost::system::error_code& ec,
    T8 const& t8)
{
    if( t8.error )
    {
        ec = ::boost::json::error::syntax;
        return;
    }

    jv = "T8";
}

//----------------------------------------------------------

struct T9
{
    int num;
};

void
tag_invoke(
    ::boost::json::value_from_tag,
    ::boost::json::value& jv,
    T9 const& t9)
{
    if( t9.num == 0 )
        throw std::invalid_argument("");
    if( t9.num < 0 )
        throw ::boost::system::system_error(
            make_error_code(::boost::json::error::syntax));

    jv = "T9";
}

//----------------------------------------------------------

struct T10
{
    int n;
    double d;
};
BOOST_DESCRIBE_STRUCT(T10, (), (n, d))

//----------------------------------------------------------

struct T11 : T10
{
    std::string s;

    void
    set_n(int v)
    {
        n = v;
    }

    void
    set_d(double v)
    {
        d = v;
    }

    void
    set_b(bool v)
    {
        b = v;
    }

private:
    bool b;

    BOOST_DESCRIBE_CLASS(T11, (T10), (s), (), (b))
};

struct Base1
{
    int a = 1;
};
BOOST_DESCRIBE_STRUCT(Base1, (), (a))

struct Base2
{
    int a = 2;
};
BOOST_DESCRIBE_STRUCT(Base2, (), (a))

struct Derived: Base1, Base2
{
    int a = 3;
};
BOOST_DESCRIBE_STRUCT(Derived, (Base1, Base2), ())

//----------------------------------------------------------

BOOST_DEFINE_ENUM_CLASS(E1, a, b, c)

//----------------------------------------------------------

struct custom_context
{ };

struct T12
{ };

void
tag_invoke(
    ::boost::json::value_from_tag,
    ::boost::json::value& jv,
    T12 const&,
    custom_context const& )
{
    jv = "T12";
}

//----------------------------------------------------------

struct another_context
{ };

struct T13
{ };

void
tag_invoke(
    ::boost::json::value_from_tag,
    ::boost::json::value& jv,
    T13 const&,
    another_context const& )
{
    jv = "T13";
}

//----------------------------------------------------------

template< class T, class Ctx >
void
tag_invoke(
    ::boost::json::value_from_tag,
    ::boost::json::value& jv,
    std::vector< T > const& vec,
    another_context const&,
    Ctx const& ctx )
{
    boost::json::object& jo = jv.emplace_object();
    unsigned i = 0;
    for( auto&& item: vec )
    {
        jo.emplace(
            std::to_string(++i),
            boost::json::value_from( item, ctx, jo.storage() ) );
    }
}


} // namespace value_from_test_ns

template<class T>
static
void
check(
    ::boost::json::string_view s,
    T const& t)
{
    {
        auto const jv = value_from(t,
            ::boost::json::storage_ptr{});
        auto const js =
            ::boost::json::serialize(jv);
        BOOST_TEST(js == s);
    }
    {
        auto const jv =
            ::boost::json::value_from(t);
        auto const js =
            ::boost::json::serialize(jv);
        BOOST_TEST(js == s);
    }
}

namespace boost {
namespace json {

template<>
struct is_null_like<::value_from_test_ns::T7>
    : std::true_type
{ };

template<>
struct is_described_class<::value_from_test_ns::T11>
    : std::true_type
{ };

namespace {

template< class T, class... Context >
static
void
testValueCtor( T const& t, Context const& ... ctx )
{
    BOOST_TEST(
        serialize(value_from( t, ctx... )) == serialize(value(t)) );
}

} // namespace

// integral
BOOST_CORE_STATIC_ASSERT( has_value_from<int>::value );
BOOST_CORE_STATIC_ASSERT( has_value_from<int&>::value );
BOOST_CORE_STATIC_ASSERT( has_value_from<int&&>::value );
// array
BOOST_CORE_STATIC_ASSERT( has_value_from<int[4]>::value );
BOOST_CORE_STATIC_ASSERT( has_value_from<int(&)[4]>::value );
BOOST_CORE_STATIC_ASSERT( has_value_from<int(&&)[4]>::value );
// forward range
BOOST_CORE_STATIC_ASSERT( has_value_from<std::vector<int>>::value );
BOOST_CORE_STATIC_ASSERT( has_value_from<std::vector<int>&>::value );
BOOST_CORE_STATIC_ASSERT( has_value_from<std::vector<int>&&>::value );
// tuple-like
BOOST_CORE_STATIC_ASSERT(( has_value_from<std::tuple<int, int>>::value ));
BOOST_CORE_STATIC_ASSERT(( has_value_from<std::tuple<int, int>&>::value ));
BOOST_CORE_STATIC_ASSERT(( has_value_from<std::tuple<int, int>&&>::value ));
BOOST_CORE_STATIC_ASSERT( has_value_from<key_value_pair>::value );
BOOST_CORE_STATIC_ASSERT( has_value_from<key_value_pair&>::value );
BOOST_CORE_STATIC_ASSERT( has_value_from<key_value_pair&&>::value );

// object-like
BOOST_CORE_STATIC_ASSERT((
    has_value_from<std::map<string_view, int>>::value));

class value_from_test
{
public:
    template< class... Context >
    static
    void
    testValueCtors( Context const& ... ctx )
    {
        // value_from supports every value constructor

        testValueCtor<value, Context...>( value(), ctx... );

        char const* s = "5";
        testValueCtor<char const*, Context...>( s, ctx... );
    }

    template< class... Context >
    static
    void
    testGeneral( Context const& ... ctx )
    {
        {
            int a[4] = {1, 2, 3, 4};
            value b{1, 2, 3, 4};
            value c = value_from( a, ctx... );
            BOOST_TEST(c.is_array());
            BOOST_TEST(serialize(c) == serialize(b));
        }
        {
            std::tuple<int, string, int, bool> a{1, "2", 42, true};
            value b{1, "2", 42, true};
            value c = value_from( a, ctx... );
            BOOST_TEST(c.is_array());
            BOOST_TEST(serialize(c) == serialize(b));
        }
        {
            std::array<int, 500> a;
            a.fill(0);

            value b;
            array& b_arr = b.emplace_array();
            b_arr.insert(b_arr.end(), a.begin(), a.end());

            BOOST_TEST(value_from( a, ctx... ) == b);
        }
        {
            std::pair<int, string> a{1, string("2")};
            value b{1, "2"};
            value c = value_from( a, ctx... );
            BOOST_TEST(c.is_array());
            BOOST_TEST(serialize(c) == serialize(b));
        }
        {
            // ensures that this isn't parsed as a key value pair
            std::pair<string_view, int> a{"2", 1};
            value b{"2", 1};
            value c = value_from( a, ctx... );
            BOOST_TEST(c.is_array());
            BOOST_TEST(serialize(c) == serialize(b));
        }
        {
            key_value_pair a{"2", 1};
            value b{"2", 1};
            value c = value_from( a, ctx... );
            BOOST_TEST(c.is_array());
            BOOST_TEST(serialize(c) == serialize(b));
        }
        {
            ::value_from_test_ns::T7 a;
            value b = value_from( a, ctx... );
            BOOST_TEST(b.is_null());
        }
    }

    template< class... Context >
    static
    void
    testAssociative( Context const& ... ctx )
    {
        {
            std::map<string_view, int> a =
                {{"a", 1}, {"b", 2}, {"c", 3}};
            value b = {{"a", 1}, {"b", 2}, {"c", 3}};
            value c = value_from( a, ctx...  );
            BOOST_TEST(c.is_object());
            BOOST_TEST(a.size() == c.as_object().size());
            BOOST_TEST(b.as_object().size() == c.as_object().size());
        }
        {
            std::unordered_map<std::string, int> a =
               {{"a", 1}, {"b", 2}, {"c", 3}};
            value b = {{"a", 1}, {"b", 2}, {"c", 3}};
            value c = value_from( a, ctx... );
            BOOST_TEST(c.is_object());
            BOOST_TEST(a.size() == c.as_object().size());
            BOOST_TEST(b.as_object().size() == c.as_object().size());
        }
        {
            std::map<int, int> a =
                {{1, 1}, {2, 2}, {3, 3}};
            value b = {{1, 1}, {2, 2}, {3, 3}};
            value c = value_from( a, ctx... );
            BOOST_TEST(!c.is_object());
            BOOST_TEST(a.size() == c.as_array().size());
            BOOST_TEST(b.as_array().size() == c.as_array().size());
        }
        {
            std::unordered_map<int, int> a =
                {{1, 1}, {2, 2}, {3, 3}};
            value b = {{1, 1}, {2, 2}, {3, 3}};
            value c = value_from( a, ctx... );
            BOOST_TEST(!c.is_object());
            BOOST_TEST(a.size() == c.as_array().size());
            BOOST_TEST(b.as_array().size() == c.as_array().size());
        }
    }

    template< class... Context >
    static
    void
    testPreferUserCustomizations( Context const& ... ctx )
    {
        value_from_test_ns::T5 t5;
        BOOST_TEST((::boost::json::value_from( t5, ctx...  ) == "T5"));
    }

    void
    testTrySize()
    {
        {
            std::vector<int> v{1, 2, 3};
            using impl = detail::size_implementation<decltype(v)>;
            BOOST_TEST(detail::try_size(v, impl()) == 3);
        }
        {
            int arr[4] = {1, 2, 3, 4};
            using impl = detail::size_implementation<decltype(arr)>;
            BOOST_TEST(detail::try_size(arr, impl()) == 4);
        }
        {
            value_from_test_ns::T6 t;
            using impl = detail::size_implementation<decltype(t)>;
            BOOST_TEST(detail::try_size(t, impl()) == 3);
        }
        {
            value_from_test_ns::T1 t;
            using impl = detail::size_implementation<decltype(t)>;
            BOOST_TEST(detail::try_size(t, impl()) == 0);
        }
    }

    template< class... Context >
    static
    void
    testDescribed( Context const& ... ctx )
    {
        ignore_unused( ctx... );
#ifdef BOOST_DESCRIBE_CXX14
        ::value_from_test_ns::T10 t10{909, -1.45};
        value jv = value_from( t10, ctx... );
        BOOST_TEST(( jv == value{{"n", 909}, {"d", -1.45}} ));

        ::value_from_test_ns::T11 t11;
        t11.set_n(67);
        t11.set_d(-.12);
        t11.set_b(true);
        t11.s = "qwerty";
        jv = value_from( t11, ctx... );
        BOOST_TEST(( jv == value{{"n", 67}, {"d", -.12}, {"s", "qwerty"}, {"b", true}} ));

        ::value_from_test_ns::E1 e1 = ::value_from_test_ns::E1::a;
        BOOST_TEST( value_from( e1, ctx... ) == "a" );

        e1 = ::value_from_test_ns::E1::b;
        BOOST_TEST( value_from( e1, ctx... ) == "b" );

        e1 = static_cast<::value_from_test_ns::E1>(1001);
        BOOST_TEST( value_from( e1, ctx... ) == 1001 );
#endif
    }

    template< class... Context >
    static
    void testOptional( Context const& ... ctx )
    {
        ignore_unused( ctx... );
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
        std::vector<std::optional<int>> opts{1, 2, 3, {}, 5};
        value jv = value_from( opts, ctx... );
        BOOST_TEST( jv == (value{1, 2, 3, nullptr, 5}) );

        BOOST_TEST( value_from( std::nullopt, ctx... ).is_null() );
#endif
    }

    template< class... Context >
    static
    void
    testVariant( Context const& ... ctx )
    {
        ignore_unused( ctx... );
#ifndef BOOST_NO_CXX17_HDR_VARIANT
        std::variant<int, ::value_from_test_ns::T5, double> v = 4;
        value jv = value_from( v, ctx... );
        BOOST_TEST(jv == 4);

        v = 0.5;
        jv = value_from( v, ctx... );
        BOOST_TEST(jv == 0.5);

        v = ::value_from_test_ns::T5{};
        jv = value_from( v, ctx... );
        BOOST_TEST(jv == "T5");

        BOOST_TEST( value() == value_from( std::monostate(), ctx... ) );
#endif // BOOST_NO_CXX17_HDR_VARIANT
    }

    template< class... Context >
    static
    void testPath( Context const& ... ctx )
    {
        ignore_unused( ctx... );
#ifndef BOOST_NO_CXX17_HDR_FILESYSTEM
        std::vector<std::filesystem::path> paths{
            "from/here", "to/there", "", "c:/" , "..", "../"};
        value jv = value_from( paths, ctx... );
        BOOST_TEST(
            jv == (value{"from/here", "to/there", "", "c:/" , "..", "../"}) );
#endif // BOOST_NO_CXX17_HDR_FILESYSTEM
    }

    void
    testContext()
    {
        value jv = value_from(
            value_from_test_ns::T1{}, value_from_test_ns::custom_context() );
        BOOST_TEST( jv == 42 );

        jv = value_from(
            value_from_test_ns::T12{},
            value_from_test_ns::custom_context() );
        BOOST_TEST( jv == "T12" );

        jv = value_from(
            value_from_test_ns::T13{},
            std::make_tuple(
                value_from_test_ns::custom_context(),
                value_from_test_ns::another_context() ) );
        BOOST_TEST( jv == "T13" );

        jv = value_from(
            std::pair<value_from_test_ns::T12, value_from_test_ns::T13>(),
            std::make_tuple(
                value_from_test_ns::custom_context(),
                value_from_test_ns::another_context() ) );
        BOOST_TEST( jv == (array{ "T12", "T13" }) );


        auto ctx = std::make_tuple(
            value_from_test_ns::custom_context(),
            value_from_test_ns::another_context() );
        using Ctx = decltype(ctx);
        using Sup = detail::supported_context<
            Ctx,
            std::vector<value_from_test_ns::T12>,
            detail::value_from_conversion >;
        BOOST_CORE_STATIC_ASSERT( Sup::index::value == 1 );
        BOOST_CORE_STATIC_ASSERT((
            std::is_same<
                Sup::type,
                value_from_test_ns::another_context>::value));

        jv = value_from(
            std::vector<value_from_test_ns::T12>(2),
            std::make_tuple(
                value_from_test_ns::custom_context(),
                value_from_test_ns::another_context() ) );
        BOOST_TEST( jv == (object{ {"1", "T12"}, {"2", "T12"} }) );
    }

    struct run_templated_tests
    {
        // this overload supports zero or one default constructible contexts
        // and used with mp_for_each
        template< class... Context >
        void operator()( mp11::mp_list< Context... > )
        {
            testValueCtors( Context()... );
            testGeneral( Context()... );
            testAssociative( Context()... );
            testPreferUserCustomizations( Context()... );
            testDescribed( Context()... );
            testOptional( Context()... );
            testVariant( Context()... );
            testPath( Context()... );
        }
    };

    void
    run()
    {
        check("42", ::value_from_test_ns::T1{});
        check("[[1,2,3,4],\"test\"]", ::value_from_test_ns::T2{});

        mp11::mp_for_each<
            mp11::mp_list<
                mp11::mp_list<>,
                mp11::mp_list<detail::no_context>,
                mp11::mp_list<value_from_test_ns::custom_context>,
                mp11::mp_list<
                    std::tuple<value_from_test_ns::custom_context>>,
                mp11::mp_list<
                    std::tuple<
                        std::tuple<value_from_test_ns::custom_context>>>,
                mp11::mp_list<
                    std::tuple<
                        detail::no_context,
                        value_from_test_ns::custom_context>>
            >>( run_templated_tests() );

        testContext();
        testTrySize();
    }
};

TEST_SUITE(value_from_test, "boost.json.value_from");

} // namespace json
} // namespace boost