Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Remove core::list and replace uses with std::list (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
paradust7 committed May 21, 2022
1 parent 3e81f38 commit 128cf16
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 694 deletions.
157 changes: 87 additions & 70 deletions include/IGUIElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
#define __I_GUI_ELEMENT_H_INCLUDED__

#include "IReferenceCounted.h"
#include "irrList.h"
#include "rect.h"
#include "irrString.h"
#include "IEventReceiver.h"
#include "EGUIElementTypes.h"
#include "EGUIAlignment.h"
#include "IAttributes.h"
#include "IGUIEnvironment.h"
#include <cassert>
#include <algorithm>
#include <list>
#include <vector>

namespace irr
{
Expand Down Expand Up @@ -50,12 +53,9 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
//! Destructor
virtual ~IGUIElement()
{
// delete all children
core::list<IGUIElement*>::Iterator it = Children.begin();
for (; it != Children.end(); ++it)
{
(*it)->Parent = 0;
(*it)->drop();
for (auto child : Children) {
child->Parent = nullptr;
child->drop();
}
}

Expand Down Expand Up @@ -239,10 +239,9 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
recalculateAbsolutePosition(false);

// update all children
core::list<IGUIElement*>::Iterator it = Children.begin();
for (; it != Children.end(); ++it)
for (auto child : Children)
{
(*it)->updateAbsolutePosition();
child->updateAbsolutePosition();
}
}

Expand All @@ -263,20 +262,19 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
{
IGUIElement* target = 0;

// we have to search from back to front, because later children
// might be drawn over the top of earlier ones.

core::list<IGUIElement*>::ConstIterator it = Children.getLast();

if (isVisible())
{
while(it != Children.end())
// we have to search from back to front, because later children
// might be drawn over the top of earlier ones.
auto it = Children.rbegin();
auto ie = Children.rend();
while (it != ie)
{
target = (*it)->getElementFromPoint(point);
if (target)
return target;

--it;
++it;
}
}

Expand Down Expand Up @@ -308,17 +306,19 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
//! Removes a child.
virtual void removeChild(IGUIElement* child)
{
core::list<IGUIElement*>::Iterator it = Children.begin();
for (; it != Children.end(); ++it)
if ((*it) == child)
{
(*it)->Parent = 0;
(*it)->drop();
Children.erase(it);
return;
}
assert(child->Parent == this);
Children.erase(child->ParentPos);
child->Parent = nullptr;
child->drop();
}

//! Removes all children.
virtual void removeAllChildren() {
while (!Children.empty()) {
auto child = Children.back();
child->remove();
}
}

//! Removes this element from its parent.
virtual void remove()
Expand All @@ -333,9 +333,8 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
{
if ( isVisible() )
{
core::list<IGUIElement*>::Iterator it = Children.begin();
for (; it != Children.end(); ++it)
(*it)->draw();
for (auto child : Children)
child->draw();
}
}

Expand All @@ -345,9 +344,8 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
{
if ( isVisible() )
{
core::list<IGUIElement*>::Iterator it = Children.begin();
for (; it != Children.end(); ++it)
(*it)->OnPostRender( timeMs );
for (auto child : Children)
child->OnPostRender( timeMs );
}
}

Expand Down Expand Up @@ -555,45 +553,33 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver

//! Brings a child to front
/** \return True if successful, false if not. */
virtual bool bringToFront(IGUIElement* element)
virtual bool bringToFront(IGUIElement* child)
{
core::list<IGUIElement*>::Iterator it = Children.begin();
for (; it != Children.end(); ++it)
{
if (element == (*it))
{
Children.erase(it);
Children.push_back(element);
return true;
}
}

return false;
if (child->Parent != this)
return false;
if (std::next(child->ParentPos) == Children.end()) // already there
return true;
Children.erase(child->ParentPos);
child->ParentPos = Children.insert(Children.end(), child);
return true;
}


//! Moves a child to the back, so it's siblings are drawn on top of it
/** \return True if successful, false if not. */
virtual bool sendToBack(IGUIElement* child)
{
core::list<IGUIElement*>::Iterator it = Children.begin();
if (child == (*it)) // already there
if (child->Parent != this)
return false;
if (child->ParentPos == Children.begin()) // already there
return true;
for (; it != Children.end(); ++it)
{
if (child == (*it))
{
Children.erase(it);
Children.push_front(child);
return true;
}
}

return false;
Children.erase(child->ParentPos);
child->ParentPos = Children.insert(Children.begin(), child);
return true;
}

//! Returns list with children of this element
virtual const core::list<IGUIElement*>& getChildren() const
virtual const std::list<IGUIElement*>& getChildren() const
{
return Children;
}
Expand All @@ -610,14 +596,13 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
{
IGUIElement* e = 0;

core::list<IGUIElement*>::ConstIterator it = Children.begin();
for (; it != Children.end(); ++it)
for (auto child : Children)
{
if ((*it)->getID() == id)
return (*it);
if (child->getID() == id)
return child;

if (searchchildren)
e = (*it)->getElementFromId(id, true);
e = child->getElementFromId(id, true);

if (e)
return e;
Expand Down Expand Up @@ -663,7 +648,7 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
if (wanted==-2)
wanted = 1073741824; // maximum s32

core::list<IGUIElement*>::ConstIterator it = Children.begin();
auto it = Children.begin();

s32 closestOrder, currentOrder;

Expand Down Expand Up @@ -806,10 +791,40 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
child->remove(); // remove from old parent
child->LastParentRect = getAbsolutePosition();
child->Parent = this;
Children.push_back(child);
child->ParentPos = Children.insert(Children.end(), child);
}
}

#ifndef NDEBUG
template<typename Iterator>
static size_t _fastSetChecksum(Iterator begin, Iterator end) {
std::hash<typename Iterator::value_type> hasher;
size_t checksum = 0;
for (Iterator it = begin; it != end; ++it) {
size_t h = hasher(*it);
checksum ^= 966073049 + (h * 3432918353) + ((h >> 16) * 461845907);
}
return checksum;
}
#endif

// Reorder children [from, to) to the order given by `neworder`
void reorderChildren(
std::list<IGUIElement*>::iterator from,
std::list<IGUIElement*>::iterator to,
const std::vector<IGUIElement*> &neworder)
{
assert(_fastSetChecksum(from, to) == _fastSetChecksum(neworder.begin(), neworder.end()));
for (auto e : neworder)
{
*from = e;
e->ParentPos = from;
++from;
}
assert(from == to);
}


// not virtual because needed in constructor
void recalculateAbsolutePosition(bool recursive)
{
Expand Down Expand Up @@ -931,22 +946,24 @@ class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
if ( recursive )
{
// update all children
core::list<IGUIElement*>::Iterator it = Children.begin();
for (; it != Children.end(); ++it)
for (auto child : Children)
{
(*it)->recalculateAbsolutePosition(recursive);
child->recalculateAbsolutePosition(recursive);
}
}
}

protected:

//! List of all children of this element
core::list<IGUIElement*> Children;
std::list<IGUIElement*> Children;

//! Pointer to the parent
IGUIElement* Parent;

//! Our position in the parent list. Only valid when Parent != nullptr
std::list<IGUIElement*>::iterator ParentPos;

//! relative rect of element
core::rect<s32> RelativeRect;

Expand Down
20 changes: 10 additions & 10 deletions include/ISceneNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "irrString.h"
#include "aabbox3d.h"
#include "matrix4.h"
#include "irrList.h"
#include "IAttributes.h"
#include <list>

namespace irr
{
Expand All @@ -24,7 +24,7 @@ namespace scene
class ISceneManager;

//! Typedef for list of scene nodes
typedef core::list<ISceneNode*> ISceneNodeList;
typedef std::list<ISceneNode*> ISceneNodeList;

//! Scene node interface.
/** A scene node is a node in the hierarchical scene graph. Every scene
Expand Down Expand Up @@ -81,7 +81,7 @@ namespace scene
{
if (IsVisible)
{
ISceneNodeList::Iterator it = Children.begin();
ISceneNodeList::iterator it = Children.begin();
for (; it != Children.end(); ++it)
(*it)->OnRegisterSceneNode();
}
Expand All @@ -103,7 +103,7 @@ namespace scene

// perform the post render process on all children

ISceneNodeList::Iterator it = Children.begin();
ISceneNodeList::iterator it = Children.begin();
for (; it != Children.end(); ++it)
(*it)->OnAnimate(timeMs);
}
Expand Down Expand Up @@ -289,7 +289,7 @@ namespace scene
e.g. because it couldn't be found in the children list. */
virtual bool removeChild(ISceneNode* child)
{
ISceneNodeList::Iterator it = Children.begin();
ISceneNodeList::iterator it = Children.begin();
for (; it != Children.end(); ++it)
if ((*it) == child)
{
Expand All @@ -309,7 +309,7 @@ namespace scene
*/
virtual void removeAll()
{
ISceneNodeList::Iterator it = Children.begin();
ISceneNodeList::iterator it = Children.begin();
for (; it != Children.end(); ++it)
{
(*it)->Parent = 0;
Expand Down Expand Up @@ -519,7 +519,7 @@ namespace scene

//! Returns a const reference to the list of all children.
/** \return The list of all children of this node. */
const core::list<ISceneNode*>& getChildren() const
const std::list<ISceneNode*>& getChildren() const
{
return Children;
}
Expand Down Expand Up @@ -611,7 +611,7 @@ namespace scene

// clone children

ISceneNodeList::Iterator it = toCopyFrom->Children.begin();
ISceneNodeList::iterator it = toCopyFrom->Children.begin();
for (; it != toCopyFrom->Children.end(); ++it)
(*it)->clone(this, newManager);
}
Expand All @@ -622,7 +622,7 @@ namespace scene
{
SceneManager = newManager;

ISceneNodeList::Iterator it = Children.begin();
ISceneNodeList::iterator it = Children.begin();
for (; it != Children.end(); ++it)
(*it)->setSceneManager(newManager);
}
Expand All @@ -646,7 +646,7 @@ namespace scene
ISceneNode* Parent;

//! List of all children of this node
core::list<ISceneNode*> Children;
std::list<ISceneNode*> Children;

//! Pointer to the scene manager
ISceneManager* SceneManager;
Expand Down
Loading

0 comments on commit 128cf16

Please sign in to comment.