2005-04-29 18:06:50 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
//osgIntrospection - Copyright (C) 2005 Marco Jez
|
|
|
|
|
2004-12-09 13:28:20 +08:00
|
|
|
#ifndef OSGINTROSPECTION_PROPERTYINFO_
|
|
|
|
#define OSGINTROSPECTION_PROPERTYINFO_
|
|
|
|
|
|
|
|
#include <osgIntrospection/Export>
|
|
|
|
#include <osgIntrospection/Type>
|
|
|
|
#include <osgIntrospection/MethodInfo>
|
|
|
|
#include <osgIntrospection/Attributes>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace osgIntrospection
|
|
|
|
{
|
|
|
|
|
2005-03-14 17:28:31 +08:00
|
|
|
/// This class keeps information about a class' property. A property is
|
|
|
|
/// defined by a name and a set of methods that store and retrieve
|
|
|
|
/// values. When the user wants to "get" the value of a property, the
|
|
|
|
/// getter method will be invoked and its value returned. When the user
|
|
|
|
/// wants to "set" the value of a property, the setter method will be
|
|
|
|
/// called. There are three kinds of property: simple (get/set), indexed
|
|
|
|
/// (get[i1, i2, ...]/set[i1, i2, ...]), and array (count/add/get[i]/
|
|
|
|
/// set[i]).
|
|
|
|
/// Objects of class PropertyInfo can't be modified once they have been
|
|
|
|
/// created, but they can be queried without restrictions. You can either
|
|
|
|
/// retrieve the accessor methods and invoke them manually, or you can
|
|
|
|
/// call getValue() / setValue() etc. methods to perform direct operations
|
|
|
|
/// on the property, given an instance of the declaring type to work on.
|
|
|
|
/// The latter technique is preferred because it checks for custom
|
|
|
|
/// attributes associated to the PropertyInfo object and passes control
|
|
|
|
/// to them when needed.
|
|
|
|
///
|
|
|
|
class OSGINTROSPECTION_EXPORT PropertyInfo: public CustomAttributeProvider
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Direct initialization constructor for simple and indexed
|
|
|
|
/// properties.
|
|
|
|
/// You must pass the Type object associated to the class that
|
|
|
|
/// declares the property, the Type object that describes the
|
|
|
|
/// type of the property's value, the property name and the
|
|
|
|
/// getter/setter methods. Either the setter or the getter can
|
|
|
|
/// be null, meaning a restricted access. If both are null, the
|
|
|
|
/// user is expected to add a custom accessor attribute to this
|
|
|
|
/// PropertyInfo object.
|
|
|
|
/// If the getter method has parameters, the property is considered
|
|
|
|
/// to be indexed. The same is true if the getter is null and the
|
|
|
|
/// setter has more than one parameter.
|
2005-04-29 19:19:58 +08:00
|
|
|
PropertyInfo(const Type& decltype, const Type& ptype, const std::string& name, const MethodInfo* getm, const MethodInfo* setm)
|
2005-04-04 21:50:07 +08:00
|
|
|
: CustomAttributeProvider(),
|
2005-04-29 19:19:58 +08:00
|
|
|
_decltype(decltype),
|
|
|
|
_ptype(ptype),
|
|
|
|
_name(name),
|
|
|
|
_getm(getm),
|
|
|
|
_setm(setm),
|
|
|
|
_numm(0),
|
|
|
|
_addm(0),
|
|
|
|
_remm(0),
|
|
|
|
_is_array(false)
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
if (_getm)
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
for (ParameterInfoList::size_type i=0; i<_getm->getParameters().size(); ++i)
|
|
|
|
_indices.push_back(_getm->getParameters().at(i));
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
if (_setm)
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
for (ParameterInfoList::size_type i=0; i<(_setm->getParameters().size()-1); ++i)
|
|
|
|
_indices.push_back(_setm->getParameters().at(i));
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Direct initialization constructor for "array" properties.
|
|
|
|
/// You must pass the Type object associated to the type that
|
|
|
|
/// declares the property, the Type object that describes the
|
|
|
|
/// type of the property's value, the property name and the
|
2005-04-04 21:50:07 +08:00
|
|
|
/// getter/setter/counter/adder/remover methods.
|
2005-04-29 19:19:58 +08:00
|
|
|
PropertyInfo(const Type& decltype, const Type& ptype, const std::string& name, const MethodInfo* getm, const MethodInfo* setm, const MethodInfo* numm, const MethodInfo* addm, const MethodInfo* remm)
|
2005-04-04 21:50:07 +08:00
|
|
|
: CustomAttributeProvider(),
|
2005-04-29 19:19:58 +08:00
|
|
|
_decltype(decltype),
|
|
|
|
_ptype(ptype),
|
|
|
|
_name(name),
|
|
|
|
_getm(getm),
|
|
|
|
_setm(setm),
|
|
|
|
_numm(numm),
|
|
|
|
_addm(addm),
|
|
|
|
_remm(remm),
|
|
|
|
_is_array(true)
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of indices
|
|
|
|
inline int getNumIndices() const
|
|
|
|
{
|
|
|
|
return static_cast<int>(getIndexParameters().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the name of the property being described.
|
2005-04-29 19:19:58 +08:00
|
|
|
inline virtual const std::string& getName() const
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _name;
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the type that declares the property.
|
2005-04-29 19:19:58 +08:00
|
|
|
inline virtual const Type& getDeclaringType() const
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _decltype;
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the type of the reflected property.
|
2005-04-29 19:19:58 +08:00
|
|
|
inline const Type& getPropertyType() const
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
|
|
|
const PropertyTypeAttribute *pta = getAttribute<PropertyTypeAttribute>(false);
|
|
|
|
if (pta) return pta->getPropertyType();
|
2005-04-29 19:19:58 +08:00
|
|
|
return _ptype;
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the getter method.
|
2005-04-29 19:19:58 +08:00
|
|
|
inline const MethodInfo* getGetMethod() const
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _getm;
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the setter method.
|
2005-04-29 19:19:58 +08:00
|
|
|
inline const MethodInfo* getSetMethod() const
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _setm;
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the counter method.
|
2005-04-29 19:19:58 +08:00
|
|
|
inline const MethodInfo* getCountMethod() const
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _numm;
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the adder method.
|
2005-04-29 19:19:58 +08:00
|
|
|
inline const MethodInfo* getAddMethod() const
|
2005-03-14 17:28:31 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _addm;
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
2005-04-04 21:50:07 +08:00
|
|
|
|
|
|
|
/// Returns the remover method.
|
2005-04-29 19:19:58 +08:00
|
|
|
inline const MethodInfo* getRemoveMethod() const
|
2005-04-04 21:50:07 +08:00
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _remm;
|
2005-04-04 21:50:07 +08:00
|
|
|
}
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Returns whether the property's value can be retrieved.
|
|
|
|
inline bool canGet() const
|
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return (_getm != 0) || isDefined<CustomPropertyGetAttribute>(false);
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the property's value can be set.
|
|
|
|
inline bool canSet() const
|
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _setm != 0 || isDefined<CustomPropertySetAttribute>(false);
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the property's array of values can be counted.
|
|
|
|
inline bool canCount() const
|
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _numm != 0 || isDefined<CustomPropertyCountAttribute>(false);
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether items can be added to the array property.
|
|
|
|
inline bool canAdd() const
|
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _addm != 0 || isDefined<CustomPropertyAddAttribute>(false);
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
2005-04-04 21:50:07 +08:00
|
|
|
|
|
|
|
/// Returns whether items can be removed from the array property.
|
|
|
|
inline bool canRemove() const
|
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _remm != 0 || isDefined<CustomPropertyRemoveAttribute>(false);
|
2005-04-04 21:50:07 +08:00
|
|
|
}
|
|
|
|
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Returns whether the property is simple.
|
|
|
|
inline bool isSimple() const
|
|
|
|
{
|
|
|
|
return !isIndexed() && !isArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the property is indexed.
|
|
|
|
inline bool isIndexed() const
|
|
|
|
{
|
|
|
|
return getNumIndices() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the property is an array.
|
|
|
|
inline bool isArray() const
|
|
|
|
{
|
2005-04-29 19:19:58 +08:00
|
|
|
return _is_array;
|
2005-03-14 17:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the list of index parameters.
|
|
|
|
/// If the property is not indexed, this list is empty. If neither
|
|
|
|
/// the get method nor the set method are defined, this list is
|
|
|
|
/// empty unless a custom indexing attribute is defined.
|
2005-04-29 19:19:58 +08:00
|
|
|
const ParameterInfoList& getIndexParameters() const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Returns a list of valid values that can be used for the specified
|
|
|
|
/// index. If a custom indexing attribute is defined for this property,
|
|
|
|
/// then it will be queried for the index set, otherwise the index
|
|
|
|
/// will be treated as an enumeration and the set of enumeration
|
|
|
|
/// values will be returned.
|
2005-04-29 19:19:58 +08:00
|
|
|
void getIndexValueSet(int whichindex, const Value& instance, ValueList& values) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
2005-04-04 21:50:07 +08:00
|
|
|
/// Invokes the getter method on the given const instance and
|
2005-03-14 17:28:31 +08:00
|
|
|
/// returns the property's value. If a custom getter attribute
|
|
|
|
/// is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
Value getValue(const Value& instance) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
2005-04-04 21:50:07 +08:00
|
|
|
/// Invokes the getter method on the given instance and
|
|
|
|
/// returns the property's value. If a custom getter attribute
|
|
|
|
/// is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
Value getValue(Value& instance) const;
|
2005-04-04 21:50:07 +08:00
|
|
|
|
2005-03-14 17:28:31 +08:00
|
|
|
/// Invokes the setter method on the given instance and
|
|
|
|
/// sets the property's value. If a custom setter attribute
|
|
|
|
/// is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
void setValue(Value& instance, const Value& value) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
2005-04-04 21:50:07 +08:00
|
|
|
/// Invokes the getter method on the given const instance passing a
|
|
|
|
/// list of indices and returns the indexed property's value. If a
|
|
|
|
/// custom getter attribute is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
Value getIndexedValue(Value& instance, ValueList& indices) const;
|
2005-04-04 21:50:07 +08:00
|
|
|
|
2005-03-14 17:28:31 +08:00
|
|
|
/// Invokes the getter method on the given instance passing a list
|
|
|
|
/// of indices and returns the indexed property's value. If a custom
|
|
|
|
/// getter attribute is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
Value getIndexedValue(const Value& instance, ValueList& indices) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Invokes the setter method on the given instance passing a list
|
|
|
|
/// of indices and sets the indexed property's value. If a custom
|
|
|
|
/// setter attribute is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
void setIndexedValue(Value& instance, ValueList& indices, const Value& value) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Invokes the counter method on the given instance and returns
|
|
|
|
/// the number of items of the array property. If a custom counter
|
|
|
|
/// attribute is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
int getNumArrayItems(const Value& instance) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
2005-04-04 21:50:07 +08:00
|
|
|
/// Invokes the getter method on the given const instance and returns
|
2005-03-14 17:28:31 +08:00
|
|
|
/// the i-th item of the array property. If a custom getter attribute
|
|
|
|
/// us defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
Value getArrayItem(const Value& instance, int i) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
2005-04-04 21:50:07 +08:00
|
|
|
/// Invokes the getter method on the given instance and returns
|
|
|
|
/// the i-th item of the array property. If a custom getter attribute
|
|
|
|
/// us defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
Value getArrayItem(Value& instance, int i) const;
|
2005-04-04 21:50:07 +08:00
|
|
|
|
2005-03-14 17:28:31 +08:00
|
|
|
/// Invokes the setter method on the given instance and sets
|
|
|
|
/// the i-th item of the array property. If a custom setter attribute
|
|
|
|
/// is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
void setArrayItem(Value& instance, int i, const Value& value) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Invokes the adder method on the given instance and adds
|
|
|
|
/// an item to the array property. If a custom adder attribute is
|
|
|
|
/// defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
void addArrayItem(Value& instance, const Value& value) const;
|
2005-04-04 21:50:07 +08:00
|
|
|
|
|
|
|
/// Invokes the remover method on the given instance and removes
|
|
|
|
/// an item from the array property. If a custom remover attribute
|
|
|
|
/// is defined, it will be invoked instead.
|
2005-04-29 19:19:58 +08:00
|
|
|
void removeArrayItem(Value& instance, int i) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Returns the default value associated to the reflected property.
|
|
|
|
/// If no default value has been specified, this method tries to
|
|
|
|
/// create an instance of the property type and then returns its
|
|
|
|
/// value. There are some attributes that change the behavior of
|
|
|
|
/// this method, for example NoDefaultValueAttribute.
|
|
|
|
Value getDefaultValue() const;
|
|
|
|
|
|
|
|
protected:
|
2005-04-29 19:19:58 +08:00
|
|
|
virtual void getInheritedProviders(CustomAttributeProviderList& providers) const;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
private:
|
2005-04-29 19:19:58 +08:00
|
|
|
const Type& _decltype;
|
|
|
|
const Type& _ptype;
|
|
|
|
std::string _name;
|
|
|
|
const MethodInfo* _getm;
|
|
|
|
const MethodInfo* _setm;
|
|
|
|
const MethodInfo* _numm;
|
|
|
|
const MethodInfo* _addm;
|
|
|
|
const MethodInfo* _remm;
|
|
|
|
ParameterInfoList _indices;
|
|
|
|
bool _is_array;
|
2005-03-14 17:28:31 +08:00
|
|
|
};
|
2004-12-09 13:28:20 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|