TCLAP/include/tclap/XorHandler.h
macbishop 35aa53232d Moving the implementation of tclap to the header files presented me with two
major problems. 1) There where static functions and variables that could cause
link errors if tclap where used in different files (e.g. file1.cc and file2.cc
included tclap then compiling both files would give hard symbols for some
variables which would produce multiple definition when linking) 2) The
dependencies of tclap was a bit strange (CmdLine depends on Args and Args
depends on CmdLine for instance)

The first problem I solved by removing all static variables putting them in
static member functions (which are weak-symbols). So for instance every where
there previously was something like x = _delimiter there now is x = delimiter()
or in case of write acces delimiterRef() = x instead of _delimiter = x (I had
to append the Ref because there where already functions with the same name as
the variables). To solve the problem with static functions I simply inlined
them. This causes the compiler to produce a weak symbol or inline if
appropriate. We can put the functions inside the class declaration later to
make the code look better. This worked fine in all but two cases. In the
ValueArg and MultiArg classes I had to do a "hack" to work around the
specialization template for extractValue<std::string>. The code for this is
very simple but it might look strange an stupid at first but it is only to
resolve the specialisation to a weak symbol. What I did was I put the
implementations of extractValue in a helper class and I could then create a
specialized class instead of function and everything worked out. I think now in
retrospect there might be better solutions to this but I'll think a bit more on
it (maybe some type of inlining on the specialized version would suffice but
I'm not sure).

To handle the dependencies I had to do some rewriting. The first step was to
introduce a new class CmdLineInterface that is a purely abstract base of
CmdLine that specifies the functions needed by Arg and friends. Thus Arg
classes now takes an CmdLineInterface object as input instead (however only
CmdLine can ever be instantiated of-course). With this extra class cleaning up
the dependencies was quite simple, I've attached a dependency graph to the mail
(depgraph.png). I also cleaned up the #includes so now only what actually needs
inclusion is included. A nice side effect of this is that the impl. of CmdLine
is now put back into CmdLine.h (where I guess you wanted it) which (recursivly)
includes everything else needed.

Just to make things clear for myself regarding the class dependencies I made a
class TCLAP::Exception that inherits from std::exception and is a base of
ArgException (Exception does nothing currently). If we don't want the Exception
class it can be removed, however I think it could be a nice logic to have a
base Exception class that every exception inherits from, but we can discuss
that when we decide how to handle exceptions.
2004-09-26 18:27:47 +00:00

178 lines
4.4 KiB
C++

/******************************************************************************
*
* file: XorHandler.h
*
* Copyright (c) 2003, Michael E. Smoot .
* Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
* All rights reverved.
*
* See the file COPYING in the top directory of this distribution for
* more information.
*
* THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#ifndef TCLAP_XORHANDLER_H
#define TCLAP_XORHANDLER_H
#include <tclap/Arg.h>
#include <tclap/PrintSensibly.h>
#include <string>
#include <vector>
#include <algorithm>
#include <ostream>
namespace TCLAP {
/**
* This class handles lists of Arg's that are to be XOR'd on the command
* line. This is used by CmdLine and you shouldn't ever use it.
*/
class XorHandler
{
protected:
/**
* The list of of lists of Arg's to be or'd together.
*/
std::vector< std::vector<Arg*> > _orList;
public:
/**
* Constructor. Does nothing.
*/
XorHandler( ) {}
/**
* Add a list of Arg*'s that will be orred together.
* \param ors - list of Arg* that will be xor'd.
*/
void add( std::vector<Arg*>& ors );
/**
* Checks whether the specified Arg is in one of the xor lists and
* if it does match one, returns the size of the xor list that the
* Arg matched. If the Arg matches, then it also sets the rest of
* the Arg's in the list. You shouldn't use this.
* \param a - The Arg to be checked.
*/
int check( const Arg* a );
/**
* Returns the XOR specific short usage.
*/
std::string shortUsage();
/**
* Prints the XOR specific long usage.
* \param os - Stream to print to.
*/
void printLongUsage(std::ostream& os);
/**
* Simply checks whether the Arg is contained in one of the arg
* lists.
* \param a - The Arg to be checked.
*/
bool contains( const Arg* a );
};
//////////////////////////////////////////////////////////////////////
//BEGIN XOR.cpp
//////////////////////////////////////////////////////////////////////
inline void XorHandler::add( std::vector<Arg*>& ors )
{
_orList.push_back( ors );
}
inline std::string XorHandler::shortUsage()
{
std::string out = "";
for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
{
out += " {";
for ( ArgVectorIterator it = _orList[i].begin();
it != _orList[i].end(); it++ )
out += (*it)->shortID() + "|";
out[out.length()-1] = '}';
}
return out;
}
inline void XorHandler::printLongUsage( std::ostream& os )
{
for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
{
for ( ArgVectorIterator it = _orList[i].begin();
it != _orList[i].end();
it++ )
{
spacePrint( os, (*it)->longID(), 75, 3, 3 );
spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
if ( it+1 != _orList[i].end() )
spacePrint(os, "-- OR --", 75, 9);
}
os << std::endl << std::endl;
}
}
inline int XorHandler::check( const Arg* a )
{
// iterate over each XOR list
for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
{
// if the XOR list contains the arg..
if ( find( _orList[i].begin(), _orList[i].end(), a ) !=
_orList[i].end() )
{
// go through and set each arg that is not a
for ( ArgVectorIterator it = _orList[i].begin();
it != _orList[i].end();
it++ )
if ( a != (*it) )
(*it)->xorSet();
// return the number of required args that have now been set
return (int)_orList[i].size();
}
}
if ( a->isRequired() )
return 1;
else
return 0;
}
inline bool XorHandler::contains( const Arg* a )
{
for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
for ( ArgVectorIterator it = _orList[i].begin();
it != _orList[i].end();
it++ )
if ( a == (*it) )
return true;
return false;
}
//////////////////////////////////////////////////////////////////////
//END XOR.cpp
//////////////////////////////////////////////////////////////////////
} //namespace TCLAP
#endif