#11 class FunctionConstraint : public Constraint<T>

open
nobody
None
5
2012-02-29
2012-02-29
Anonymous
No

I have derived FunctionConstraint from from Constraint<T> (TCLAP version 1.2.1)
The constructor FunctionConstraint(bool (*f)(T), std::string desc) accept a function of the kind "bool func(T val)" and a string "desc" which describe which kind of contraints are implemented in "func".
Then bool FunctionConstraint<T>::check( const T& val ) simply calls f(val) and return the bool according to the implementation of "func".
For an example on the use see below.

FunctionConstraint.h is attached. In CmdLine.h at line 42 "#include <tclap/FunctionConstraint.h>" must be added

I haven't done a proper debugging, but I've tested the new class with a bunch of different functions and different command line arguments and worked.

I hope that this new class will be included in future releases of TCLAP.

Cheers,
Francesco

########################
Example
#include ....

template<class T>
bool range(T val){ //function which returns true if 'val' is in the range, false otherwise
if( val>= 3 && val < 15) return true;
else return false;
}
std::string descr = "value in [3, 15)"; //descrition of the constraint

int main(int argc, char* argv[])
{

TCLAP::CmdLine cmd("test", ' ', "1"); //command line object

TCLAP::ValueArg<double> foo("f", "foo", "fooing around", false, 0.2, "double"); //foo
cmd.add(foo);

TCLAP::FunctionConstraint<int> choiserange(range, descr); //FunctionConstraint object
TCLAP::ValueArg<int> counts("c", "counts", "Number of counts", false, 0, &choiserange); //pass it to ValueArg
cmd.add(counts);

cmd.parse(argc, argv); //parse

std::cout << foo.getValue() << std::endl; //print out
std::cout << counts.getValue() << std::endl;
exit(0);
}

Discussion

  • Comment has been marked as spam. 
    Undo

    You can see all pending comments posted by this user  here

    Anonymous - 2012-02-29

    Class implementing contraints from a custom function

     
    Attachments
  • Comment has been marked as spam. 
    Undo

    You can see all pending comments posted by this user  here

    Anonymous - 2012-03-01

    I forgot to say that following two files must also be changed
    include/Makefile.am, line 26, add "FunctionConstraint.h/"
    include/Makefile.in, line 175, add "FunctionConstraint.h/"

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks