{"id":104845,"date":"2021-02-11T07:00:00","date_gmt":"2021-02-11T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104845"},"modified":"2021-02-11T07:52:19","modified_gmt":"2021-02-11T15:52:19","slug":"20210211-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210211-00\/?p=104845","title":{"rendered":"The COM static store, part 4: Aggregating into a single object"},"content":{"rendered":"<p>So far, we&#8217;ve been looking at how you can <a title=\"The COM static store, part 1: Introduction\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210208-00\/?p=104812\"> use the COM static store to hold a singleton which will be run down when COM uninitializes<\/a>. But what if you have a lot of things you want to save? I mean, you could put each one individually in the COM static store, but that gets quite cumbersome. And some of the things might not even be COM objects, or they may be COM objects that do not support <code>IInspectable<\/code>. What can you do about those?<\/p>\n<p>You can wrap all of your global state into a single object that supports <code>IInspectable<\/code>, and put that single object into the COM static store. That way, you can just grab one thing out of the COM static store instead of having to go back to the COM static store for each one.<\/p>\n<pre>\/\/ C++\/WinRT\r\nstruct SharedState :\r\n    winrt::implements&lt;SharedState,\r\n                      winrt::Windows::Foundation::IInspectable&gt;\r\n{\r\n    int some_value = 0;\r\n    winrt::com_ptr&lt;IStream&gt; stream;\r\n    std::vector&lt;winrt::com_ptr&lt;IStorage&gt;&gt; storages;\r\n};\r\n\r\nwinrt::com_ptr&lt;SharedState&gt;\r\nGetSingletonSharedState()\r\n{\r\n    auto props = CoreApplication::Properties();\r\n    if (auto found = props.TryLookup(L\"SharedState\")) {\r\n        return <a title=\"How do I convert from the C++\/WinRT projection type to the C++\/WinRT implementation type?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200827-00\/?p=104133\">as_self<\/a>&lt;SharedState&gt;(found);\r\n    }\r\n    auto value = winrt::make_self&lt;SharedState&gt;();\r\n    static winrt::slim_mutex lock;\r\n    winrt::slim_lock_guard const guard{ lock };\r\n    if (auto found = props.TryLookup(L\"SharedState\")) {\r\n        return as_self&lt;SharedState&gt;(found);\r\n    }\r\n    props.Insert(name, *value);\r\n    return value;\r\n}\r\n<\/pre>\n<p>Now, when you need stuff, you can ask for the singleton <code>SharedState<\/code> and access everything, rather than having to get each one individually.<\/p>\n<p>But wait, we can do even better still.<\/p>\n<p>Next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Batch all the things you need into a single object.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-104845","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Batch all the things you need into a single object.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=104845"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104845\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=104845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}