comparison Framework/Toolbox/GenericToolbox.h @ 1328:fd616c4a5904 broker

Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 27 Mar 2020 12:54:27 +0100
parents fef1ec42a7db
children ab81ee8fce1f
comparison
equal deleted inserted replaced
1327:4f8db2d202c8 1328:fd616c4a5904
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 #pragma once 21 #pragma once
22 22
23 #include <Core/Compatibility.h>
24
25 #include <boost/shared_ptr.hpp>
26
23 #include <string> 27 #include <string>
24 #include <stdint.h> 28 #include <stdint.h>
25 #include <math.h> 29 #include <math.h>
26 30
31 #include <memory>
27 32
28 namespace OrthancStone 33 namespace OrthancStone
29 { 34 {
30 namespace GenericToolbox 35 namespace GenericToolbox
31 { 36 {
279 } 284 }
280 285
281 /** 286 /**
282 Same as GetRgbValuesFromString 287 Same as GetRgbValuesFromString
283 */ 288 */
284 bool GetRgbaValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha, const char* text); 289 bool GetRgbaValuesFromString(uint8_t& red,
290 uint8_t& green,
291 uint8_t& blue,
292 uint8_t& alpha,
293 const char* text);
285 294
286 /** 295 /**
287 Same as GetRgbValuesFromString 296 Same as GetRgbValuesFromString
288 */ 297 */
289 inline bool GetRgbaValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha, const std::string& text) 298 inline bool GetRgbaValuesFromString(uint8_t& red,
299 uint8_t& green,
300 uint8_t& blue,
301 uint8_t& alpha,
302 const std::string& text)
290 { 303 {
291 return GetRgbaValuesFromString(red, green, blue, alpha, text.c_str()); 304 return GetRgbaValuesFromString(red, green, blue, alpha, text.c_str());
292 } 305 }
306
307
308 template<typename Wrappee>
309 struct HoldingRef
310 {
311 HoldingRef(Wrappee* object)
312 {
313 // a crash here means that the object is not stored in a shared_ptr
314 // using shared_ptr is mandatory for this
315 object_ = object->shared_from_this();
316 }
317 boost::shared_ptr<Wrappee> object_;
318
319 static void* Wrap(Wrappee* object)
320 {
321 std::unique_ptr<HoldingRef<Wrappee > > up(new HoldingRef<Wrappee>(object));
322 void* userData = reinterpret_cast<void*>(up.release());
323 return userData;
324 }
325
326 static boost::shared_ptr<Wrappee> Unwrap(void* userData)
327 {
328 // the stored shared_ptr will be deleted because of wrapping
329 // the data in a RAII
330 std::unique_ptr<HoldingRef<Wrappee > >
331 up(reinterpret_cast<HoldingRef<Wrappee>*>(userData));
332 boost::shared_ptr<Wrappee> object = up->object_;
333 return object;
334 }
335 };
293 } 336 }
294 } 337 }