XRTraits C++ OpenXR Utilities
twocall-size-hint-and-initialized.cpp

This example shows a typical usage of doTwoCallWithSizeHint(): when locating views. The number of views should in theory be known, but for safety and consistency this function is specified in two-call style.

It also shows a few uses of xrtraits::Initialized - one (viewState) with no arguments (which sets just the type and next), and one (viewLocateInfo) with some arguments that aren't a chained struct. In this case, type and next are initialized like normal, while the subsequent members of XrViewLocateInfo, displayTime and space, are initialized from frameTime and local respectively.

Because a size hint was provided, this call will initially create a vector of numViews XrView structures before attempting to call xrLocateViews. This will generally reduce the number of calls to xrLocateViews to 1, since the initial capacity should be sufficient. The wrapper will make a call like the following as required to retrieve all properties (adjusting the parameters and array between calls):

{c++}
XrResult result = xrLocateViews(session, &viewLocateInfo, &viewState,
capacityInput, &countOutput, array);

Note how the several additional arguments were forwarded to the call before the capacity, count, and array args.

// Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
#include <iostream>
int main()
{
uint32_t numViews = 2; // Previously initialized as required.
XrSpace local; // Previously initialized
XrSession session; // Previously initialized
XrTime frameTime; // Previously initialized
Initialized<XrViewState> viewState; // Output
Initialized<XrViewLocateInfo> viewLocateInfo{frameTime, local}; // Input
auto views = doTwoCallWithSizeHint<XrView>(
numViews, xrLocateViews, session, &viewLocateInfo, &viewState);
return 0;
}