I implement a decorator pattern on immutable objects with an idiom pointer to an implementation. Basically my setup is as follows
struct Object : ObjectBase { void doSmth() override { impl->doSmth(); }
Here, the decorateWith function should have a different behavior depending on whether the object it called is temporary or not. If it is called on a non-temporary object, it should return a new instance of the object, where I need to make a deep copy of the current object and save it in the decorator's unique_ptr, while the impl pointer of the new object itself points to the decorator. If, however, decorateWith is called temporarily, just create an ObjectDecorator and simply move the pointer-pointer of the current object to the impl pointer of the decorator and let the object point to the new decorator.
In order to impose that I need a way to determine from inside the call to decorate with whether the object is temporary or not, and then use the send tags based on the result of this check. Is it possible?
Best Xodion
EDIT: An example caller code might look like this:
decorateWith called on non-temporary
int main() { Object x{}; // this call does not modify x so it can be reused later Object y = x.decorateWith{std::make_unique<ObjectDecorator>()}; y.doSmth(); // do some other stuff here // here, if I had moved the impl-unique_ptr in the decorateWith // call this would now segfault since I'd call nullptr->doSmth(); x.doSmth(); }
decorateWith called temporary
int main() { Object x = Object{}.decorateWith(std::make_unique<ObjectDecorator>()) .decorateWith(std::make_unique<ObjectDecorator>()) .decorateWith(std::make_unique<ObjectDecorator>());
source share