I'm using this kind of code for some different plugins I'm making:
#include <iostream>
#include <functional>
template <class T>
struct SharedDataEngine {
T mSharedData;
std::function<void(T &destination)> mCopySharedData;
SharedDataEngine(std::function<void(T &destination)> copySharedData) : mCopySharedData(copySharedData) {
}
void storeSharedData() {
if (mCopySharedData != NULL) {
mCopySharedData(mSharedData);
}
}
};
struct Plugin {
float mPhase = 10.0f;
struct SharedData {
float mPhase;
};
SharedDataEngine<SharedData> mSharedDataEngine{[this](SharedData &destination) {
destination.mPhase = mPhase;
}};
};
int main() {
Plugin plugin;
std::cout << plugin.mSharedDataEngine.mSharedData.mPhase << std::endl;
plugin.mPhase = 20.0f;
plugin.mSharedDataEngine.storeSharedData();
std::cout << plugin.mSharedDataEngine.mSharedData.mPhase << std::endl;
}
It must be soft on runtime, since it deals with audio at higher sample-rates. I'd like to ensure that functional w/ lambdas won't involve run-time "extra"processing.
Thus, would be nice to me to understand how compiler will translate that mCopySharedData
within the structs. Any chance to get some fancy pseudo-code/c++ style code?
To see if there are not any "magic" once it calls that function. I could use godbolt of course and see myself the code, but that's assembly, not really my kind of language :)
EDIT:
I wish it would translate mCopySharedData(mSharedData)
into somethings like this:
mSharedData.mPhase = pointerToPlugin->mPhase;
Is it correct?