2
0
mirror of https://github.com/boostorg/json.git synced 2026-02-12 00:02:14 +00:00
Files
json/test/throw_parser.hpp
2019-10-17 06:30:50 -07:00

164 lines
2.4 KiB
C++

//
// Copyright (c) 2018-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/vinniefalco/json
//
#ifndef BOOST_JSON_TEST_THROW_PARSER_HPP
#define BOOST_JSON_TEST_THROW_PARSER_HPP
#include <boost/json/basic_parser.hpp>
#include <stdexcept>
namespace boost {
namespace json {
struct test_exception
: std::exception
{
char const*
what() const noexcept
{
return "test exception";
}
};
class throw_parser
: public basic_parser
{
std::size_t n_ = std::size_t(-1);
char buf[256];
void
maybe_throw()
{
if(n_ && --n_ > 0)
return;
throw test_exception{};
}
void
on_stack_info(
stack& s) noexcept override
{
s.base = buf;
s.capacity = sizeof(buf);
}
void
on_stack_grow(
stack&,
unsigned,
error_code& ec) override
{
ec = error::too_deep;
}
void
on_document_begin(
error_code&) override
{
maybe_throw();
}
void
on_object_begin(
error_code&) override
{
maybe_throw();
}
void
on_object_end(
error_code&) override
{
maybe_throw();
}
void
on_array_begin(
error_code&) override
{
maybe_throw();
}
void
on_array_end(
error_code&) override
{
maybe_throw();
}
void
on_key_data(
string_view,
error_code&) override
{
maybe_throw();
}
void
on_key_end(
string_view,
error_code&) override
{
maybe_throw();
}
void
on_string_data(
string_view,
error_code&) override
{
maybe_throw();
}
void
on_string_end(
string_view,
error_code&) override
{
maybe_throw();
}
void
on_number(
number,
error_code&) override
{
maybe_throw();
}
void
on_bool(
bool,
error_code&) override
{
maybe_throw();
}
void
on_null(error_code&) override
{
maybe_throw();
}
public:
throw_parser() = default;
explicit
throw_parser(
std::size_t n)
: n_(n)
{
}
};
} // json
} // boost
#endif