OpenSceneGraph/include/osgIntrospection/CustomAttributeProvider
Robert Osfield 3c2872a36a From Emmanuel Roche, "I'm joining two zip files to this mail for the modified sources and include files of osgIntrospection.
The modifications I made are very small but they are absolutely usefull to use osgIntrospection with visual studio 7.1 or 8 in debug modes.
This should also solve other minor common problems (converter memory leak, virtual destructor for PropertyInfo, etc...).

I choosed two function names : Reflection::uninitialize() and Type::reset(), this can of course be changed if someone has a better idea...

I made the changes against OSG 2.2.0 public release. I tested the result with VS 7.1, VS 7.1 SP1, VS 8.0 SP1 and AQTime 5.0 on Windows XP SP2... All 4 seem to agree : they detected memory leaks before and don't anymore.

Sorry I haven't take the time to test that on linux but the changes are so small I doubt there could be a problem... I let you check that on your side  :-).

I hope this will help making OSG an even more wonderfull library."
2008-02-25 16:26:30 +00:00

131 lines
4.8 KiB
C++

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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
#ifndef OSGINTROSPECTION_CUSTOMATTRIBUTEPROVIDER_
#define OSGINTROSPECTION_CUSTOMATTRIBUTEPROVIDER_
#include <osgIntrospection/Export>
#include <osgIntrospection/CustomAttribute>
#include <vector>
#include <typeinfo>
namespace osgIntrospection
{
// forward declarations
class Type;
class CustomAttribute;
class CustomAttributeProvider;
// vector of attributes
typedef std::vector<const CustomAttribute* > CustomAttributeList;
// 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
{
return attribs_;
}
/// Returns the list of custom attributes.
inline CustomAttributeList& getCustomAttributes()
{
return attribs_;
}
/// Adds a new attribute to the list.
inline CustomAttributeProvider *addAttribute(const CustomAttribute* attr)
{
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;
/// 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;
/// 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);
}
}
private:
CustomAttributeList attribs_;
};
}
#endif