// // shared_cache.h // serializer // // Created by Sam Jaffe on 3/15/23. // #pragma once #include #include namespace serializer { class SharedCache { public: using Key = std::string; template using Record = std::shared_ptr; protected: struct Impl { virtual ~Impl() = default; }; private: std::unique_ptr p_impl = std::make_unique(); public: SharedCache() = default; /** * @brief Store an object within this cache, returning a pointer to the * cached object. In order to be nullptr-safe, the unspecialized version * of this function returns a shared_ptr containing a copy of value. * Specialized versions exist in shared_cache.cxx, which actually perform * caching. * @param key The name of the value, for looking up later * @param value The value to store within this cache * @return A shared_ptr containing the cached value */ template Record store(Key const & key, T const & value) { return std::make_shared(value); } /** * @brief Fetch an object that was cached within this object. * @param key The name of the cached object, as called in store() * @return A shared_ptr containing the requested object, or null if it isn't * found in this cache. * @throws std::domain_error if no specialization is defined */ template Record fetch(Key const & key) const { throw std::domain_error(typeid(T).name()); } protected: template SharedCache(std::unique_ptr ptr) : p_impl(std::move(ptr)) {} }; }