Atlas  0.7.0
Networking protocol for the Worldforge system.
PresentationBridge.cpp
1 /*
2  Copyright (C) 2013 Erik Ogenvik
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 2 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, write to the Free Software
16  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #include "PresentationBridge.h"
20 
21 namespace Atlas
22 {
23 
24 PresentationBridge::PresentationBridge(std::ostream& stream) :
25  mStream(stream), mMaxItemsPerLevel(0), mIsSkipEntry(false), mStartFilterLevel(1)
26 {
27  mStream.precision(6);
28 }
29 
30 void PresentationBridge::streamBegin()
31 {
32  addPadding();
33 }
34 void PresentationBridge::streamMessage()
35 {
36  addPadding();
37 }
38 void PresentationBridge::streamEnd()
39 {
40  removePadding();
41 }
42 
43 void PresentationBridge::mapMapItem(std::string name)
44 {
45  if (checkAndUpdateMaxItemCounter()) {
46  mStream << mPadding << name << std::endl;
47  }
48  addPadding();
49 }
50 void PresentationBridge::mapListItem(std::string name)
51 {
52  if (checkAndUpdateMaxItemCounter()) {
53  mStream << mPadding << name << std::endl;
54  }
55  mMapsInList.push(0);
56  addPadding();
57 }
58 void PresentationBridge::mapIntItem(std::string name, std::int64_t i)
59 {
60  if (checkAndUpdateMaxItemCounter()) {
61  mStream << mPadding << name << ": " << i << std::endl;
62  }
63 }
64 void PresentationBridge::mapFloatItem(std::string name, double d)
65 {
66  if (checkAndUpdateMaxItemCounter()) {
67  mStream << mPadding << name << ": " << std::fixed << d << std::endl;
68  }
69 }
70 void PresentationBridge::mapStringItem(std::string name, std::string s)
71 {
72  if (checkAndUpdateMaxItemCounter()) {
73  mStream << mPadding << name << ": " << s << std::endl;
74  }
75 }
76 void PresentationBridge::mapNoneItem(std::string name) {
77  if (checkAndUpdateMaxItemCounter()) {
78  mStream << mPadding << name << ": " << std::endl;
79  }
80 }
81 
82 void PresentationBridge::mapEnd()
83 {
84  removePadding();
85 }
86 
87 void PresentationBridge::listMapItem()
88 {
89  int items = mMapsInList.top();
90  if (checkAndUpdateMaxItemCounter()) {
91  //Check if we've already printed a map for this list, and if so print a separator
92  if (items) {
93  mStream << mPadding << "---" << std::endl;
94  }
95  }
96  mMapsInList.pop();
97  mMapsInList.push(items + 1);
98  addPadding();
99 }
100 
101 void PresentationBridge::listListItem()
102 {
103  mMapsInList.push(0);
104  addPadding();
105 }
106 void PresentationBridge::listIntItem(std::int64_t i)
107 {
108  if (checkAndUpdateMaxItemCounter()) {
109  mStream << mPadding << ": " << i << std::endl;
110  }
111 }
112 void PresentationBridge::listFloatItem(double d)
113 {
114  if (checkAndUpdateMaxItemCounter()) {
115  mStream << mPadding << ": " << d << std::endl;
116  }
117 }
118 void PresentationBridge::listStringItem(std::string s)
119 {
120  if (checkAndUpdateMaxItemCounter()) {
121  mStream << mPadding << ": " << s << std::endl;
122  }
123 }
124 void PresentationBridge::listNoneItem() {
125  if (checkAndUpdateMaxItemCounter()) {
126  mStream << mPadding << ": " << std::endl;
127  }
128 }
129 void PresentationBridge::listEnd()
130 {
131  mMapsInList.pop();
132  removePadding();
133 }
134 void PresentationBridge::addPadding()
135 {
136  mEntriesPerLevelCounter.push_back(0);
137  mPadding += " ";
138 }
139 
140 void PresentationBridge::removePadding()
141 {
142  size_t itemsThisLevel = mEntriesPerLevelCounter.back();
143  bool wasSkipping = mIsSkipEntry;
144  mEntriesPerLevelCounter.pop_back();
145  if (mMaxItemsPerLevel) {
146  size_t level = 0;
147  for (size_t entry : mEntriesPerLevelCounter) {
148  mIsSkipEntry = entry >= mMaxItemsPerLevel && level >= mStartFilterLevel;
149  level++;
150  }
151  }
152 
153  if (wasSkipping != mIsSkipEntry) {
154  mStream << mPadding << "... (" << (itemsThisLevel - mMaxItemsPerLevel) << " more items) ..." << std::endl;
155  }
156 
157  mPadding.erase(mPadding.end() - 2, mPadding.end());
158 }
159 
160 void PresentationBridge::setMaxItemsPerLevel(size_t maxItems)
161 {
162  mMaxItemsPerLevel = maxItems;
163 }
164 
165 void PresentationBridge::setStartFilteringLevel(size_t startFilteringLevel)
166 {
167  mStartFilterLevel = startFilteringLevel;
168 }
169 
170 bool PresentationBridge::checkAndUpdateMaxItemCounter()
171 {
172 
173  if (mMaxItemsPerLevel && !mEntriesPerLevelCounter.empty() && mEntriesPerLevelCounter.size() > mStartFilterLevel) {
174  size_t& itemsInLevel = mEntriesPerLevelCounter.back();
175  itemsInLevel++;
176  if (itemsInLevel >= mMaxItemsPerLevel) {
177  mIsSkipEntry = true;
178  }
179  if (mIsSkipEntry) {
180  return false;
181  }
182  }
183  return true;
184 }
185 
186 
187 }
Definition: Bridge.h:21