OpenSceneGraph/include/osgIntrospection/CustomAttributeProvider

131 lines
4.8 KiB
Plaintext
Raw Normal View History

2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2005-04-29 18:06:50 +08:00
*
* 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
#ifndef OSGINTROSPECTION_CUSTOMATTRIBUTEPROVIDER_
#define OSGINTROSPECTION_CUSTOMATTRIBUTEPROVIDER_
#include <osgIntrospection/Export>
#include <osgIntrospection/CustomAttribute>
#include <vector>
#include <typeinfo>
namespace osgIntrospection
{
2005-03-14 17:28:31 +08:00
// forward declarations
class Type;
class CustomAttribute;
class CustomAttributeProvider;
// vector of attributes
typedef std::vector<const CustomAttribute* > CustomAttributeList;
2005-03-14 17:28:31 +08:00
// vector of attribute providers
typedef std::vector<const CustomAttributeProvider *> CustomAttributeProviderList;
/// This is the base class for custom attribute providers, that is objects
/// that can be assigned a list of custom attributes. Methods defined in
/// this class provide the means for adding, retrieving and searching for
/// attributes.
class OSGINTROSPECTION_EXPORT CustomAttributeProvider
{
public:
/// Returns the const list of custom attributes.
inline const CustomAttributeList& getCustomAttributes() const
2005-03-14 17:28:31 +08:00
{
return attribs_;
}
/// Returns the list of custom attributes.
inline CustomAttributeList& getCustomAttributes()
2005-03-14 17:28:31 +08:00
{
return attribs_;
}
/// Adds a new attribute to the list.
inline CustomAttributeProvider *addAttribute(const CustomAttribute* attr)
2005-03-14 17:28:31 +08:00
{
attribs_.push_back(attr);
return this;
}
/// Returns whether at least one attribute of the given type is
/// present in the attribute list. If the inherit parameter is
/// set to true, the search is forwarded to base types.
bool isDefined(const Type& type, bool inherit) const;
2005-03-14 17:28:31 +08:00
/// Returns whether at least one attribute of the given type is
/// present in the attribute list. If the inherit parameter is
/// set to true, the search is forwarded to base types.
/// [template version]
template<typename T> inline bool isDefined(bool inherit) const
{
for (CustomAttributeList::const_iterator i=attribs_.begin(); i!=attribs_.end(); ++i)
if (typeid(**i) == typeid(T)) return true;
if (inherit)
{
CustomAttributeProviderList providers;
getInheritedProviders(providers);
for (CustomAttributeProviderList::const_iterator i=providers.begin(); i!=providers.end(); ++i)
{
if ((*i)->isDefined<T>(true)) return true;
}
}
return false;
}
/// Searchs for an attribute of the given type and returns a pointer
/// to it if found, a null pointer otherwise. If the inherit parameter
/// is set to true, the search is forwarded to base types.
const CustomAttribute* getAttribute(const Type& type, bool inherit) const;
2005-03-14 17:28:31 +08:00
/// Searchs for an attribute of the given type and returns a pointer
/// to it if found, a null pointer otherwise. If the inherit parameter
/// is set to true, the search is forwarded to base types.
/// [template version]
template<typename T> inline const T *getAttribute(bool inherit) const
{
for (CustomAttributeList::const_iterator i=attribs_.begin(); i!=attribs_.end(); ++i)
if (typeid(**i) == typeid(T)) return static_cast<const T *>(*i);
if (inherit)
{
CustomAttributeProviderList providers;
getInheritedProviders(providers);
for (CustomAttributeProviderList::const_iterator i=providers.begin(); i!=providers.end(); ++i)
{
const T *ca = (*i)->getAttribute<T>(true);
if (ca) return ca;
}
}
return 0;
}
protected:
virtual void getInheritedProviders(CustomAttributeProviderList& providers) const = 0;
virtual ~CustomAttributeProvider() {
for(CustomAttributeList::iterator it = attribs_.begin(); it != attribs_.end(); ++it) {
delete (*it);
}
}
2005-03-14 17:28:31 +08:00
private:
CustomAttributeList attribs_;
};
}
#endif