Click here to Skip to main content
Click here to Skip to main content

Auto Value

By , 27 Feb 2004
Rate this:
Please Sign up or sign in to vote.

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

License

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

About the Author

c-smile
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 PinmemberMichal Mecinski29-Feb-04 0:53 
GeneralRe: don't use null_value Pinmemberc-smile29-Feb-04 7:18 
GeneralSuggestion Pinmemberkaaos28-Feb-04 20:17 
GeneralRe: Suggestion Pinmemberc-smile29-Feb-04 7:33 
GeneralRe: Suggestion PinmemberLuca Piccarreta2-Mar-05 13:46 
GeneralRe: Suggestion Pinmemberc-smile2-Mar-05 17:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140421.2 | Last Updated 28 Feb 2004
Article Copyright 2004 by c-smile
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid