/proc/self/cwd/pagespeed/kernel/base/atom.h
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one |
3 | | * or more contributor license agreements. See the NOTICE file |
4 | | * distributed with this work for additional information |
5 | | * regarding copyright ownership. The ASF licenses this file |
6 | | * to you under the Apache License, Version 2.0 (the |
7 | | * "License"); you may not use this file except in compliance |
8 | | * with the License. You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, |
13 | | * software distributed under the License is distributed on an |
14 | | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | * KIND, either express or implied. See the License for the |
16 | | * specific language governing permissions and limitations |
17 | | * under the License. |
18 | | */ |
19 | | |
20 | | #ifndef PAGESPEED_KERNEL_BASE_ATOM_H_ |
21 | | #define PAGESPEED_KERNEL_BASE_ATOM_H_ |
22 | | |
23 | | #include <set> |
24 | | |
25 | | #include "pagespeed/kernel/base/string_util.h" |
26 | | |
27 | | namespace net_instaweb { |
28 | | |
29 | | struct CaseFold; |
30 | | struct CasePreserve; |
31 | | template <class CharTransform> |
32 | | class SymbolTable; |
33 | | |
34 | | // Atoms are idempotent representations of strings, created |
35 | | // via a symbol table. |
36 | | class Atom { |
37 | | public: |
38 | 0 | Atom(const Atom& src) : str_(src.str_) {} |
39 | | Atom(); |
40 | 6.47M | ~Atom() {} // atoms are memory-managed by SymbolTables. |
41 | | |
42 | 0 | Atom& operator=(const Atom& src) { |
43 | 0 | if (&src != this) { |
44 | 0 | str_ = src.str_; |
45 | 0 | } |
46 | 0 | return *this; |
47 | 0 | } |
48 | | |
49 | | // Returns the address of the canonical StringPiece representing this Atom. |
50 | | // The underlying StringPiece object (and its data) are owned by the |
51 | | // SymbolTable. |
52 | 6.47M | const StringPiece* Rep() const { return str_; } |
53 | | |
54 | | // This is comparing the underlying StringPiece pointers. It is invalid |
55 | | // to compare Atoms from different symbol tables. |
56 | 0 | bool operator==(const Atom& sym) const { return str_ == sym.str_; } |
57 | | |
58 | | // This is comparing the underlying StringPiece pointers. It is invalid |
59 | | // to compare Atoms from different symbol tables. |
60 | 0 | bool operator!=(const Atom& sym) const { return str_ != sym.str_; } |
61 | | |
62 | | // SymbolTable is a friend of Symbol because SymbolTable is the |
63 | | // only class that has the right to construct a new Atom from |
64 | | // a StringPiece*. |
65 | | friend class SymbolTable<CaseFold>; |
66 | | friend class SymbolTable<CasePreserve>; |
67 | | |
68 | | private: |
69 | 6.47M | explicit Atom(const StringPiece* str) : str_(str) {} |
70 | | const StringPiece* str_; |
71 | | }; |
72 | | |
73 | | // Once interned, Atoms are very cheap to put in a set, using |
74 | | // pointer-comparison. |
75 | | struct AtomCompare { |
76 | 0 | bool operator()(const Atom& a1, const Atom& a2) const { |
77 | 0 | // Compares pointers. Note that this assumes we don't overlap the |
78 | 0 | // StringPiece's, which is the case for the implementation of SymbolTable. |
79 | 0 | return a1.Rep()->data() < a2.Rep()->data(); |
80 | 0 | } |
81 | | }; |
82 | | |
83 | | // A set of atoms can be constructed very efficiently. Note that |
84 | | // iteration over this set will *not* be in alphabetical order. |
85 | | using AtomSet = std::set<Atom, AtomCompare>; |
86 | | |
87 | | } // namespace net_instaweb |
88 | | |
89 | | #endif // PAGESPEED_KERNEL_BASE_ATOM_H_ |