Path: blob/master/thirdparty/graphite/src/inc/json.h
10279 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2011, SIL International, All rights reserved.23// JSON pretty printer for graphite font debug output logging.4// Created on: 15 Dec 20115// Author: Tim Eves67#pragma once89#include "inc/Main.h"10#include <cassert>11#include <cstdio>12#include <cstdint>13#include "inc/List.h"1415namespace graphite2 {1617class json18{19// Prevent copying20json(const json &);21json & operator = (const json &);2223typedef void (*_context_t)(json &);2425FILE * const _stream;26char _contexts[128], // context stack27* _context, // current context (top of stack)28* _flatten; // if !0 points to context above which29// pretty printed output should occur.30Vector<void *> _env;3132void context(const char current) throw();33void indent(const int d=0) throw();34void push_context(const char, const char) throw();35void pop_context() throw();3637public:38class closer;3940using string = const char *;41using number = double;42enum class integer : std::intmax_t {};43enum class integer_u : std::uintmax_t {};44using boolean = bool;45static const std::nullptr_t null;4647void setenv(unsigned int index, void *val) { _env.reserve(index + 1); if (index >= _env.size()) _env.insert(_env.end(), _env.size() - index + 1, 0); _env[index] = val; }48void *getenv(unsigned int index) const { return _env[index]; }49const Vector<void *> &getenvs() const { return _env; }5051static void flat(json &) throw();52static void close(json &) throw();53static void object(json &) throw();54static void array(json &) throw();55static void item(json &) throw();5657json(FILE * stream) throw();58~json() throw ();5960FILE * stream() const throw();6162json & operator << (string) throw();63json & operator << (number) throw();64json & operator << (integer) throw();65json & operator << (integer_u) throw();66json & operator << (boolean) throw();67json & operator << (std::nullptr_t) throw();68json & operator << (_context_t) throw();6970operator bool() const throw();71bool good() const throw();72bool eof() const throw();7374CLASS_NEW_DELETE;75};7677class json::closer78{79// Prevent copying.80closer(const closer &);81closer & operator = (const closer &);8283json * const _j;84public:85closer(json * const j) : _j(j) {}86~closer() throw() { if (_j) *_j << close; }87};8889inline90json::json(FILE * s) throw()91: _stream(s), _context(_contexts), _flatten(0)92{93if (good())94fflush(s);95}969798inline99json::~json() throw ()100{101while (_context > _contexts) pop_context();102}103104inline105FILE * json::stream() const throw() { return _stream; }106107108inline109json & json::operator << (json::_context_t ctxt) throw()110{111ctxt(*this);112return *this;113}114115inline116json & operator << (json & j, signed char d) throw() { return j << json::integer(d); }117118inline119json & operator << (json & j, unsigned char d) throw() { return j << json::integer_u(d); }120121inline122json & operator << (json & j, short int d) throw() { return j << json::integer(d); }123124inline125json & operator << (json & j, unsigned short int d) throw() { return j << json::integer_u(d); }126127inline128json & operator << (json & j, int d) throw() { return j << json::integer(d); }129130inline131json & operator << (json & j, unsigned int d) throw() { return j << json::integer_u(d); }132133inline134json & operator << (json & j, long int d) throw() { return j << json::integer(d); }135136inline137json & operator << (json & j, unsigned long int d) throw() { return j << json::integer_u(d); }138139inline140json & operator << (json & j, long long int d) throw() { return j << json::integer(d); }141142inline143json & operator << (json & j, unsigned long long int d) throw() { return j << json::integer_u(d); }144145inline146json::operator bool() const throw() { return good(); }147148inline149bool json::good() const throw() { return _stream && ferror(_stream) == 0; }150151inline152bool json::eof() const throw() { return feof(_stream) != 0; }153154} // namespace graphite2155156157