Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Auto Value

0.00/5 (No votes)
27 Feb 2004 1  
Auto Value is an implementation of variables having undefined state

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; } 
 
    /* Idea of val methods - handmade inheritance. */
    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 :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here