Browse Source

Vector: add ENDPOS const; remove hack from Erase() (#2958)

1vanK 3 years ago
parent
commit
0487b5547b

+ 3 - 0
Source/Urho3D/AngelScript/Generated_GlobalVariables.cpp

@@ -103,6 +103,9 @@ void ASRegisterGeneratedGlobalVariables(asIScriptEngine* engine)
     // const u32 ELEMENT_TYPESIZES[] | File: ../GraphicsAPI/GraphicsDefs.h
     // Not registered because array
 
+    // constexpr i32 ENDPOS | File: ../Container/Vector.h
+    engine->RegisterGlobalProperty("const int ENDPOS", (void*)&ENDPOS);
+
     // static const String EP_AUTOLOAD_PATHS | File: ../Engine/EngineDefs.h
     engine->RegisterGlobalProperty("const String EP_AUTOLOAD_PATHS", (void*)&EP_AUTOLOAD_PATHS);
 

+ 2 - 2
Source/Urho3D/AngelScript/Generated_Members.h

@@ -15423,8 +15423,8 @@ template <class T> void RegisterMembers_Node(asIScriptEngine* engine, const char
     // void Node::SetOwner(Connection* owner)
     // Not registered because have @manualbind mark
 
-    // void Node::AddChild(Node* node, i32 index = NINDEX)
-    engine->RegisterObjectMethod(className, "void AddChild(Node@+, int = NINDEX)", AS_METHODPR(T, AddChild, (Node*, i32), void), AS_CALL_THISCALL);
+    // void Node::AddChild(Node* node, i32 index = ENDPOS)
+    engine->RegisterObjectMethod(className, "void AddChild(Node@+, int = ENDPOS)", AS_METHODPR(T, AddChild, (Node*, i32), void), AS_CALL_THISCALL);
 
     // void Node::AddComponent(Component* component, unsigned id, CreateMode mode)
     engine->RegisterObjectMethod(className, "void AddComponent(Component@+, uint, CreateMode)", AS_METHODPR(T, AddComponent, (Component*, unsigned, CreateMode), void), AS_CALL_THISCALL);

+ 15 - 16
Source/Urho3D/Container/Vector.h

@@ -24,6 +24,9 @@ namespace Urho3D
 /// No index.
 inline constexpr i32 NINDEX = -1;
 
+/// End position.
+inline constexpr i32 ENDPOS = -1;
+
 /// %Vector template class.
 template <class T> class Vector : public VectorBase
 {
@@ -57,7 +60,7 @@ public:
     /// Construct with initial data.
     Vector(const T* data, i32 size)
     {
-        assert(size > 0);
+        assert((size >= 0 && data) || (!size && !data));
 
         if constexpr (std::is_trivial<T>::value && std::is_standard_layout<T>::value)
         {
@@ -320,21 +323,21 @@ public:
             Resize(size_ - 1);
     }
 
-    /// Insert an element at position. If pos is NINDEX, append the new value at the end.
+    /// Insert an element at position. If pos is ENDPOS, append the new value at the end.
     void Insert(i32 pos, const T& value)
     {
-        assert((pos >= 0 && pos <= size_) || pos == NINDEX);
+        assert((pos >= 0 && pos <= size_) || pos == ENDPOS);
         DoInsertElements(pos, &value, &value + 1, CopyTag{});
     }
 
-    /// Insert an element at position. If pos is NINDEX, append the new value at the end.
+    /// Insert an element at position. If pos is ENDPOS, append the new value at the end.
     void Insert(i32 pos, T&& value)
     {
-        assert((pos >= 0 && pos <= size_) || pos == NINDEX);
+        assert((pos >= 0 && pos <= size_) || pos == ENDPOS);
 
         if constexpr (std::is_trivial<T>::value && std::is_standard_layout<T>::value)
         {
-            if (pos == NINDEX)
+            if (pos == ENDPOS)
                 pos = size_;
 
             i32 oldSize = size_;
@@ -348,14 +351,14 @@ public:
         }
     }
 
-    /// Insert another vector at position. If pos is NINDEX, append the new elements at the end.
+    /// Insert another vector at position. If pos is ENDPOS, append the new elements at the end.
     void Insert(i32 pos, const Vector<T>& vector)
     {
-        assert((pos >= 0 && pos <= size_) || pos == NINDEX);
+        assert((pos >= 0 && pos <= size_) || pos == ENDPOS);
 
         if constexpr (std::is_trivial<T>::value && std::is_standard_layout<T>::value)
         {
-            if (pos == NINDEX)
+            if (pos == ENDPOS)
                 pos = size_;
 
             i32 oldSize = size_;
@@ -465,10 +468,6 @@ public:
     /// Erase a range of elements.
     void Erase(i32 pos, i32 length = 1)
     {
-        // TODO: Remove this
-        if (pos + length > size_)
-            return;
-
         assert(pos >= 0 && length >= 0 && pos + length <= size_);
 
         if (!length)
@@ -846,13 +845,13 @@ private:
     }
 
     /// Insert elements into the vector using copy or move constructor.
-    /// If pos is NINDEX, append the new elements at the end.
+    /// If pos is ENDPOS, append the new elements at the end.
     template <class Tag, class RandomIteratorT>
     Iterator DoInsertElements(i32 pos, RandomIteratorT start, RandomIteratorT end, Tag)
     {
-        assert((pos >= 0 && pos <= size_) || pos == NINDEX);
+        assert((pos >= 0 && pos <= size_) || pos == ENDPOS);
 
-        if (pos == NINDEX)
+        if (pos == ENDPOS)
             pos = size_;
 
         const i32 numElements = (i32)(end - start);

+ 1 - 1
Source/Urho3D/Scene/Node.cpp

@@ -786,7 +786,7 @@ Node* Node::CreateTemporaryChild(const String& name, CreateMode mode, unsigned i
 
 void Node::AddChild(Node* node, i32 index)
 {
-    assert((index >= 0 && index <= children_.Size()) || index == NINDEX);
+    assert((index >= 0 && index <= children_.Size()) || index == ENDPOS);
 
     // Check for illegal or redundant parent assignment
     if (!node || node == this || node->parent_ == this)

+ 2 - 2
Source/Urho3D/Scene/Node.h

@@ -291,8 +291,8 @@ public:
     /// Create a temporary child scene node (with specified ID if provided).
     Node* CreateTemporaryChild(const String& name = String::EMPTY, CreateMode mode = REPLICATED, unsigned id = 0);
 
-    /// Add a child scene node at a specific index. If index is not explicitly specified or is NINDEX, append the new child at the end.
-    void AddChild(Node* node, i32 index = NINDEX);
+    /// Add a child scene node at a specific index. If index is not explicitly specified or is ENDPOS, append the new child at the end.
+    void AddChild(Node* node, i32 index = ENDPOS);
 
     /// Remove a child scene node.
     void RemoveChild(Node* node);

+ 2 - 1
Source/Urho3D/UI/ListView.cpp

@@ -421,7 +421,8 @@ void ListView::RemoveItem(UIElement* item, unsigned index)
                         if (childItem->GetIndent() > baseIndent)
                         {
                             childItem->SetSelected(false);
-                            selections_.Erase(j);
+                            if (j < selections_.Size()) // TODO: Rework?
+                                selections_.Erase(j);
                             contentElement_->RemoveChildAtIndex(i + 1);
                             overlayContainer_->RemoveChildAtIndex(i + 1);
                             ++removed;