Debian Codegraph @

FaCT++.Python/lib/Kernel/tNameSet.h

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef TNAMESET_H
21 #define TNAMESET_H
22
23 #include <string>
24 #include <map>
25
26 /// base class for creating Named Entries; template parameter should be derived from TNamedEntry
27 template <typename T>
28 class TNameCreator
29 {
30 public: // interface
31 /// empty c'tor
32 TNameCreator() = default;
33 /// empty d'tor
34 virtual ~TNameCreator() = default;
35
36 /// create new Named Entry
37 virtual T* makeEntry ( const std::string& name ) const { return new T(name); }
38 }; // TNameCreator
39
40
41 /// Implementation of NameSets by binary trees; template parameter should be derived from TNamedEntry
42 template <typename T>
43 class TNameSet
44 {
45 protected: // types
46 /// base type
47 typedef std::map <std::string, T*> NameTree;
48
49 protected: // members
50 /// Base holding all names
51 NameTree Base;
52 /// creator of new name
53 TNameCreator<T>* Creator;
54
55 public: // interface
56 /// c'tor (empty)
57 TNameSet() : Creator(new TNameCreator<T>) {}
58 /// c'tor (with given Name Creating class)
59 explicit TNameSet ( TNameCreator<T>* p ) : Creator(p) {}
60 /// no copy c'tor
61 TNameSet ( const TNameSet& ) = delete;
62 /// no assignment
63 TNameSet& operator = ( const TNameSet& ) = delete;
64 /// d'tor (delete all entries)
65 virtual ~TNameSet() { clear(); delete Creator; }
66
67 /// return pointer to existing id or NULL if no such id defined
68 T* get ( const std::string& id ) const
69 {
70 auto p = Base.find(id);
71 return p == Base.end() ? nullptr : p->second;
72 }
73 /// unconditionally add new element with name ID to the set; return new element
74 T* add ( const std::string& id )
75 {
76 T* pne = Creator->makeEntry(id);
77 Base[id] = pne;
78 return pne;
79 }
80 /// Insert id to the nameset (if necessary); @return pointer to id structure created by external creator
81 T* insert ( const std::string& id )
82 {
83 T* pne = get(id);
84 if ( pne == nullptr ) // no such Id
85 pne = add(id);
86 return pne;
87 }
88 /// remove given entry from the set
89 void remove ( const std::string& id )
90 {
91 auto p = Base.find(id);
92
93 if ( p != Base.end () ) // found such Id
94 {
95 delete p->second;
96 Base.erase(p);
97 }
98 }
99 /// clear name set
100 void clear ( void )
101 {
102 for ( auto& p: Base )
103 delete p.second;
104
105 Base.clear();
106 }
107 /// clear the Entry field in all entities
108 template <typename U>
109 friend void clearEntriesCache ( TNameSet<U>& ns );
110 /// get size of a name set
111 size_t size ( void ) const { return Base.size(); }
112 }; // TNameSet
113
114 /// clear the Entry field in all entities
115 /// work only for T derived from TNamedEntity
116 template <typename T>
117 void clearEntriesCache ( TNameSet<T>& ns )
118 {
119 for ( auto& bind: ns.Base )
120 bind.second->setEntry(nullptr);
121 }
122
123 #endif