storage_databases.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "storage/cache/storage_cache_database.h"
  9. #include "base/binary_guard.h"
  10. namespace Storage {
  11. namespace Cache {
  12. namespace details {
  13. struct Settings;
  14. } // namespace details
  15. class Database;
  16. } // namespace Cache
  17. class Databases;
  18. class DatabasePointer {
  19. public:
  20. DatabasePointer(const DatabasePointer &other) = delete;
  21. DatabasePointer(DatabasePointer &&other);
  22. DatabasePointer &operator=(const DatabasePointer &other) = delete;
  23. DatabasePointer &operator=(DatabasePointer &&other);
  24. ~DatabasePointer();
  25. Cache::Database *get() const;
  26. Cache::Database &operator*() const;
  27. Cache::Database *operator->() const;
  28. explicit operator bool() const;
  29. private:
  30. friend class Databases;
  31. DatabasePointer(
  32. not_null<Databases*> owner,
  33. const std::unique_ptr<Cache::Database> &value);
  34. void destroy();
  35. Cache::Database *_value = nullptr;
  36. not_null<Databases*> _owner;
  37. };
  38. class Databases {
  39. public:
  40. DatabasePointer get(
  41. const QString &path,
  42. const Cache::details::Settings &settings);
  43. private:
  44. friend class DatabasePointer;
  45. struct Kept {
  46. Kept(std::unique_ptr<Cache::Database> &&database);
  47. std::unique_ptr<Cache::Database> database;
  48. base::binary_guard destroying;
  49. };
  50. void destroy(Cache::Database *database);
  51. std::map<QString, Kept> _map;
  52. };
  53. } // namespace Storage