Pavel
Igor🇺🇦
Roy
Example, i have unordered_map where i storage shared_ptr in value, i'm try find key, if key not found i need return shared_ptr<> ()
Roy
Igor🇺🇦
Roy
If I'm sure that I need a read-only object at the moment, then I don't need to increase the counter and waste performance
Pavel
It can happen even during the callstack when it returned (ok, not exactly like this)
Roy
Roy
this is a specific task: return an object by reference if it exists, or create an empty shared_ptr and return it so that the user can check if it is empty
Alex
Roy
Roy
i can use pointer in this situation, but i like reference design
Roy
so you can use shared_ptr in your classes and if you reload your resources you not lost your data
Igor🇺🇦
Pavel
Roy
example
auto object = storage.get<type>(); // get method return shared_ptr by ref
Pavel
Roy
i mean user know what doit .reset method in shared_ptr or if you take it by reference and set new value
Roy
sorry my english in low level
Roy
hmm, wait, i have idea, maybe create method Has and return iterator on object from this method?
Roy
and use context
if(const auto it = HasResource<Type>(); it) {
auto& resource = GetResource<Type>(it);
}
Igor🇺🇦
Roy
Roy
just i'm stuck in ass situation
Roy
Wait i'm upload my code on pastebin
Igor🇺🇦
always
But you said that in some situations you want to return reference and in some value. You can't have both because it the same method signature from C++ perspective
Igor🇺🇦
and anyway - if an atomic increment is such a significant performance penalty return raw pointers.
Roy
Yes, the point is that I envisioned a design that is based only on references.
The Has method is used to check the validity of objects, however, this calls the .find method in the map.
Here example
if(HasResource(key)) {
auto& resource = GetResource(key);
}
The problem is that the class that stores the resources is based on unordered_map.
HasResource method calls .find, and GetResource uses .find
It turns out two calls to .find, which is a waste of performance.
Igor🇺🇦
Roy
Igor🇺🇦
Pavel
Roy
Igor🇺🇦
Why does it matter? Use reference if you wish show
Roy
or it's bad practice return shared_ptr by reference from objects pool
Pavel
Yea, if your code is bad or you remove resources and want use it
I think you can make 2 functions:
One returns a raw pointer (nullptr if nothing found), the second is returning shared_ptr by value (shared_ptr<T>() if nothing found).
First one should be used if you don't want to store a pointer to the resource, the second if you want to store a pointer to it.
The first function gives the minimal overhead.
The second one gives overhead only in the case when the object was found but the caller still decided that (s)he doesn't want to store it.
Alex
Roy
Roy
Roy
Example, you load file config.json in memory and add all resources in class, after you want reload this file resources again, you extrace shared_ptr from storage class and try reload files, if reload config file failed you can push your extracted shared_ptr again in class
Roy
Alex
Igor🇺🇦
Pavel
Roy
Pavel
Ah, wait, auto, nevermind
Roy
but how check has object or not? what is in map not exist your resource?
Roy
Ok, wait, let me put my code on pastebin
Igor🇺🇦
This is what I'm doing now
std::shared_ptr<std::string>& foo() {
static auto value = std::make_shared<std::string>();
return value;
}
void main() {
auto& object = foo();
if (!object) return;
}
Roy
Roy
i mean, your counter in shared_ptr increased, test it
Pavel
Pavel
Or it's intended without it?
Roy
and you both forgot the & in the return value?
No, you not understand, i need check exist resource or not and after get resource.
But for check exist resource or not you need call unordered_map.find method and if your resource exist you need call get resource method, but he to call .find method, so it's call .find twice
Igor🇺🇦
Pavel
Igor🇺🇦
Roy
https://pastebin.com/vmwvNEkR
Ricardo
Sorry. Please i have a problen with my menu program. I had to do three class ( personal, employer and compte) and let choose by the user any class he want to open and modify it.
Roy
Example usage
struct Item {};
auto& storage = ResourceManager::CreateStorage<Item>();
storage.AddResource(100);
storage.AddResource(200);
if(storage.HasResource(100)) {
auto& resource = storage.GetResource(100);
}
Roy
Soo what problem? Problem in HasResource and GetResource, call .find method twice in unordered_map, it's small overhead but if you iterate in big storage it's not small overhead
Roy
Example, if you have storage where you have 1m resources, you call .find 2m times
Igor🇺🇦
Roy