KiCad PCB EDA Suite
Loading...
Searching...
No Matches
conn_cache.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <cstdint>
23#include <limits>
24#include <map>
25#include <memory>
26#include <stdexcept>
27#include <utility>
28
29namespace SCH_CONNECTIVITY
30{
37{
38public:
39 CACHE_VERSIONS() = default;
40 CACHE_VERSIONS( const CACHE_VERSIONS& ) = delete;
42
43 uint64_t Next()
44 {
45 if( m_latest == std::numeric_limits<uint64_t>::max() )
46 throw std::overflow_error( "Connectivity cache versions exhausted" );
47
48 return ++m_latest;
49 }
50
51private:
52 uint64_t m_latest = 0;
53};
54
55
59template <typename KEY, typename VALUE, typename LESS = std::less<KEY>>
61{
62public:
63 struct ENTRY
64 {
66 uint64_t version;
67 };
68
69 explicit CACHE_TABLE( CACHE_VERSIONS& aVersions, LESS aLess = {} ) :
70 m_versions( aVersions ),
71 m_entries( aLess )
72 {
73 }
74 CACHE_TABLE( const CACHE_TABLE& ) = delete;
75 CACHE_TABLE& operator=( const CACHE_TABLE& ) = delete;
76
80 const ENTRY* Find( const KEY& aKey ) const { return Find<KEY>( aKey ); }
81
82 template <typename LOOKUP_KEY>
83 const ENTRY* Find( const LOOKUP_KEY& aKey ) const
84 {
85 auto it = m_entries.find( aKey );
86 return it == m_entries.end() ? nullptr : it->second.get();
87 }
88
89 const ENTRY& Set( const KEY& aKey, VALUE aValue )
90 {
91 auto it = m_entries.find( aKey );
92
93 if( it != m_entries.end() && it->second->value == aValue )
94 return *it->second;
95
96 const uint64_t version = m_versions.Next();
97 // Construct before replacement so a failed value move cannot retain an obsolete version.
98 auto entry = std::make_unique<const ENTRY>( ENTRY{ std::move( aValue ), version } );
99 return *m_entries.insert_or_assign( aKey, std::move( entry ) ).first->second;
100 }
101
102 void Erase( const KEY& aKey ) { m_entries.erase( aKey ); }
103 void Clear() { m_entries.clear(); }
104
105 const auto& Entries() const { return m_entries; }
106
107private:
109 std::map<KEY, std::unique_ptr<const ENTRY>, LESS> m_entries;
110};
111} // namespace SCH_CONNECTIVITY
CACHE_TABLE(const CACHE_TABLE &)=delete
std::map< KEY, std::unique_ptr< const ENTRY >, LESS > m_entries
Definition conn_cache.h:109
const ENTRY * Find(const LOOKUP_KEY &aKey) const
Definition conn_cache.h:83
const auto & Entries() const
Definition conn_cache.h:105
CACHE_VERSIONS & m_versions
Definition conn_cache.h:108
CACHE_TABLE & operator=(const CACHE_TABLE &)=delete
const ENTRY & Set(const KEY &aKey, VALUE aValue)
Definition conn_cache.h:89
void Erase(const KEY &aKey)
Definition conn_cache.h:102
CACHE_TABLE(CACHE_VERSIONS &aVersions, LESS aLess={})
Definition conn_cache.h:69
const ENTRY * Find(const KEY &aKey) const
Entry references survive unchanged writes, but not replacement or erasure of their key.
Definition conn_cache.h:80
One sequence for all cache tables for the lifetime of an engine session.
Definition conn_cache.h:37
CACHE_VERSIONS(const CACHE_VERSIONS &)=delete
CACHE_VERSIONS & operator=(const CACHE_VERSIONS &)=delete
Value keys and the key session of the schematic connectivity engine.
@ VALUE
Field Value of part, i.e. "3.3K".