aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/repo/document_type_repo_factory.h
blob: d3ed0358fd14384c69162c15adee26a752d55bd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <memory>
#include <mutex>
#include <map>
#include <vespa/document/config/documenttypes_config_fwd.h>

namespace document {

class DocumentTypeRepo;

/*
 * Factory class for document type repos. Same instance is returned
 * for equal config.
 */
class DocumentTypeRepoFactory {
    struct DocumentTypeRepoEntry {
        std::weak_ptr<const DocumentTypeRepo> repo;
        std::unique_ptr<const DocumenttypesConfig> config;

        DocumentTypeRepoEntry(std::weak_ptr<const DocumentTypeRepo> repo_in,
                      std::unique_ptr<const DocumenttypesConfig> config_in)
            : repo(std::move(repo_in)),
              config(std::move(config_in))
        {
        }
        DocumentTypeRepoEntry(DocumentTypeRepoEntry &&) = default;
        ~DocumentTypeRepoEntry();
    };
    using DocumentTypeRepoMap = std::map<const void *, DocumentTypeRepoEntry>;
    class Deleter;

    static std::mutex _mutex;
    static DocumentTypeRepoMap _repos;

    static void deleteRepo(DocumentTypeRepo *repoRawPtr) noexcept;
public:
    /*
     * Since same instance is returned for equal config, we return a shared
     * pointer to a const repo.  The repo should be considered immutable.
     */
    static std::shared_ptr<const DocumentTypeRepo> make(const DocumenttypesConfig &config);
    static bool empty();
};

}