Commit 41b1dd76 authored by Jan Möbius's avatar Jan Möbius

Added an BaseObjectIterator

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@5047 383ad7c9-94d9-4d36-a494-682f7c89f535
parent ecb87abf
......@@ -379,6 +379,85 @@ class DLLEXPORT ObjectIterator {
};
/** \brief Core Data Iterator used to iterate over all objects (Including groups)
*
* This is the core iterator for the whole framework. You should use this iterator to access your data.\n
* You can Choose if the iterator returns only Target, Source or all objects.\n
* Additionally you can set the type of objects returned by the iterator.
* The ObjectIterator will only return real Objects. Groups are not considered to be objects
*/
class DLLEXPORT BaseObjectIterator {
public :
/// type of the Objects the iterator works on
typedef BaseObject value_type;
/// handle type (just an int)
typedef BaseObject* value_handle;
/// reference type
typedef value_type& reference;
/// basic pointer type
typedef value_type* pointer;
/** \brief Use this constructor for iterating through your data.
*
* @param _restriction Use this parameter to define which objects will be returned.\n
* You can select between ALL_OBJECTS , TARGET_OBJECTS , SOURCE_OBJECTS.
* @param _dataType Use this parameter to select the returned object types.
* You can use DATA_ALL DATA_POLY_MESH DATA_TRIANGLE_MESH DATA_VOLUME
*/
BaseObjectIterator(IteratorRestriction _restriction = ALL_OBJECTS , DataType _dataType = DATA_ALL );
/// additional constructor starting at a given position
BaseObjectIterator(BaseObject* pos, IteratorRestriction _restriction = ALL_OBJECTS , DataType _dataType = DATA_ALL );
/// return the current position of the iterator
operator value_handle() { return pos_; };
/// compare iterators
bool operator==( const BaseObjectIterator& _rhs);
/// compare iterators
bool operator!=( const BaseObjectIterator& _rhs);
/// assign iterators
BaseObjectIterator& operator=( const BaseObjectIterator& _rhs);
/// dereference
pointer operator->();
/// next element
BaseObjectIterator& operator++();
/// last element
BaseObjectIterator& operator--();
/// dereference the iterator
BaseObject* operator*();
/// return current position of the iterator
BaseObject* index() { return pos_; };
private :
/// current position of the iterator
BaseObject* pos_;
/// returned data types of the iterator
DataType dataType_;
/// Restriction of the iterator
IteratorRestriction restriction_;
/** Takes an object and goes through the object tree to the next BaseObjectData
* It stops at the root node.
*/
inline void proceedToNextBaseObject(BaseObject*& _object);
};
// /// Return Iterator to Mesh End
// MeshIterator meshes_end();
......@@ -386,6 +465,10 @@ class DLLEXPORT ObjectIterator {
DLLEXPORT
ObjectIterator objectsEnd();
/// Return Iterator to Object End
DLLEXPORT
ObjectIterator baseObjectsEnd();
/** @} */
//=======================================
......
//=============================================================================
//
// OpenFlipper
// Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
// www.openflipper.org
//
//-----------------------------------------------------------------------------
//
// License
//
// OpenFlipper is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenFlipper 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
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenFlipper. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
// $Revision: 3800 $
// $Author: moebius $
// $Date: 2008-11-26 10:10:45 +0100 (Mi, 26. Nov 2008) $
//
//=============================================================================
//=============================================================================
//
// Plugin Functions
//
//=============================================================================
#include <OpenFlipper/common/Types.hh>
#include "PluginFunctions.hh"
namespace PluginFunctions {
BaseObjectIterator::BaseObjectIterator( IteratorRestriction _restriction , DataType _dataType) {
// Initialize with invalid pos
pos_ = 0;
// Store the restriction for the operator ( Source/Target )
restriction_ = _restriction;
// Store the requested DataType
dataType_ = _dataType;
// Start at the root Node
BaseObject* currentPos = objectRoot();
// Take the first element which is an BaseObject
proceedToNextBaseObject(currentPos);
while ( (currentPos != objectRoot()) ) {
// Return only target objects if requested
if ( (restriction_ == TARGET_OBJECTS) && (! currentPos->target() ) ) {
proceedToNextBaseObject(currentPos);
continue;
}
// Return only source objects if requested
if ( (restriction_ == SOURCE_OBJECTS) && (! currentPos->source() ) ) {
proceedToNextBaseObject(currentPos);
continue;
}
// Return only the right dataType
if ( _dataType != DATA_ALL )
if ( ! (currentPos->dataType( dataType_ ) ) ) {
proceedToNextBaseObject(currentPos);
continue;
}
// found a valid object
pos_ = dynamic_cast<BaseObject* > (currentPos);
break;
}
}
BaseObjectIterator::BaseObjectIterator(BaseObject* pos, IteratorRestriction _restriction , DataType _data)
{
restriction_ = _restriction;
pos_ = pos;
dataType_ = _data;
};
bool BaseObjectIterator::operator==( const BaseObjectIterator& _rhs) {
return ( _rhs.pos_ == pos_ );
}
bool BaseObjectIterator::operator!=( const BaseObjectIterator& _rhs) {
return ( _rhs.pos_ != pos_ );
}
BaseObjectIterator& BaseObjectIterator::operator=( const BaseObjectIterator& _rhs) {
pos_ = _rhs.pos_;
return *this;
}
BaseObjectIterator::pointer BaseObjectIterator::operator->(){
return pos_;
}
BaseObjectIterator& BaseObjectIterator::operator++() {
// Convert our pointer to the basic one
BaseObject* currentPos = dynamic_cast< BaseObject* >(pos_);
// Get the next objectData element in the tree
proceedToNextBaseObject(currentPos);
while ( (currentPos != objectRoot() ) ) {
// Return only target objects if requested
if ( (restriction_ == TARGET_OBJECTS) && (! currentPos->target() ) ) {
proceedToNextBaseObject(currentPos);
continue;
}
// Return only source objects if requested
if ( (restriction_ == SOURCE_OBJECTS) && (! currentPos->source() ) ) {
proceedToNextBaseObject(currentPos);
continue;
}
// Return only the right dataType
if ( ! (currentPos->dataType( dataType_ ) ) ) {
proceedToNextBaseObject(currentPos);
continue;
}
// found a valid object
pos_ = dynamic_cast<BaseObject* > (currentPos);
return *this;
}
// No valid object found
pos_ = 0;
return *this;
}
BaseObjectIterator& BaseObjectIterator::operator--() {
std::cerr << "TODO :--" << std::endl;
return *this;
}
/** This operator returns a pointer to the current object the iterator
* points to.
*
* @return Pointer to the current ObjectData
*/
BaseObject* BaseObjectIterator::operator*() {
return pos_;
}
/// Return Iterator to Object End
BaseObjectIterator BaseObjectsEnd() {
return BaseObjectIterator(0);
}
void BaseObjectIterator::proceedToNextBaseObject(BaseObject*& _object) {
_object = _object->next();
// Go through the tree and stop at the root node or if we found a BaseObject Object
while ( (_object != objectRoot()) && !dynamic_cast<BaseObject* > (_object) )
_object = _object->next();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment