Click here to Skip to main content
15,880,427 members
Articles / Desktop Programming / WTL
Article

Auto Value

Rate me:
Please Sign up or sign in to vote.
3.92/5 (6 votes)
27 Feb 2004BSD1 min read 48.4K   13   6
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, along with any associated source code and files, is licensed under The BSD License


Written By
Founder Terra Informatica Software
Canada Canada
Andrew Fedoniouk.

MS in Physics and Applied Mathematics.
Designing software applications and systems since 1991.

W3C HTML5 Working Group, Invited Expert.

Terra Informatica Software, Inc.
http://terrainformatica.com

Comments and Discussions

 
Generaldon't use null_value Pin
Michal Mecinski29-Feb-04 0:53
Michal Mecinski29-Feb-04 0:53 
GeneralRe: don't use null_value Pin
c-smile29-Feb-04 7:18
c-smile29-Feb-04 7:18 
GeneralSuggestion Pin
kaaos28-Feb-04 20:17
kaaos28-Feb-04 20:17 
The methods that accept a 'T default' should be modified to accept a 'const T & default' in case T is a non-primitive and avoid the unnecessary construction and copy of whatever object T represents.
GeneralRe: Suggestion Pin
c-smile29-Feb-04 7:33
c-smile29-Feb-04 7:33 
GeneralRe: Suggestion Pin
Luca Piccarreta2-Mar-05 13:46
Luca Piccarreta2-Mar-05 13:46 
GeneralRe: Suggestion Pin
c-smile2-Mar-05 17:08
c-smile2-Mar-05 17:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.