libcmle documentation

(c) 2008 Steffen Wendzel (steffen (at) ploetner-it (dot) de)

file version 0.1, last edit: mon-21-jan, 23:10, by: swendzel

libcmle ranges

Ranges in libcmle are not hard to understand but can be very useful. Ranges make it possibly to let "someone else" (=libcmle) take care about values of variables. This makes it possibly to save lots of work and can prevent errors.

Syntax

This all works with libcmles own datatypes what are the usual number datatypes like int, long, long double, float and the like used within the _mdt template class:

_mdt<type> var_name;

There exist two methods, one to define a maximum value and a function to call if the value is reached (and of course a 3rd parameter, an void * argument given to the function. A 2nd method implements the same functionality for the minimal value.

void setrange_max(T _v_max, void (*_ef)(void *), void *_ev);
void setrange_min(T _v_min, void (*_ef)(void *), void *_ev);

_v_max is the maximal allowed value, _v_min is the minimal allowed value. _ef is a function pointer to a emergency function libcmle will call if the value is out of range and the value of _ev will be given to the parameter void * of this function when called.

Example

Lets say you have to write a programm that monitors the temperature of some nuclear plant components and that the temperature of an element is stored in an integer variable.

libcmle will be used to take care about the temperature and will do whatever you want, if the temperature goes over the maximal acceptable value you defined. The code could look like this:

#include <iostream>
#include <libcmle.h>

/* this is your emergency function that will be called if the temperature is
 * too high. We currently don't use the 'v' parameter (that will have the value
 * given in the 3rd parameter of setrange_max().
 */
void
emergency(void *v)
{
	std::cout << "ERROR: max temp emergency! run!! run!!!" << std::endl;
	// do some emergency temp shrinking stuff here
	exit(1);
}

int
main()
{
	/* define temp to be the integer version of the libcmle internal datatype using
	 * the typical C++ template syntax.
	 */
	_mdt<int> temp;
	
	/* the maximum allowed temperature are 300° and if more than 300° are reached,
	 * libcmle will call the emergency function. The 3rd parameter is the value for
	 * the 'void *' parameter of the emergency function. We currently don't need it.
	 */
	temp.setrange_max(300, emergency, NULL);
	
	for (temp = 280; temp < 1000 ; temp++)
		std::cout << "current temp value: " << temp << std::endl;
	
	return 0;
}

The output will look so:

$ g++ -o test test.C -lcmle
$ ./test
current temp value: 280
current temp value: 281
current temp value: 282
current temp value: 283
current temp value: 284
current temp value: 285
current temp value: 286
current temp value: 287
current temp value: 288
current temp value: 289
current temp value: 290
current temp value: 291
current temp value: 292
current temp value: 293
current temp value: 294
current temp value: 295
current temp value: 296
current temp value: 297
current temp value: 298
current temp value: 299
current temp value: 300
ERROR: max temp emergency! run!! run!!!

You see: You don't have to implement checks for the temp value. Imagine that you would need to check the value in many different situations and in many different parts of your source code. You don't need to care when using libcmle since it will do the job for you!