I know, I know. The question just screams "you shouldn't be in that situation!" but hear me out...
I have a game engine that performs the updates in one thread and the renders in another. The renders want to read the data from the previous frame while the update updates the data as it would be for the next frame.
So, to solve this I have a POD struct, mutable_data, that is everything in the object that can change (velocity, position, etc...) which I have two instances of inside the object, one const and the other not. At the end of one iteration of the game loop the non-const data will be the up-to-date data object and I want to then copy this over the const instance so that they are now equal and ready for the next iteration.
Is this possible?
I've drafted up an example program that hopefully makes it clear what I mean:
#include <iostream>
struct mutable_data
{
int i = 1;
bool j = true;
char k = 'a';
};
struct my_data
{
my_data() : constData() {}
int i() const { return constData.i; }
bool j() const { return constData.j; }
char k() const { return constData.k; }
void set_i(int i) { data.i = i; }
void set_j(bool j) { data.j = j; }
void set_k(char k) { data.k = k; }
void update() { constData = data; }
private:
mutable_data const constData;
mutable_data data;
};
int main()
{
my_data d;
std::cout << "i: " << d.i() << ", j: " << d.j() << ", k: " << d.k() << std::endl;
d.set_i(10);
d.set_j(false);
d.set_k('z');
std::cout << "i: " << d.i() << ", j: " << d.j() << ", k: " << d.k() << std::endl;
std::cout << "===============" << std::endl;
d.update();
std::cout << "i: " << d.i() << ", j: " << d.j() << ", k: " << d.k() << std::endl;
}