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

BoolTool

0.00/5 (No votes)
24 Feb 2002 1  
A quick method of manipulating the current state of a multi-state variable using enums.

Introduction

There are many instances where one has to keep track of several control variables, which are mostly bool type, and perform different actions depending on which of the control variable is enabled or true. For example, I needed to do the same for a program called VCADD (Vinay - CADD) I was developing for myself where placing points, making a 2D figure, making a 3D figure, rotating in different directions etc all depended on different control variables along with the mouseclicks. So, I came up with this.

Consider a situation where one object is moved towards another towards either the left, right, top or bottom.

No bool type variable is operated upon here, instead there is an unnamed enumeration and 2 arrays.

enum {NONE = 0,LEFT,RIGHT,TOP,BOTTOM};
int Ref[] = {NONE,LEFT,RIGHT,TOP,BOTTOM};
char *Names[] = {"NONE","LEFT","RIGHT","TOP","BOTTOM"};

Note - array refers to Ref unless mentioned otherwise.

The array Names is optional and is need only for the warning message.

There are only 4 functions needed to enable or disable a particular variable.

void GetIndex(int n)

This function is an important one. It just traverses the array and returns the index of the parameter supplied if successful or -1 if not.

void SetTrue(int n)

This function sets the parameter supplied true.

void SetFalse(int n)

This function sets the parameter supplied false.

bool IsTrue(int n)

Returns true if parameter supplied is true or false otherwise.

The logic (or may be the convention) is-

Whichever is the element at index 0 in the array is true or enabled and the rest are false or disabled.

Initially, NONE is true because it is the first element.

The function SetTrue(int), initially, just swaps the parameter supplied with NONE or generally, with whatever at the first position. So, according to the convention, it is set true and others false.

The function SetFalse(int) swaps NONE with the parameter supplied, thus putting NONE at the first position and making it true. At the point of time when SetFalse(int) is called, the elements of the array might have been shuffled; so GetIndex(int) is called to know the position of NONE.

SetFalse(int) also checks if the parameter to be disabled was enabled just before calling it. This one is very important because

  1. Initially the array will be like this
    NONE LEFT RIGHT TOP BOTTOM
  2. SetTrue(TOP) makes it
    TOP LEFT RIGHT NONE BOTTOM
  3. SetFalse(TOP) makes it
    NONE LEFT RIGHT TOP BOTTOM

    and IsTrue(TOP) correctly returns false.

Without the check, instead, if the second step were to be like this

  1. SetFalse(TOP) makes it
    TOP LEFT RIGHT NONE BOTTOM

    and IsTrue(TOP) will return true!! To avoid this, SetFalse(int) performs the check.

    So, with the check in place, it gives a warning message and this is the only reason the Names array is present.

The function IsTrue(int) returns true if the parameter supplied is at the first position and false if not.

That's it. No bool variable is required. 

I thought namespace would be the best for this kind of situation. I also need to know if there are any simpler techniques. Expecting feedback.

[Ma-thth-e Sig-thee-ni]

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