vsl_indent.cxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_indent.cxx
2 //:
3 // \file
4 
5 #include <iostream>
6 #include <map>
7 #include <utility>
8 #include "vsl_indent.h"
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 
13 constexpr int default_tab = 2;
14 
15 typedef std::pair<int,int> indent_data_type;
16 
17 // Get pointer to tab and indent data for os
18 indent_data_type* indent_data(std::ostream& os)
19 {
20  typedef std::map<void*, indent_data_type, std::less<void*> > maps2i_type;
21  // Global record of tab information for streams.
22  // Allows data to persist beyond the lifetime of the indent object itself,
23  // which may be mercifully brief
24  static maps2i_type indent_data_map;
25 
26  auto entry = indent_data_map.find(&os);
27  if (entry==indent_data_map.end())
28  {
29  // Create a new entry
30  indent_data_map[&os]=indent_data_type(0,default_tab);
31  entry = indent_data_map.find(&os);
32  }
33 
34  return &((*entry).second);
35 }
36 
37 //: Increments current indent for given stream
38 void vsl_indent_inc(std::ostream& os)
39 {
40  indent_data(os)->first++;
41 }
42 
43 //: Decrements current indent for given stream
44 void vsl_indent_dec(std::ostream& os)
45 {
46  indent_data(os)->first--;
47 }
48 
49 //: Set number of spaces per increment step
50 void vsl_indent_set_tab(std::ostream& os, int t)
51 {
52  indent_data(os)->second = t;
53 }
54 
55 //: Number of spaces per increment step
56 int vsl_indent_tab(std::ostream& os)
57 {
58  return indent_data(os)->second;
59 }
60 
61 //: Set indentation to zero
62 void vsl_indent_clear(std::ostream& os)
63 {
64  indent_data(os)->first =0;
65 }
66 
67 std::ostream& operator<<(std::ostream& os, const vsl_indent& /*indent*/)
68 {
69  indent_data_type* data = indent_data(os);
70 
71  int n = data->first * data->second;
72  for (int i=0;i<n;i++) os<<' ';
73  return os;
74 }
75 
76 //: Tidy up the internal indent map to remove potential memory leaks
77 // The details of indents for each stream are stored in a static
78 // map. When testing for memory leaks, this is flagged, creating
79 // lots of noise in the output of memory leak checkers.
80 // This call empties the map, removing the potential leak.
81 // Pragmatically it is called in the vsl_delete_all_loaders()
82 //
83 // This should no longer be needed, since that static map was made a static
84 // inside the function indent_data() instead of a global one. - PVr.
86 {
87 }
88 
89 // removed explicit instantiation of map<void*, pair<int, int> > -- fsm.
indent_data_type * indent_data(std::ostream &os)
Definition: vsl_indent.cxx:18
void vsl_indent_set_tab(std::ostream &os, int t)
Set number of spaces per increment step.
Definition: vsl_indent.cxx:50
void vsl_indent_clear_all_data()
Tidy up the internal indent map to remove potential memory leaks.
Definition: vsl_indent.cxx:85
int vsl_indent_tab(std::ostream &os)
Number of spaces per increment step.
Definition: vsl_indent.cxx:56
constexpr int default_tab
Definition: vsl_indent.cxx:13
void vsl_indent_inc(std::ostream &os)
Increments current indent for given stream.
Definition: vsl_indent.cxx:38
Put indents into output streams, to produce more legible printed output.
Definition: vsl_indent.h:88
void vsl_indent_dec(std::ostream &os)
Decrements current indent for given stream.
Definition: vsl_indent.cxx:44
std::ostream & operator<<(std::ostream &os, const vsl_indent &)
Outputs current indent to os.
Definition: vsl_indent.cxx:67
void vsl_indent_clear(std::ostream &os)
Set indentation to zero.
Definition: vsl_indent.cxx:62
std::pair< int, int > indent_data_type
Definition: vsl_indent.cxx:15