| 123456789101112131415161718192021222324252627282930313233343536373839 |
- #pragma once
- #include <map>
- #include <optional>
- #include <jvalidate/detail/expect.h>
- #include <jvalidate/forward.h>
- #include <jvalidate/uri.h>
- namespace jvalidate {
- template <Adapter A> class DocumentCache {
- public:
- using value_type = typename A::value_type;
- private:
- URIResolver<A> resolve_;
- std::map<URI, value_type> cache_;
- public:
- DocumentCache() = default;
- DocumentCache(URIResolver<A> const & resolve) : resolve_(resolve) {}
- operator bool() const { return resolve_; }
- std::optional<A> try_load(URI const & uri) {
- if (not resolve_) {
- return std::nullopt;
- }
- auto [it, created] = cache_.try_emplace(uri);
- if (created && not resolve_(uri, it->second)) {
- cache_.erase(it);
- return std::nullopt;
- }
- return A(it->second);
- }
- };
- }
|