Skip to content

Commit 208d078

Browse files
authored
Merge pull request #57 from njoy/feature/type-cleanup
Feature/type cleanup
2 parents 3ec3ec3 + 8354b1c commit 208d078

File tree

13 files changed

+437
-356
lines changed

13 files changed

+437
-356
lines changed

autogen/json2class.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,18 @@ std::string escape(const std::string &str)
237237
// for initialization, as with: m.type var{return value of this function}
238238
std::string initializer(const InfoMetadata &m)
239239
{
240-
// Leave an empty (or all-whitespace) m.defaultValue as-is;
241-
// this is the "no initializer" case
240+
// Leave an empty or all-whitespace m.defaultValue as-is.
241+
// This is the "no initializer" case.
242242
if (allws(m.defaultValue))
243243
return "";
244244

245-
// If of string type, add double quotes;
246-
// we assume that the input does NOT have those already!
247-
if (m.type == "std::string" ||
248-
m.type == "UTF8Text" ||
249-
m.type == "XMLName")
245+
// If of string type, add double quotes.
246+
// We assume that the input does NOT have the quotes already!
247+
// Note: we may not need all of these, but the GNDS manual does describe
248+
// several string-like types, so we might as well include them.
249+
if (m.type == "XMLName" || m.type == "UTF8Text" ||
250+
m.type == "printableText" || m.type == "quotedText" ||
251+
m.type == "tdText" || m.type == "string" || m.type == "std::string")
250252
return '"' + escape(m.defaultValue) + '"';
251253

252254
// Leave as-is;
@@ -2127,31 +2129,41 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
21272129
writer out(per.cppPython);
21282130

21292131
static const std::map<std::string,std::pair<std::string,std::string>> map = {
2132+
// ---------------------- ----------- -----------------
21302133
// In per.dataType or The A name to use
21312134
// in per.metadata's appropriate for the function
21322135
// valueType defaultValue C++ type that returns them
2136+
// ---------------------- ----------- -----------------
21332137

2134-
// Described in the GNDS manual
2138+
// Described in the GNDS manual.
2139+
// I'm not sure which of the several types that map to std::string can,
2140+
// or would, appear in any GNDS specifications in such a way that we'd
2141+
// need it here, but listing all "string-like" types shouldn't hurt.
21352142
{ "Integer32" , { "int" , "ints" } },
2143+
{ "UInteger32" , { "unsigned" , "uints" } },
21362144
{ "Float64" , { "double" , "doubles" } },
21372145
{ "XMLName" , { "std::string" , "strings" } },
21382146
{ "UTF8Text" , { "std::string" , "strings" } },
2147+
{ "printableText" , { "std::string" , "strings" } },
2148+
{ "quotedText" , { "std::string" , "strings" } },
2149+
{ "tdText" , { "std::string" , "strings" } },
21392150

2140-
// Our versions of the above
2151+
// Our versions of the above.
21412152
{ "int" , { "int" , "ints" } },
2153+
{ "unsigned" , { "unsigned" , "uints" } },
21422154
{ "double" , { "double" , "doubles" } },
21432155
{ "string" , { "std::string" , "strings" } },
21442156
{ "std::string" , { "std::string" , "strings" } },
21452157

2146-
// Allow other sensible things
2158+
// Allow other sensible things.
21472159
{ "char" , { "char" , "chars" } },
21482160
{ "signed char" , { "signed char" , "schars" } },
21492161
{ "short" , { "short" , "shorts" } },
21502162
{ "long" , { "long" , "longs" } },
21512163
{ "long long" , { "long long" , "longlongs" } },
21522164
{ "unsigned char" , { "unsigned char" , "uchars" } },
21532165
{ "unsigned short" , { "unsigned short" , "ushorts" } },
2154-
{ "unsigned int" , { "unsigned int" , "uints" } },
2166+
{ "unsigned int" , { "unsigned" , "uints" } },
21552167
{ "unsigned long" , { "unsigned long" , "ulongs" } },
21562168
{ "unsigned long long" , { "unsigned long long" , "ulonglongs" } },
21572169
{ "float" , { "float" , "floats" } },

src/GNDStk/BodyText/src/assign.hpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,10 @@ operator=(const std::vector<T> &vec)
5353
// set the raw string to "", because it's no longer considered meaningful
5454
rawstring = "";
5555

56-
// length, start
56+
// length, start, valueType
5757
length(vec.size());
5858
start(0);
59-
60-
// valueType: best guess
61-
if constexpr (std::is_same_v<T,Integer32>)
62-
valueType("Integer32");
63-
else if constexpr (std::is_same_v<T,Float64>)
64-
valueType("Float64");
65-
else if constexpr (std::is_same_v<T,UTF8Text>)
66-
valueType("UTF8Text");
67-
else
68-
valueType("");
59+
valueType(detail::MapTypeString<T>::value[0]);
6960

7061
// assign vector
7162
if constexpr (runtime)

src/GNDStk/BodyText/src/get.hpp

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -174,33 +174,13 @@ std::enable_if_t<
174174
// length, start, and valueType.
175175

176176
// For this get(), the caller has stipulated a particular vector type.
177-
// We'll print a warning if that type appears to conflict with valueType,
178-
// or if valueType isn't something we know what to do with. In any event,
179-
// we'll return what the caller requested. Note that valueType = "" (the
180-
// empty string) is acceptable with any element type.
181-
182-
bool consistent = true;
183-
if (valueType() == "Integer32") {
184-
if (!std::is_same_v<T,Integer32>)
185-
consistent = false;
186-
} else if (valueType() == "Float64") {
187-
if (!std::is_same_v<T,Float64>)
188-
consistent = false;
189-
} else if (valueType() == "UTF8Text") {
190-
if (!std::is_same_v<T,UTF8Text>)
191-
consistent = false;
192-
} else if (valueType() != "") {
177+
// We'll print a warning if that vector type appears to conflict with
178+
// valueType. Regardless, we'll return what the caller requested. Note
179+
// that valueType == "" is acceptable with any element type.
180+
if (valueType() != "" && !detail::MapTypeString<T>::find(valueType())) {
193181
log::warning(
194-
"Unrecognized valueType == \"{}\"; ignoring",
195-
valueType()
196-
);
197-
log::member(context_rebuilding);
198-
}
199-
200-
if (!consistent) {
201-
log::warning(
202-
"Element type T may be inconsistent with valueType == \"{}\";\n",
203-
"we'll create the requested std::vector<T> anyway",
182+
"Vector element type may be inconsistent with valueType \"{}\";\n"
183+
"we'll create the requested std::vector<> anyway",
204184
valueType()
205185
);
206186
log::member(context_rebuilding);
@@ -401,16 +381,13 @@ std::conditional_t<
401381
> get() const
402382
{
403383
if constexpr (runtime) {
404-
if (valueType() == "Integer32")
405-
get<std::vector<Integer32>>();
406-
else if (valueType() == "Float64")
407-
get<std::vector<Float64>>();
408-
else
409-
get<std::vector<std::string>>();
410-
411-
// We can't return the specific variant *alternative* that exists right
412-
// now, because that depended on valueType (run-time). So, we'll return
413-
// the whole variant, for whatever use that might have to a caller.
384+
detail::MapStringType(
385+
valueType(),
386+
[this](auto &&t) { get<std::vector<std::decay_t<decltype(t)>>>(); }
387+
);
388+
// We can't return the specific variant alternative that was just put
389+
// in place; it depended on a run-time check. So, we return the whole
390+
// variant, for whatever use that might have to a caller.
414391
return variant;
415392
} else {
416393
// Simpler, but we do still need a get (in case the *string* is active).
@@ -552,8 +529,8 @@ operator[](const std::size_t n)
552529
template<class D = DATA> \
553530
std::enable_if_t< \
554531
detail::isVoid<D> || \
555-
std::is_same_v<TYPE,D>, std::vector<TYPE> & \
556-
> name() { return get<std::vector<TYPE>>(); } \
532+
std::is_same_v<TYPE,D>, std::vector<TYPE> & \
533+
> name() { return get<std::vector<TYPE>>(); } \
557534
\
558535
template<class D = DATA> \
559536
std::enable_if_t< \
@@ -564,26 +541,23 @@ operator[](const std::size_t n)
564541
template<class D = DATA> \
565542
std::enable_if_t< \
566543
detail::isVoid<D> || \
567-
std::is_same_v<TYPE,D>, TYPE & \
568-
> name(const std::size_t n) { return get<TYPE>(n); }
569-
570-
GNDSTK_MAKE_GETTER(strings, std::string);
571-
GNDSTK_MAKE_GETTER(chars, char);
572-
573-
GNDSTK_MAKE_GETTER(schars, signed char);
574-
GNDSTK_MAKE_GETTER(shorts, short);
575-
GNDSTK_MAKE_GETTER(ints, int);
576-
GNDSTK_MAKE_GETTER(longs, long);
577-
GNDSTK_MAKE_GETTER(longlongs, long long);
578-
579-
GNDSTK_MAKE_GETTER(uchars, unsigned char);
580-
GNDSTK_MAKE_GETTER(ushorts, unsigned short);
581-
GNDSTK_MAKE_GETTER(uints, unsigned int);
582-
GNDSTK_MAKE_GETTER(ulongs, unsigned long);
583-
GNDSTK_MAKE_GETTER(ulonglongs, unsigned long long);
584-
585-
GNDSTK_MAKE_GETTER(floats, float);
586-
GNDSTK_MAKE_GETTER(doubles, double);
587-
GNDSTK_MAKE_GETTER(longdoubles, long double);
544+
std::is_same_v<TYPE,D>, TYPE & \
545+
> name(const std::size_t n) { return get<TYPE>(n); }
546+
547+
GNDSTK_MAKE_GETTER(strings, std::string)
548+
GNDSTK_MAKE_GETTER(chars, char)
549+
GNDSTK_MAKE_GETTER(schars, signed char)
550+
GNDSTK_MAKE_GETTER(shorts, short)
551+
GNDSTK_MAKE_GETTER(ints, int)
552+
GNDSTK_MAKE_GETTER(longs, long)
553+
GNDSTK_MAKE_GETTER(longlongs, long long)
554+
GNDSTK_MAKE_GETTER(uchars, unsigned char)
555+
GNDSTK_MAKE_GETTER(ushorts, unsigned short)
556+
GNDSTK_MAKE_GETTER(uints, unsigned int)
557+
GNDSTK_MAKE_GETTER(ulongs, unsigned long)
558+
GNDSTK_MAKE_GETTER(ulonglongs, unsigned long long)
559+
GNDSTK_MAKE_GETTER(floats, float)
560+
GNDSTK_MAKE_GETTER(doubles, double)
561+
GNDSTK_MAKE_GETTER(longdoubles, long double)
588562

589563
#undef GNDSTK_MAKE_GETTER

src/GNDStk/BodyText/src/params.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Quoted [slightly edited] from the official JSON specification files for GNDS:
1919
For start = N, the first N values are zero and are not stored.
2020
2121
valueType
22-
Specifies the type of data in the body (e.g., Integer32, Float64).
22+
Specifies the type of data in the body (e.g., "Integer32", "Float64").
2323
Only one type of data can be stored in each instance of a values node.
2424
2525
In some places, e.g. the JSON-format GNDS spec files, these are listed in

src/GNDStk/BodyText/src/toNode.hpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,10 @@ void toNode(std::string &text, DERIVED &derived) const
5656
// Compute length, start, and valueType
5757
vars.length = size(); // independent of trim
5858
vars.start = bounds.first; // dependent on trim, per the bounds computation
59-
if constexpr (runtime) {
60-
vars.valueType =
61-
std::holds_alternative<std::vector<Integer32>>(variant) ? "Integer32"
62-
: std::holds_alternative<std::vector<Float64 >>(variant) ? "Float64"
63-
: std::holds_alternative<std::vector<UTF8Text >>(variant) ? "UTF8Text"
64-
: ""; // fallback
65-
} else {
66-
vars.valueType =
67-
std::is_same_v<Integer32,DATA> ? "Integer32"
68-
: std::is_same_v<Float64, DATA> ? "Float64"
69-
: std::is_same_v<UTF8Text, DATA> ? "UTF8Text"
70-
: ""; // fallback
71-
}
59+
if constexpr (runtime)
60+
vars.valueType = detail::visitMapTypeString(variant);
61+
else
62+
vars.valueType = detail::MapTypeString<DATA>::value[0];
7263
pushToDerived(derived);
7364

7465
// Values

src/GNDStk/BodyText/test/assign.test.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,42 @@ SCENARIO("BodyText<DATA == void> assignment operators") {
6767
}
6868
}
6969

70-
// Assign from vector<Integer32>; should set valueType
71-
WHEN("We assign from a vector<Integer32>") {
70+
// Assign from vector<int>; should set valueType
71+
WHEN("We assign from a vector<int>") {
7272
THEN("valueType is set correctly") {
7373
BodyText<true,void> b;
7474

7575
b.string("foo").valueType("unknown");
7676
CHECK(b.valueType() == "unknown");
7777

78-
b = std::vector<Integer32>{{10,20,30}};
78+
b = std::vector<int>{{10,20,30}};
7979
CHECK(b.valueType() == "Integer32");
8080
}
8181
}
8282

83-
// Assign from vector<Float64>; should set valueType
84-
WHEN("We assign from a vector<Float64>") {
83+
// Assign from vector<double>; should set valueType
84+
WHEN("We assign from a vector<double>") {
8585
THEN("valueType is set correctly") {
8686
BodyText<true,void> b;
8787

8888
b.string("foo").valueType("unknown");
8989
CHECK(b.valueType() == "unknown");
9090

91-
b = std::vector<Float64>{{1.23,4.56,7.89}};
91+
b = std::vector<double>{{1.23,4.56,7.89}};
9292
CHECK(b.valueType() == "Float64");
9393
}
9494
}
9595

96-
// For now, non-{Integer32,Float64} sets valueType == ""
97-
WHEN("We assign from a vector<non-{Integer32,Float64}>") {
96+
// For now, non-{int,double} sets valueType == ""
97+
WHEN("We assign from a vector<non-{int,double}>") {
9898
THEN("valueType is set correctly") {
9999
BodyText<true,void> b;
100100

101101
b.string("foo").valueType("unknown");
102102
CHECK(b.valueType() == "unknown");
103103

104104
b = std::vector<char>{'a','b','c'};
105-
CHECK(b.valueType() == "");
105+
CHECK(b.valueType() == "char");
106106
}
107107
}
108108

@@ -172,42 +172,42 @@ SCENARIO("BodyText<DATA != void> assignment operators") {
172172
}
173173
}
174174

175-
// Assign from vector<Integer32>; should set valueType
176-
WHEN("We assign from a vector<Integer32>") {
175+
// Assign from vector<int>; should set valueType
176+
WHEN("We assign from a vector<int>") {
177177
THEN("valueType is set correctly") {
178-
BodyText<true,Integer32> b;
178+
BodyText<true,int> b;
179179

180180
b.string("foo").valueType("unknown");
181181
CHECK(b.valueType() == "unknown");
182182

183-
b = std::vector<Integer32>{{10,20,30}};
183+
b = std::vector<int>{{10,20,30}};
184184
CHECK(b.valueType() == "Integer32");
185185
}
186186
}
187187

188-
// Assign from vector<Float64>; should set valueType
189-
WHEN("We assign from a vector<Float64>") {
188+
// Assign from vector<double>; should set valueType
189+
WHEN("We assign from a vector<double>") {
190190
THEN("valueType is set correctly") {
191-
BodyText<true,Float64> b;
191+
BodyText<true,double> b;
192192

193193
b.string("foo").valueType("unknown");
194194
CHECK(b.valueType() == "unknown");
195195

196-
b = std::vector<Float64>{{1.23,4.56,7.89}};
196+
b = std::vector<double>{{1.23,4.56,7.89}};
197197
CHECK(b.valueType() == "Float64");
198198
}
199199
}
200200

201-
// For now, non-{Integer32,Float64} sets valueType == ""
202-
WHEN("We assign from a vector<non-{Integer32,Float64}>") {
201+
// For now, non-{int,double} sets valueType == ""
202+
WHEN("We assign from a vector<non-{int,double}>") {
203203
THEN("valueType is set correctly") {
204204
BodyText<true,char> b;
205205

206206
b.string("foo").valueType("unknown");
207207
CHECK(b.valueType() == "unknown");
208208

209209
b = std::vector<char>{'a','b','c'};
210-
CHECK(b.valueType() == "");
210+
CHECK(b.valueType() == "char");
211211
}
212212
}
213213

0 commit comments

Comments
 (0)