Skip to content

Commit

Permalink
ScriptInterface small fixes (#4242)
Browse files Browse the repository at this point in the history
Preparing the ground for #4225.

Description of changes:
- document the creation policy of script interface objects
- guard against double parsing in `ObjectList::do_call_method()`
  • Loading branch information
kodiakhq[bot] authored May 9, 2021
2 parents bdbfa60 + d93f935 commit ae3094f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/python/espressomd/script_interface.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ cdef class PScriptInterface:
\*\*kwargs
Parameters for the core class constructor (method 2).
policy : :obj:`str`, \{'GLOBAL', 'LOCAL'\}
Creation policy.
Creation policy. The managed object exists either on all MPI nodes
with 'GLOBAL' (default), or only on the head node with 'LOCAL'.
Attributes
----------
Expand Down
9 changes: 6 additions & 3 deletions src/script_interface/ObjectList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef SCRIPT_INTERFACE_REGISTRY_HPP
#define SCRIPT_INTERFACE_REGISTRY_HPP
#ifndef SCRIPT_INTERFACE_OBJECT_LIST_HPP
#define SCRIPT_INTERFACE_OBJECT_LIST_HPP

#include "script_interface/ScriptInterface.hpp"
#include "script_interface/get_value.hpp"
Expand Down Expand Up @@ -60,7 +60,7 @@ class ObjectList : public BaseType {
/**
* @brief Removes all occurrences of an element from the list.
*
* @param element The element to add.
* @param element The element to remove.
*/
void remove(std::shared_ptr<ManagedType> const &element) {
remove_in_core(element);
Expand Down Expand Up @@ -93,13 +93,15 @@ class ObjectList : public BaseType {
get_value<std::shared_ptr<ManagedType>>(parameters.at("object"));

add(obj_ptr);
return none;
}

if (method == "remove") {
auto obj_ptr =
get_value<std::shared_ptr<ManagedType>>(parameters.at("object"));

remove(obj_ptr);
return none;
}

if (method == "get_elements") {
Expand All @@ -114,6 +116,7 @@ class ObjectList : public BaseType {

if (method == "clear") {
clear();
return none;
}

if (method == "size") {
Expand Down
5 changes: 5 additions & 0 deletions src/script_interface/tests/ObjectList_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#include "script_interface/LocalContext.hpp"
#include "script_interface/ObjectList.hpp"

#include <algorithm>
#include <memory>
#include <vector>

using namespace ScriptInterface;

struct ObjectListImpl : ObjectList<ObjectHandle> {
Expand Down Expand Up @@ -75,6 +79,7 @@ BOOST_AUTO_TEST_CASE(clearing_elements) {
list.add(ObjectRef{});
list.clear();
BOOST_CHECK(list.elements().empty());
BOOST_CHECK(list.mock_core.empty());
}

BOOST_AUTO_TEST_CASE(serialization) {
Expand Down

0 comments on commit ae3094f

Please sign in to comment.