#pragma once #include #include #include #include #include #include #include namespace jvalidate { template class DocumentCache { public: using value_type = typename A::value_type; private: URIResolver resolve_; std::map references_; std::map cache_; public: DocumentCache() = default; DocumentCache(URIResolver const & resolve) : resolve_(resolve) {} void cache_reference(URI const & where, A const & handle) { EXPECT(references_.emplace(where, handle).second); } operator bool() const { return resolve_; } std::optional try_load(detail::Reference const & ref) { if (auto it = references_.find(ref.uri()); it != references_.end()) { return ref.pointer().walk(it->second); } if (not resolve_) { return std::nullopt; } auto [it, created] = cache_.try_emplace(ref.uri()); if (created && not resolve_(ref.uri(), it->second)) { cache_.erase(it); return std::nullopt; } return ref.pointer().walk(A(it->second)); } }; }