w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RlinkAddrMap.cpp
Go to the documentation of this file.
1// $Id: RlinkAddrMap.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2011-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2018-11-16 1070 1.0.4 use auto; use emplace,make_pair; use range loop
8// 2017-04-07 868 1.0.3 Dump(): add detail arg
9// 2013-02-03 481 1.0.2 use Rexception
10// 2011-11-28 434 1.0.1 Print(): use proper cast for lp64 compatibility
11// 2011-03-06 367 1.0 Initial version
12// 2011-03-05 366 0.1 First draft
13// ---------------------------------------------------------------------------
14
19#include <algorithm>
20
21#include "RlinkAddrMap.hpp"
22
23#include "librtools/RosFill.hpp"
26
27using namespace std;
28
34// all method definitions in namespace Retro
35namespace Retro {
36
37//------------------------------------------+-----------------------------------
39
41 : fNameMap(),
42 fAddrMap(),
43 fMaxLength(0)
44{}
45
46//------------------------------------------+-----------------------------------
48
50{}
51
52//------------------------------------------+-----------------------------------
54
56{
57 fNameMap.clear();
58 fAddrMap.clear();
59 return;
60}
61
62//------------------------------------------+-----------------------------------
64
65bool RlinkAddrMap::Insert(const std::string& name, uint16_t addr)
66{
67 if (fNameMap.find(name) != fNameMap.end()) return false;
68 if (fAddrMap.find(addr) != fAddrMap.end()) return false;
69
70 fNameMap.emplace(make_pair(name, addr));
71 fAddrMap.emplace(make_pair(addr, name));
72 fMaxLength = max(fMaxLength, name.length());
73
74 return true;
75}
76
77//------------------------------------------+-----------------------------------
79
80bool RlinkAddrMap::Erase(const std::string& name)
81{
82 auto it = fNameMap.find(name);
83 if (it == fNameMap.end()) return false;
84
85 fMaxLength = 0; // force recalculate
86 if (fNameMap.erase(name) == 0)
87 throw Rexception("RlinkAddrMap::Erase()",
88 "BugCheck: fNameMap erase failed");
89 if (fAddrMap.erase(it->second) == 0)
90 throw Rexception("RlinkAddrMap::Erase()",
91 "BugCheck: fAddrMap erase failed");
92
93 return true;
94}
95
96//------------------------------------------+-----------------------------------
98
99bool RlinkAddrMap::Erase(uint16_t addr)
100{
101 auto it = fAddrMap.find(addr);
102 if (it == fAddrMap.end()) return false;
103
104 fMaxLength = 0; // force recalculate
105 if (fAddrMap.erase(addr) == 0)
106 throw Rexception("RlinkAddrMap::Erase()",
107 "BugCheck: fAddrMap erase failed");
108 if (fNameMap.erase(it->second) == 0)
109 throw Rexception("RlinkAddrMap::Erase()",
110 "BugCheck: fNameMap erase failed");
111
112 return true;
113}
114
115//------------------------------------------+-----------------------------------
117
118bool RlinkAddrMap::Find(const std::string& name, uint16_t& addr) const
119{
120 auto it = fNameMap.find(name);
121 if (it == fNameMap.end()) return false;
122
123 addr = it->second;
124
125 return true;
126}
127
128//------------------------------------------+-----------------------------------
130
131bool RlinkAddrMap::Find(uint16_t addr, std::string& name) const
132{
133 auto it = fAddrMap.find(addr);
134 if (it == fAddrMap.end()) return false;
135
136 name = it->second;
137
138 return true;
139}
140
141//------------------------------------------+-----------------------------------
143
145{
146 if (fMaxLength == 0) {
147 for (auto& o: fAddrMap) {
148 fMaxLength = max(fMaxLength, o.second.length());
149 }
150 }
151 return fMaxLength;
152}
153
154//------------------------------------------+-----------------------------------
156
157void RlinkAddrMap::Print(std::ostream& os, int ind) const
158{
159 size_t maxlen = max(size_t(6), MaxNameLength());
160
161 RosFill bl(ind);
162 for (auto& o: fAddrMap) {
163 os << bl << RosPrintf(o.second.c_str(), "-s",maxlen)
164 << " : " << RosPrintf(o.first, "$x0", 6)
165 << " " << RosPrintf(o.first, "o0", 6) << endl;
166 }
167
168 return;
169}
170
171//------------------------------------------+-----------------------------------
173
174void RlinkAddrMap::Dump(std::ostream& os, int ind, const char* text,
175 int detail) const
176{
177 RosFill bl(ind);
178 os << bl << (text?text:"--") << "RlinkAddrMap @ " << this << endl;
179 if (detail < 0) {
180 os << bl << " fAddrMap.size: " << fAddrMap.size() << endl;
181 } else {
182 Print(os,ind+2);
183 }
184 return;
185}
186
187} // end namespace Retro
FIXME_docs.
Definition: Rexception.hpp:29
size_t MaxNameLength() const
FIXME_docs.
nmap_t fNameMap
name->addr map
size_t fMaxLength
max name length
amap_t fAddrMap
addr->name map
bool Erase(const std::string &name)
FIXME_docs.
void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
~RlinkAddrMap()
Destructor.
bool Insert(const std::string &name, uint16_t addr)
FIXME_docs.
bool Find(const std::string &name, uint16_t &addr) const
FIXME_docs.
void Print(std::ostream &os, int ind=0) const
FIXME_docs.
RlinkAddrMap()
Default constructor.
void Clear()
FIXME_docs.
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
RosPrintfS< bool > RosPrintf(bool value, const char *form=0, int width=0, int prec=0)
Creates a print object for the formatted output of a bool value.
Definition: RosPrintf.ipp:38
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47