Mercurial > code
changeset 440:738751252248
Js: add optionalProperty
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 23 Oct 2015 08:16:03 +0200 |
parents | aaf975293996 |
children | 21f1a6ed0570 |
files | C++/modules/Js/Js.h |
diffstat | 1 files changed, 38 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Js/Js.h Fri Oct 23 08:15:48 2015 +0200 +++ b/C++/modules/Js/Js.h Fri Oct 23 08:16:03 2015 +0200 @@ -818,6 +818,44 @@ } /** + * Get an optional property `name` from the object at the specified index. + * + * @param index the object index + * @param name the property name + * @param def the default value + * @return the value or def + * @note The stack is unchanged + */ + template <typename Type, typename DefaultValue> + inline auto optionalProperty(int index, const std::string &name, DefaultValue &&def) -> decltype(optional<Type>(0, std::forward<DefaultValue>(def))) + { + duk_get_prop_string(m_handle.get(), index, name.c_str()); + decltype(optional<Type>(0, std::forward<DefaultValue>(def))) value = optional<Type>(-1, std::forward<DefaultValue>(def)); + duk_pop(m_handle.get()); + + return value; + } + + /** + * Get an optional property by index, for arrays + * + * @param index the object index + * @param position the position int the object + * @param def the default value + * @return the value or def + * @note The stack is unchanged + */ + template <typename Type, typename DefaultValue> + inline auto optionalProperty(int index, int position, DefaultValue &&def) -> decltype(optional<Type>(0, std::forward<DefaultValue>(def))) + { + duk_get_prop_index(m_handle.get(), index, position); + decltype(optional<Type>(0, std::forward<DefaultValue>(def))) value = optional<Type>(-1, std::forward<DefaultValue>(def)); + duk_pop(m_handle.get()); + + return value; + } + + /** * Set a property to the object at the specified index. * * @param index the object index