Skip to content

Commit 3a8bbb4

Browse files
committed
List: Add allocatedSizePtr() method
1 parent 0ff30d0 commit 3a8bbb4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

include/scratchcpp/list.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
4646
*/
4747
inline size_t *sizePtr() { return &m_size; }
4848

49+
/*!
50+
* Returns a pointer to the allocated list size.
51+
* \note This is used internally by compiled code for various optimizations.
52+
*/
53+
inline const size_t *allocatedSizePtr() const { return m_dataPtr->sizePtr(); }
54+
4955
/*! Returns the list size. */
5056
inline size_t size() const { return m_size; }
5157

test/scratch_classes/list_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ TEST(ListTest, SizePtr)
6262
List list("", "test list");
6363
ASSERT_TRUE(list.sizePtr());
6464
ASSERT_EQ(*list.sizePtr(), 0);
65+
const size_t *ptr = list.sizePtr();
6566
ASSERT_TRUE(list.empty());
6667

6768
list.append("Lorem");
@@ -71,6 +72,7 @@ TEST(ListTest, SizePtr)
7172

7273
list.append("dolor");
7374
ASSERT_EQ(*list.sizePtr(), 3);
75+
ASSERT_EQ(list.sizePtr(), ptr);
7476
ASSERT_FALSE(list.empty());
7577

7678
list.removeAt(0);
@@ -82,6 +84,21 @@ TEST(ListTest, SizePtr)
8284
ASSERT_EQ(list.size(), 100);
8385
}
8486

87+
TEST(ListTest, AllocatedSizePtr)
88+
{
89+
List list("", "test list");
90+
ASSERT_TRUE(list.allocatedSizePtr());
91+
ASSERT_EQ(*list.allocatedSizePtr(), 0);
92+
const size_t *ptr = list.allocatedSizePtr();
93+
94+
list.append("Lorem");
95+
list.append("ipsum");
96+
97+
ASSERT_GT(*list.allocatedSizePtr(), 0);
98+
ASSERT_EQ(list.allocatedSizePtr(), ptr);
99+
ASSERT_NE(list.allocatedSizePtr(), list.sizePtr());
100+
}
101+
85102
TEST(ListTest, Size)
86103
{
87104
List list("", "test list");

0 commit comments

Comments
 (0)