4 ms·
One downside to this approach as far as I interpret it, widget_handle widget = widget_manager_new_widget(manager); is that it appears to require the user
by jmts 8y ago
One downside to this approach as far as I interpret it,
widget_handle widget = widget_manager_new_widget(manager);
is that it appears to require the user to either remember two values,
widget_tweak(manager, widget);
or the developer to push a variable into (at least) file scope,
widget_tweak(widget)
{
actual_widget = widget_manager_lookup_widget(manager, widget);
}
That's not super nice.
Most of the benefits described can be had by using pools rather than a naive heap allocator.
Seem like a nice way to hide bugs, though.
- hcs 8y agoI think there is meant to be only one "manager" per object (thus, handle) type.
- jmts 8y agoI had envisaged my 'manager' variable above as type widget_manager. Having a global manager of all things, or independent manager makes no difference to my argument. The manager has to live in file-scope or above since there is no room in the handle to save a reference to it. At that point, you're creating a singleton and reducing the flexibility of the module. You now have all the usual issues with globals and singletons. Unless of course you're passing around structures containing a handle and pointer to its manager...
- fsloth 8y agoThere is nothing wrong with globals and singletons. They are very good patterns. Some areas of code deal with global state, and there is no sense in pretending it is not so. On the other hand some other areas definetly need a closure of their own (like a workload dumped to a worker thread). The usual issues with globals pop up when people whant to run shortcuts and never implement the data separation from global to local when they should, think they can be used to replace a database like system, or - the horrors - use some multi level stack based system to push and pop global state as they merrily progress along the call stack - i.e run computations while using the global state as some sort of auxiliary storage unit.
- AstralStorm 8y agoGenerally it is better to have a context with a well defined lifetime rather than the other two options. Additionally thread safety puts very stringent requirements on global state, further making singletons and language goals worthless with few exceptions.