paludis Version 3.0.2
The one true package mangler
example_match_package.cc

This example demonstrates how to use paludis::match_package and paludis::match_package_in_set.

/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/** \file
*
* Example \ref example_match_package.cc "example_match_package.cc" .
*
* \ingroup g_query
*/
/** \example example_match_package.cc
*
* This example demonstrates how to use paludis::match_package and
* paludis::match_package_in_set.
*/
#include <paludis/paludis.hh>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
using std::left;
using std::setw;
int main(int argc, char * argv[])
{
int exit_status(0);
try
{
CommandLine::get_instance()->run(argc, argv,
"example_match_package", "EXAMPLE_MATCH_PACKAGE_OPTIONS", "EXAMPLE_MATCH_PACKAGE_CMDLINE");
/* We start with an Environment, respecting the user's '--environment' choice. */
std::shared_ptr<Environment> env(EnvironmentFactory::get_instance()->create(
CommandLine::get_instance()->a_environment.argument()));
/* Fetch all installed packages. */
std::shared_ptr<const PackageIDSequence> ids((*env)[selection::AllVersionsSorted(
/* Fetch the 'system' and 'world' sets. Ordinarily we should check for
* zero pointers here, but these two sets will always exist. */
std::shared_ptr<const SetSpecTree> system(env->set(SetName("system")));
std::shared_ptr<const SetSpecTree> world(env->set(SetName("world")));
/* For each ID: */
for (const auto & id : *ids)
{
/* Is it paludis? */
QualifiedPackageName("sys-apps/paludis")), id, nullptr, { }))
cout << left << setw(50) << (stringify(*id) + ":") << " " << "paludis" << endl;
/* No. Is it in system or world? */
else if (match_package_in_set(*env, *system, id, { }))
cout << left << setw(50) << (stringify(*id) + ":") << " " << "system" << endl;
else if (match_package_in_set(*env, *world, id, { }))
cout << left << setw(50) << (stringify(*id) + ":") << " " << "world" << endl;
else
cout << left << setw(50) << (stringify(*id) + ":") << " " << "nothing" << endl;
}
}
catch (const Exception & e)
{
/* Paludis exceptions can provide a handy human-readable backtrace and
* an explanation message. Where possible, these should be displayed. */
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;
}
Definition exception.hh:77
std::string backtrace(const std::string &delim) const
const char * what() const noexcept override
const std::string & message() const noexcept
PartiallyMadePackageDepSpec & package(const QualifiedPackageName &)
Definition name.hh:112
static CommandLine * get_instance() PALUDIS_ATTRIBUTE((warn_unused_result))
Definition filter.hh:207
Definition generator.hh:147
Definition selection.hh:172
PartiallyMadePackageDepSpec make_package_dep_spec(const PartiallyMadePackageDepSpecOptions &) PALUDIS_VISIBLE
WrappedValue< SetNameTag > SetName
Definition name-fwd.hh:131
std::string stringify(const T_ &item)
Definition stringify.hh:166
Definition about_metadata-fwd.hh:24
bool match_package(const Environment &env, const PackageDepSpec &spec, const std::shared_ptr< const PackageID > &id, const std::shared_ptr< const PackageID > &spec_id, const MatchPackageOptions &options) PALUDIS_ATTRIBUTE((warn_unused_result)) PALUDIS_VISIBLE
bool match_package_in_set(const Environment &env, const SetSpecTree &spec, const std::shared_ptr< const PackageID > &id, const MatchPackageOptions &options) PALUDIS_ATTRIBUTE((warn_unused_result)) PALUDIS_VISIBLE