Skip to content

Commit 6213b3c

Browse files
authored
Merge branch 'master' into quat_buildrotmat
2 parents 431491c + 668725f commit 6213b3c

7 files changed

Lines changed: 88 additions & 5 deletions

File tree

Sofa/framework/Core/src/sofa/core/objectmodel/Data.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,27 @@ class WriteAccessor< core::objectmodel::Data<T> > : public WriteAccessor<T>
409409

410410
protected:
411411
data_container_type& data;
412+
bool m_moved = false;
412413

413414
/// @internal used by WriteOnlyAccessor
414415
WriteAccessor( container_type* c, data_container_type& d) : Inherit(*c), data(d) {}
415416

416417
public:
418+
WriteAccessor(WriteAccessor&& other) noexcept : Inherit(std::move(other)), data(other.data)
419+
{
420+
other.m_moved = true;
421+
}
422+
417423
WriteAccessor(data_container_type& d) : Inherit(*d.beginEdit()), data(d) {}
418424
WriteAccessor(data_container_type* d) : Inherit(*d->beginEdit()), data(*d) {}
419-
~WriteAccessor() { data.endEdit(); }
425+
426+
~WriteAccessor()
427+
{
428+
if (!m_moved)
429+
{
430+
data.endEdit();
431+
}
432+
}
420433
};
421434

422435

Sofa/framework/Core/test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 3.22)
33
project(Sofa.Core_test)
44

55
set(SOURCE_FILES
6-
accessor/ReadAccessor.cpp
7-
accessor/WriteAccessor.cpp
6+
accessor/ReadAccessor_test.cpp
7+
accessor/WriteAccessor_test.cpp
88
collision/NarrowPhaseDetection_test.cpp
99
loader/MeshLoader_test.cpp
1010
objectmodel/AspectPool_test.cpp
File renamed without changes.

Sofa/framework/Core/test/accessor/WriteAccessor.cpp renamed to Sofa/framework/Core/test/accessor/WriteAccessor_test.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <gtest/gtest.h>
2323

2424
#include <sofa/core/objectmodel/Data.h>
25+
#include <sofa/core/objectmodel/DataCallback.h>
2526
#include <sofa/type/vector.h>
2627

2728
namespace sofa
@@ -79,5 +80,74 @@ TEST(WriteAccessor, VectorTypes)
7980
EXPECT_FLOAT_EQ(vector.getValue()[3], 6.f);
8081
}
8182

83+
TEST(WriteAccessor, MoveConstructor)
84+
{
85+
Data<float> floatValue { 12.f };
86+
int initialCounter = floatValue.getCounter();
87+
88+
{
89+
sofa::helper::WriteAccessor floatAccessor(floatValue);
90+
EXPECT_EQ(floatValue.getCounter(), initialCounter + 1);
91+
EXPECT_FLOAT_EQ(floatAccessor.ref(), 12.f);
92+
93+
sofa::helper::WriteAccessor floatAccessorMoved(std::move(floatAccessor));
94+
EXPECT_EQ(floatValue.getCounter(), initialCounter + 1);
95+
EXPECT_FLOAT_EQ(floatAccessorMoved.ref(), 12.f);
96+
97+
// Even if we moved from it, we can still call ref() and wref()
98+
// but it is not recommended as it doesn't hold the responsibility of endEdit anymore.
99+
EXPECT_FLOAT_EQ(floatAccessor.ref(), 12.f);
100+
101+
floatAccessorMoved.wref() = 14.f;
102+
EXPECT_FLOAT_EQ(floatValue.getValue(), 14.f);
103+
}
104+
105+
Data<float> data { 1.f };
106+
int endEditCount = 0;
107+
sofa::core::objectmodel::DataCallback cb;
108+
cb.addInput(&data);
109+
cb.addCallback([&endEditCount](){
110+
endEditCount++;
111+
});
112+
113+
{
114+
sofa::helper::WriteAccessor acc1(data);
115+
EXPECT_EQ(endEditCount, 0); // beginEdit doesn't trigger callback
116+
{
117+
sofa::helper::WriteAccessor acc2(std::move(acc1));
118+
EXPECT_EQ(endEditCount, 0);
119+
}
120+
// acc2 out of scope, endEdit called
121+
EXPECT_EQ(endEditCount, 1);
122+
}
123+
// acc1 out of scope, if move constructor worked correctly, endEdit should NOT be called again.
124+
EXPECT_EQ(endEditCount, 1);
125+
}
126+
127+
TEST(WriteAccessor, MoveConstructorVector)
128+
{
129+
Data<sofa::type::vector<float>> vector { sofa::type::vector<float> { 0.f, 1.f, 2.f, 3.f, 4.f} };
130+
int endEditCount = 0;
131+
sofa::core::objectmodel::DataCallback cb;
132+
cb.addInput(&vector);
133+
cb.addCallback([&endEditCount](){
134+
endEditCount++;
135+
});
136+
137+
{
138+
sofa::helper::WriteAccessor acc1(vector);
139+
EXPECT_EQ(acc1.size(), 5);
140+
{
141+
sofa::helper::WriteAccessor acc2(std::move(acc1));
142+
EXPECT_EQ(acc2.size(), 5);
143+
EXPECT_EQ(acc1.size(), 5); // Still valid but no longer responsible for endEdit
144+
acc2[0] = 10.f;
145+
}
146+
EXPECT_EQ(endEditCount, 1);
147+
}
148+
EXPECT_EQ(endEditCount, 1);
149+
EXPECT_FLOAT_EQ(vector.getValue()[0], 10.f);
150+
}
151+
82152

83153
}

Sofa/framework/Helper/test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ set(SOURCE_FILES
1313
StringUtils_test.cpp
1414
TagFactory_test.cpp
1515
Utils_test.cpp
16-
accessor/ReadAccessor.cpp
17-
accessor/WriteAccessor.cpp
16+
accessor/ReadAccessor_test.cpp
17+
accessor/WriteAccessor_test.cpp
1818
io/MeshOBJ_test.cpp
1919
io/STBImage_test.cpp
2020
io/XspLoader_test.cpp

Sofa/framework/Helper/test/accessor/ReadAccessor.cpp renamed to Sofa/framework/Helper/test/accessor/ReadAccessor_test.cpp

File renamed without changes.

Sofa/framework/Helper/test/accessor/WriteAccessor.cpp renamed to Sofa/framework/Helper/test/accessor/WriteAccessor_test.cpp

File renamed without changes.

0 commit comments

Comments
 (0)