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 strtoul(
prefix + vstart, &endptr, 10);
123 std::ostringstream
os;
124 if (!validatePrefix(
prefix)) {
126 os <<
"In gs::ClassId::initialize: bad class name prefix \"" <<
prefix 127 <<
"\". Check for problematic parentheses.";
129 os <<
"In gs::ClassId::initialize: NULL class name prefix.";
130 throw gs::IOInvalidArgument(
os.str());
142 bool ClassId::makeName() {
144 char *
buf = localbuf;
145 const unsigned idLen = id_.size();
147 buf =
new char[idLen + 1
U];
148 const char *from = id_.data();
149 bool inVersion =
false;
151 for (
unsigned ifrom = 0; ifrom < idLen; ++ifrom) {
152 if (from[ifrom] ==
'(') {
159 }
else if (from[ifrom] ==
')') {
166 }
else if (!inVersion)
167 buf[ito++] = from[ifrom];
182 bool ClassId::makeVersion() {
183 bool correct =
false;
184 const unsigned ns = id_.size();
185 const char *
const buf = id_.data();
186 const char *sep =
buf + (ns - 1
U);
193 const char *closingBrace = sep;
194 for (; sep !=
buf; --sep)
199 version_ = strtoul(sep + 1, &endptr, 10);
200 if (endptr > sep + 1 && endptr == closingBrace)
207 ClassId::ClassId(
const std::string &
id) : id_(
id) {
208 if (!(!id_.empty() && makeName() && makeVersion())) {
209 std::ostringstream
os;
210 os <<
"In gs::ClassId::ClassId(const std::string&): " 211 <<
"invalid input id string \"" << id_ <<
"\"";
212 throw gs::IOInvalidArgument(
os.str());
216 ClassId::ClassId(std::istream &
in,
int) {
220 "In gs::ClassId::ClassId(std::istream&, int): " 221 "input stream failure");
223 if (!(!id_.empty() && makeName() && makeVersion())) {
224 std::ostringstream
os;
225 os <<
"In gs::ClassId::ClassId(std::istream&, int): " 226 <<
"read invalid id string \"" << id_ <<
"\"";
227 throw IOInvalidData(
os.str());
236 ClassId::ClassId() : name_(
""), id_(
"(0)"), version_(0
U), isPtr_(
false) {}
238 ClassId ClassId::invalidId() {
243 bool ClassId::isTemplate()
const {
244 const std::size_t leftBrak = id_.find(
'<');
245 const std::size_t rightBrak = id_.rfind(
'>');
246 return leftBrak != std::string::npos && rightBrak != std::string::npos && leftBrak < rightBrak;
249 void ClassId::templateParameters(
std::vector<std::vector<ClassId>> *
params)
const {
252 const std::size_t leftBrak = id_.find(
'<');
253 const std::size_t rightBrak = id_.rfind(
'>');
254 if (leftBrak != std::string::npos && rightBrak != std::string::npos && leftBrak < rightBrak) {
256 unsigned ncommas = 0;
258 for (std::size_t
pos = leftBrak + 1;
pos < rightBrak; ++
pos) {
259 const char c = id_[
pos];
264 else if (
c ==
',' && nbrackets == 0)
270 std::ostringstream
os;
271 os <<
"In gs::ClassId::templateParameters: " 272 <<
"unbalanced angle brackets in the " 273 <<
"template id \"" << id_ <<
"\"";
274 throw gs::IOInvalidArgument(
os.str());
278 params->resize(ncommas + 1);
279 for (
unsigned i = 0;
i <= ncommas; ++
i)
285 std::size_t begin = leftBrak + 1;
286 for (std::size_t
pos = begin;
pos < rightBrak; ++
pos) {
287 const char c = id_[
pos];
292 else if (
c ==
',' && nbrackets == 0) {
293 while (isspace(id_[begin]))
295 std::size_t end =
pos - 1;
296 while (isspace(id_[end]))
299 (*params)[ncommas].push_back(ClassId(id_.substr(begin, end - begin)));
304 while (isspace(id_[begin]))
306 std::size_t end = rightBrak - 1;
307 while (isspace(id_[end]))
310 (*params)[ncommas].push_back(ClassId(id_.substr(begin, end - begin)));
static AlgebraicMatrix initialize()