I'm trying to write a template class that will wrap a type that could be either a primitive (uint8_t up to float) or a char* string. If the class wraps a primitive type, setting its value to any primitive will do a straight cast, while setting a char* will to an ascii to primitive conversion (atoi, sprintf, etc). If it wraps a char*, the class should manage an internal char* buffer and convert primitive to char*.
If I limit my example to only uint32_t values, this is the template that would deal with any primitive type (with obvious error checking details omitted):
template<typename T>
class Measurement
{
public:
Measurement();
~Measurement();
void setU32Value(uint32_t value);
void setStringValue(const char* std);
uint32_t asU32() const;
const char* asString() const;
private:
T _value;
};
template<typename T>
void Measurement<T>::setU32Value(uint32_t value)
{
_value = (T)value;
}
template<typename T>
void Measurement<T>::setStringValue(const char* value)
{
_value = (T)::atoi(value);
}
template<typename T>
uint32_t Measurement<T>::asU32() const
{
return (uint32_t)_value;
}
template<typename T>
const char* Measurement<T>::asString() const
{
ostringstream oss;
oss << _value;
return oss.str();
}
But it won't work if I try to Measurement<char*> because it won't convert setU32Value wont' convert "value" to a string representation and the asU32() won't convert it back.
Can anyone suggest a way to make this work?
Thanks.
Aucun commentaire:
Enregistrer un commentaire