1 #include "Alignment/Geners/interface/IOException.hh"
9 #include "Alignment/Geners/interface/ClassId.hh"
10 #include "Alignment/Geners/interface/IOException.hh"
11 #include "Alignment/Geners/interface/binaryIO.hh"
16 void ClassId::setVersion(
const unsigned newVersion) {
17 if (version_ != newVersion) {
18 version_ = newVersion;
21 const std::size_t lastOpen = id_.find_last_of(
'(');
22 assert(lastOpen != std::string::npos);
24 std::ostringstream os;
25 os << id_.substr(0, lastOpen) <<
'(' << version_ <<
')';
32 void ClassId::ensureSameId(
const ClassId &
id)
const {
34 throw gs::IOInvalidArgument(
"In gs::ClassId::ensureSameId: reference id is not valid");
36 throw gs::IOInvalidArgument(
"In gs::ClassId::ensureSameId: argument id is not valid");
38 std::ostringstream os;
39 os <<
"In gs::ClassId::ensureSameId: expected \"" << id_ <<
"\", got \"" <<
id.id_ <<
"\"";
40 throw gs::IOInvalidArgument(os.str());
44 void ClassId::ensureSameName(
const ClassId &
id)
const {
46 throw gs::IOInvalidArgument(
"In gs::ClassId::ensureSameName: reference id is not valid");
48 throw gs::IOInvalidArgument(
"In gs::ClassId::ensureSameName: argument id is not valid");
49 if (name_ !=
id.name_) {
50 std::ostringstream os;
51 os <<
"In gs::ClassId::ensureSameName: expected class name \"" << name_ <<
"\", got \"" <<
id.name_ <<
"\"";
52 throw gs::IOInvalidArgument(os.str());
56 void ClassId::ensureSameVersion(
const ClassId &
id)
const {
58 throw gs::IOInvalidArgument(
"In gs::ClassId::ensureSameVersion: reference id is not valid");
60 throw gs::IOInvalidArgument(
"In gs::ClassId::ensureSameVersion: argument id is not valid");
61 if (version_ !=
id.version_) {
62 std::ostringstream os;
63 os <<
"In gs::ClassId::ensureSameVersion: expected version " << version_ <<
" for class " <<
id.name_ <<
", got "
65 throw gs::IOInvalidArgument(os.str());
69 void ClassId::ensureVersionInRange(
const unsigned vmin,
const unsigned vmax)
const {
71 throw gs::IOInvalidArgument(
"In gs::ClassId::ensureVersionInRange: id is not valid");
72 if (version_ < vmin || version_ > vmax) {
73 std::ostringstream os;
74 os <<
"In gs::ClassId::ensureVersionInRange: expected version"
75 <<
" number for class " << name_ <<
" to be in range [" << vmin <<
", " << vmax <<
"], got " << version_;
76 throw gs::IOInvalidArgument(os.str());
80 bool ClassId::validatePrefix(
const char *
prefix) {
84 const unsigned len = strlen(
prefix);
91 bool inVersion =
false;
93 for (
unsigned i = 0;
i < len; ++
i) {
97 if (inVersion ||
i == 0)
110 unsigned long dummy = strtoul(
prefix + vstart, &endptr, 10);
124 std::ostringstream os;
125 if (!validatePrefix(
prefix)) {
127 os <<
"In gs::ClassId::initialize: bad class name prefix \"" <<
prefix
128 <<
"\". Check for problematic parentheses.";
130 os <<
"In gs::ClassId::initialize: NULL class name prefix.";
131 throw gs::IOInvalidArgument(os.str());
143 bool ClassId::makeName() {
145 char *
buf = localbuf;
146 const unsigned idLen = id_.size();
148 buf =
new char[idLen + 1
U];
149 const char *from = id_.data();
150 bool inVersion =
false;
152 for (
unsigned ifrom = 0; ifrom < idLen; ++ifrom) {
153 if (from[ifrom] ==
'(') {
160 }
else if (from[ifrom] ==
')') {
167 }
else if (!inVersion)
168 buf[ito++] = from[ifrom];
183 bool ClassId::makeVersion() {
184 bool correct =
false;
185 const unsigned ns = id_.size();
186 const char *
const buf = id_.data();
187 const char *sep =
buf + (ns - 1
U);
194 const char *closingBrace = sep;
195 for (; sep !=
buf; --sep)
200 version_ = strtoul(sep + 1, &endptr, 10);
201 if (endptr > sep + 1 && endptr == closingBrace)
208 ClassId::ClassId(
const std::string &
id) : id_(
id) {
209 if (!(!id_.empty() && makeName() && makeVersion())) {
210 std::ostringstream os;
211 os <<
"In gs::ClassId::ClassId(const std::string&): "
212 <<
"invalid input id string \"" << id_ <<
"\"";
213 throw gs::IOInvalidArgument(os.str());
217 ClassId::ClassId(std::istream &
in,
int) {
221 "In gs::ClassId::ClassId(std::istream&, int): "
222 "input stream failure");
224 if (!(!id_.empty() && makeName() && makeVersion())) {
225 std::ostringstream os;
226 os <<
"In gs::ClassId::ClassId(std::istream&, int): "
227 <<
"read invalid id string \"" << id_ <<
"\"";
228 throw IOInvalidData(os.str());
237 ClassId::ClassId() : name_(
""), id_(
"(0)"), version_(0
U), isPtr_(
false) {}
239 ClassId ClassId::invalidId() {
244 bool ClassId::isTemplate()
const {
245 const std::size_t leftBrak = id_.find(
'<');
246 const std::size_t rightBrak = id_.rfind(
'>');
247 return leftBrak != std::string::npos && rightBrak != std::string::npos && leftBrak < rightBrak;
250 void ClassId::templateParameters(std::vector<std::vector<ClassId>> *
params)
const {
253 const std::size_t leftBrak = id_.find(
'<');
254 const std::size_t rightBrak = id_.rfind(
'>');
255 if (leftBrak != std::string::npos && rightBrak != std::string::npos && leftBrak < rightBrak) {
257 unsigned ncommas = 0;
259 for (std::size_t
pos = leftBrak + 1;
pos < rightBrak; ++
pos) {
260 const char c = id_[
pos];
265 else if (
c ==
',' && nbrackets == 0)
271 std::ostringstream os;
272 os <<
"In gs::ClassId::templateParameters: "
273 <<
"unbalanced angle brackets in the "
274 <<
"template id \"" << id_ <<
"\"";
275 throw gs::IOInvalidArgument(os.str());
279 params->resize(ncommas + 1);
280 for (
unsigned i = 0;
i <= ncommas; ++
i)
286 std::size_t
begin = leftBrak + 1;
288 const char c = id_[
pos];
293 else if (
c ==
',' && nbrackets == 0) {
294 while (isspace(id_[
begin]))
296 std::size_t
end =
pos - 1;
297 while (isspace(id_[
end]))
300 (*params)[ncommas].push_back(ClassId(id_.substr(
begin,
end -
begin)));
305 while (isspace(id_[
begin]))
307 std::size_t
end = rightBrak - 1;
308 while (isspace(id_[
end]))
311 (*params)[ncommas].push_back(ClassId(id_.substr(
begin,
end -
begin)));