new 4475edb243ed4627f4c5f2c470ca40b3def034d4
[tagua/yd.git] / src / core / repository.h
blobf96454a8948b1ede9aeed321efa537a38ce3b78b
1 #ifndef CORE__REPOSITORY_H
2 #define CORE__REPOSITORY_H
4 #include <QMap>
5 #include <QString>
6 #include "component.h"
7 #include "export.h"
9 /**
10 * @brief A hierarchical collection of component defining a variant.
12 * Components of a variant are maintained in a hierarchical structure
13 * called repository. All components have a path that uniquely identifies
14 * them inside a given variant.
16 * Non-leaf nodes are not actual components, and are called namespaces.
17 * Namespaces can be nested.
19 * Since the number of components inside a repository is not likely to
20 * grow over a dozen or so, there is not practical need for this structure
21 * to reflect its hierarchical representation, so we simply maintain all
22 * components inside a path -> component association.
24 class TAGUA_EXPORT Repository {
25 public:
26 typedef QMap<QString, ComponentPtr> ComponentMap;
27 private:
28 ComponentMap m_components;
29 Repository* m_proxy;
30 public:
31 Repository();
33 /**
34 * Add a new component to the repository at the specified path.
35 * If a component already exists at that path, it will be silently
36 * overwritten.
38 void addComponent(const QString& path, ComponentPtr component);
40 /**
41 * Retrieve a component from a given path.
42 * @return The component at path @a path, or a null pointer if
43 * no component exists at that path.
45 ComponentPtr getComponent(const QString& path) const;
47 /**
48 * Remove a component from a given path. Do nothing if no
49 * component exists at that path.
50 * @return The component removed from path @a path.
52 ComponentPtr takeComponent(const QString& path);
54 /**
55 * Determine the list of components living inside a given namespace.
56 * Nested namespaces will be silently ignored.
58 ComponentMap listComponents(QString path) const;
60 /**
61 * Add a proxy repository, that will be queried when a component
62 * is not found here.
64 void setProxy(Repository* proxy);
67 // template <typename Interface>
68 // Interface* requestInterface(Component* component) {
69 // Interface* iface = dynamic_cast<Interface*>(component);
70 // if (iface)
71 // return iface;
72 // else
73 // return new typename Interface::Adaptor(iface);
74 // }
76 template <typename Interface>
77 Interface* requestInterface(Component* component) {
78 return dynamic_cast<Interface*>(component);
81 #endif // CORE__REPOSITORY_H