I have written code for a controller that has buttons and lamps. It is based on Arduino/ATmega2560. RAM is very limited. Changing to another device is no option.
Each button and lamp is modelled as a C++ class instance. They are derived from an abstract class Thing
, which contains a pointer mpTarget
that indicates connections between Thing
s. The targets are initially assigned at runtime and do not change during runtime.
I need to save memory. As I have ~600 Thing
s, it would help me alot if all those mpTarget
s were stored in ROM (and not in RAM), because the targets are known at compile time.
So I played with constexpr
. However, it seems to be impossible to make a class only partially constexpr. I cannot make the whole class constexpr
, because there are also member variables that change during runtime (mState
in the example below).
What would be a good way to achieve this behavior? constexpr? Templates? Anything?
Sample code:
#include <iostream>
class Thing
{
public:
// only called in setup()
void setTarget(Thing & pTarget)
{
mpTarget = & pTarget;
}
virtual void doSomething(int value) {};
protected:
// known at compile time - can we make this constexpr?
Thing * mpTarget;
};
class Button : public Thing
{
public:
void setState(int newState)
{
mState = mState + newState;
mpTarget->doSomething(mState);
}
private:
// changes during runtime
int mState;
};
class Lamp: public Thing
{
public:
virtual void doSomething(int value)
{
std::cout << value << std::endl;
};
};
Button myButton;
Lamp myLamp;
int main()
{
myButton.setTarget(myLamp);
while (1)
{
myButton.setState(123);
}
}
Aucun commentaire:
Enregistrer un commentaire