C++ Integration
Driving Storyboard from a C++ application. The concepts are identical to C#; the API shape differs.
Table of Contents
What Changes
Set ApplicationType to Cpp on the project. Everything in
Configuration Reference applies unchanged — the same
StoryboardConfiguration.json, the same launch configurations, the same surface binding.
| C# | C++ | |
|---|---|---|
| Client type | StoryboardClient |
AhsokaStoryboard::StoryboardAPI |
| Generated file | GeneratedStoryboardObjects.cs |
GeneratedStoryboardObjects.h + .cpp |
| Receiving | MessageReceived event, or the dispatcher |
A BlockingQueue<StoryboardMessage> you poll |
| Model to message | model.Message |
model.get_StoryboardMessage() |
| Message to model | message.GetObject<T>() |
T model(message) |
| Channel id | Message.ChannelId |
message.ChannelId |
The staging difference is that the SDK lands in the project folder rather than obj/, because a
C++ build has no obj/.
Generated Code
Code generation reads the same custom.evt and emits a header and an implementation file into
generatorOutputFolder, in the namespace from generatorNameSpace.
Generated classes differ by direction, which is stricter than the C# side:
| Direction | Members generated |
|---|---|
Out (0x40) |
get_StoryboardMessage() — build a message to send |
In (0x20) |
A constructor taking a StoryboardMessage, and set_StoryboardMessage() |
So an outbound event can only be built, and an inbound event can only be read — the compiler enforces the direction rather than leaving it to convention.
AhsokaStoryboardDemo::ThermostatUpdate update;
update.TargetTemperature = 45;
update.CurrentTemperature = 58;
update.Ac = 1;
auto message = update.get_StoryboardMessage();
Each class also exposes get_Name() and get_Format(), the equivalents of the C# Name and
Format constants.
Unlike the C# side, generated members are plain public fields, not properties backed by the
message buffer. Field types are Storyboard's own typedefs — sb_uint32_t, sb_float_t,
sb_string_t and so on — and an array field becomes a fixed C array:
sb_uint16_t CylinderTemps[8];
Because the fields are ordinary members, assigning to one does not write anything to the wire. The
buffer is built when you call get_StoryboardMessage(), which is why that call must come after the
assignments rather than before.
The StoryboardAPI Class
#include "AhsokaServices.h"
#include "StoryboardAPI.h"
#include "GeneratedStoryboardObjects.h"
AhsokaStoryboard::StoryboardAPI api;
AhsokaCore::BlockingQueue<StoryboardMessage> queue;
api.AddInputChannel("thermostat_backend");
api.AddOutputChannel("thermostat_frontend");
api.StartStoryboardWithInputQueue(&queue);
Channel rules are exactly as in C#: add every channel before starting, names must match the Storyboard application name, 25-character limit, outbound ids assigned in call order.
AddOutputChannel returns the channel id — capture it rather than assuming positions:
int dashboardChannelId = api.AddOutputChannel(DashboardChannel);
int detailChannelId = api.AddOutputChannel(DetailChannel);
Shut down with api.StopStoryboard().
Receiving Through a Queue
Instead of an event, the C++ API pushes received messages onto a blocking queue that you drain on your own thread:
static void ReceiveThread(StoryboardApp* app)
{
while (app->signals.IsRunning())
{
auto received = app->queue.Pop();
if (received.Name == "change_brightness")
{
AhsokaStoryboardDemo::ChangeBrightness command(received);
app->ApplyBrightness(command.Value, command.Increase == 1);
}
else if (received.Name == "shutdown")
{
app->signals.RequestExit(SIGQUIT);
}
}
}
Pop() blocks until a message arrives. Dispatching on Name is the same pattern as the C# switch,
and constructing the generated type from the message is the equivalent of GetObject<T>().
Note
As in C#, a received message carries no usable channel information. Applications listening to several engines distinguish them by event name.
Sending
AhsokaStoryboardDemo::ThermostatUpdate update;
update.TargetTemperature = 70;
auto message = update.get_StoryboardMessage();
api.SendEvent(message);
get_StoryboardMessage() builds a fresh message each call from the object's current field values,
so set the fields first.
Multiple Displays
Same three mechanisms as Driving Multiple Displays. The difference
is that C++ has no OnInit hook, so stamp the channel id on the message at the point of sending:
AhsokaMultiScreenDemo::DashboardUpdate dashboard;
dashboard.Rpm = rpm;
dashboard.Load = load;
dashboard.Mode = Modes[modeIndex];
auto dashboardMessage = dashboard.get_StoryboardMessage();
dashboardMessage.ChannelId = dashboardChannelId;
api.SendEvent(dashboardMessage);
AhsokaMultiScreenDemo::DetailUpdate detail;
detail.Rpm = rpm;
detail.Load = load;
detail.Mode = Modes[modeIndex];
auto detailMessage = detail.get_StoryboardMessage();
detailMessage.ChannelId = detailChannelId;
api.SendEvent(detailMessage);
Warning
Because there is no constructor hook, forgetting the ChannelId assignment is easier in C++ than
in C#. Keep the get_StoryboardMessage() call and the ChannelId assignment adjacent, as above,
so a missing one is visible.
Building
Generate stages the Storyboard SDK into the project's .platform_support tree and writes a
CMakeLists.txt alongside the engine, so a CMake build picks it up.
The application links against the Ahsoka C++ runtime and includes the generated header. On a development host the .NET service executable is started alongside:
const std::string pathToExecutable = "./CommandLine/Ahsoka.CommandLine";
auto serviceProcess = AhsokaRuntimeProcess::StartServiceExecutable(pathToExecutable);
On a device the service manager starts it, so this step is skipped — the demos do it only for local development.
Full Example
#include "AhsokaServices.h"
#include "AhsokaRuntimeProcess.h"
#include "GeneratedStoryboardObjects.h"
#include "StoryboardAPI.h"
#include "SignalHandler.h"
class StoryboardApp
{
public:
void Run(std::string pathToServiceExe)
{
auto serviceProcess = AhsokaRuntimeProcess::StartServiceExecutable(pathToServiceExe);
api.AddInputChannel("thermostat_backend");
api.AddOutputChannel("thermostat_frontend");
api.StartStoryboardWithInputQueue(&queue);
std::thread sendThread(SendThread, this);
std::thread receiveThread(ReceiveThread, this);
AhsokaRuntime::ReleaseStartup();
signals.WaitForExit();
api.StopStoryboard();
serviceProcess->EndProcess();
if (sendThread.joinable()) sendThread.join();
if (receiveThread.joinable()) receiveThread.join();
}
private:
AhsokaStoryboard::StoryboardAPI api;
AhsokaCore::BlockingQueue<StoryboardMessage> queue;
SignalHandler signals; // SIGTERM, SIGINT, SIGHUP, SIGQUIT
};
Ahsoka.CPP.Storyboard ships in the package as a complete working version of this.