Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 38ff857

Browse files
committed
Integer template specializations: use builtin types instead of stdint
Template resolution is tied to name mangling. Since we can't know for sure which builtin type is used for each of int64_t, uint32_t, etc. we can't rely on these typedefs. Builtin types, however, are reliable. As an exemple: on some ABIs, size_t can be a long long but uint64_t a mere long which means that for this particular platform, a call with a size_t argument will not match a template instantiation with a uint64_t. Signed-off-by: David Wagner <[email protected]>
1 parent 155f46f commit 38ff857

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

utility/convert.hpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,43 @@ struct ConvertionAllowed<bool> : std::true_type
5252
{
5353
};
5454
template <>
55-
struct ConvertionAllowed<uint64_t> : std::true_type
55+
struct ConvertionAllowed<long long> : std::true_type
5656
{
5757
};
5858
template <>
59-
struct ConvertionAllowed<int64_t> : std::true_type
59+
struct ConvertionAllowed<unsigned long long> : std::true_type
6060
{
6161
};
6262
template <>
63-
struct ConvertionAllowed<uint32_t> : std::true_type
63+
struct ConvertionAllowed<long> : std::true_type
6464
{
6565
};
6666
template <>
67-
struct ConvertionAllowed<int32_t> : std::true_type
67+
struct ConvertionAllowed<unsigned long> : std::true_type
6868
{
6969
};
7070
template <>
71-
struct ConvertionAllowed<uint16_t> : std::true_type
71+
struct ConvertionAllowed<int> : std::true_type
7272
{
7373
};
7474
template <>
75-
struct ConvertionAllowed<int16_t> : std::true_type
75+
struct ConvertionAllowed<unsigned int> : std::true_type
7676
{
7777
};
7878
template <>
79-
struct ConvertionAllowed<int8_t> : std::true_type
79+
struct ConvertionAllowed<short> : std::true_type
8080
{
8181
};
8282
template <>
83-
struct ConvertionAllowed<uint8_t> : std::true_type
83+
struct ConvertionAllowed<unsigned short> : std::true_type
84+
{
85+
};
86+
template <>
87+
struct ConvertionAllowed<unsigned char> : std::true_type
88+
{
89+
};
90+
template <>
91+
struct ConvertionAllowed<signed char> : std::true_type
8492
{
8593
};
8694
template <>
@@ -98,11 +106,11 @@ struct ConvertionAllowedVia : std::false_type
98106
{
99107
};
100108
template <>
101-
struct ConvertionAllowedVia<uint8_t, uint32_t> : std::true_type
109+
struct ConvertionAllowedVia<unsigned char, unsigned int> : std::true_type
102110
{
103111
};
104112
template <>
105-
struct ConvertionAllowedVia<int8_t, int32_t> : std::true_type
113+
struct ConvertionAllowedVia<signed char, int> : std::true_type
106114
{
107115
};
108116

@@ -191,16 +199,16 @@ static inline bool convertTo(const std::string &str, T &result)
191199
return details::convertTo<T>(str, result);
192200
}
193201

194-
/** Specialization for uint8_t of convertTo template function.
202+
/** Specialization for unsigned char of convertTo template function.
195203
*
196204
* This function follows the same paradigm than it's generic version.
197205
*
198-
* The generic version was converting int8 as it was a character
199-
* (uint8_t is an alias to unsigned char on most compiler).
206+
* The generic version was converting char as it was a character
207+
* (unsigned char is an alias to unsigned char on most compiler).
200208
* Thus converting "1" would return 49 ie '1'.
201209
* As convertTo is thought as an _numerical_ convertion tool
202210
* (contrary to boost::lexical_cast for example),
203-
* forbid considering the input as a character and consider uint8_t
211+
* forbid considering the input as a character and consider unsigned char
204212
* (aka unsigned char) as a number exclusively.
205213
*
206214
* @param[in] str the string to parse.
@@ -209,21 +217,20 @@ static inline bool convertTo(const std::string &str, T &result)
209217
* @return true if conversion was successful, false otherwise.
210218
*/
211219
template <>
212-
inline bool convertTo<uint8_t>(const std::string &str, uint8_t &result)
220+
inline bool convertTo<unsigned char>(const std::string &str, unsigned char &result)
213221
{
214-
return details::convertToVia<uint8_t, uint32_t>(str, result);
222+
return details::convertToVia<unsigned char, unsigned int>(str, result);
215223
}
216224

