|
| 1 | +/* |
| 2 | +* projectM -- Milkdrop-esque visualisation SDK |
| 3 | + * Copyright (C)2003-2007 projectM Team |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2.1 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | + * See 'LICENSE.txt' included within this release |
| 19 | + * |
| 20 | + */ |
| 21 | +#pragma once |
| 22 | + |
| 23 | +#include <projectM-4/debug.h> |
| 24 | + |
| 25 | +#include <cstdint> |
| 26 | +#include <exception> |
| 27 | +#include <map> |
| 28 | +#include <string> |
| 29 | +#include <utility> |
| 30 | + |
| 31 | +namespace libprojectM { |
| 32 | + |
| 33 | +/** |
| 34 | + * @class ExpressionVariableWatcher |
| 35 | + * @brief Container class managing active expression variable watches. |
| 36 | + * |
| 37 | + * All watches are stored in a three-tiered map, with the first map using the block and index as key. |
| 38 | + * The second level map associated each watched variable to the struct which will receive the |
| 39 | + * values. This struct and is also used by the outside application to display or otherwise evaluate |
| 40 | + * the values after a frame is rendered. |
| 41 | + */ |
| 42 | +class ExpressionVariableWatcher |
| 43 | +{ |
| 44 | +public: |
| 45 | + /** |
| 46 | + * @brief Exception for an invalid block argument in GetBlockWatches(). |
| 47 | + */ |
| 48 | + class InvalidBlockException : public std::exception |
| 49 | + { |
| 50 | + public: |
| 51 | + InvalidBlockException(std::string message) |
| 52 | + : m_message(std::move(message)) |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + ~InvalidBlockException() override = default; |
| 57 | + |
| 58 | + auto what() const noexcept -> const char* override |
| 59 | + { |
| 60 | + return m_message.c_str(); |
| 61 | + } |
| 62 | + |
| 63 | + auto message() const -> const std::string& |
| 64 | + { |
| 65 | + return m_message; |
| 66 | + } |
| 67 | + |
| 68 | + private: |
| 69 | + std::string m_message; |
| 70 | + }; |
| 71 | + |
| 72 | + using ExpressionBlocks = projectm_expression_blocks; //!< Alias for the projectm_expression_blocks public API enum. |
| 73 | + using IndexedExpressionBlock = std::pair<ExpressionBlocks, uint32_t>; //!< A pair if ExpressionBlocks and the custom shape/waveform or sprite index. |
| 74 | + using VariableName = std::string; //!< the name of a watched variable, lower-case. |
| 75 | + using VariableValues = projectm_expression_variable_values; //!< Alias for the projectm_expression_variable_values public API struct. |
| 76 | + using BlockWatches = std::map<VariableName, VariableValues>; //!< A map of variable names to the associated value structs. |
| 77 | + |
| 78 | + /** |
| 79 | + * @brief Adds a new watch, or returns a pointer to the previously registered one. |
| 80 | + * @param block The expression code block to add the watch for. |
| 81 | + * @param index The custom shape/waveform or user sprite index. |
| 82 | + * @param variableName The variable to watch. |
| 83 | + * @return A pointer to the newly added values struct, if the watch was successfully added. |
| 84 | + */ |
| 85 | + auto Add(ExpressionBlocks block, uint32_t index, VariableName variableName) -> const VariableValues*; |
| 86 | + |
| 87 | + /** |
| 88 | + * @brief Removes a previously registered variable watch. |
| 89 | + * @param block The expression code block to remove the watch from. |
| 90 | + * @param index The custom shape/waveform or user sprite index. |
| 91 | + * @param variableName The variable to unwatch. |
| 92 | + * @return If the previous watch was found, a pointer to the initially registered values struct, |
| 93 | + * or nullptr if no watch was found. |
| 94 | + */ |
| 95 | + auto Remove(ExpressionBlocks block, uint32_t index, VariableName variableName) -> const VariableValues*; |
| 96 | + |
| 97 | + /** |
| 98 | + * Clears all previously registered watches. |
| 99 | + */ |
| 100 | + void Clear(); |
| 101 | + |
| 102 | + /** |
| 103 | + * @brief Returns all watched variables and their value structs for the given block and index. |
| 104 | + * @throws InvalidBlockException Thrown if the passed block value is invalid. |
| 105 | + * @param block The expression code block to return the watch list for. |
| 106 | + * @param index The custom shape/waveform or user sprite index to return the watch list for. |
| 107 | + * @return A reference to a map of type BlockWatches with all registered watched variables. |
| 108 | + */ |
| 109 | + auto GetBlockWatches(ExpressionBlocks block, uint32_t index) -> BlockWatches&; |
| 110 | + |
| 111 | +private: |
| 112 | + /** |
| 113 | + * @brief Registered watches. |
| 114 | + * |
| 115 | + * A three-tiered map of indexed blocks, variables and the value struct returned to the application. |
| 116 | + */ |
| 117 | + std::map<IndexedExpressionBlock, BlockWatches> m_watches; |
| 118 | +}; |
| 119 | + |
| 120 | +} // namespace libprojectM |
0 commit comments