aboutsummaryrefslogtreecommitdiff
path: root/doc/api/cplusplus/examples/example_package_id.cc
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-10-15 11:10:38 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-10-15 11:10:38 +0000
commit503481c165de28bc0a5db25b7848708df7cb2668 (patch)
tree41eac48b7d7915ca0e7e88bb5dc9ea65225d3b99 /doc/api/cplusplus/examples/example_package_id.cc
parent8f51d97de6b38b8bc8a9b129baecf4bebd183c7b (diff)
downloadpaludis-503481c165de28bc0a5db25b7848708df7cb2668.tar.gz
paludis-503481c165de28bc0a5db25b7848708df7cb2668.tar.xz
Start reworking docs
Diffstat (limited to 'doc/api/cplusplus/examples/example_package_id.cc')
-rw-r--r--doc/api/cplusplus/examples/example_package_id.cc160
1 files changed, 160 insertions, 0 deletions
diff --git a/doc/api/cplusplus/examples/example_package_id.cc b/doc/api/cplusplus/examples/example_package_id.cc
new file mode 100644
index 000000000..b684e3a08
--- /dev/null
+++ b/doc/api/cplusplus/examples/example_package_id.cc
@@ -0,0 +1,160 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/** \file
+ *
+ * Example \ref example_package_id.cc "example_package_id.cc" .
+ *
+ * \ingroup g_package_id
+ */
+
+/** \example example_package_id.cc
+ *
+ * This example demonstrates how to use PackageID.
+ *
+ * See \ref example_action.cc "example_action.cc" for more on actions. See \ref
+ * example_metadata_key.cc "example_metadata_key.cc" for more on metadata keys.
+ * See \ref example_mask.cc "example_mask.cc" for more on masks.
+ */
+
+#include <paludis/paludis.hh>
+#include "example_command_line.hh"
+#include <iostream>
+#include <iomanip>
+#include <set>
+
+using namespace paludis;
+using namespace examples;
+
+using std::cout;
+using std::endl;
+using std::left;
+using std::setw;
+using std::hex;
+using std::boolalpha;
+
+int main(int argc, char * argv[])
+{
+ int exit_status(0);
+
+ try
+ {
+ CommandLine::get_instance()->run(argc, argv,
+ "example_package_id", "EXAMPLE_PACKAGE_ID_OPTIONS", "EXAMPLE_PACKAGE_ID_CMDLINE");
+
+ /* We start with an Environment, respecting the user's '--environment' choice. */
+ tr1::shared_ptr<Environment> env(EnvironmentMaker::get_instance()->make_from_spec(
+ CommandLine::get_instance()->a_environment.argument()));
+
+ /* Fetch package IDs for 'sys-apps/paludis'. */
+ tr1::shared_ptr<const PackageIDSequence> ids(env->package_database()->query(
+ query::Matches(PackageDepSpec("sys-apps/paludis", pds_pm_permissive)),
+ qo_order_by_version));
+
+ /* For each ID: */
+ for (PackageIDSet::ConstIterator i(ids->begin()), i_end(ids->end()) ;
+ i != i_end ; ++i)
+ {
+ /* IDs can be written to an ostream. */
+ cout << **i << ":" << endl;
+
+ /* Start by outputting some basic properties: */
+ cout << left << setw(30) << " Name:" << " " << (*i)->name() << endl;
+ cout << left << setw(30) << " Version:" << " " << (*i)->version() << endl;
+ cout << left << setw(30) << " Slot:" << " " << (*i)->slot() << endl;
+ cout << left << setw(30) << " Repository Name:" << " " << (*i)->repository()->name() << endl;
+
+ /* The PackageID::canonical_form method should be used when
+ * outputting a package (the ostream << operator uses this). */
+ cout << left << setw(30) << " idcf_full:" << " " << (*i)->canonical_form(idcf_full) << endl;
+ cout << left << setw(30) << " idcf_version:" << " " << (*i)->canonical_form(idcf_version) << endl;
+ cout << left << setw(30) << " idcf_no_version:" << " " << (*i)->canonical_form(idcf_no_version) << endl;
+
+ /* Let's see what keys we have. Other examples cover keys in depth,
+ * so we'll just use the basic methods here. */
+ cout << left << setw(30) << " Keys:" << " " << endl;
+ for (PackageID::MetadataConstIterator k((*i)->begin_metadata()), k_end((*i)->end_metadata()) ;
+ k != k_end ; ++k)
+ cout << left << setw(30) << (" " + (*k)->raw_name() + ":") << " " << (*k)->human_name() << endl;
+
+ /* And what about masks? Again, these are covered in depth
+ * elsewhere. */
+ if ((*i)->masked())
+ {
+ cout << left << setw(30) << " Masks:" << " " << endl;
+ for (PackageID::MasksConstIterator m((*i)->begin_masks()), m_end((*i)->end_masks()) ;
+ m != m_end ; ++m)
+ cout << left << setw(30) << (" " + stringify((*m)->key()) + ":") << " " << (*m)->description() << endl;
+ }
+
+ /* Let's see which actions we support. There's no particularly nice
+ * way of doing this, since it's not something we'd expect to be
+ * doing. */
+ std::set<std::string> actions;
+ {
+ SupportsActionTest<InstallAction> install_action;
+ if ((*i)->supports_action(install_action))
+ actions.insert("install");
+
+ SupportsActionTest<InstalledAction> installed_action;
+ if ((*i)->supports_action(installed_action))
+ actions.insert("installed");
+
+ SupportsActionTest<UninstallAction> uninstall_action;
+ if ((*i)->supports_action(uninstall_action))
+ actions.insert("uninstall");
+
+ SupportsActionTest<PretendAction> pretend_action;
+ if ((*i)->supports_action(pretend_action))
+ actions.insert("pretend");
+
+ SupportsActionTest<ConfigAction> config_action;
+ if ((*i)->supports_action(config_action))
+ actions.insert("config");
+
+ SupportsActionTest<UninstallAction> fetch_action;
+ if ((*i)->supports_action(fetch_action))
+ actions.insert("fetch");
+
+ SupportsActionTest<UninstallAction> info_action;
+ if ((*i)->supports_action(info_action))
+ actions.insert("info");
+ }
+ cout << left << setw(30) << " Actions:" << " " << join(actions.begin(), actions.end(), " ") << endl;
+
+ /* And various misc methods. Clients don't usually use these
+ * directly. */
+ cout << left << setw(30) << " breaks_portage:" << " " << boolalpha << (*i)->breaks_portage() << endl;
+ cout << left << setw(30) << " extra_hash_value:" << " " << "0x" << hex << (*i)->extra_hash_value() << endl;
+
+ cout << 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;
+}
+
+