~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/base/atom.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2010 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
// Author: jmarantz@google.com (Joshua Marantz)
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_BASE_ATOM_H_
20
 
#define PAGESPEED_KERNEL_BASE_ATOM_H_
21
 
 
22
 
#include <set>
23
 
#include "pagespeed/kernel/base/string_util.h"
24
 
 
25
 
namespace net_instaweb {
26
 
 
27
 
struct CaseFold;
28
 
struct CasePreserve;
29
 
template<class CharTransform> class SymbolTable;
30
 
 
31
 
// Atoms are idempotent representations of strings, created
32
 
// via a symbol table.
33
 
class Atom {
34
 
 public:
35
 
  Atom(const Atom& src) : str_(src.str_) {}
36
 
  Atom();
37
 
  ~Atom() {}  // atoms are memory-managed by SymbolTables.
38
 
 
39
 
  Atom& operator=(const Atom& src) {
40
 
    if (&src != this) {
41
 
      str_ = src.str_;
42
 
    }
43
 
    return *this;
44
 
  }
45
 
 
46
 
  // Returns the address of the canonical StringPiece representing this Atom.
47
 
  // The underlying StringPiece object (and its data) are owned by the
48
 
  // SymbolTable.
49
 
  const StringPiece* Rep() const { return str_; }
50
 
 
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_;
55
 
  }
56
 
 
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_;
61
 
  }
62
 
 
63
 
  // SymbolTable is a friend of Symbol because SymbolTable is the
64
 
  // only class that has the right to construct a new Atom from
65
 
  // a StringPiece*.
66
 
  friend class SymbolTable<CaseFold>;
67
 
  friend class SymbolTable<CasePreserve>;
68
 
 
69
 
 private:
70
 
  explicit Atom(const StringPiece* str) : str_(str) {}
71
 
  const StringPiece* str_;
72
 
};
73
 
 
74
 
// Once interned, Atoms are very cheap to put in a set, using
75
 
// pointer-comparison.
76
 
struct AtomCompare {
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();
81
 
  }
82
 
};
83
 
 
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;
87
 
 
88
 
}  // namespace net_instaweb
89
 
 
90
 
#endif  // PAGESPEED_KERNEL_BASE_ATOM_H_