aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/e_choice_value.cc
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-01-31 16:17:41 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-01-31 16:17:41 +0000
commit8cd3f89f1e7817fafca33e410b24af18fc333874 (patch)
tree1f991906a1512f4b8f508db07834575671fde660 /paludis/repositories/e/e_choice_value.cc
parent6eeca897e480fbb24cc5bfd6df6cf97fc5d82594 (diff)
downloadpaludis-8cd3f89f1e7817fafca33e410b24af18fc333874.tar.gz
paludis-8cd3f89f1e7817fafca33e410b24af18fc333874.tar.xz
Reuse common choice values
Diffstat (limited to 'paludis/repositories/e/e_choice_value.cc')
-rw-r--r--paludis/repositories/e/e_choice_value.cc107
1 files changed, 107 insertions, 0 deletions
diff --git a/paludis/repositories/e/e_choice_value.cc b/paludis/repositories/e/e_choice_value.cc
index 2251efa97..a89866fb2 100644
--- a/paludis/repositories/e/e_choice_value.cc
+++ b/paludis/repositories/e/e_choice_value.cc
@@ -20,10 +20,38 @@
#include <paludis/repositories/e/e_choice_value.hh>
#include <paludis/repositories/e/use_desc.hh>
#include <paludis/util/make_null_shared_ptr.hh>
+#include <paludis/util/hashes.hh>
+#include <paludis/util/mutex.hh>
+#include <paludis/util/pimp-impl.hh>
+#include <paludis/util/singleton-impl.hh>
+#include <unordered_map>
using namespace paludis;
using namespace paludis::erepository;
+namespace
+{
+ class EChoiceValue :
+ public ChoiceValue
+ {
+ private:
+ const EChoiceValueParams _params;
+
+ public:
+ EChoiceValue(const EChoiceValueParams &);
+
+ const UnprefixedChoiceName unprefixed_name() const;
+ const ChoiceNameWithPrefix name_with_prefix() const;
+ bool enabled() const;
+ bool enabled_by_default() const;
+ bool locked() const;
+ const std::string description() const;
+ bool explicitly_listed() const;
+ const std::string parameter() const PALUDIS_ATTRIBUTE((warn_unused_result));
+ const std::shared_ptr<const PermittedChoiceValueParameterValues> permitted_parameter_values() const PALUDIS_ATTRIBUTE((warn_unused_result));
+ };
+}
+
EChoiceValue::EChoiceValue(const EChoiceValueParams & p) :
_params(p)
{
@@ -83,3 +111,82 @@ EChoiceValue::permitted_parameter_values() const
return make_null_shared_ptr();
}
+namespace
+{
+ struct EChoiceValueParamsHash
+ {
+ Hash<ChoiceNameWithPrefix> choice_name_with_prefix_hash;
+ Hash<ChoicePrefixName> choice_prefix_name_hash;
+ Hash<std::string> description_hash;
+ Hash<bool> enabled_hash;
+ Hash<bool> enabled_by_default_hash;
+ Hash<bool> explicitly_listed_hash;
+ Hash<bool> locked_hash;
+ Hash<UnprefixedChoiceName> unprefixed_choice_name_hash;
+
+ std::size_t operator() (const EChoiceValueParams & p) const
+ {
+ return 0
+ ^ choice_name_with_prefix_hash(p.choice_name_with_prefix())
+ ^ choice_prefix_name_hash(p.choice_prefix_name())
+ ^ description_hash(p.description())
+ ^ enabled_hash(p.enabled())
+ ^ enabled_by_default_hash(p.enabled_by_default())
+ ^ explicitly_listed_hash(p.explicitly_listed())
+ ^ locked_hash(p.locked())
+ ^ unprefixed_choice_name_hash(p.unprefixed_choice_name())
+ ;
+ }
+ };
+
+ struct EChoiceValueParamsCompare
+ {
+ bool operator() (const EChoiceValueParams & a, const EChoiceValueParams & b) const
+ {
+ return true
+ && (a.choice_name_with_prefix() == b.choice_name_with_prefix())
+ && (a.choice_prefix_name() == b.choice_prefix_name())
+ && (a.description() == b.description())
+ && (a.enabled() == b.enabled())
+ && (a.enabled_by_default() == b.enabled_by_default())
+ && (a.explicitly_listed() == b.explicitly_listed())
+ && (a.locked() == b.locked())
+ && (a.unprefixed_choice_name() == b.unprefixed_choice_name())
+ ;
+ }
+ };
+
+ typedef std::unordered_map<EChoiceValueParams, std::shared_ptr<const EChoiceValue>, EChoiceValueParamsHash, EChoiceValueParamsCompare> Store;
+}
+
+namespace paludis
+{
+ template <>
+ struct Imp<EChoiceValueStore>
+ {
+ mutable Mutex mutex;
+ mutable Store store;
+ };
+}
+
+EChoiceValueStore::EChoiceValueStore() :
+ _imp()
+{
+}
+
+EChoiceValueStore::~EChoiceValueStore() = default;
+
+const std::shared_ptr<const ChoiceValue>
+EChoiceValueStore::fetch(const EChoiceValueParams & p) const
+{
+ Lock lock(_imp->mutex);
+ auto i(_imp->store.find(p));
+ if (i == _imp->store.end())
+ i = _imp->store.insert(std::make_pair(p, std::make_shared<EChoiceValue>(p))).first;
+
+ return i->second;
+}
+
+template class Pimp<erepository::EChoiceValueStore>;
+template class Singleton<erepository::EChoiceValueStore>;
+