/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2021 * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * * software and associated documentation files (the "Software"), to deal in the Software * * without restriction, including without limitation the rights to use, copy, modify, * * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to the following * * conditions: * * * * The above copyright notice and this permission notice shall be included in all copies * * or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ #ifndef __OPENSPACE_CORE___PROPERTYDELEGATE___H__ #define __OPENSPACE_CORE___PROPERTYDELEGATE___H__ #include struct lua_State; namespace openspace::properties { /** * The PropertyDelegate class is used by (among others) the TemplateProperty and the * NumericalProperty classes to outsource the definitions of class names, default values, * etc. Using the PropertyDelegate, it is possible to create new TemplateProperty types * without subclassing the TemplateProperty, but rather creating a specialized instance * of PropertyDelegate. See * (https://github.com/OpenSpace/OpenSpace/wiki/Concepts-Properties) for more detailed * information. * \see TemplateProperty * \see NumericalProperty * \tparam T The full class for which this specialized instance of PropertyDelegate is * responsible. For example T = TemplateProperty. */ template class PropertyDelegate { public: /** * This method returns the class name for the class T. The default * implementation will lead to a compile-time error if the class method is not * specialized. * \return The class name for the class T */ static std::string className(); /** * This method will return the preferred default value for the class T. * The default implementation will lead to a compile-time error if the class method is * not specialized. * \return The default value that the class T should use * \tparam U The type by which the class T is specialized. If * T = TemplateProperty, then U = std::string */ template static U defaultValue(); /** * This method will return the preferred default minimum value for the class * T. The default implementation will lead to a compile-time error if the * class method is not specialized. This method is not used in TemplateProperty, but * only NumericalProperty, so the TemplateProperty does not require this method to be * specialized. * \return The default minimum value that the class T should use * \tparam U The type by which the class T is specialized. If * T = NumericalProperty, then U = int */ template static U defaultMinimumValue(); /** * This method will return the preferred default maximum value for the class * T. The default implementation will lead to a compile-time error if the * class method is not specialized. This method is not used in TemplateProperty, but * only NumericalProperty, so the TemplateProperty does not require this method to be * specialized. * \return The default maximum value that the class T should use * \tparam U The type by which the class T is specialized. If * T = NumericalProperty, then U = int */ template static U defaultMaximumValue(); /** * The method returns the default stepping value for the class T used in * GUI elements. The default implementation will lead to a compile-time error if the * class method is not specialized. This method is not used in TemplateProperty, but * only NumericalProperty, so the TemplateProperty does not require this method to be * specialized. * \return The default stepping that the class T should use * \tparam U The type by which the class T is specialized. If * T = NumericalProperty, then U = int */ template static U defaultSteppingValue(); /** * This method converts the top value from the Lua stack into a value of type * U and reports the success back to the caller. The default * implementation will lead to a compile-time error if the class method is not * specialized. * \param state The Lua state from which the value is retrieved * \param success Will be true if the conversion succeeded; * false otherwise * \return The value that was created by converting the top value from the stack * \tparam U The type by which the class T is specialized. If * T = TemplateProperty, then U = std::string */ template static U fromLuaValue(lua_State* state, bool& success); /** * This method converts the passed value, encodes it and places it on the * top value of the Lua stack and returns the success back to the caller. The default * implementation will lead to a compile-time error if the class method is not * specialized. * \param state The Lua state from which the value is retrieved * \param value The value that will be converted into a Lua object * \return true if the conversion succeeded; false otherwise * \tparam U The type by which the class T is specialized. If * T = TemplateProperty, then U = std::string */ template static bool toLuaValue(lua_State* state, const U& value); /** * Returns the Lua type that will be put onto the stack in the * PropertyDelegate::toLuaValue method and which will be consumed by the * PropertyDelegate::fromLuaValue method. The returned value can belong to the set of * Lua types: LUA_TNONE, LUA_TNIL, * LUA_TBOOLEAN, LUA_TLIGHTUSERDATA, * LUA_TNUMBER, LUA_TSTRING, LUA_TTABLE, * LUA_TFUNCTION, LUA_TUSERDATA, or * LUA_TTHREAD. The default implementation will return * LUA_TNONE. The default implementation will lead to a compile-time * error if the class method is not specialized. * \return The Lua type that will be consumed or produced by the * PropertyDelegate::toLuaValue and PropertyDelegate::fromLuaValue methods. */ static int typeLua(); template static U fromString(const std::string& value, bool& success); template static bool toString(std::string& outValue, const U& inValue); }; } // namespace openspace::properties #include #endif // __OPENSPACE_CORE___PROPERTYDELEGATE___H__