From f48ac9f0c3fae1e5ea04768183e07823879a7d79 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 24 Jan 2019 09:48:30 +0100 Subject: BlockTypeRegistry: Added hint manipulation --- src/BlockTypeRegistry.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-) (limited to 'src/BlockTypeRegistry.h') diff --git a/src/BlockTypeRegistry.h b/src/BlockTypeRegistry.h index 6a24445c5..3fbcc44c2 100644 --- a/src/BlockTypeRegistry.h +++ b/src/BlockTypeRegistry.h @@ -40,7 +40,7 @@ public: /** Retrieves the value associated with the specified hint for this specific BlockTypeName and BlockState. - Queries callbacks first, then hints if a callback doesn't exist. + Queries hint callbacks first, then static hints if a callback doesn't exist. Returns an empty string if hint not found at all. */ AString hintValue( const AString & aHintName, @@ -52,6 +52,15 @@ public: const AString & blockTypeName() const { return mBlockTypeName; } std::shared_ptr handler() const { return mHandler; } + /** Sets (creates or updates) a static hint. + Hints provided by callbacks are unaffected by this - callbacks are "higher priority", they overwrite anything set here. + Logs an info message if the hint is already provided by a hint callback. */ + void setHint(const AString & aHintKey, const AString & aHintValue); + + /** Removes a hint. + Silently ignored if the hint hasn't been previously set. */ + void removeHint(const AString & aHintKey); + private: @@ -64,10 +73,12 @@ private: /** The callbacks to call for various interaction. */ std::shared_ptr mHandler; - /** Optional hints for any subsystem to use, such as "IsSnowable" -> "1". */ + /** Optional static hints for any subsystem to use, such as "IsSnowable" -> "1". + Hint callbacks are of higher priority than mHints - if a hint is provided by a mHintCallback, its value in mHints is ignored. */ std::map mHints; - /** The callbacks for dynamic evaluation of hints, such as "LightValue" -> function(BlockTypeName, BlockState). */ + /** The callbacks for dynamic evaluation of hints, such as "LightValue" -> function(BlockTypeName, BlockState). + Hint callbacks are of higher priority than mHints - if a hint is provided by a mHintCallback, its value in mHints is ignored. */ std::map mHintCallbacks; }; @@ -86,6 +97,7 @@ class BlockTypeRegistry public: // fwd: class AlreadyRegisteredException; + class NotRegisteredException; /** Creates an empty new instance of the block type registry */ @@ -109,6 +121,22 @@ public: /** Removes all registrations done by the specified plugin. */ void removeAllByPlugin(const AString & aPluginName); + /** Sets (adds or overwrites) a single Hint value for a BlockType. + Throws NotRegisteredException if the BlockTypeName is not registered. */ + void setBlockTypeHint( + const AString & aBlockTypeName, + const AString & aHintKey, + const AString & aHintValue + ); + + /** Removes a previously registered single Hint value for a BlockType. + Throws NotRegisteredException if the BlockTypeName is not registered. + Silently ignored if the Hint hasn't been previously set. */ + void removeBlockTypeHint( + const AString & aBlockTypeName, + const AString & aHintKey + ); + private: @@ -156,3 +184,31 @@ private: std::shared_ptr aNewRegistration ); }; + + + + + +/** The exception thrown from BlockTypeRegistry::setBlockTypeHint() if the block type has not been registered before. */ +class BlockTypeRegistry::NotRegisteredException: public std::runtime_error +{ + using Super = std::runtime_error; + +public: + + /** Creates a new instance of the exception that provides info on both the original registration and the newly attempted + registration that caused the failure. */ + NotRegisteredException( + const AString & aBlockTypeName, + const AString & aHintKey, + const AString & aHintValue + ); + + // Simple getters: + const AString & blockTypeName() const { return mBlockTypeName; } + + +private: + + const AString mBlockTypeName; +}; -- cgit v1.2.3