xapian-core  2.0.0
pretty.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2010,2011,2012,2014,2016,2017,2019,2023,2024,2026 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef XAPIAN_INCLUDED_PRETTY_H
22 #define XAPIAN_INCLUDED_PRETTY_H
23 
24 #include <list>
25 #include <map>
26 #include <ostream>
27 #include <string>
28 #include <string_view>
29 #include <vector>
30 
31 #include "api/smallvector.h"
32 
33 #include "xapian/intrusive_ptr.h"
34 #include "xapian/types.h"
35 
36 template<class S>
37 struct PrettyOStream {
39  S & os;
40 
41  PrettyOStream(S & os_) : os(os_) { }
42  template<typename T> PrettyOStream & operator|(const T & t) {
43  os << ", ";
44  return *this << t;
45  }
46 };
47 
48 struct Literal {
49  const char * _lit;
50  explicit Literal(const char * lit) : _lit(lit) { }
51  explicit Literal(const std::string & s) : _lit(s.c_str()) { }
52 };
53 
55 template<class S, class T>
56 inline PrettyOStream<S> &
57 operator<<(PrettyOStream<S> &ps, const T & t)
58 {
59  ps.os << t;
60  return ps;
61 }
62 
70 template<class S>
71 inline PrettyOStream<S> &
72 operator<<(PrettyOStream<S> &ps, const Literal & t)
73 {
74  ps.os << t._lit;
75  return ps;
76 }
77 
78 template<class S, class T>
79 inline PrettyOStream<S> &
80 operator<<(PrettyOStream<S> &ps, const T * t)
81 {
82  if (!t) {
83  ps.os << "NULL";
84  return ps;
85  }
86  ps.os << '&';
87  return ps << *t;
88 }
89 
90 template<class S, class T>
91 inline PrettyOStream<S> &
92 operator<<(PrettyOStream<S> &ps, const T ** t)
93 {
94  ps.os << (void*)t;
95  return ps;
96 }
97 
98 template<class S>
99 inline PrettyOStream<S> &
100 operator<<(PrettyOStream<S> &ps, const void * t)
101 {
102  ps.os << "(void*)" << t;
103  return ps;
104 }
105 
106 // FIXME: We probably don't want to inline this, but need to arrange to
107 // put it somewhere sane out-of-line.
108 inline void write_ch(std::ostream & os, unsigned char ch)
109 {
110  if (ch < 32 || ch >= 127) {
111  os << '\\';
112  if (ch >= 7 && ch <= 13) {
113  os << "abtnvfr"[ch - 7];
114  } else {
115  os << char('0' | (ch >> 6));
116  os << char('0' | ((ch >> 3) & 7));
117  os << char('0' | (ch & 7));
118  }
119  } else if (ch == '\\') {
120  os << "\\\\";
121  } else if (ch == '"') {
122  os << "\\\"";
123  } else {
124  os << ch;
125  }
126 }
127 
128 template<class S>
129 inline PrettyOStream<S> &
130 operator<<(PrettyOStream<S> &ps, const char * str)
131 {
132  ps.os << '"';
133  while (*str) {
134  write_ch(ps.os, *str++);
135  }
136  ps.os << '"';
137  return ps;
138 }
139 
140 template<class S>
141 inline PrettyOStream<S> &
142 operator<<(PrettyOStream<S> &ps, const std::string & str)
143 {
144  ps.os << '"';
145  for (char ch : str) {
146  write_ch(ps.os, ch);
147  }
148  ps.os << '"';
149  return ps;
150 }
151 
152 template<class S>
153 inline PrettyOStream<S> &
154 operator<<(PrettyOStream<S> &ps, std::string &)
155 {
156  ps.os << "std::string&";
157  return ps;
158 }
159 
160 template<class S>
161 inline PrettyOStream<S> &
162 operator<<(PrettyOStream<S> &ps, std::string *)
163 {
164  ps.os << "std::string*";
165  return ps;
166 }
167 
168 template<class S>
169 inline PrettyOStream<S>&
170 operator<<(PrettyOStream<S>& ps, std::string_view str)
171 {
172  ps.os << '"';
173  for (char ch : str) {
174  write_ch(ps.os, ch);
175  }
176  ps.os << '"';
177  return ps;
178 }
179 
180 template<class S>
181 inline PrettyOStream<S>&
182 operator<<(PrettyOStream<S>& ps, const std::string_view* p_str)
183 {
184  if (p_str)
185  ps.os << *p_str;
186  else
187  ps.os << "NULL";
188  return ps;
189 }
190 
191 template<class S>
192 inline PrettyOStream<S> &
193 operator<<(PrettyOStream<S> &ps, unsigned char ch)
194 {
195  ps.os << '\'';
196  if (ch < 32 || ch >= 127) {
197  ps.os << '\\';
198  if (ch >= 7 && ch <= 13) {
199  ps.os << "abtnvfr"[ch - 7];
200  } else if (ch == '\0') {
201  ps.os << "\\0";
202  } else {
203  ps.os << "0123456789abcdef"[ch >> 4];
204  ps.os << "0123456789abcdef"[ch & 0x0f];
205  }
206  } else if (ch == '\\') {
207  ps.os << "\\\\";
208  } else if (ch == '\'') {
209  ps.os << "\\'";
210  } else {
211  ps.os << ch;
212  }
213  ps.os << '\'';
214  return ps;
215 }
216 
217 template<class S>
218 inline PrettyOStream<S> &
219 operator<<(PrettyOStream<S> &ps, bool b)
220 {
221  ps.os << (b ? "true" : "false");
222  return ps;
223 }
224 
225 /*
226 template<class S>
227 inline PrettyOStream<S> &
228 operator<<(PrettyOStream<S> &ps, bool &)
229 {
230  ps.os << "bool&";
231  return ps;
232 }
233 */
234 
235 template<class S>
236 inline PrettyOStream<S> &
238 {
239  ps.os << "(Xapian::termcount*)" << (void*)p;
240  return ps;
241 }
242 
243 template<class S, typename T>
244 inline PrettyOStream<S> &
245 operator<<(PrettyOStream<S> &ps, std::list<T> &) {
246  ps.os << "std::list&";
247  return ps;
248 }
249 
250 template<class S, typename T>
251 inline PrettyOStream<S> &
252 operator<<(PrettyOStream<S> &ps, const std::list<T> &) {
253  ps.os << "std::list";
254  // FIXME: could show first up to N elements.
255  return ps;
256 }
257 
258 template<class S, typename K, typename V>
259 inline PrettyOStream<S> &
260 operator<<(PrettyOStream<S> &ps, std::map<K, V> *) {
261  ps.os << "std::map*";
262  return ps;
263 }
264 
265 template<class S, typename K, typename V>
266 inline PrettyOStream<S> &
267 operator<<(PrettyOStream<S> &ps, std::map<K, V> &) {
268  ps.os << "std::map&";
269  return ps;
270 }
271 
272 template<class S, typename K, typename V>
273 inline PrettyOStream<S> &
274 operator<<(PrettyOStream<S> &ps, const std::map<K, V> & m) {
275  ps.os << "std::map(" << m.size() << ')';
276  // FIXME: could show first up to N elements.
277  return ps;
278 }
279 
280 template<class S, typename T>
281 inline PrettyOStream<S> &
282 operator<<(PrettyOStream<S> &ps, const std::vector<T> & v) {
283  ps.os << "std::vector(" << v.size() << ')';
284  // FIXME: could show first up to N elements.
285  return ps;
286 }
287 
288 template<class S, typename T>
289 inline PrettyOStream<S> &
291  ps.os << "Vec(" << v.size() << ')';
292  // FIXME: could show first up to N elements.
293  return ps;
294 }
295 
296 template<class S, typename T>
297 inline PrettyOStream<S> &
299  ps.os << "VecCOW(" << v.size() << ')';
300  // FIXME: could show first up to N elements.
301  return ps;
302 }
303 
304 template<class S, typename T, typename U>
305 inline PrettyOStream<S> &
306 operator<<(PrettyOStream<S> &ps, const std::pair<T, U>& v) {
307  ps.os << "std::pair(" << v.first << ", " << v.second << ')';
308  return ps;
309 }
310 
311 namespace Xapian {
312  class Centroid;
313  class Cluster;
314  class ClusterSet;
315  class ExpandDecider;
316  class FreqSource;
317  class KeyMaker;
318  class LatLongMetric;
319  class MatchDecider;
320  class Point;
321  class PointType;
322  class Registry;
323  class TermListGroup;
324  class Weight;
325  namespace Internal {
326  class AndContext;
327  class ExpandStats;
328  class ExpandWeight;
329  class OrContext;
330  struct PostListAndEstimate;
331  }
332 }
333 
334 namespace Glass {
335  class RootInfo;
336 }
337 
338 class GlassCursor;
339 class GlassDatabase;
341 class GlassTable;
342 
343 class HoneyTable;
344 
345 #define XAPIAN_PRETTY_AS_CLASSNAME(C)\
346 template<class S>\
347 inline PrettyOStream<S> &\
348 operator<<(PrettyOStream<S> &ps, const C &) {\
349  ps.os << #C;\
350  return ps;\
351 }
352 
377 
378 template<class S>
379 inline PrettyOStream<S> &
381  ps.os << "(Xapian:Weight*)" << (const void*)p;
382  return ps;
383 }
384 
385 class RemoteConnection;
386 
387 template<class S>
388 inline PrettyOStream<S> &
390  ps.os << "RemoteConnection";
391  return ps;
392 }
393 
395 
396 template<class S>
397 inline PrettyOStream<S> &
399  ps.os << "(Database::Internal*)" << (const void*)p;
400  return ps;
401 }
402 
403 template<class S, class T>
404 inline PrettyOStream<S> &
406  ps.os << "intrusive_ptr->";
407  return ps << t.get();
408 }
409 
410 #endif // XAPIAN_INCLUDED_PRETTY_H
A cursor pointing to a position in a Btree table, for reading several entries in order,...
Definition: glass_cursor.h:148
A backend designed for efficient indexing and retrieval, using compressed posting lists and a btree s...
Class managing a Btree table in a Glass database.
Definition: glass_table.h:432
A RemoteConnection object provides a bidirectional connection to another RemoteConnection object on a...
Class to represent cluster centroids in the vector space.
Definition: cluster.h:342
Class for storing the results returned by the Clusterer.
Definition: cluster.h:452
Class to represents a Cluster which contains Points and Centroid of the Cluster.
Definition: cluster.h:370
Virtual base class for Database internals.
Virtual base class for expand decider functor.
Definition: expanddecider.h:38
Base class for TermListGroup Stores and provides terms that are contained in a document and their res...
Definition: cluster.h:136
Collates statistics while calculating term weight in an ESet.
Definition: expandweight.h:37
Class for calculating ESet term weights.
Definition: expandweight.h:110
A smart pointer that uses intrusive reference counting.
Definition: intrusive_ptr.h:83
Virtual base class for key making functors.
Definition: keymaker.h:44
Base class for calculating distances between two lat/long coordinates.
Definition: geospatial.h:302
Abstract base class for match deciders.
Definition: matchdecider.h:37
Abstract class representing a point in the VSM.
Definition: cluster.h:225
Class to represent a document as a point in the Vector Space Model.
Definition: cluster.h:320
Registry for user subclasses.
Definition: registry.h:47
A class for construction of termlists which store the terms for a document along with the number of d...
Definition: cluster.h:188
Suitable for "simple" type T.
Definition: smallvector.h:62
size_type size() const
Definition: smallvector.h:135
Abstract base class for weighting schemes.
Definition: weight.h:38
PositionList * p
Virtual base class for Database internals.
string str(int value)
Convert int to std::string.
Definition: str.cc:91
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
#define XAPIAN_PRETTY_AS_CLASSNAME(C)
Definition: pretty.h:345
PrettyOStream< S > & operator<<(PrettyOStream< S > &ps, const T &t)
Default is to output as std::ostream would.
Definition: pretty.h:57
void write_ch(std::ostream &os, unsigned char ch)
Definition: pretty.h:108
Custom vector implementations using small vector optimisation.
Definition: pretty.h:48
Literal(const char *lit)
Definition: pretty.h:50
const char * _lit
Definition: pretty.h:49
Literal(const std::string &s)
Definition: pretty.h:51
PrettyOStream & operator|(const T &t)
Definition: pretty.h:42
PrettyOStream(S &os_)
Definition: pretty.h:41
S & os
The std::ostream object we're outputting to.
Definition: pretty.h:39
typedefs for Xapian