'Is there any ready made pattern or library to implement Memento pattern for ones classes instead of reinventing the wheel?

I have classes like this:

class MyClass{
    int myField1;
    int myField2;

    MyMemento* getMemento(){
        auto m = new MyMemento();
        m->myField1 = myField1;
        m->myField2 = myField2;
    }

    void restore(MyMemento* m){
        myField1 = m->myField1;
        myField2 = m->myField2;
    }
}

class MyMemento{
public:
    int myField1;
    int myField2;
}

I have many classes like MyClass, each with their own unique member variables. But the pattern of the functions for creating / restoring the memento is the same, and the actual memento object simply contains variables matching each class.

This seems very tedious to implement for each class, so was wondering if there is some readymade class, library or pattern I can use to implement this instead ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source