Introduction
Sometimes in programming it is necessary to represent values having "undefined" or "unknown" values.
Let's say you have your own objects or structures persisted in XML. While reading XML some fields could be omitted and you should deal with this situation somehow. And vice versa, while serializing your classes into XML, it is better not to output unknown values. Just to reduce pollution.
Template below is my attempt to generalize the problem. At least for myself. Hope it will help to anybody else.
To define such Auto Value you should provide base type (T
), some default value(T default_value
) and select some T null_value
which will represent undefined state.
Example 1 - "tristate" values having TRUE
, FALSE
and undefined state:
typedef t_value<BOOL,0,0xFF> tristate_v;
Example 2 - salary value
typedef t_value<DWORD,0,0xFFFFFFFF> salary_v;
Now we can declare our business objects like:
struct employee
{
tristate_v is_married;
salary_v salary;
};
One obvious advantage of value_t is that it takes as much space in memory as its base type and its values initialized by default. Therefore you don't need to write long list of initializers for your structures.
Implementation
Implementation is pretty straightforward and I don't think that anybody needs any comments about it.
template<typename T, T default_value, T null_value >
struct t_value
{
T _v;
t_value(): _v(null_value) {}
t_value(const T& iv): _v(iv) {}
t_value(const t_value& c): _v(c._v) {}
operator T() const { return _v == null_value? default_value: _v; }
t_value& operator = (T nv) { _v = nv; return *this; }
static T null_val() { return null_value; }
bool undefined() const { return _v == null_value; }
bool defined() const { return _v != null_value; }
void clear() { _v = null_value; }
static T val(const t_value& v1,T defval)
{ return v1.defined()? v1._v:defval; }
static t_value val(const t_value& v1,const t_value& v2)
{
if(v1.defined()) return v1;
return v2;
}
static t_value val(const t_value& v1,const t_value& v2,
const t_value& v3)
{
if(v1.defined()) return v1;
if(v2.defined()) return v2;
return v3;
}
static T val(const t_value& v1,const t_value& v2,T defval)
{
t_value tv = val(v1,v2);
return tv.defined()? tv._v: defval;
}
static T val(const t_value& v1,const t_value& v2,
const t_value& v3,T defval)
{
t_value tv = val(v1,v2,v3);
return tv.defined()? tv._v: defval;
}
};
And of course I am using this template for storing HTML attributes in my HTMEngine :)