Weathr, a Comprehensive Demonstration of C++ 14, DirectX, and XAML
Most technology demonstrations tend to be fairly light weight, showing only a couple key concepts in a fairly trivial application. Weathr is different in that it is a fully functional 3D weather map complete with live service integration and semantic zoom.
The core of Weathr is an ISO C++ using modern coding conventions. Among other things that means no use of the keywords new and delete. Instead nearly everything is handled through the combination of shared, unique, and weak smart pointers.
The user interface is built on a combination of DirectX, for 3D components, and XAML. The later requires the use of C++/CX, the language extension Microsoft created specifically for WinRT based applications.
Another technology being showcased is PPL or Parallel Patterns Library. This allows for the use of asynchronous methods that are chained together using lambda expressions. PPL is used mainly to interact with Bing Maps and World Weather Online via the C++ REST SDK. Memory management for asynchronous can be tricky so Thomas Petchel writes,
Another pattern I’ve discovered occurs when you have a chain of asynchronous tasks and one task creates an object or resource, and another, subsequent, task uses that resource. Although you can often simply create the resource in one task and pass it to its subsequent task, this is not always possible. The issues here are that of lifetime and indirection – whether the object has a C++ or C++/CX type, because we’re working asynchronously, the calling function that sets up the background work will soon exit, and thus any objects allocated on the stack will fall out of scope. To keep objects shared among tasks alive, you must capture smart pointers (typically, a shared_ptr for C++ and C++/CX objects) in all lambdas (or functors) that reference them. By using shared_ptr, you create a level of indirection that enables the object to be allocated (e.g. written to) in one task and used (e.g. read) by a subsequent task.
Ensuring that the async callbacks are being run on the correct thread is another issue. Thomas has this tip for verifying that is occurring,
In Debug builds, record the thread ID of the ASTA thread at startup. In each task continuation, assert that we’re either running on the main (ASTA) thread or a background thread.
In task continuations, specify explicitly whether to run on the current context (e.g. the context that established the task chain, which might be the ASTA thread or MTA thread) or on a background (MTA) context.
Other techniques and technologies explored in this sample application include HTTP request throttling, semantic zoom, gesture recognition, and Windows 8 live tiles.
Weathr is available on CodePlex under the Apache 2 license.
Anatole Tresch Mar 03, 2015