I am wondering whether there is a design pattern that nicely does, what I currently achieve by the following construct:
I have a object that I want to create based on a set of input objects. In my particular case those are stream like objects. During the calculation I need to keep track of so much state, that I do not want to pass it around as function parameters, but also do not want to polute the namespace in which I need the value of the calculation. So what I do is create a class that roughly looks like this:
class objectCreator {
public:
objectCreator(stream_type & stream) : stream(stream) {
create();
}
std::unique_ptr<value_type> get() { return std::move(result); }
private:
void
create() { /* ... */ }
std::unique_ptr<value_type> result;
stream_type & stream;
// various state holding variables
};
My problem is, that clearly calling get()
twice does not make sense. I could guard against it, by checking if it has been called already, but I would like to use a construct, that inherently prevents one from calling it twice. (Note: I already wrap this object in a function call somewhere and it should not be used anywhere else, but that's just not beautiful.)