Click here to Skip to main content
11,933,905 members (36,720 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked

Auto Value

, 27 Feb 2004 BSD
Rate this:
Please Sign up or sign in to vote.
Auto Value is an implementation of variables having undefined state


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 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 Smile | :)


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

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.

You may also be interested in...

Comments and Discussions

Generaldon't use null_value Pin
Michal Mecinski29-Feb-04 1:53
memberMichal Mecinski29-Feb-04 1:53 
GeneralRe: don't use null_value Pin
c-smile29-Feb-04 8:18
memberc-smile29-Feb-04 8:18 
GeneralSuggestion Pin
kaaos28-Feb-04 21:17
memberkaaos28-Feb-04 21:17 
GeneralRe: Suggestion Pin
c-smile29-Feb-04 8:33
memberc-smile29-Feb-04 8:33 
GeneralRe: Suggestion Pin
Luca Piccarreta2-Mar-05 14:46
memberLuca Piccarreta2-Mar-05 14:46 
GeneralRe: Suggestion Pin
c-smile2-Mar-05 18:08
memberc-smile2-Mar-05 18: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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 28 Feb 2004
Article Copyright 2004 by c-smile
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid