gfx/graphite2/src/inc/json.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*  GRAPHITE2 LICENSING
     3     Copyright 2011, SIL International
     4     All rights reserved.
     6     This library is free software; you can redistribute it and/or modify
     7     it under the terms of the GNU Lesser General Public License as published
     8     by the Free Software Foundation; either version 2.1 of License, or
     9     (at your option) any later version.
    11     This program is distributed in the hope that it will be useful,
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    14     Lesser General Public License for more details.
    16     You should also have received a copy of the GNU Lesser General Public
    17     License along with this library in the file named "LICENSE".
    18     If not, write to the Free Software Foundation, 51 Franklin Street,
    19     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    20     internet at http://www.fsf.org/licenses/lgpl.html.
    22 Alternatively, the contents of this file may be used under the terms of the
    23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    24 License, as published by the Free Software Foundation, either version 2
    25 of the License or (at your option) any later version.
    26 */
    27 // JSON pretty printer for graphite font debug output logging.
    28 // Created on: 15 Dec 2011
    29 //     Author: Tim Eves
    31 #pragma once
    32 #include "inc/Main.h"
    33 #include <cassert>
    34 #include <stdio.h>
    36 namespace graphite2 {
    38 class json
    39 {
    40     // Prevent copying
    41     json(const json &);
    42     json & operator = (const json &);
    44     typedef void (*_context_t)(json &);
    45     class _null_t {};
    47     FILE * const    _stream;
    48     char            _contexts[128], // context stack
    49                   * _context,       // current context (top of stack)
    50                   * _flatten;       // if !0 points to context above which
    51                                     //  pretty printed output should occur.
    53     void context(const char current) throw();
    54     void indent(const int d=0) throw();
    55     void push_context(const char, const char) throw();
    56     void pop_context() throw();
    58 public:
    59     class closer;
    61     typedef const char *    string;
    62     typedef double          number;
    63     typedef long signed int integer;
    64     typedef bool            boolean;
    65     static const _null_t    null;
    67     static void flat(json &) throw();
    68     static void close(json &) throw();
    69     static void object(json &) throw();
    70     static void array(json &) throw();
    71     static void item(json &) throw();
    73     json(FILE * stream) throw();
    74     ~json() throw ();
    76     FILE * stream() const throw();
    78     json & operator << (string) throw();
    79     json & operator << (number) throw();
    80     json & operator << (integer) throw();
    81     json & operator << (long unsigned int d) throw();
    82     json & operator << (boolean) throw();
    83     json & operator << (_null_t) throw();
    84     json & operator << (_context_t) throw();
    86     operator bool() const throw();
    87     bool good() const throw();
    88     bool eof() const throw();
    90     CLASS_NEW_DELETE;
    91 };
    93 class json::closer
    94 {
    95     // Prevent copying.
    96     closer(const closer &);
    97     closer & operator = (const closer &);
    99     json * const    _j;
   100 public:
   101     closer(json * const j) : _j(j) {}
   102     ~closer() throw() { if (_j)  *_j << close; }
   103 };
   105 inline
   106 json::json(FILE * s) throw()
   107 : _stream(s), _context(_contexts), _flatten(0)
   108 {
   109     if (good())
   110         fflush(s);
   111 }
   114 inline
   115 json::~json() throw ()
   116 {
   117     while (_context > _contexts)    pop_context();
   118 }
   120 inline
   121 FILE * json::stream() const throw()     { return _stream; }
   124 inline
   125 json & json::operator << (json::_context_t ctxt) throw()
   126 {
   127     ctxt(*this);
   128     return *this;
   129 }
   131 inline
   132 json & operator << (json & j, signed char d) throw()        { return j << json::integer(d); }
   134 inline
   135 json & operator << (json & j, short signed int d) throw()   { return j << json::integer(d); }
   137 inline
   138 json & operator << (json & j, signed int d) throw()         { return j << json::integer(d); }
   140 inline
   141 json & operator << (json & j, unsigned char d) throw()      { return j << json::integer(d); }
   143 inline
   144 json & operator << (json & j, short unsigned int d) throw() { return j << json::integer(d); }
   146 inline
   147 json & operator << (json & j, unsigned int d) throw()       { return j << json::integer(d); }
   149 inline
   150 json & operator << (json & j, char c) throw ()
   151 {
   152     const char str[2] = {c,0};
   153     return j << str;
   154 }
   156 inline
   157 json::operator bool() const throw()     { return good(); }
   159 inline
   160 bool json::good() const throw()         { return _stream && ferror(_stream) == 0; }
   162 inline
   163 bool json::eof() const throw()          { return feof(_stream) != 0; }
   165 } // namespace graphite2

mercurial