aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-07-23 19:04:56 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-07-23 19:04:56 +0000
commit2d3b1d91179f9b7ff630a037825fcc77b3bcd99d (patch)
treec356ae99ea40d4f25fdf61b2b1fed2892893afc1
parentc9cd8a6ed689157772d6f8c1530e2711dc515433 (diff)
downloadpaludis-2d3b1d91179f9b7ff630a037825fcc77b3bcd99d.tar.gz
paludis-2d3b1d91179f9b7ff630a037825fcc77b3bcd99d.tar.xz
More header and libwrapiter cleanups
-rw-r--r--paludis/args/args_handler.cc100
-rw-r--r--paludis/args/args_handler.hh73
-rw-r--r--paludis/args/args_option.cc89
-rw-r--r--paludis/args/args_option.hh101
-rw-r--r--paludis/args/args_visitor.cc9
-rw-r--r--paludis/args/args_visitor.hh8
-rw-r--r--paludis/util/collection.hh1
-rw-r--r--src/paludis/sync.cc1
8 files changed, 250 insertions, 132 deletions
diff --git a/paludis/args/args_handler.cc b/paludis/args/args_handler.cc
index 36874d8..16c2267 100644
--- a/paludis/args/args_handler.cc
+++ b/paludis/args/args_handler.cc
@@ -19,9 +19,11 @@
#include "args.hh"
#include "args_dumper.hh"
+#include <paludis/util/system.hh>
#include <algorithm>
#include <sstream>
-#include <paludis/util/system.hh>
+#include <list>
+#include <map>
/** \file
* Implementation for ArgsHandler.
@@ -31,7 +33,23 @@
using namespace paludis::args;
-ArgsHandler::ArgsHandler()
+namespace paludis
+{
+ template<>
+ struct Implementation<ArgsHandler> :
+ InternalCounted<Implementation<ArgsHandler> >
+ {
+ std::list<ArgsGroup *> groups;
+ std::list<std::string> parameters;
+ std::list<std::string> usage_lines;
+
+ std::map<std::string, ArgsOption *> longopts;
+ std::map<char, ArgsOption *> shortopts;
+ };
+}
+
+ArgsHandler::ArgsHandler() :
+ PrivateImplementationPattern<ArgsHandler>(new Implementation<ArgsHandler>)
{
}
@@ -40,10 +58,16 @@ ArgsHandler::~ArgsHandler()
}
void
+ArgsHandler::add_usage_line(const std::string & l)
+{
+ _imp->usage_lines.push_back(l);
+}
+
+void
ArgsHandler::add(ArgsGroup * const g)
{
/// \bug Should check for name uniqueness.
- _groups.push_back(g);
+ _imp->groups.push_back(g);
}
void
@@ -64,7 +88,7 @@ ArgsHandler::run(const int argc, const char * const * const argv)
args.insert(args.end(), &argv[1], &argv[argc]);
- std::list<std::string>::iterator argit = args.begin(), arge = args.end();
+ libwrapiter::ForwardIterator<ArgsVisitor, std::string> argit(args.begin()), arge(args.end());
ArgsVisitor visitor(&argit, arge);
@@ -80,8 +104,8 @@ ArgsHandler::run(const int argc, const char * const * const argv)
else if (0 == arg.compare(0, 2, "--"))
{
arg.erase(0, 2);
- std::map<std::string, ArgsOption*>::iterator it = _longopts.find(arg);
- if (it == _longopts.end())
+ std::map<std::string, ArgsOption *>::iterator it = _imp->longopts.find(arg);
+ if (it == _imp->longopts.end())
throw BadArgument("--" + arg);
(*it).second->accept(&visitor);
}
@@ -90,8 +114,8 @@ ArgsHandler::run(const int argc, const char * const * const argv)
arg.erase(0, 1);
for (std::string::iterator c = arg.begin(); c != arg.end(); ++c)
{
- std::map<char, ArgsOption*>::iterator it = _shortopts.find(*c);
- if (it == _shortopts.end())
+ std::map<char, ArgsOption *>::iterator it = _imp->shortopts.find(*c);
+ if (it == _imp->shortopts.end())
{
throw BadArgument(std::string("-") + *c);
}
@@ -100,18 +124,19 @@ ArgsHandler::run(const int argc, const char * const * const argv)
}
else
{
- _parameters.push_back(arg);
+ _imp->parameters.push_back(arg);
}
}
- _parameters.insert(_parameters.end(), argit, args.end());
+ _imp->parameters.insert(_imp->parameters.end(),
+ argit, libwrapiter::ForwardIterator<ArgsVisitor, std::string>(args.end()));
}
void
ArgsHandler::dump_to_stream(std::ostream & s) const
{
ArgsDumper dump(s);
- std::list<ArgsGroup *>::const_iterator g(_groups.begin()), g_end(_groups.end());
+ std::list<ArgsGroup *>::const_iterator g(_imp->groups.begin()), g_end(_imp->groups.end());
for ( ; g != g_end ; ++g)
{
s << (*g)->name() << ":" << std::endl;
@@ -122,12 +147,61 @@ ArgsHandler::dump_to_stream(std::ostream & s) const
}
}
-#ifndef DOXYGEN
+ArgsHandler::ParametersIterator
+ArgsHandler::begin_parameters() const
+{
+ return ParametersIterator(_imp->parameters.begin());
+}
+
+ArgsHandler::ParametersIterator
+ArgsHandler::end_parameters() const
+{
+ return ParametersIterator(_imp->parameters.end());
+}
+
+bool
+ArgsHandler::empty() const
+{
+ return _imp->parameters.empty();
+}
+
std::ostream &
paludis::args::operator<< (std::ostream & s, const ArgsHandler & h)
{
h.dump_to_stream(s);
return s;
}
-#endif
+
+void
+ArgsHandler::add_option(ArgsOption * const opt, const std::string & long_name,
+ const char short_name)
+{
+ _imp->longopts[long_name] = opt;
+ if (short_name != '\0')
+ _imp->shortopts[short_name] = opt;
+}
+
+ArgsHandler::UsageLineIterator
+ArgsHandler::begin_usage_lines() const
+{
+ return UsageLineIterator(_imp->usage_lines.begin());
+}
+
+ArgsHandler::UsageLineIterator
+ArgsHandler::end_usage_lines() const
+{
+ return UsageLineIterator(_imp->usage_lines.end());
+}
+
+ArgsHandler::ArgsGroupsIterator
+ArgsHandler::begin_args_groups() const
+{
+ return ArgsGroupsIterator(_imp->groups.begin());
+}
+
+ArgsHandler::ArgsGroupsIterator
+ArgsHandler::end_args_groups() const
+{
+ return ArgsGroupsIterator(_imp->groups.end());
+}
diff --git a/paludis/args/args_handler.hh b/paludis/args/args_handler.hh
index d86f29f..d162b73 100644
--- a/paludis/args/args_handler.hh
+++ b/paludis/args/args_handler.hh
@@ -20,12 +20,15 @@
#ifndef PALUDIS_GUARD_ARGS_ARGS_HANDLER_HH
#define PALUDIS_GUARD_ARGS_ARGS_HANDLER_HH 1
-#include <map>
-#include <ostream>
#include <paludis/args/args_group.hh>
#include <paludis/util/instantiation_policy.hh>
+#include <paludis/util/private_implementation_pattern.hh>
+
+#include <iosfwd>
#include <string>
+#include <libwrapiter/libwrapiter_forward_iterator.hh>
+
/** \file
* Declaration for ArgsHandler.
*
@@ -41,27 +44,18 @@ namespace paludis
*
* \ingroup grplibpaludisargs
*/
- class ArgsHandler : private InstantiationPolicy<ArgsHandler, instantiation_method::NonCopyableTag>
+ class ArgsHandler :
+ private InstantiationPolicy<ArgsHandler, instantiation_method::NonCopyableTag>,
+ private PrivateImplementationPattern<ArgsHandler>
{
friend class ArgsGroup;
friend std::ostream & operator<< (std::ostream &, const ArgsHandler &);
- private:
- std::list<ArgsGroup *> _groups;
- std::list<std::string> _parameters;
- std::list<std::string> _usage_lines;
-
- std::map<std::string, ArgsOption *> _longopts;
- std::map<char, ArgsOption *> _shortopts;
-
protected:
/**
* Add a new usage line.
*/
- void add_usage_line(const std::string & l)
- {
- _usage_lines.push_back(l);
- }
+ void add_usage_line(const std::string & l);
/**
* Add an new ArgsGroup (called by the ArgsGroup constructor).
@@ -91,34 +85,21 @@ namespace paludis
///\name Iterate over our parameters (non - and -- switches and their values)
///\{
- typedef std::list<std::string>::const_iterator ParametersIterator;
+ typedef libwrapiter::ForwardIterator<ArgsHandler, const std::string> ParametersIterator;
- ParametersIterator begin_parameters() const
- {
- return _parameters.begin();
- }
+ ParametersIterator begin_parameters() const;
- ParametersIterator end_parameters() const
- {
- return _parameters.end();
- }
+ ParametersIterator end_parameters() const;
- bool empty() const
- {
- return _parameters.empty();
- }
+ bool empty() const;
///\}
/**
* Add an ArgsOption instance.
*/
- void add_option(ArgsOption *opt, const std::string long_name, const char short_name = '\0')
- {
- _longopts[long_name] = opt;
- if (short_name != '\0')
- _shortopts[short_name] = opt;
- }
+ void add_option(ArgsOption * const, const std::string & long_name,
+ const char short_name = '\0');
///\name About our application (for documentation)
///\{
@@ -151,34 +132,22 @@ namespace paludis
///\name Iterate over our usage lines (for documentation)
///\{
- typedef std::list<std::string>::const_iterator UsageLineIterator;
+ typedef libwrapiter::ForwardIterator<ArgsHandler, const std::string> UsageLineIterator;
- UsageLineIterator begin_usage_lines() const
- {
- return _usage_lines.begin();
- }
+ UsageLineIterator begin_usage_lines() const;
- UsageLineIterator end_usage_lines() const
- {
- return _usage_lines.end();
- }
+ UsageLineIterator end_usage_lines() const;
///\}
///\name Iterate over our groups
///\{
- typedef std::list<ArgsGroup *>::const_iterator ArgsGroupsIterator;
+ typedef libwrapiter::ForwardIterator<ArgsHandler, ArgsGroup * const> ArgsGroupsIterator;
- ArgsGroupsIterator begin_args_groups() const
- {
- return _groups.begin();
- }
+ ArgsGroupsIterator begin_args_groups() const;
- ArgsGroupsIterator end_args_groups() const
- {
- return _groups.end();
- }
+ ArgsGroupsIterator end_args_groups() const;
///\}
};
diff --git a/paludis/args/args_option.cc b/paludis/args/args_option.cc
index 505fe95..c195820 100644
--- a/paludis/args/args_option.cc
+++ b/paludis/args/args_option.cc
@@ -19,6 +19,8 @@
#include "args.hh"
#include "bad_value.hh"
+#include <set>
+#include <vector>
/** \file
* Implementation for ArgsOption.
@@ -67,10 +69,39 @@ StringArg::StringArg(ArgsGroup * const g, const std::string & long_name,
{
}
+namespace paludis
+{
+ template<>
+ struct Implementation<StringSetArg> :
+ InternalCounted<Implementation<StringSetArg> >
+ {
+ std::set<std::string> args;
+ };
+}
+
StringSetArg::StringSetArg(ArgsGroup * const g, const std::string & long_name,
const char short_name, const std::string & description) :
- ArgsOption(g, long_name, short_name, description)
+ ArgsOption(g, long_name, short_name, description),
+ PrivateImplementationPattern<StringSetArg>(new Implementation<StringSetArg>)
+{
+}
+
+StringSetArg::Iterator
+StringSetArg::args_begin() const
{
+ return Iterator(_imp->args.begin());
+}
+
+StringSetArg::Iterator
+StringSetArg::args_end() const
+{
+ return Iterator(_imp->args.end());
+}
+
+void
+StringSetArg::add_argument(const std::string & arg)
+{
+ _imp->args.insert(arg);
}
IntegerArg::IntegerArg(ArgsGroup * const group, const std::string& long_name,
@@ -105,10 +136,27 @@ namespace
};
}
+namespace paludis
+{
+ template<>
+ struct Implementation<EnumArg> :
+ InternalCounted<Implementation<EnumArg> >
+ {
+ std::vector<std::pair<std::string, std::string> > allowed_args;
+ };
+
+ template<>
+ struct Implementation<EnumArg::EnumArgOptions> :
+ InternalCounted<Implementation<EnumArg::EnumArgOptions> >
+ {
+ std::vector<std::pair<std::string, std::string> > options;
+ };
+}
+
void EnumArg::set_argument(const std::string & arg)
{
- if (_allowed_args.end() == std::find_if(_allowed_args.begin(),
- _allowed_args.end(), ArgIs(arg)))
+ if (_imp->allowed_args.end() == std::find_if(_imp->allowed_args.begin(),
+ _imp->allowed_args.end(), ArgIs(arg)))
throw (BadValue("--" + long_name(), arg));
_argument = arg;
@@ -118,17 +166,46 @@ EnumArg::~EnumArg()
{
}
-EnumArg::EnumArgOptions::EnumArgOptions(std::string opt, std::string desc)
+EnumArg::EnumArgOptions::EnumArgOptions(std::string opt, std::string desc) :
+ PrivateImplementationPattern<EnumArgOptions>(new Implementation<EnumArgOptions>)
{
- _options.push_back(std::make_pair(opt, desc));
+ _imp->options.push_back(std::make_pair(opt, desc));
}
EnumArg::EnumArgOptions & EnumArg::EnumArgOptions::operator() (std::string opt, std::string desc)
{
- _options.push_back(std::make_pair(opt, desc));
+ _imp->options.push_back(std::make_pair(opt, desc));
return *this;
}
EnumArg::EnumArgOptions::~EnumArgOptions()
{
}
+
+EnumArg::EnumArg(ArgsGroup * const group, const std::string & long_name,
+ const char short_name, const std::string & description,
+ const EnumArgOptions & opts, const std::string & default_arg) :
+ ArgsOption(group, long_name, short_name, description),
+ PrivateImplementationPattern<EnumArg>(new Implementation<EnumArg>),
+ _argument(default_arg),
+ _default_arg(default_arg)
+{
+ _imp->allowed_args = opts._imp->options;
+}
+
+EnumArg::AllowedArgIterator
+EnumArg::begin_allowed_args() const
+{
+ return AllowedArgIterator(_imp->allowed_args.begin());
+}
+
+EnumArg::AllowedArgIterator
+EnumArg::end_allowed_args() const
+{
+ return AllowedArgIterator(_imp->allowed_args.end());
+}
+
+StringSetArg::~StringSetArg()
+{
+}
+
diff --git a/paludis/args/args_option.hh b/paludis/args/args_option.hh
index bce3c86..fb0c278 100644
--- a/paludis/args/args_option.hh
+++ b/paludis/args/args_option.hh
@@ -22,9 +22,7 @@
#define PALUDIS_GUARD_ARGS_ARGS_OPTION_HH 1
#include <paludis/args/args_visitor.hh>
-#include <set>
-#include <string>
-#include <vector>
+#include <paludis/util/private_implementation_pattern.hh>
/** \file
* Declaration for ArgsOption.
@@ -175,38 +173,37 @@ namespace paludis
*
* \ingroup grplibpaludisargs
*/
- class StringSetArg : public ArgsOption, public Visitable<StringSetArg, ArgsVisitorTypes>
+ class StringSetArg :
+ public ArgsOption,
+ public Visitable<StringSetArg, ArgsVisitorTypes>,
+ private PrivateImplementationPattern<StringSetArg>
{
- private:
- std::set<std::string> _args;
-
public:
+ ///\name Basic operations
+ ///\{
- /**
- * Type used to iterate over specified args.
- */
- typedef std::set<std::string>::const_iterator Iterator;
-
- /**
- * Constructor
- */
StringSetArg(ArgsGroup * const, const std::string & long_name,
const char short_name, const std::string & description);
- /**
- * Retrieve an iterator to the first arg
- */
- Iterator args_begin() const { return _args.begin(); }
+ ~StringSetArg();
- /**
- * Retrieve an iterator one past the last arg
- */
- Iterator args_end() const { return _args.end(); }
+ ///\}
+
+ ///\name Iterate over our args.
+ ///\{
+
+ typedef libwrapiter::ForwardIterator<StringArg, const std::string> Iterator;
+
+ Iterator args_begin() const;
+
+ Iterator args_end() const;
+
+ ///\}
/**
- * Add an argument to the set returned by [ args_begin(), args_end() )
+ * Add an argument to the set.
*/
- void add_argument(const std::string & arg) { _args.insert(arg); }
+ void add_argument(const std::string & arg);
};
@@ -269,10 +266,12 @@ namespace paludis
*
* \ingroup grplibpaludisargs
*/
- class EnumArg : public ArgsOption, public Visitable<EnumArg, ArgsVisitorTypes>
+ class EnumArg :
+ public ArgsOption,
+ public Visitable<EnumArg, ArgsVisitorTypes>,
+ private PrivateImplementationPattern<EnumArg>
{
private:
- const std::vector<std::pair<std::string, std::string> > _allowed_args;
std::string _argument;
const std::string _default_arg;
@@ -284,11 +283,10 @@ namespace paludis
*
* \ingroup grplibpaludisargs
*/
- class EnumArgOptions
+ class EnumArgOptions :
+ private PrivateImplementationPattern<EnumArgOptions>
{
- private:
- friend class EnumArg;
- std::vector<std::pair<std::string, std::string> > _options;
+ friend class EnumArg;
public:
/**
@@ -304,7 +302,7 @@ namespace paludis
/**
* Adds another (option, description) pair.
*/
- EnumArgOptions& operator() (const std::string, const std::string);
+ EnumArgOptions & operator() (const std::string, const std::string);
};
/**
@@ -312,19 +310,17 @@ namespace paludis
*/
EnumArg(ArgsGroup * const group, const std::string & long_name,
const char short_name, const std::string & description,
- const EnumArgOptions & opts, const std::string & default_arg) :
- ArgsOption(group, long_name, short_name, description),
- _allowed_args(opts._options), _argument(default_arg),
- _default_arg(default_arg)
- {
- }
+ const EnumArgOptions & opts, const std::string & default_arg);
~EnumArg();
/**
* Fetch the argument that was given to this option.
*/
- const std::string & argument() const { return _argument; }
+ const std::string & argument() const
+ {
+ return _argument;
+ }
/**
* Set the argument returned by argument(), having verified that
@@ -335,23 +331,22 @@ namespace paludis
/**
* Fetch the default option, as specified to the constructor.
*/
- const std::string & default_arg() const { return _default_arg; }
+ const std::string & default_arg() const
+ {
+ return _default_arg;
+ }
- /**
- * Type used to iterate over valid arguments to this option
- */
- typedef std::vector<std::pair<std::string, std::string> >::const_iterator AllowedArgIterator;
+ ///\name Iterate over our allowed arguments and associated descriptions
+ ///\{
- /**
- * Returns an iterator pointing to a pair containing the first valid argument,
- * and its description.
- */
- AllowedArgIterator begin_allowed_args() const { return _allowed_args.begin(); }
+ typedef libwrapiter::ForwardIterator<EnumArg,
+ const std::pair<std::string, std::string> > AllowedArgIterator;
- /**
- * Returns an iterator pointing just beyond the last valid argument.
- */
- AllowedArgIterator end_allowed_args() const { return _allowed_args.end(); }
+ AllowedArgIterator begin_allowed_args() const;
+
+ AllowedArgIterator end_allowed_args() const;
+
+ ///\}
};
}
}
diff --git a/paludis/args/args_visitor.cc b/paludis/args/args_visitor.cc
index 979debd..d847e81 100644
--- a/paludis/args/args_visitor.cc
+++ b/paludis/args/args_visitor.cc
@@ -19,8 +19,8 @@
#include "args_option.hh"
#include "bad_value.hh"
#include "missing_value.hh"
-#include <paludis/util/visitor.hh>
+#include <paludis/util/visitor.hh>
#include <paludis/util/destringify.hh>
#include <sstream>
@@ -34,12 +34,13 @@
using namespace paludis;
using namespace args;
-ArgsVisitor::ArgsVisitor(std::list<std::string>::iterator *ai,
- std::list<std::string>::iterator ae) : _args_index(ai), _args_end(ae)
+ArgsVisitor::ArgsVisitor(libwrapiter::ForwardIterator<ArgsVisitor, std::string> * ai,
+ libwrapiter::ForwardIterator<ArgsVisitor, std::string> ae) : _args_index(ai), _args_end(ae)
{
}
-const std::string& ArgsVisitor::get_param(const ArgsOption * const arg)
+const std::string &
+ArgsVisitor::get_param(const ArgsOption * const arg)
{
if (++(*_args_index) == _args_end)
{
diff --git a/paludis/args/args_visitor.hh b/paludis/args/args_visitor.hh
index 17a9eac..be70562 100644
--- a/paludis/args/args_visitor.hh
+++ b/paludis/args/args_visitor.hh
@@ -21,10 +21,11 @@
#ifndef PALUDIS_GUARD_ARGS_ARGS_VISITOR_HH
#define PALUDIS_GUARD_ARGS_ARGS_VISITOR_HH 1
-#include <list>
#include <paludis/util/visitor.hh>
#include <string>
+#include <libwrapiter/libwrapiter_forward_iterator.hh>
+
/** \file
* Declaration for ArgsVisitor
*
@@ -59,7 +60,7 @@ namespace paludis
class ArgsVisitor : public ArgsVisitorTypes::Visitor
{
private:
- std::list<std::string>::iterator *_args_index, _args_end;
+ libwrapiter::ForwardIterator<ArgsVisitor, std::string> * _args_index, _args_end;
const std::string& get_param(const ArgsOption * const);
@@ -67,7 +68,8 @@ namespace paludis
/**
* Constructor
*/
- ArgsVisitor(std::list<std::string>::iterator *, std::list<std::string>::iterator);
+ ArgsVisitor(libwrapiter::ForwardIterator<ArgsVisitor, std::string> *,
+ libwrapiter::ForwardIterator<ArgsVisitor, std::string>);
/// Visit an ArgsOption.
void visit(ArgsOption * const);
diff --git a/paludis/util/collection.hh b/paludis/util/collection.hh
index 9fa6727..70e12db 100644
--- a/paludis/util/collection.hh
+++ b/paludis/util/collection.hh
@@ -24,7 +24,6 @@
#include <paludis/util/instantiation_policy.hh>
#include <libwrapiter/libwrapiter.hh>
#include <iterator>
-#include <functional>
/** \file
* Various wrappers around collections of items, for convenience and
diff --git a/src/paludis/sync.cc b/src/paludis/sync.cc
index 6fd7fd1..3266dc2 100644
--- a/src/paludis/sync.cc
+++ b/src/paludis/sync.cc
@@ -24,6 +24,7 @@
#include <iostream>
#include <paludis/paludis.hh>
#include <string>
+#include <set>
/** \file
* Handle the --sync action for the main paludis program.