Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternatives to std::make_shared #58

Open
DeveloperPaul123 opened this issue Oct 17, 2023 · 4 comments
Open

Alternatives to std::make_shared #58

DeveloperPaul123 opened this issue Oct 17, 2023 · 4 comments

Comments

@DeveloperPaul123
Copy link

Would it be possible to add the ability to create instances with something other than std::make_shared. Some of our objects require a custom deleter and it seems that we cannot inject the deleter when registering types.

Is there a way to accomplish this now by inheriting from a container builder?

@judgeaxl
Copy link

What you would do is to register a factory that returns the pointer initialized the way you want it to be using builder.registerInstanceFactory. There's an example on the wiki: https://github.com/ybainier/Hypodermic/wiki/Registering-components#register-a-factory

@DeveloperPaul123
Copy link
Author

We're aware of this feature, but it forces us to have to explicitly pass all the necessary parameters to the type (via context.resolve<>) when constructing it.

Is there a way to instead change the default to something else other than std::make_shared and benefit from the auto-wiring and just use registerType()?

@judgeaxl
Copy link

I see what you're getting at. I imagine it would be possible to add something like a .withDeleter(x) when registering a type. That feels like the best fit to the existing API instead of replacing std::make_shared wholesale. However, I'm not a maintainer and while I have dug very deep into this library for various reasons, I think it would probably take a bit of work to add it.

@judgeaxl
Copy link

Actually, I looked back at the logic for resolving arguments automatically, and you'd have to provide both the deleter, and allocator, or a method that does both, so just .withDeleter would not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants