diff --git a/src/core/buffer_object.cpp b/src/core/buffer_object.cpp index da671119..57b8a886 100644 --- a/src/core/buffer_object.cpp +++ b/src/core/buffer_object.cpp @@ -25,8 +25,11 @@ bool BufferObject::deserialize() } /*************/ -void BufferObject::setSerializedObject(SerializedObject&& obj) +bool BufferObject::setSerializedObject(SerializedObject&& obj) { + if (obj.size() == 0) + return false; + if (_serializedObjectWaitingMutex.try_lock()) { _serializedObject = std::move(obj); @@ -38,7 +41,11 @@ void BufferObject::setSerializedObject(SerializedObject&& obj) deserialize(); _serializedObjectWaitingMutex.unlock(); }); + + return true; } + + return false; } /*************/ diff --git a/src/core/buffer_object.h b/src/core/buffer_object.h index 649cad53..e2443d6b 100644 --- a/src/core/buffer_object.h +++ b/src/core/buffer_object.h @@ -151,10 +151,15 @@ class BufferObject : public GraphObject virtual SerializedObject serialize() const = 0; /** - * Set the next serialized object to deserialize to buffer + * Set the next serialized object to deserialize to buffer. Deserialization is + * done asynchronously, in a separate thread if the system allows for it. Use + * hasSerializedObjectWaiting to check whether a deserialization is waiting + * If another object is currently being set for deserialization, the call + * to this method will do nothing and return false. * \param obj Serialized object + * \return Return true if the object has been set for deserialization, false otherwise */ - void setSerializedObject(SerializedObject&& obj); + bool setSerializedObject(SerializedObject&& obj); /** * Check whether a serialized object is waiting for deserialization diff --git a/src/core/root_object.cpp b/src/core/root_object.cpp index e914d851..c4656e26 100644 --- a/src/core/root_object.cpp +++ b/src/core/root_object.cpp @@ -192,8 +192,7 @@ bool RootObject::setFromSerializedObject(const std::string& name, SerializedObje auto objectAsBuffer = std::dynamic_pointer_cast(object); if (objectAsBuffer) { - objectAsBuffer->setSerializedObject(std::move(obj)); - return true; + return objectAsBuffer->setSerializedObject(std::move(obj)); } } else diff --git a/src/core/root_object.h b/src/core/root_object.h index b8ef142f..a5fc6fe2 100644 --- a/src/core/root_object.h +++ b/src/core/root_object.h @@ -199,7 +199,12 @@ class RootObject : public BaseObject bool set(const std::string& name, const std::string& attrib, const Values& args, bool async = true); /** - * Set an object from its serialized form. If non existant, it is handled by the handleSerializedObject method. + * Set an object from its serialized form. If non existant, it is handled + * by the handleSerializedObject method. + * Note that if the object exists, this method calls itself + * BufferObject::setFromSerializedObject, and that the deserialization is + * handled asynchronously. Use BufferObject::hasSerializedObjectWaiting to + * check whether a deserialization is waiting. * \param name Object name * \param obj Serialized object * \return Return true if the object has been set diff --git a/tests/unit_tests/core/buffer_object.cpp b/tests/unit_tests/core/buffer_object.cpp index 893e9084..e4cd1f6e 100644 --- a/tests/unit_tests/core/buffer_object.cpp +++ b/tests/unit_tests/core/buffer_object.cpp @@ -67,9 +67,7 @@ TEST_CASE("Testing serialization") auto buffer = BufferObjectMock(); auto timestamp = buffer.getTimestamp(); - buffer.setSerializedObject({}); - while (buffer.hasSerializedObjectWaiting()) - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - - CHECK_NE(timestamp, buffer.getTimestamp()); + auto result = buffer.setSerializedObject({}); + CHECK_EQ(result, false); + CHECK_EQ(timestamp, buffer.getTimestamp()); } diff --git a/tests/unit_tests/core/root_object.cpp b/tests/unit_tests/core/root_object.cpp index 442096f6..894c1a53 100644 --- a/tests/unit_tests/core/root_object.cpp +++ b/tests/unit_tests/core/root_object.cpp @@ -136,16 +136,20 @@ TEST_CASE("Testing RootObject serialized object set") auto timestamp = image->getTimestamp(); auto result = root.setFromSerializedObject(imageName, SerializedObject()); image->update(); - CHECK_EQ(result, true); + CHECK_EQ(result, false); CHECK_EQ(timestamp, image->getTimestamp()); auto otherName = "otherImage"; auto otherImage = std::dynamic_pointer_cast(root.createObject("image", otherName).lock()); otherImage->set(512, 512, 3, ImageBufferSpec::Type::UINT8); otherImage->update(); + while (otherImage->hasSerializedObjectWaiting()) + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + result = root.setFromSerializedObject(imageName, otherImage->serialize()); while (image->hasSerializedObjectWaiting()) std::this_thread::sleep_for(std::chrono::milliseconds(5)); + image->update(); CHECK_EQ(result, true); CHECK_NE(timestamp, image->getTimestamp());