2
* Copyright 2010 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
#ifndef PAGESPEED_KERNEL_BASE_ATOM_H_
20
#define PAGESPEED_KERNEL_BASE_ATOM_H_
23
#include "pagespeed/kernel/base/string_util.h"
25
namespace net_instaweb {
29
template<class CharTransform> class SymbolTable;
31
// Atoms are idempotent representations of strings, created
32
// via a symbol table.
35
Atom(const Atom& src) : str_(src.str_) {}
37
~Atom() {} // atoms are memory-managed by SymbolTables.
39
Atom& operator=(const Atom& src) {
46
// Returns the address of the canonical StringPiece representing this Atom.
47
// The underlying StringPiece object (and its data) are owned by the
49
const StringPiece* Rep() const { return str_; }
51
// This is comparing the underlying StringPiece pointers. It is invalid
52
// to compare Atoms from different symbol tables.
53
bool operator==(const Atom& sym) const {
54
return str_ == sym.str_;
57
// This is comparing the underlying StringPiece pointers. It is invalid
58
// to compare Atoms from different symbol tables.
59
bool operator!=(const Atom& sym) const {
60
return str_ != sym.str_;
63
// SymbolTable is a friend of Symbol because SymbolTable is the
64
// only class that has the right to construct a new Atom from
66
friend class SymbolTable<CaseFold>;
67
friend class SymbolTable<CasePreserve>;
70
explicit Atom(const StringPiece* str) : str_(str) {}
71
const StringPiece* str_;
74
// Once interned, Atoms are very cheap to put in a set, using
75
// pointer-comparison.
77
bool operator()(const Atom& a1, const Atom& a2) const {
78
// Compares pointers. Note that this assumes we don't overlap the
79
// StringPiece's, which is the case for the implementation of SymbolTable.
80
return a1.Rep()->data() < a2.Rep()->data();
84
// A set of atoms can be constructed very efficiently. Note that
85
// iteration over this set will *not* be in alphabetical order.
86
typedef std::set<Atom, AtomCompare> AtomSet;
88
} // namespace net_instaweb
90
#endif // PAGESPEED_KERNEL_BASE_ATOM_H_