example_selection.cc
This example demonstrates how to use the standard Selection, Generator and Filter classes.
#include <paludis/paludis.hh>
#include "example_command_line.hh"
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
namespace
{
void show_selection(const std::tr1::shared_ptr<const Environment> & env, const Selection & selection)
{
cout << selection << ":" << endl;
std::tr1::shared_ptr<const PackageIDSequence> ids((*env)[selection]);
if (! ids->empty())
std::copy(indirect_iterator(ids->begin()), indirect_iterator(ids->end()),
std::ostream_iterator<const PackageID>(cout, "\n"));
cout << endl;
}
}
int main(int argc, char * argv[])
{
int exit_status(0);
try
{
CommandLine::get_instance()->run(argc, argv,
"example_selection", "EXAMPLE_SELECTION_OPTIONS", "EXAMPLE_SELECTION_CMDLINE");
std::tr1::shared_ptr<Environment> env(EnvironmentFactory::get_instance()->create(
CommandLine::get_instance()->a_environment.argument()));
show_selection(env, selection::AllVersionsSorted(
generator::Matches(make_package_dep_spec().package(QualifiedPackageName("sys-apps/paludis")), MatchPackageOptions())));
show_selection(env, selection::AllVersionsSorted(
generator::Matches(make_package_dep_spec().package(QualifiedPackageName("sys-apps/paludis")), MatchPackageOptions()) |
filter::SupportsAction<InstalledAction>()));
show_selection(env, selection::AllVersionsSorted(
generator::Matches(make_package_dep_spec().package(QualifiedPackageName("sys-apps/paludis")), MatchPackageOptions()) |
filter::SupportsAction<InstallAction>() |
filter::NotMasked()));
show_selection(env, selection::BestVersionOnly(
generator::Matches(make_package_dep_spec().package(QualifiedPackageName("sys-apps/paludis")), MatchPackageOptions()) |
filter::SupportsAction<InstallAction>() |
filter::NotMasked()));
}
catch (const Exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.backtrace("\n * ")
<< e.message() << " (" << e.what() << ")" << 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_status;
}