I am coding a visualizer of graph algorithms. Each vertex of the graph has a style (color, size etc.). As long as the algorithm has done nothing to a vertex, that vertex has a default style. The default style needs to be configurable at run-time.
I came up with the following solution based on the singleton pattern, which seems a bit awkward to me (as indicated in the comment). Is there a better one?
struct Style {
int color;
// size etc. are skipped for brevity
};
// Changeable default style
struct DefaultStyle {
static DefaultStyle &instance() {
static DefaultStyle res;
return res;
}
DefaultStyle() = default;
DefaultStyle(DefaultStyle const &) = delete;
void operator=(DefaultStyle const &) = delete;
Style s{5};
// didn't see a benefit in making it private,
// since I would need to provide get() and set() anyways.
};
int main()
{
auto &s = DefaultStyle::instance().s;
// this need to access the style indirectly (.s) seems awkward
s.color = 10;
std::cout << DefaultStyle::instance().s.color << std::endl;
// the change is globally visible
return 0;
}
Cairo
, no file output, only drawing on screen.