0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FlyweightString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
25 
26 #ifndef HYPERTABLE_FLYWEIGHTSTRING_H
27 #define HYPERTABLE_FLYWEIGHTSTRING_H
28 
29 #include <set>
30 #include <string>
31 
33 #include "StringExt.h"
34 
35 namespace Hypertable {
36 
39 
44  public:
45 
48  }
49 
54 
62  const char *get(const char *str) {
63  if (str == 0)
64  return 0;
65  CstrSet::iterator iter = m_strings.find(str);
66  if (iter == m_strings.end())
67  iter = m_strings.insert(m_arena.dup(str)).first;
68  return *iter;
69  }
70 
78  const char *get(const std::string& str) {
79  CstrSet::iterator iter = m_strings.find(str.c_str());
80  if (iter == m_strings.end())
81  iter = m_strings.insert(m_arena.dup(str)).first;
82  return *iter;
83  }
84 
93  const char *get(const char *str, size_t len) {
94  if (str == 0)
95  return 0;
96  CstrSet::iterator iter = m_strings.find(str);
97  if (iter == m_strings.end())
98  iter = m_strings.insert(m_arena.dup(str, len)).first;
99  return *iter;
100  }
101 
105  void clear() {
106  m_strings.clear();
107  m_arena.free();
109  }
110 
111  private:
113  typedef std::set<const char*, LtCstr, CstrSetAlloc> CstrSet;
114 
117 
119  CstrSet m_strings;
120  };
121 
123 
124 }
125 
126 #endif // HYPERTABLE_FLYWEIGHTSTRING_H
std::set< const char *, LtCstr, CstrSetAlloc > CstrSet
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
~FlyweightString()
The destructor deletes all internal pointers and clears the set; pointers retrieved with...
void free()
Free the whole arena.
Definition: PageArena.h:312
PageArena memory allocator for STL classes.
The PageArenaAllocator is a STL allocator based on PageArena.
The Flyweight string set stores duplicate strings efficiently.
PageArenaAllocator< const char * > CstrSetAlloc
CharT * dup(const CharT *s)
Duplicate a null terminated string; memory is allocated from the pool.
Definition: PageArena.h:274
STL Strict Weak Ordering for comparing c-style strings.
Definition: StringExt.h:45
The PageArena allocator is simple and fast, avoiding individual mallocs/frees.
Definition: PageArena.h:69
Hypertable definitions
void clear()
Clears and deallocates the set of strings.
String extensions and helpers: sets, maps, append operators etc.
CstrSet m_strings
The std::set holding the strings.
CharArena m_arena
The page arena.