{"id":243,"date":"2016-02-23T11:23:10","date_gmt":"2016-02-23T11:23:10","guid":{"rendered":"http:\/\/www.crazygaze.com\/blog\/?p=243"},"modified":"2016-08-24T15:43:44","modified_gmt":"2016-08-24T15:43:44","slug":"easy-way-to-share-data-between-instances","status":"publish","type":"post","link":"https:\/\/www.crazygaze.com\/blog\/2016\/02\/23\/easy-way-to-share-data-between-instances\/","title":{"rendered":"Easy way to share data between instances"},"content":{"rendered":"<p>Sometimes you need to share data between instances, but you need that data to be accessed externally, automatically created when needed, and destroyed when not needed anymore.<\/p>\n<p>You can use something\u00a0like this:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntemplate&lt;typename T&gt;\r\nstd::shared_ptr&lt;T&gt; getSharedData()\r\n{\r\n    static std::mutex mtx;\r\n    static std::weak_ptr&lt;T&gt; ptr;\r\n    std::lock_guard&lt;std::mutex&gt; lk(mtx);\r\n    auto p = ptr.lock();\r\n    if (p)\r\n        return p;\r\n    p = std::make_shared&lt;T&gt;();\r\n    ptr = p;\r\n    return p;\r\n}\r\n<\/pre>\n<p>This way you can have instances getting a strong reference to that data, creating it if necessary.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct FooSharedData \r\n{\r\n    \/\/ ... details here\r\n};\r\n\r\nclass Foo\r\n{\r\npublic:\r\n    Foo() : m_shared(getSharedData&lt;FooSharedData&gt;()) { }\r\nprivate:\r\n    std::shared_ptr&lt;FooSharedData&gt; m_shared;\r\n};\r\n\r\nvoid someFunction()\r\n{\r\n    {\r\n        Foo a; \/\/ Will create the FooSharedData instance\r\n        Foo b; \/\/ Will grab the existing FooSharedData instance\r\n        \/\/ Can access it externally too\r\n        auto data = getSharedData&lt;FooSharedData&gt;();\r\n    }\r\n    \/\/ At this point, there are no more strong references to the FooSharedData instance,\r\n    \/\/ so it gets deleted\r\n\r\n    \/\/ This will recreate a FooSharedData instance, since the previous one was destroyed\r\n    Foo c;\r\n};\r\n<\/pre>\n<h3>License<\/h3>\n<p>The source code in this article is licensed under the <a href=\"https:\/\/creativecommons.org\/publicdomain\/zero\/1.0\/\">CC0 license<\/a>, so feel free to copy, modify, share, do whatever you want with it.<br \/>\nNo attribution is required, but I&#8217;ll be happy if you do.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need to share data between instances, but you need that data to be accessed externally, automatically created when needed, and destroyed when not needed anymore. You can use something\u00a0like this: This way you can have instances getting a strong reference to that data, creating it if necessary. License The source code in this [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[50,3],"tags":[14,12,10],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7jpe0-3V","_links":{"self":[{"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/posts\/243"}],"collection":[{"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":2,"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":735,"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/posts\/243\/revisions\/735"}],"wp:attachment":[{"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.crazygaze.com\/blog\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}