Menu

#11 class FunctionConstraint : public Constraint<T>

Future
open
None
5
2019-05-06
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

  • Anonymous

    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/"

     
  • Marco Di Bartolomeo

    Is there any reason why this patch was ignored? I think that the library would benefit from some pre-made constraint classes.

     
    • Daniel Aarno

      Daniel Aarno - 2017-08-24

      I think the idea is good, but the patch requires more work.
      1) It should support a function object rather than just function pointer
      2) There needs to be example/tests added

      If you're interrested in contributing to such a patch and creating a pull request I'll be happy to review and merge it once done. Otherwise I may end up doing it when I'm bored enough, which may be never :)

       
  • Mike Smoot

    Mike Smoot - 2017-08-22

    The only reason this has been ignored is my lack of time or energy to dedicate to tclap. I agree that this patch would be useful.

     
  • Daniel Aarno

    Daniel Aarno - 2017-08-23

    I'll try to take a look in the weekend.

     
  • Daniel Aarno

    Daniel Aarno - 2017-08-24
    • assigned_to: Daniel Aarno
    • Group: --> Unstable (example)
     
  • Daniel Aarno

    Daniel Aarno - 2019-05-06
    • Group: Unstable (example) --> Future
     

Log in to post a comment.