Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into topology_changes_fix
- Loading branch information
Showing
21 changed files
with
545 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
SofaKernel/framework/framework_test/core/objectmodel/DataCallback_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
/****************************************************************************** | ||
* SOFA, Simulation Open-Framework Architecture, development version * | ||
* (c) 2006-2019 INRIA, USTL, UJF, CNRS, MGH * | ||
* * | ||
* This program 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 2.1 of the License, or (at * | ||
* your option) any later version. * | ||
* * | ||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. * | ||
******************************************************************************* | ||
* Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
* * | ||
* Contact information: contact@sofa-framework.org * | ||
******************************************************************************/ | ||
#include <sofa/core/objectmodel/DataCallback.h> | ||
using sofa::core::objectmodel::DataCallback; | ||
|
||
#include <sofa/core/objectmodel/BaseObject.h> | ||
|
||
#include <sofa/helper/testing/BaseTest.h> | ||
using sofa::helper::testing::BaseTest ; | ||
|
||
|
||
namespace sofa { | ||
|
||
|
||
/// Test suite for data callbacks | ||
struct DataCallback_test: public BaseTest | ||
{ | ||
class TestObject : public sofa::core::objectmodel::BaseObject | ||
{ | ||
public: | ||
/// data attached to an object | ||
Data<int> d_objdata1; | ||
Data<int> d_objdata2; | ||
DataCallback m_datacallback1; | ||
DataCallback m_datacallback2; | ||
DataCallback m_datacallbackAll; | ||
|
||
void printData1() | ||
{ | ||
msg_info("DataCallback_test") << "TestObject : Value of objdata1 changed : " | ||
<< this->d_objdata1.getValue(); | ||
msg_warning("DataCallback_test") << "TestObject : Value of objdata2 did not changed : " | ||
<< this->d_objdata2.getValue(); | ||
} | ||
void printData2() | ||
{ | ||
msg_advice("DataCallback_test") << "TestObject : Value of objdata2 changed : " | ||
<< this->d_objdata2.getValue(); | ||
msg_error("DataCallback_test") << "TestObject : Value of objdata1 did not changed : " | ||
<< this->d_objdata1.getValue(); | ||
} | ||
void printDataAll() | ||
{ | ||
msg_fatal("DataCallback_test") << "TestObject : Value of objdata1 or objdata2 changed : " | ||
<< this->d_objdata1.getValue() << " | " | ||
<< this->d_objdata2.getValue(); | ||
} | ||
|
||
TestObject() | ||
: sofa::core::objectmodel::BaseObject() | ||
, d_objdata1(initData(&d_objdata1, 0, "objdata1", "objdata1")) | ||
, d_objdata2(initData(&d_objdata2, 1, "objdata2", "objdata2")) | ||
, m_datacallback1() | ||
, m_datacallback2() | ||
, m_datacallbackAll( ) | ||
{ | ||
m_datacallback1.addInput(&d_objdata1); | ||
m_datacallback2.addInput(&d_objdata2); | ||
m_datacallbackAll.addInputs({&d_objdata1, &d_objdata2}); | ||
} | ||
}; | ||
}; | ||
|
||
|
||
TEST_F(DataCallback_test, testDataCallbackWithBind_1) | ||
{ | ||
TestObject obj; | ||
obj.m_datacallback1.addCallback(std::bind(&TestObject::printData1, &obj)); | ||
|
||
EXPECT_EQ( obj.d_objdata1.getValue(), 0 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
|
||
//callback is expected to print an info and a warning message | ||
EXPECT_MSG_EMIT(Info) ; | ||
EXPECT_MSG_EMIT(Warning) ; | ||
obj.d_objdata1.setValue(123); | ||
EXPECT_EQ( obj.d_objdata1.getValue(), 123 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
} | ||
|
||
TEST_F(DataCallback_test, testDataCallbackWithOldSyntax_1) | ||
{ | ||
TestObject obj; | ||
obj.m_datacallback1.addCallback([&obj](){obj.printData1();}); | ||
|
||
EXPECT_EQ( obj.d_objdata1.getValue(), 0 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
|
||
//callback is expected to print an info and a warning message | ||
EXPECT_MSG_EMIT(Info) ; | ||
EXPECT_MSG_EMIT(Warning) ; | ||
obj.d_objdata1.setValue(123); | ||
EXPECT_EQ( obj.d_objdata1.getValue(), 123 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
} | ||
|
||
TEST_F(DataCallback_test, testDataCallback_1) | ||
{ | ||
TestObject obj; | ||
obj.m_datacallback1.addCallback([&obj](){obj.printData1();}); | ||
|
||
EXPECT_EQ( obj.d_objdata1.getValue(), 0 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
|
||
/// callback is expected to print an info and a warning message | ||
EXPECT_MSG_EMIT(Info) ; | ||
EXPECT_MSG_EMIT(Warning) ; | ||
obj.d_objdata1.setValue(123); | ||
EXPECT_EQ( obj.d_objdata1.getValue(), 123 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
} | ||
|
||
TEST_F(DataCallback_test, testDataCallback_2) | ||
{ | ||
TestObject obj; | ||
obj.m_datacallback2.addCallback([&obj](){obj.printData2();}); | ||
|
||
EXPECT_EQ( obj.d_objdata1.getValue(), 0 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
|
||
//callback is expected to print an advice and an error message | ||
EXPECT_MSG_EMIT(Advice) ; | ||
EXPECT_MSG_EMIT(Error) ; | ||
obj.d_objdata2.setValue(456); | ||
EXPECT_EQ( obj.d_objdata1.getValue(), 0 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 456 ) ; | ||
} | ||
|
||
/// In this test we show how we can use the Data &DataCallback without | ||
/// any BaseObject. | ||
TEST_F(DataCallback_test, testDataCallbackExample_1) | ||
{ | ||
Data<int> a; | ||
Data<int> b; | ||
DataCallback cb; | ||
cb.addInputs({&a,&b}); | ||
|
||
std::vector<int> results; | ||
|
||
cb.addCallback([&a,&b, &results](){ | ||
msg_info("Example1") << a.getValue() << "+" << b.getValue() << "=" << a.getValue() + b.getValue(); | ||
results.push_back(a.getValue() + b.getValue()); | ||
}); | ||
cb.addCallback([&a,&b, &results](){ | ||
msg_info("Example1") << a.getValue() << "*" << b.getValue() << "=" << a.getValue() * b.getValue(); | ||
results.push_back(a.getValue() * b.getValue()); | ||
}); | ||
|
||
a.setValue(5); | ||
EXPECT_EQ(results.size(), 2); | ||
EXPECT_EQ(results[0], 5); | ||
EXPECT_EQ(results[1], 0); | ||
|
||
b.setValue(6); | ||
EXPECT_EQ(results.size(), 4); | ||
EXPECT_EQ(results[2], 11); | ||
EXPECT_EQ(results[3], 30); | ||
|
||
b.setValue(7); | ||
EXPECT_EQ(results.size(), 6); | ||
EXPECT_EQ(results[4], 12); | ||
EXPECT_EQ(results[5], 35); | ||
} | ||
|
||
|
||
TEST_F(DataCallback_test, testDataCallback_All) | ||
{ | ||
TestObject obj; | ||
obj.m_datacallbackAll.addCallback([&obj](){obj.printDataAll();}); | ||
|
||
EXPECT_EQ( obj.d_objdata1.getValue(), 0 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
|
||
//callback is expected to print a fatal message | ||
EXPECT_MSG_EMIT(Fatal) ; | ||
obj.d_objdata1.setValue(234); | ||
EXPECT_EQ( obj.d_objdata1.getValue(), 234 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 1 ) ; | ||
EXPECT_MSG_EMIT(Fatal) ; // how to expect an other (fatal) message ? | ||
obj.d_objdata2.setValue(987); | ||
EXPECT_EQ( obj.d_objdata1.getValue(), 234 ) ; | ||
EXPECT_EQ( obj.d_objdata2.getValue(), 987 ) ; | ||
} | ||
|
||
|
||
}// namespace sofa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
SofaKernel/framework/sofa/core/objectmodel/DataCallback.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/****************************************************************************** | ||
* SOFA, Simulation Open-Framework Architecture, development version * | ||
* (c) 2006-2019 INRIA, USTL, UJF, CNRS, MGH * | ||
* * | ||
* This program 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 2.1 of the License, or (at * | ||
* your option) any later version. * | ||
* * | ||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. * | ||
******************************************************************************* | ||
* Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
* * | ||
* Contact information: contact@sofa-framework.org * | ||
******************************************************************************/ | ||
#include <sofa/core/objectmodel/DataCallback.h> | ||
|
||
namespace sofa | ||
{ | ||
|
||
namespace core | ||
{ | ||
|
||
namespace objectmodel | ||
{ | ||
|
||
void DataCallback::addInputs(std::initializer_list<BaseData*> data) | ||
{ | ||
for(BaseData* d : data) | ||
{ | ||
addInput(d); | ||
} | ||
} | ||
|
||
void DataCallback::addCallback(std::function<void(void)> f) | ||
{ | ||
m_callbacks.push_back(f); | ||
} | ||
|
||
void DataCallback::notifyEndEdit(const core::ExecParams* params) | ||
{ | ||
if (!m_updating) | ||
{ | ||
m_updating = true; | ||
for (auto& callback : m_callbacks) | ||
callback(); | ||
|
||
sofa::core::objectmodel::DDGNode::notifyEndEdit(params); | ||
m_updating = false; | ||
} | ||
else | ||
{ | ||
msg_warning("DataCallback") << "A DataCallback seems to have a circular dependency, please fix it to remove this warning."; | ||
} | ||
} | ||
|
||
const std::string& DataCallback::getName() const | ||
{ | ||
static std::string s=""; | ||
return s; | ||
} | ||
|
||
sofa::core::objectmodel::Base* DataCallback::getOwner() const | ||
{ | ||
return nullptr; | ||
} | ||
|
||
sofa::core::objectmodel::BaseData* DataCallback::getData() const | ||
{ | ||
return nullptr; | ||
} | ||
|
||
void DataCallback::update() | ||
{ | ||
|
||
} | ||
|
||
} /// namespace objectmodel | ||
|
||
} /// namespace core | ||
|
||
} /// namespace sofa | ||
|
||
|
Oops, something went wrong.