Coverage Report

Created: 2025-06-22 06:59

/src/gdal/ogr/ogr_proj_p.h
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Project:  GDAL
4
 * Purpose:  Private header
5
 * Author:   Even Rouault <even dot rouault at spatialys dot com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#ifndef OGR_PROJ_P_H_INCLUDED
14
#define OGR_PROJ_P_H_INCLUDED
15
16
#include "proj.h"
17
18
#include "cpl_mem_cache.h"
19
20
#include <unordered_map>
21
#include <memory>
22
#include <utility>
23
24
/*! @cond Doxygen_Suppress */
25
26
PJ_CONTEXT CPL_DLL *OSRGetProjTLSContext();
27
void OSRCleanupTLSContext();
28
29
class OSRProjTLSCache
30
{
31
    struct OSRPJDeleter
32
    {
33
        void operator()(PJ *pj) const
34
0
        {
35
0
            proj_destroy(pj);
36
0
        }
37
    };
38
39
    typedef std::unique_ptr<PJ, OSRPJDeleter> UniquePtrPJ;
40
41
    struct EPSGCacheKey
42
    {
43
        int nCode_;
44
        bool bUseNonDeprecated_;
45
        bool bAddTOWGS84_;
46
47
        EPSGCacheKey(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84)
48
0
            : nCode_(nCode), bUseNonDeprecated_(bUseNonDeprecated),
49
0
              bAddTOWGS84_(bAddTOWGS84)
50
0
        {
51
0
        }
52
53
        bool operator==(const EPSGCacheKey &other) const
54
0
        {
55
0
            return nCode_ == other.nCode_ &&
56
0
                   bUseNonDeprecated_ == other.bUseNonDeprecated_ &&
57
0
                   bAddTOWGS84_ == other.bAddTOWGS84_;
58
0
        }
59
    };
60
61
    struct EPSGCacheKeyHasher
62
    {
63
        std::size_t operator()(const EPSGCacheKey &k) const
64
0
        {
65
0
            return k.nCode_ | ((k.bUseNonDeprecated_ ? 1 : 0) << 16) |
66
0
                   ((k.bAddTOWGS84_ ? 1 : 0) << 17);
67
0
        }
68
    };
69
70
    PJ_CONTEXT *m_tlsContext =
71
        nullptr;  // never use it directly. use GetPJContext()
72
    lru11::Cache<EPSGCacheKey, UniquePtrPJ, lru11::NullLock,
73
                 std::unordered_map<EPSGCacheKey,
74
                                    typename std::list<lru11::KeyValuePair<
75
                                        EPSGCacheKey, UniquePtrPJ>>::iterator,
76
                                    EPSGCacheKeyHasher>>
77
        m_oCacheEPSG{};
78
    lru11::Cache<std::string, UniquePtrPJ> m_oCacheWKT{};
79
80
    PJ_CONTEXT *GetPJContext();
81
82
    OSRProjTLSCache(const OSRProjTLSCache &) = delete;
83
    OSRProjTLSCache &operator=(const OSRProjTLSCache &) = delete;
84
85
  public:
86
0
    explicit OSRProjTLSCache(PJ_CONTEXT *tlsContext) : m_tlsContext(tlsContext)
87
0
    {
88
0
    }
89
90
    void clear();
91
92
    PJ *GetPJForEPSGCode(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84);
93
    void CachePJForEPSGCode(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84,
94
                            PJ *pj);
95
96
    PJ *GetPJForWKT(const std::string &wkt);
97
    void CachePJForWKT(const std::string &wkt, PJ *pj);
98
};
99
100
OSRProjTLSCache *OSRGetProjTLSCache();
101
102
void OGRCTDumpStatistics();
103
104
void OSRCTCleanCache();
105
106
/*! @endcond Doxygen_Suppress */
107
108
#endif