217-
/** Specialization for int8_t of convertTo template function.
225+
/** Specialization for signed char of convertTo template function.
218226
*
219-
* @see convertTo<uint8_t>
227+
* @see convertTo<unsigned char>
220228
*/
221229
template <>
222-
inline bool convertTo<int8_t>(const std::string &str, int8_t &result)
230+
inline bool convertTo<signed char>(const std::string &str, signed char &result)
223231
{
224-
return details::convertToVia<int8_t, int32_t>(str, result);
232+
return details::convertToVia<signed char, int>(str, result);
225233
}
226-
227234
/**
228235
* Specialization for float of convertTo template function.
229236
*

xmlserializer/XmlElement.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,27 @@ bool CXmlElement::CChildIterator::next(CXmlElement &xmlChildElement)
265265
}
266266

267267
template bool CXmlElement::getAttribute(const std::string &name, std::string &value) const;
268-
template bool CXmlElement::getAttribute(const std::string &name, uint16_t &value) const;
269-
template bool CXmlElement::getAttribute(const std::string &name, uint32_t &value) const;
270-
template bool CXmlElement::getAttribute(const std::string &name, int32_t &value) const;
271-
template bool CXmlElement::getAttribute(const std::string &name, uint64_t &value) const;
272268
template bool CXmlElement::getAttribute(const std::string &name, bool &value) const;
273-
template bool CXmlElement::getAttribute(const std::string &name, double &value) const;
269+
template bool CXmlElement::getAttribute(const std::string &name, short &value) const;
270+
template bool CXmlElement::getAttribute(const std::string &name, unsigned short &value) const;
271+
template bool CXmlElement::getAttribute(const std::string &name, int &value) const;
272+
template bool CXmlElement::getAttribute(const std::string &name, unsigned int &value) const;
273+
template bool CXmlElement::getAttribute(const std::string &name, long &value) const;
274+
template bool CXmlElement::getAttribute(const std::string &name, unsigned long &value) const;
275+
template bool CXmlElement::getAttribute(const std::string &name, long long &value) const;
276+
template bool CXmlElement::getAttribute(const std::string &name, unsigned long long &value) const;
274277
template bool CXmlElement::getAttribute(const std::string &name, float &value) const;
278+
template bool CXmlElement::getAttribute(const std::string &name, double &value) const;
275279

276280
template void CXmlElement::setAttribute(const std::string &name, const std::string &value);
277281
template void CXmlElement::setAttribute(const std::string &name, const bool &value);
278-
template void CXmlElement::setAttribute(const std::string &name, const int32_t &value);
279-
template void CXmlElement::setAttribute(const std::string &name, const uint32_t &value);
280-
template void CXmlElement::setAttribute(const std::string &name, const uint64_t &value);
282+
template void CXmlElement::setAttribute(const std::string &name, const short &value);
283+
template void CXmlElement::setAttribute(const std::string &name, const unsigned short &value);
284+
template void CXmlElement::setAttribute(const std::string &name, const int &value);
285+
template void CXmlElement::setAttribute(const std::string &name, const unsigned int &value);
286+
template void CXmlElement::setAttribute(const std::string &name, const long &value);
287+
template void CXmlElement::setAttribute(const std::string &name, const unsigned long &value);
288+
template void CXmlElement::setAttribute(const std::string &name, const long long &value);
289+
template void CXmlElement::setAttribute(const std::string &name, const unsigned long long &value);
281290
template void CXmlElement::setAttribute(const std::string &name, const float &value);
291+
template void CXmlElement::setAttribute(const std::string &name, const double &value);

0 commit comments

Comments
 (0)