lifetype of make_storage return instance
I am little confused about lifetype and usage of make_storage return instance.
- Is it supposed to be singleton? If yes, how can I open another connection to DB(for long operations) while keeping the instance active for small queries?
- What is best way from performance and memory perspective? Creating storage instance as per need or keeping it stored it statically?
- Is there any way to store the storage instance as class member?
- Not exactly singleton but the storage instance is designed to live as long as you need it
- Create one storage per one sqlite db, sync schema right after (optional), not one per call.
- Yes. The best way it to
typedefa storage type bydecltypeof a return type of function that makes a storage. You can see example here
One more tip: initStorage gotta be inline if you want to include a header with it in several places
Thanks. I shall experiement it and decide. Do you how much memory Storage object takes? How much costly is contruction and destruction of it?
@Ninja-007 storage is very light. It holds data you passed in make_storage call - member pointers, column/table names and table constraints objects, also one bool indicating whether 'inmemory' db and one std::shared_ptr<database_connection>
@Ninja-007 is the question still actual?
No need of further help. So closing it. Thanks for help.
Is it possible to return make_storage as pointer? It can be done by using make_storage_ptr. I want to write class method which can call make_storage and return storage object. As return type is not known, it causes complier errors with auto keyword.
I found trick to do that without dealing with pointers.
The class member declaration should be class XYZ { decltype(auto) GetDB() const; decltype(auto) GetStaticDB() const; };
in cpp of the class is decltype(auto) XYZ::GetDB() const { return make_storage(GetDBPath(), ...); } decltype(auto) XYZ::GetStaticDB() const { static auto DB = GetDB(); return DB; }
May be you can document it, it will be great for other users. I keep it open, so you can see it easily. You may close it.
Pointers won't help you with type definitions. If you need an explicit storage type used trick from update.cpp
Thanks. My problem was creating make_storage equivalent function in class. As its return type is not know till runtime or hard to write at compile time, I was looking for pointer alternative. But it was solved by using decltype(auto) as return type. Compiler calculates it. The update example also have typedef and dummy call Initstorage function. I cannot do it in class defination.
@Ninja-007 ok. Is the question still actual?
IMHO storage got to be located in a separate .hpp (and optionally .cpp) file located in Services folder that contains project's services. This header may contain initStorage function and custom types bindings if they exist.
Issue is solved. Thanks.