summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorattilamolnar <attilamolnar@hush.com>2013-04-04 22:09:21 +0200
committerattilamolnar <attilamolnar@hush.com>2013-04-08 23:13:24 +0200
commit927937d6105d17fbcd8c85bbf185477d87264bc4 (patch)
treeef5ab7eb5c0bd6ebfce721b99e1464183a09a364 /include
parent9fc218c005543384bcad73747a0574c8c6ab6289 (diff)
Rework dynamic_reference management, introduce dynamic_reference_nocheck
New dynamic references are now resolved at construction and all of them are re-resolved when a relevant service is added or removed; resolution is no longer done in operator-> dynamic_reference_nocheck is a variant of dynamic_reference that does not check for value being null in operator-> / operator* dynamic_reference still throws an exception when used in this case Both kinds of dynamic references support .check(): an exception is thrown if this is called when value is null
Diffstat (limited to 'include')
-rw-r--r--include/modules.h39
1 files changed, 34 insertions, 5 deletions
diff --git a/include/modules.h b/include/modules.h
index e09245de9..ac90d60e7 100644
--- a/include/modules.h
+++ b/include/modules.h
@@ -286,32 +286,61 @@ class CoreExport dynamic_reference_base : public interfacebase
{
private:
std::string name;
+ void resolve();
protected:
DataProvider* value;
public:
ModuleRef creator;
dynamic_reference_base(Module* Creator, const std::string& Name);
~dynamic_reference_base();
- inline void ClearCache() { value = NULL; }
inline const std::string& GetProvider() { return name; }
void SetProvider(const std::string& newname);
- void lookup();
- operator bool();
+ void check();
+ operator bool() { return (value != NULL); }
static void reset_all();
};
+inline void dynamic_reference_base::check()
+{
+ if (!value)
+ throw ModuleException("Dynamic reference to '" + name + "' failed to resolve");
+}
+
template<typename T>
class dynamic_reference : public dynamic_reference_base
{
public:
dynamic_reference(Module* Creator, const std::string& Name)
: dynamic_reference_base(Creator, Name) {}
+
inline T* operator->()
{
- if (!value)
- lookup();
+ check();
return static_cast<T*>(value);
}
+
+ T* operator*()
+ {
+ return operator->();
+ }
+};
+
+template<typename T>
+class dynamic_reference_nocheck : public dynamic_reference_base
+{
+ public:
+ dynamic_reference_nocheck(Module* Creator, const std::string& Name)
+ : dynamic_reference_base(Creator, Name) {}
+
+ T* operator->()
+ {
+ return static_cast<T*>(value);
+ }
+
+ T* operator*()
+ {
+ return operator->();
+ }
};
/** Priority types which can be used by Module::Prioritize()