65.9K
CodeProject is changing. Read more.
Home

A set of universal data check functions

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.07/5 (13 votes)

Dec 25, 2002

2 min read

viewsIcon

53930

downloadIcon

244

A set of universal data check functions can be used in any systems with checking requirement.

Introduction

We need to check the range of user data in many program. It is a hell for us to verify all data for a program with several thousands parameters. The best way is to check it automatically. In order to make it running , the trivial thing is how to write check routings in uniform way. I got it. Just a simple tips in C++. I provide the source code for every body. In later articles , I will give you a special control for using these routines in universal way. :)

Checking theory

The basic theory to check the range of data is very simple but trivial. In mathematical, the range of data is domain. In a 1D condition , a domain has one or two limits, one for large boundary and one for small boundary . Usually , we write it as [min,max] for close domain or (min , max ) as open domain. Our check routines shall include all kinds conditions we may encountered.

I list the following condition:

1) Let it be , no any check !
2) given number must be less than the limit.                    number < min
3) given number must be great than the limit.                    number > max
4) given number must be within a range                        min < number < max
5) given number must be less or equal to limit                    number <= min
6) given number must be great or equal to limit                    number >= max
7) given number must be within a range with lower equal                    min<= number < max
8) given number must be within a range with greater equal              min < number <= max
9) given number must be within a range equal to bound                min <= number <= max
10) given number must be outside a range without equal                  number < min  AND number > max
11) given number must be outside a range with lower equal              number <= min  AND number > max
12) given number must be outside a range with lower equal              number < min  AND number >= max
13) given number must be outside a range with lower equal              number <= min  AND number >= max
 The following condition is important for compatility.
14 ) given number must be less than the limit ( the limit is set as max )    number <max
15 ) given number must be great than the limit ( the limit is set as min )    number >min
16 ) given number must be less than or equal to  the limit ( the limit is set as max ) number <= max
17 ) given number must be great than or equal to  the limit ( the limit is set as min ) number >=min

Dealing data type

Another headache is how to use these rulers on every kind of data structures. C++ provides server method, such as string , double , int ,long , datetime etc. Each has different way to check. That the relation sign has different meaning. The unsafe way is providing a macro. I think the better way is using template of C++, which is strong type , can make our code more safe when running.

template<class T>
BOOL internalCheckLess( T& value , T& minv, T& maxv)
{
    return value < minv;
}

template<class T>
BOOL internalCheckGreat( T& value , T& minv, T& maxv)
{
    return value > maxv;
}

template<class T>
BOOL internalCheckLessMax( T& value , T& minv, T& maxv)
{
    return value < maxv;
}

template<class T>
BOOL internalCheckGreatMin( T& value , T& minv, T& maxv)
{
    return value > minv;
}

template<class T>
BOOL internalCheckLessMaxEqu( T& value , T& minv, T& maxv)
{
    return value <= maxv;
}

template<class T>
BOOL internalCheckGreatMinEqu( T& value , T& minv, T& maxv)
{
    return value >= minv;
}


template<class T>
BOOL internalCheckBoth( T& value , T& minv, T& maxv)
{
    return value > minv && value < maxv ;
}


template<class T>
BOOL internalCheckLessEqu( T& value , T& minv, T& maxv)
{
    return value <= minv;
}

template<class T>
BOOL internalCheckGreatEqu( T& value , T& minv, T& maxv)
{
    return value >= maxv;
}


template<class T>
BOOL internalCheckBothEqu( T& value , T& minv, T& maxv)
{
    return value >= minv && value <= maxv ;
}

template<class T>
BOOL internalCheckBothLessEqu( T& value , T& minv, T& maxv)
{
    return value >= minv && value < maxv ;
}

template<class T>
BOOL internalCheckBothGreatEqu( T& value , T& minv, T& maxv)
{
    return value > minv && value <= maxv ;
}
template<class T>
BOOL internalCheckOutEqu( T& value , T& minv, T& maxv)
{
    return value <= minv && value >= maxv ;
}

template<class T>
BOOL internalCheckOutLessEqu( T& value , T& minv, T& maxv)
{
    return value <= minv && value > maxv ;
}

template<class T>
BOOL internalCheckOutGreatEqu( T& value , T& minv, T& maxv)
{
    return value < minv && value >= maxv ;
}

template<class T>
BOOL internalCheckOut( T& value , T& minv, T& maxv)
{
    return value < minv && value > maxv ;
}

Promption

When the checking routine found that an error occurred when user inputted data, we shall raise a message to user and tell him or her that what kind of error found. Normally , we will give out a messagebox with reason . The message will be displayed according to user specified. We collect all error message in a header file for easy to be change according to their language or more proper words. We can write a function for each data type.

Why not to use class

May be it is a good idea to write these function as a class . It is a light method to write these in functions . I think it is more covinent to use function than writing a class. I will demonstrate how to use these function in a control group in next article which will be coming soon.

That’s all ! Enjoy it.

History

  Version            Author                    Date                Content
  ===========================================================================
  0.9                Johnson Zhou        2002/08/01              Initial
  1.0                Johnson Zhou        2002/12/07              Write internal function as template
  1.01                Johnson Zhou        2002/12/25              Add function of 14)-17).