VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gitem.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gitem.h
19 ///
20 
21 #ifndef G_ITEM__H
22 #define G_ITEM__H
23 
24 #include "gdef.h"
25 #include "gexception.h"
26 #include "glog.h"
27 #include <map>
28 #include <vector>
29 #include <string>
30 
31 namespace G
32 {
33  class Item ;
34 }
35 
36 /// \class G::Item
37 /// A variant class holding a string, an item map keyed by name, or an ordered
38 /// list of items. The definition is recursive, so items can be nested. The
39 /// string representation is curiously json-like.
40 ///
41 class G::Item
42 {
43 public:
44  G_EXCEPTION( Error , "item error" ) ;
45  G_EXCEPTION( ParseError , "item error: parsing failed" ) ;
46  enum Type { t_string , t_list , t_map } m_type ;
47 
48  static Item list() ;
49  ///< Factory function for a list item.
50 
51  static Item map() ;
52  ///< Factory function for a map item.
53 
54  static Item string() ;
55  ///< Factory function for a string item.
56 
57  static Item parse( const std::string & ) ;
58  ///< Parses the string representation. Throws on error.
59 
60  static bool parse( const std::string & , Item & out ) ;
61  ///< A non-throwing overload to parse the string representation.
62  ///< Returns false on error.
63 
64  explicit Item( Type type = t_string ) ;
65  ///< Constructor.
66 
67  explicit Item( const std::string & s ) ;
68  ///< Constructor for a string item.
69 
70  Type type() const ;
71  ///< Returns the item type (string, list or map).
72 
73  const Item & operator[]( const std::string & k ) const ;
74  ///< Indexing operator for a map item. Throws if no such item.
75 
76  Item & operator[]( const std::string & k ) ;
77  ///< Indexing operator for a map item. Creates an empty string
78  ///< item if no such item initially.
79 
80  Item & operator[]( size_t i ) ;
81  ///< Indexing operator for a list item. Throws if out of range.
82 
83  const Item & operator[]( size_t i ) const ;
84  ///< Indexing operator for a list item. Throws if out of range.
85 
86  void add( const std::string & s ) ;
87  ///< Adds a string item to this list item.
88 
89  void add( Item i ) ;
90  ///< Adds an item to this list item.
91 
92  void add( const std::string & k , const std::string & s ) ;
93  ///< Adds a string item to this map item. Replaces any pre-existing item with that key.
94 
95  void add( const std::string & k , const Item & i ) ;
96  ///< Adds an item to this map item. Replaces any pre-existing item with that key.
97 
98  bool has( const std::string & k ) const ;
99  ///< Returns true if this map item has the named key.
100 
101  void remove( const std::string & k ) ;
102  ///< Removes an item from this map item.
103 
104  Item keys() const ;
105  ///< Returns the keys of this map item as a list of string items.
106 
107  size_t size() const ;
108  ///< Returns the size of this map-or-list item.
109 
110  bool empty() const ;
111  ///< Returns true if an empty item.
112 
113  std::string operator()() const ;
114  ///< Returns the value of this string item.
115 
116  void out( std::ostream & s , int indent = -1 ) const ;
117  ///< Does output streaming, using a json-like format.
118 
119  void in( std::istream & s ) ;
120  ///< Does input parsing. Throws on error.
121 
122  std::string str( int indent = -1 ) const ;
123  ///< Returns a string representation, using a json-like format.
124 
125 private:
126  void check( Type type ) const ;
127  void checkNot( Type type ) const ;
128  void check() const ;
129  void need( const std::string & k ) const ;
130  void need( size_t i ) const ;
131  void update( const std::string & k ) ;
132  static bool less( const Item & a , const Item & b ) ;
133  void clear( Type type = t_string ) ;
134  void read( std::istream & , char eos ) ;
135  void readn( std::istream & , char ) ;
136  static bool escaped( const std::string & s ) ;
137  static void unescape( std::string & s ) ;
138  static void unescape( std::string & s , char bs , const char * map_in , const char * map_out ) ;
139 
140 private:
141  typedef std::map<std::string,Item> Map ;
142  std::string m_string ;
143  std::vector<Item> m_list ;
144  Map m_map ;
145  std::vector<std::string> m_keys ; // for key ordering
146 } ;
147 
148 namespace G
149 {
150  inline
151  std::ostream & operator<<( std::ostream & stream , const Item & item )
152  {
153  item.out( stream ) ;
154  return stream ;
155  }
156  inline
157  std::istream & operator>>( std::istream & stream , Item & item )
158  {
159  item.in( stream ) ;
160  return stream ;
161  }
162 }
163 
164 #endif
void out(std::ostream &s, int indent=-1) const
Does output streaming, using a json-like format.
Definition: gitem.cpp:249
static Item map()
Factory function for a map item.
Definition: gitem.cpp:33
size_t size() const
Returns the size of this map-or-list item.
Definition: gitem.cpp:162
bool empty() const
Returns true if an empty item.
Definition: gitem.cpp:168
void in(std::istream &s)
Does input parsing. Throws on error.
Definition: gitem.cpp:339
std::string str(int indent=-1) const
Returns a string representation, using a json-like format.
Definition: gitem.cpp:67
static Item parse(const std::string &)
Parses the string representation. Throws on error.
Definition: gitem.cpp:43
static Item list()
Factory function for a list item.
Definition: gitem.cpp:28
const Item & operator[](const std::string &k) const
Indexing operator for a map item. Throws if no such item.
Definition: gitem.cpp:90
std::string operator()() const
Returns the value of this string item.
Definition: gitem.cpp:184
A variant class holding a string, an item map keyed by name, or an ordered list of items...
Definition: gitem.h:41
void add(const std::string &s)
Adds a string item to this list item.
Definition: gitem.cpp:108
static Item string()
Factory function for a string item.
Definition: gitem.cpp:38
Item(Type type=t_string)
Constructor.
Definition: gitem.cpp:74
Item keys() const
Returns the keys of this map item as a list of string items.
Definition: gitem.cpp:152
bool has(const std::string &k) const
Returns true if this map item has the named key.
Definition: gitem.cpp:146
Type type() const
Returns the item type (string, list or map).
Definition: gitem.cpp:85