This example demonstrates how to handle dependency labels. It produces a summary of distfiles for all installed packages, together with a notice of whether that distfile is fetch-restricted.
#include <paludis/paludis.hh>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <map>
using namespace examples;
using std::cout;
using std::endl;
using std::setw;
using std::left;
typedef std::map<std::string, bool> ResultsMap;
namespace
{
class IsLabelRestrictedVisitor
{
public:
bool result;
IsLabelRestrictedVisitor(const bool initial) :
result(initial)
{
}
void visit(const URIListedThenMirrorsLabel &)
{
result = false;
}
void visit(const URIListedOnlyLabel &)
{
result = false;
}
void visit(const URIMirrorsOnlyLabel &)
{
result = false;
}
void visit(const URIMirrorsThenListedLabel &)
{
result = false;
}
void visit(const URILocalMirrorsOnlyLabel &)
{
result = true;
}
void visit(const URIManualOnlyLabel &)
{
result = true;
}
};
class DistfilesCollector
{
private:
ResultsMap & _results;
std::list<bool> _restricted;
public:
DistfilesCollector(ResultsMap & r, const bool initial) :
_results(r)
{
_restricted.push_back(initial);
}
{
_restricted.push_back(_restricted.back());
_restricted.pop_back();
}
{
_restricted.push_back(_restricted.back());
_restricted.pop_back();
}
{
_results.insert(std::make_pair(node.spec()->filename(), _restricted.back()));
}
{
IsLabelRestrictedVisitor v(_restricted.back());
_restricted.back() = v.
result;
}
};
}
int main(int argc, char * argv[])
{
try
{
"example_dep_label", "EXAMPLE_DEP_LABEL_OPTIONS", "EXAMPLE_DEP_LABEL_CMDLINE");
ResultsMap results;
for (const auto & id : *ids)
{
if (! id->fetches_key())
continue;
IsLabelRestrictedVisitor is_initial_label_restricted(false);
id->fetches_key()->initial_label()->accept(is_initial_label_restricted);
DistfilesCollector collector(results, is_initial_label_restricted.result);
id->fetches_key()->parse_value()->top()->accept(collector);
}
cout << left << setw(59) << "Distfile Name" << "| " << "Fetch Restricted?" << endl;
cout << std::string(59, '-') << "+" << std::string(18, '-') << endl;
for (const auto & result : results)
cout << left << setw(59) << result.first << "| " << (result.second ? "yes" : "no") << endl;
}
{
cout << endl;
cout << "Unhandled exception:" << endl
return EXIT_FAILURE;
}
catch (const std::exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.what() << endl;
return EXIT_FAILURE;
}
catch (...)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * Unknown exception type. Ouch..." << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Definition exception.hh:77
std::string backtrace(const std::string &delim) const
const char * what() const noexcept override
const std::string & message() const noexcept
static CommandLine * get_instance() PALUDIS_ATTRIBUTE((warn_unused_result))
Definition generator.hh:147
Definition selection.hh:208
IndirectIterator< Iter_ > indirect_iterator(const Iter_ &)
AcceptVisitor< Visitor_ > PALUDIS_VISIBLE accept_visitor(Visitor_ &v)
Definition visitor.hh:100
Definition about_metadata-fwd.hh:24
Definition spec_tree.hh:158