cld 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +27 -0
- data/Manifest +106 -0
- data/README.rdoc +173 -0
- data/Rakefile +15 -0
- data/base/basictypes.h +348 -0
- data/base/build_config.h +115 -0
- data/base/casts.h +156 -0
- data/base/commandlineflags.h +443 -0
- data/base/crash.h +41 -0
- data/base/dynamic_annotations.h +358 -0
- data/base/global_strip_options.h +59 -0
- data/base/log_severity.h +46 -0
- data/base/logging.h +1403 -0
- data/base/macros.h +243 -0
- data/base/port.h +54 -0
- data/base/scoped_ptr.h +428 -0
- data/base/stl_decl.h +0 -0
- data/base/stl_decl_msvc.h +107 -0
- data/base/string_util.h +29 -0
- data/base/strtoint.h +93 -0
- data/base/template_util.h +96 -0
- data/base/type_traits.h +198 -0
- data/base/vlog_is_on.h +143 -0
- data/build.sh +48 -0
- data/build.win.cmd +28 -0
- data/cld.gemspec +30 -0
- data/cld_encodings.h +95 -0
- data/encodings/compact_lang_det/#cldutil.cc# +905 -0
- data/encodings/compact_lang_det/#cldutil.h# +1205 -0
- data/encodings/compact_lang_det/#compact_lang_det_impl.h# +171 -0
- data/encodings/compact_lang_det/#ext_lang_enc.cc# +545 -0
- data/encodings/compact_lang_det/#ext_lang_enc.h# +119 -0
- data/encodings/compact_lang_det/#getonescriptspan.cc# +570 -0
- data/encodings/compact_lang_det/#getonescriptspan.h# +131 -0
- data/encodings/compact_lang_det/#tote.cc# +299 -0
- data/encodings/compact_lang_det/#tote.h# +89 -0
- data/encodings/compact_lang_det/cldutil.cc +905 -0
- data/encodings/compact_lang_det/cldutil.h +1205 -0
- data/encodings/compact_lang_det/cldutil_dbg.h +76 -0
- data/encodings/compact_lang_det/cldutil_dbg_empty.cc +76 -0
- data/encodings/compact_lang_det/compact_lang_det.cc +62 -0
- data/encodings/compact_lang_det/compact_lang_det.h +145 -0
- data/encodings/compact_lang_det/compact_lang_det_impl.cc +2574 -0
- data/encodings/compact_lang_det/compact_lang_det_impl.h +173 -0
- data/encodings/compact_lang_det/compact_lang_det_unittest_small.cc +406 -0
- data/encodings/compact_lang_det/compile.cmd +1 -0
- data/encodings/compact_lang_det/ext_lang_enc.cc +545 -0
- data/encodings/compact_lang_det/ext_lang_enc.h +119 -0
- data/encodings/compact_lang_det/generated/cld_generated_score_deltaoctachrome_0406.cc +380 -0
- data/encodings/compact_lang_det/generated/cld_generated_score_quadchrome_0406.cc +382 -0
- data/encodings/compact_lang_det/generated/compact_lang_det_generated_cjkbis_0.cc +49 -0
- data/encodings/compact_lang_det/generated/compact_lang_det_generated_ctjkvz.cc +7119 -0
- data/encodings/compact_lang_det/generated/compact_lang_det_generated_ctjkvz_0.cc +61 -0
- data/encodings/compact_lang_det/generated/compact_lang_det_generated_deltaoctachrome.cc +1263 -0
- data/encodings/compact_lang_det/generated/compact_lang_det_generated_longwords8_0.cc +53 -0
- data/encodings/compact_lang_det/generated/compact_lang_det_generated_meanscore.h +10 -0
- data/encodings/compact_lang_det/generated/compact_lang_det_generated_quads_0.cc +50 -0
- data/encodings/compact_lang_det/generated/compact_lang_det_generated_quadschrome.cc +70935 -0
- data/encodings/compact_lang_det/getonescriptspan.cc +570 -0
- data/encodings/compact_lang_det/getonescriptspan.h +131 -0
- data/encodings/compact_lang_det/letterscript_enum.cc +117 -0
- data/encodings/compact_lang_det/letterscript_enum.h +99 -0
- data/encodings/compact_lang_det/subsetsequence.cc +259 -0
- data/encodings/compact_lang_det/subsetsequence.h +44 -0
- data/encodings/compact_lang_det/subsetsequence_unittest.cc +99 -0
- data/encodings/compact_lang_det/tote.cc +299 -0
- data/encodings/compact_lang_det/tote.h +89 -0
- data/encodings/compact_lang_det/unittest_data.h +193 -0
- data/encodings/compact_lang_det/utf8propjustletter.h +1162 -0
- data/encodings/compact_lang_det/utf8propletterscriptnum.h +1222 -0
- data/encodings/compact_lang_det/utf8scannotjustletterspecial.h +1185 -0
- data/encodings/compact_lang_det/win/#cld_unilib_windows.cc# +29 -0
- data/encodings/compact_lang_det/win/cld_basictypes.h +10 -0
- data/encodings/compact_lang_det/win/cld_commandlineflags.h +28 -0
- data/encodings/compact_lang_det/win/cld_google.h +18 -0
- data/encodings/compact_lang_det/win/cld_htmlutils.h +13 -0
- data/encodings/compact_lang_det/win/cld_htmlutils_google3.cc +32 -0
- data/encodings/compact_lang_det/win/cld_htmlutils_windows.cc +29 -0
- data/encodings/compact_lang_det/win/cld_logging.h +21 -0
- data/encodings/compact_lang_det/win/cld_macros.h +19 -0
- data/encodings/compact_lang_det/win/cld_strtoint.h +26 -0
- data/encodings/compact_lang_det/win/cld_unicodetext.cc +84 -0
- data/encodings/compact_lang_det/win/cld_unicodetext.h +40 -0
- data/encodings/compact_lang_det/win/cld_unilib.h +15 -0
- data/encodings/compact_lang_det/win/cld_unilib_google3.cc +18 -0
- data/encodings/compact_lang_det/win/cld_unilib_windows.cc +29 -0
- data/encodings/compact_lang_det/win/cld_utf.h +24 -0
- data/encodings/compact_lang_det/win/cld_utf8statetable.cc +224 -0
- data/encodings/compact_lang_det/win/cld_utf8statetable.h +141 -0
- data/encodings/compact_lang_det/win/cld_utf8utils.h +22 -0
- data/encodings/compact_lang_det/win/cld_utf8utils_google3.cc +18 -0
- data/encodings/compact_lang_det/win/cld_utf8utils_windows.cc +17 -0
- data/encodings/compact_lang_det/win/normalizedunicodetext.cc +172 -0
- data/encodings/compact_lang_det/win/normalizedunicodetext.h +67 -0
- data/encodings/internal/encodings.cc +12 -0
- data/encodings/lang_enc.h +254 -0
- data/encodings/proto/encodings.pb.h +169 -0
- data/encodings/public/encodings.h +301 -0
- data/ext/cld/extconf.rb +7 -0
- data/languages/internal/#languages.cc# +337 -0
- data/languages/internal/languages.cc +337 -0
- data/languages/proto/languages.pb.h +179 -0
- data/languages/public/languages.h +379 -0
- data/lib/cld.rb +12 -0
- data/test/test.rb +570 -0
- data/thunk.cc +131 -0
- metadata +168 -0
data/base/macros.h
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file.
|
4
|
+
//
|
5
|
+
// Various Google-specific macros.
|
6
|
+
//
|
7
|
+
// This code is compiled directly on many platforms, including client
|
8
|
+
// platforms like Windows, Mac, and embedded systems. Before making
|
9
|
+
// any changes here, make sure that you're not breaking any platforms.
|
10
|
+
//
|
11
|
+
|
12
|
+
#ifndef BASE_MACROS_H_
|
13
|
+
#define BASE_MACROS_H_
|
14
|
+
|
15
|
+
#include <stddef.h> // For size_t
|
16
|
+
|
17
|
+
#include "base/type_traits.h"
|
18
|
+
|
19
|
+
|
20
|
+
// The COMPILE_ASSERT macro can be used to verify that a compile time
|
21
|
+
// expression is true. For example, you could use it to verify the
|
22
|
+
// size of a static array:
|
23
|
+
//
|
24
|
+
// COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
|
25
|
+
// content_type_names_incorrect_size);
|
26
|
+
//
|
27
|
+
// or to make sure a struct is smaller than a certain size:
|
28
|
+
//
|
29
|
+
// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
|
30
|
+
//
|
31
|
+
// The second argument to the macro is the name of the variable. If
|
32
|
+
// the expression is false, most compilers will issue a warning/error
|
33
|
+
// containing the name of the variable.
|
34
|
+
|
35
|
+
#define COMPILE_ASSERT(expr, msg) \
|
36
|
+
typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
|
37
|
+
|
38
|
+
// Implementation details of COMPILE_ASSERT:
|
39
|
+
//
|
40
|
+
// - COMPILE_ASSERT works by defining an array type that has -1
|
41
|
+
// elements (and thus is invalid) when the expression is false.
|
42
|
+
//
|
43
|
+
// - The simpler definition
|
44
|
+
//
|
45
|
+
// #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
|
46
|
+
//
|
47
|
+
// does not work, as gcc supports variable-length arrays whose sizes
|
48
|
+
// are determined at run-time (this is gcc's extension and not part
|
49
|
+
// of the C++ standard). As a result, gcc fails to reject the
|
50
|
+
// following code with the simple definition:
|
51
|
+
//
|
52
|
+
// int foo;
|
53
|
+
// COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
|
54
|
+
// // not a compile-time constant.
|
55
|
+
//
|
56
|
+
// - By using the type CompileAssert<(bool(expr))>, we ensures that
|
57
|
+
// expr is a compile-time constant. (Template arguments must be
|
58
|
+
// determined at compile-time.)
|
59
|
+
//
|
60
|
+
// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
|
61
|
+
// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
|
62
|
+
//
|
63
|
+
// CompileAssert<bool(expr)>
|
64
|
+
//
|
65
|
+
// instead, these compilers will refuse to compile
|
66
|
+
//
|
67
|
+
// COMPILE_ASSERT(5 > 0, some_message);
|
68
|
+
//
|
69
|
+
// (They seem to think the ">" in "5 > 0" marks the end of the
|
70
|
+
// template argument list.)
|
71
|
+
//
|
72
|
+
// - The array size is (bool(expr) ? 1 : -1), instead of simply
|
73
|
+
//
|
74
|
+
// ((expr) ? 1 : -1).
|
75
|
+
//
|
76
|
+
// This is to avoid running into a bug in MS VC 7.1, which
|
77
|
+
// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
|
78
|
+
|
79
|
+
|
80
|
+
// A macro to disallow the copy constructor and operator= functions
|
81
|
+
// This should be used in the private: declarations for a class
|
82
|
+
//
|
83
|
+
// For disallowing only assign or copy, write the code directly, but declare
|
84
|
+
// the intend in a comment, for example:
|
85
|
+
// void operator=(const TypeName&); // DISALLOW_ASSIGN
|
86
|
+
// Note, that most uses of DISALLOW_ASSIGN and DISALLOW_COPY are broken
|
87
|
+
// semantically, one should either use disallow both or neither. Try to
|
88
|
+
// avoid these in new code.
|
89
|
+
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
90
|
+
TypeName(const TypeName&); \
|
91
|
+
void operator=(const TypeName&)
|
92
|
+
|
93
|
+
// An older, politically incorrect name for the above.
|
94
|
+
// Prefer DISALLOW_COPY_AND_ASSIGN for new code.
|
95
|
+
#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
|
96
|
+
|
97
|
+
// A macro to disallow all the implicit constructors, namely the
|
98
|
+
// default constructor, copy constructor and operator= functions.
|
99
|
+
//
|
100
|
+
// This should be used in the private: declarations for a class
|
101
|
+
// that wants to prevent anyone from instantiating it. This is
|
102
|
+
// especially useful for classes containing only static methods.
|
103
|
+
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
|
104
|
+
TypeName(); \
|
105
|
+
DISALLOW_COPY_AND_ASSIGN(TypeName)
|
106
|
+
|
107
|
+
// The arraysize(arr) macro returns the # of elements in an array arr.
|
108
|
+
// The expression is a compile-time constant, and therefore can be
|
109
|
+
// used in defining new arrays, for example. If you use arraysize on
|
110
|
+
// a pointer by mistake, you will get a compile-time error.
|
111
|
+
//
|
112
|
+
// One caveat is that arraysize() doesn't accept any array of an
|
113
|
+
// anonymous type or a type defined inside a function. In these rare
|
114
|
+
// cases, you have to use the unsafe ARRAYSIZE() macro below. This is
|
115
|
+
// due to a limitation in C++'s template system. The limitation might
|
116
|
+
// eventually be removed, but it hasn't happened yet.
|
117
|
+
|
118
|
+
// This template function declaration is used in defining arraysize.
|
119
|
+
// Note that the function doesn't need an implementation, as we only
|
120
|
+
// use its type.
|
121
|
+
template <typename T, size_t N>
|
122
|
+
char (&ArraySizeHelper(T (&array)[N]))[N];
|
123
|
+
|
124
|
+
// That gcc wants both of these prototypes seems mysterious. VC, for
|
125
|
+
// its part, can't decide which to use (another mystery). Matching of
|
126
|
+
// template overloads: the final frontier.
|
127
|
+
#ifndef COMPILER_MSVC
|
128
|
+
template <typename T, size_t N>
|
129
|
+
char (&ArraySizeHelper(const T (&array)[N]))[N];
|
130
|
+
#endif
|
131
|
+
|
132
|
+
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
|
133
|
+
|
134
|
+
// ARRAYSIZE performs essentially the same calculation as arraysize,
|
135
|
+
// but can be used on anonymous types or types defined inside
|
136
|
+
// functions. It's less safe than arraysize as it accepts some
|
137
|
+
// (although not all) pointers. Therefore, you should use arraysize
|
138
|
+
// whenever possible.
|
139
|
+
//
|
140
|
+
// The expression ARRAYSIZE(a) is a compile-time constant of type
|
141
|
+
// size_t.
|
142
|
+
//
|
143
|
+
// ARRAYSIZE catches a few type errors. If you see a compiler error
|
144
|
+
//
|
145
|
+
// "warning: division by zero in ..."
|
146
|
+
//
|
147
|
+
// when using ARRAYSIZE, you are (wrongfully) giving it a pointer.
|
148
|
+
// You should only use ARRAYSIZE on statically allocated arrays.
|
149
|
+
//
|
150
|
+
// The following comments are on the implementation details, and can
|
151
|
+
// be ignored by the users.
|
152
|
+
//
|
153
|
+
// ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
|
154
|
+
// the array) and sizeof(*(arr)) (the # of bytes in one array
|
155
|
+
// element). If the former is divisible by the latter, perhaps arr is
|
156
|
+
// indeed an array, in which case the division result is the # of
|
157
|
+
// elements in the array. Otherwise, arr cannot possibly be an array,
|
158
|
+
// and we generate a compiler error to prevent the code from
|
159
|
+
// compiling.
|
160
|
+
//
|
161
|
+
// Since the size of bool is implementation-defined, we need to cast
|
162
|
+
// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
|
163
|
+
// result has type size_t.
|
164
|
+
//
|
165
|
+
// This macro is not perfect as it wrongfully accepts certain
|
166
|
+
// pointers, namely where the pointer size is divisible by the pointee
|
167
|
+
// size. Since all our code has to go through a 32-bit compiler,
|
168
|
+
// where a pointer is 4 bytes, this means all pointers to a type whose
|
169
|
+
// size is 3 or greater than 4 will be (righteously) rejected.
|
170
|
+
//
|
171
|
+
// Kudos to Jorg Brown for this simple and elegant implementation.
|
172
|
+
//
|
173
|
+
// - wan 2005-11-16
|
174
|
+
//
|
175
|
+
// Starting with Visual C++ 2005, WinNT.h includes ARRAYSIZE.
|
176
|
+
#if !defined(COMPILER_MSVC) || (defined(_MSC_VER) && _MSC_VER < 1400)
|
177
|
+
#define ARRAYSIZE(a) \
|
178
|
+
((sizeof(a) / sizeof(*(a))) / \
|
179
|
+
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
|
180
|
+
#endif
|
181
|
+
|
182
|
+
// A macro to turn a symbol into a string
|
183
|
+
#define AS_STRING(x) AS_STRING_INTERNAL(x)
|
184
|
+
#define AS_STRING_INTERNAL(x) #x
|
185
|
+
|
186
|
+
|
187
|
+
// One of the type traits, is_pod, makes it possible to query whether
|
188
|
+
// a type is a POD type. It is impossible for type_traits.h to get
|
189
|
+
// this right without compiler support, so it fails conservatively. It
|
190
|
+
// knows that fundamental types and pointers are PODs, but it can't
|
191
|
+
// tell whether user classes are PODs. The DECLARE_POD macro is used
|
192
|
+
// to inform the type traits library that a user class is a POD.
|
193
|
+
//
|
194
|
+
// Implementation note: the typedef at the end is just to make it legal
|
195
|
+
// to put a semicolon after DECLARE_POD(foo).
|
196
|
+
//
|
197
|
+
//
|
198
|
+
// So what's a POD? The C++ standard (clause 9 paragraph 4) gives a
|
199
|
+
// full definition, but a good rule of thumb is that a struct is a POD
|
200
|
+
// ("plain old data") if it doesn't use any of the features that make
|
201
|
+
// C++ different from C. A POD struct can't have constructors,
|
202
|
+
// destructors, assignment operators, base classes, private or
|
203
|
+
// protected members, or virtual functions, and all of its member
|
204
|
+
// variables must themselves be PODs.
|
205
|
+
|
206
|
+
#define DECLARE_POD(TypeName) \
|
207
|
+
namespace base { \
|
208
|
+
template<> struct is_pod<TypeName> : true_type { }; \
|
209
|
+
} \
|
210
|
+
typedef int Dummy_Type_For_DECLARE_POD \
|
211
|
+
|
212
|
+
// We once needed a different technique to assert that a nested class
|
213
|
+
// is a POD. This is no longer necessary, and DECLARE_NESTED_POD is
|
214
|
+
// just a synonym for DECLARE_POD. We continue to provide
|
215
|
+
// DECLARE_NESTED_POD only so we don't have to change client
|
216
|
+
// code. Regardless of whether you use DECLARE_POD or
|
217
|
+
// DECLARE_NESTED_POD: use it after the outer class. Using it within a
|
218
|
+
// class definition will give a compiler error.
|
219
|
+
#define DECLARE_NESTED_POD(TypeName) DECLARE_POD(TypeName)
|
220
|
+
|
221
|
+
// Declare that TemplateName<T> is a POD whenever T is
|
222
|
+
#define PROPAGATE_POD_FROM_TEMPLATE_ARGUMENT(TemplateName) \
|
223
|
+
namespace base { \
|
224
|
+
template <typename T> struct is_pod<TemplateName<T> > : is_pod<T> { }; \
|
225
|
+
} \
|
226
|
+
typedef int Dummy_Type_For_PROPAGATE_POD_FROM_TEMPLATE_ARGUMENT
|
227
|
+
|
228
|
+
// Macro that does nothing if TypeName is a POD, and gives a compiler
|
229
|
+
// error if TypeName is a non-POD. You should put a descriptive
|
230
|
+
// comment right next to the macro call so that people can tell what
|
231
|
+
// the compiler error is about.
|
232
|
+
//
|
233
|
+
// Implementation note: this works by taking the size of a type that's
|
234
|
+
// complete when TypeName is a POD and incomplete otherwise.
|
235
|
+
|
236
|
+
template <typename Boolean> struct ERROR_TYPE_MUST_BE_POD;
|
237
|
+
template <> struct ERROR_TYPE_MUST_BE_POD<base::true_type> { };
|
238
|
+
#define ENFORCE_POD(TypeName) \
|
239
|
+
enum { dummy_##TypeName \
|
240
|
+
= sizeof(ERROR_TYPE_MUST_BE_POD< \
|
241
|
+
typename base::is_pod<TypeName>::type>) }
|
242
|
+
|
243
|
+
#endif // BASE_MACROS_H_
|
data/base/port.h
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file.
|
4
|
+
|
5
|
+
#ifndef BASE_PORT_H_
|
6
|
+
#define BASE_PORT_H_
|
7
|
+
|
8
|
+
#include <stdarg.h>
|
9
|
+
#include "base/build_config.h"
|
10
|
+
|
11
|
+
#ifdef COMPILER_MSVC
|
12
|
+
#define GG_LONGLONG(x) x##I64
|
13
|
+
#define GG_ULONGLONG(x) x##UI64
|
14
|
+
#else
|
15
|
+
#define GG_LONGLONG(x) x##LL
|
16
|
+
#define GG_ULONGLONG(x) x##ULL
|
17
|
+
#endif
|
18
|
+
|
19
|
+
// Per C99 7.8.14, define __STDC_CONSTANT_MACROS before including <stdint.h>
|
20
|
+
// to get the INTn_C and UINTn_C macros for integer constants. It's difficult
|
21
|
+
// to guarantee any specific ordering of header includes, so it's difficult to
|
22
|
+
// guarantee that the INTn_C macros can be defined by including <stdint.h> at
|
23
|
+
// any specific point. Provide GG_INTn_C macros instead.
|
24
|
+
|
25
|
+
#define GG_INT8_C(x) (x)
|
26
|
+
#define GG_INT16_C(x) (x)
|
27
|
+
#define GG_INT32_C(x) (x)
|
28
|
+
#define GG_INT64_C(x) GG_LONGLONG(x)
|
29
|
+
|
30
|
+
#define GG_UINT8_C(x) (x ## U)
|
31
|
+
#define GG_UINT16_C(x) (x ## U)
|
32
|
+
#define GG_UINT32_C(x) (x ## U)
|
33
|
+
#define GG_UINT64_C(x) GG_ULONGLONG(x)
|
34
|
+
|
35
|
+
// It's possible for functions that use a va_list, such as StringPrintf, to
|
36
|
+
// invalidate the data in it upon use. The fix is to make a copy of the
|
37
|
+
// structure before using it and use that copy instead. va_copy is provided
|
38
|
+
// for this purpose. MSVC does not provide va_copy, so define an
|
39
|
+
// implementation here. It is not guaranteed that assignment is a copy, so the
|
40
|
+
// StringUtil.VariableArgsFunc unit test tests this capability.
|
41
|
+
#if defined(COMPILER_GCC)
|
42
|
+
#define GG_VA_COPY(a, b) (va_copy(a, b))
|
43
|
+
#elif defined(COMPILER_MSVC)
|
44
|
+
#define GG_VA_COPY(a, b) (a = b)
|
45
|
+
#endif
|
46
|
+
|
47
|
+
// Define an OS-neutral wrapper for shared library entry points
|
48
|
+
#if defined(OS_WIN)
|
49
|
+
#define API_CALL __stdcall
|
50
|
+
#else
|
51
|
+
#define API_CALL
|
52
|
+
#endif
|
53
|
+
|
54
|
+
#endif // BASE_PORT_H_
|
data/base/scoped_ptr.h
ADDED
@@ -0,0 +1,428 @@
|
|
1
|
+
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file.
|
4
|
+
|
5
|
+
#ifndef BASE_SCOPED_PTR_H__
|
6
|
+
#define BASE_SCOPED_PTR_H__
|
7
|
+
|
8
|
+
// This is an implementation designed to match the anticipated future TR2
|
9
|
+
// implementation of the scoped_ptr class, and its closely-related brethren,
|
10
|
+
// scoped_array, scoped_ptr_malloc, and make_scoped_ptr.
|
11
|
+
//
|
12
|
+
// See http://wiki/Main/ScopedPointerInterface for the spec that drove this
|
13
|
+
// file.
|
14
|
+
|
15
|
+
#include <assert.h>
|
16
|
+
#include <stdlib.h>
|
17
|
+
#include <cstddef>
|
18
|
+
|
19
|
+
#ifdef OS_EMBEDDED_QNX
|
20
|
+
// NOTE(akirmse):
|
21
|
+
// The C++ standard says that <stdlib.h> declares both ::foo and std::foo
|
22
|
+
// But this isn't done in QNX version 6.3.2 200709062316.
|
23
|
+
using std::free;
|
24
|
+
using std::malloc;
|
25
|
+
using std::realloc;
|
26
|
+
#endif
|
27
|
+
|
28
|
+
template <class C> class scoped_ptr;
|
29
|
+
template <class C, class Free> class scoped_ptr_malloc;
|
30
|
+
template <class C> class scoped_array;
|
31
|
+
|
32
|
+
template <class C>
|
33
|
+
scoped_ptr<C> make_scoped_ptr(C *);
|
34
|
+
|
35
|
+
// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
|
36
|
+
// automatically deletes the pointer it holds (if any).
|
37
|
+
// That is, scoped_ptr<T> owns the T object that it points to.
|
38
|
+
// Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
|
39
|
+
// Also like T*, scoped_ptr<T> is thread-compatible, and once you
|
40
|
+
// dereference it, you get the threadsafety guarantees of T.
|
41
|
+
//
|
42
|
+
// The size of a scoped_ptr is small:
|
43
|
+
// sizeof(scoped_ptr<C>) == sizeof(C*)
|
44
|
+
template <class C>
|
45
|
+
class scoped_ptr {
|
46
|
+
public:
|
47
|
+
|
48
|
+
// The element type
|
49
|
+
typedef C element_type;
|
50
|
+
|
51
|
+
// Constructor. Defaults to intializing with NULL.
|
52
|
+
// There is no way to create an uninitialized scoped_ptr.
|
53
|
+
// The input parameter must be allocated with new.
|
54
|
+
explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
|
55
|
+
|
56
|
+
// Destructor. If there is a C object, delete it.
|
57
|
+
// We don't need to test ptr_ == NULL because C++ does that for us.
|
58
|
+
~scoped_ptr() {
|
59
|
+
enum { type_must_be_complete = sizeof(C) };
|
60
|
+
delete ptr_;
|
61
|
+
}
|
62
|
+
|
63
|
+
// Reset. Deletes the current owned object, if any.
|
64
|
+
// Then takes ownership of a new object, if given.
|
65
|
+
// this->reset(this->get()) works.
|
66
|
+
void reset(C* p = NULL) {
|
67
|
+
if (p != ptr_) {
|
68
|
+
enum { type_must_be_complete = sizeof(C) };
|
69
|
+
delete ptr_;
|
70
|
+
ptr_ = p;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
// Accessors to get the owned object.
|
75
|
+
// operator* and operator-> will assert() if there is no current object.
|
76
|
+
C& operator*() const {
|
77
|
+
assert(ptr_ != NULL);
|
78
|
+
return *ptr_;
|
79
|
+
}
|
80
|
+
C* operator->() const {
|
81
|
+
assert(ptr_ != NULL);
|
82
|
+
return ptr_;
|
83
|
+
}
|
84
|
+
C* get() const { return ptr_; }
|
85
|
+
|
86
|
+
// Comparison operators.
|
87
|
+
// These return whether a scoped_ptr and a raw pointer refer to
|
88
|
+
// the same object, not just to two different but equal objects.
|
89
|
+
bool operator==(const C* p) const { return ptr_ == p; }
|
90
|
+
bool operator!=(const C* p) const { return ptr_ != p; }
|
91
|
+
|
92
|
+
// Swap two scoped pointers.
|
93
|
+
void swap(scoped_ptr& p2) {
|
94
|
+
C* tmp = ptr_;
|
95
|
+
ptr_ = p2.ptr_;
|
96
|
+
p2.ptr_ = tmp;
|
97
|
+
}
|
98
|
+
|
99
|
+
// Release a pointer.
|
100
|
+
// The return value is the current pointer held by this object.
|
101
|
+
// If this object holds a NULL pointer, the return value is NULL.
|
102
|
+
// After this operation, this object will hold a NULL pointer,
|
103
|
+
// and will not own the object any more.
|
104
|
+
C* release() {
|
105
|
+
C* retVal = ptr_;
|
106
|
+
ptr_ = NULL;
|
107
|
+
return retVal;
|
108
|
+
}
|
109
|
+
|
110
|
+
private:
|
111
|
+
C* ptr_;
|
112
|
+
|
113
|
+
// google3 friend class that can access copy ctor (although if it actually
|
114
|
+
// calls a copy ctor, there will be a problem) see below
|
115
|
+
friend scoped_ptr<C> make_scoped_ptr<C>(C *p);
|
116
|
+
|
117
|
+
// Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't
|
118
|
+
// make sense, and if C2 == C, it still doesn't make sense because you should
|
119
|
+
// never have the same object owned by two different scoped_ptrs.
|
120
|
+
template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
|
121
|
+
template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
|
122
|
+
|
123
|
+
// Disallow evil constructors
|
124
|
+
scoped_ptr(const scoped_ptr&);
|
125
|
+
void operator=(const scoped_ptr&);
|
126
|
+
};
|
127
|
+
|
128
|
+
// Free functions
|
129
|
+
template <class C>
|
130
|
+
inline void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) {
|
131
|
+
p1.swap(p2);
|
132
|
+
}
|
133
|
+
|
134
|
+
template <class C>
|
135
|
+
inline bool operator==(const C* p1, const scoped_ptr<C>& p2) {
|
136
|
+
return p1 == p2.get();
|
137
|
+
}
|
138
|
+
|
139
|
+
template <class C>
|
140
|
+
inline bool operator==(const C* p1, const scoped_ptr<const C>& p2) {
|
141
|
+
return p1 == p2.get();
|
142
|
+
}
|
143
|
+
|
144
|
+
template <class C>
|
145
|
+
inline bool operator!=(const C* p1, const scoped_ptr<C>& p2) {
|
146
|
+
return p1 != p2.get();
|
147
|
+
}
|
148
|
+
|
149
|
+
template <class C>
|
150
|
+
inline bool operator!=(const C* p1, const scoped_ptr<const C>& p2) {
|
151
|
+
return p1 != p2.get();
|
152
|
+
}
|
153
|
+
|
154
|
+
template <class C>
|
155
|
+
scoped_ptr<C> make_scoped_ptr(C *p) {
|
156
|
+
// This does nothing but to return a scoped_ptr of the type that the passed
|
157
|
+
// pointer is of. (This eliminates the need to specify the name of T when
|
158
|
+
// making a scoped_ptr that is used anonymously/temporarily.) From an
|
159
|
+
// access control point of view, we construct an unnamed scoped_ptr here
|
160
|
+
// which we return and thus copy-construct. Hence, we need to have access
|
161
|
+
// to scoped_ptr::scoped_ptr(scoped_ptr const &). However, it is guaranteed
|
162
|
+
// that we never actually call the copy constructor, which is a good thing
|
163
|
+
// as we would call the temporary's object destructor (and thus delete p)
|
164
|
+
// if we actually did copy some object, here.
|
165
|
+
return scoped_ptr<C>(p);
|
166
|
+
}
|
167
|
+
|
168
|
+
// scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
|
169
|
+
// with new [] and the destructor deletes objects with delete [].
|
170
|
+
//
|
171
|
+
// As with scoped_ptr<C>, a scoped_array<C> either points to an object
|
172
|
+
// or is NULL. A scoped_array<C> owns the object that it points to.
|
173
|
+
// scoped_array<T> is thread-compatible, and once you index into it,
|
174
|
+
// the returned objects have only the threadsafety guarantees of T.
|
175
|
+
//
|
176
|
+
// Size: sizeof(scoped_array<C>) == sizeof(C*)
|
177
|
+
template <class C>
|
178
|
+
class scoped_array {
|
179
|
+
public:
|
180
|
+
|
181
|
+
// The element type
|
182
|
+
typedef C element_type;
|
183
|
+
|
184
|
+
// Constructor. Defaults to intializing with NULL.
|
185
|
+
// There is no way to create an uninitialized scoped_array.
|
186
|
+
// The input parameter must be allocated with new [].
|
187
|
+
explicit scoped_array(C* p = NULL) : array_(p) { }
|
188
|
+
|
189
|
+
// Destructor. If there is a C object, delete it.
|
190
|
+
// We don't need to test ptr_ == NULL because C++ does that for us.
|
191
|
+
~scoped_array() {
|
192
|
+
enum { type_must_be_complete = sizeof(C) };
|
193
|
+
delete[] array_;
|
194
|
+
}
|
195
|
+
|
196
|
+
// Reset. Deletes the current owned object, if any.
|
197
|
+
// Then takes ownership of a new object, if given.
|
198
|
+
// this->reset(this->get()) works.
|
199
|
+
void reset(C* p = NULL) {
|
200
|
+
if (p != array_) {
|
201
|
+
enum { type_must_be_complete = sizeof(C) };
|
202
|
+
delete[] array_;
|
203
|
+
array_ = p;
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
// Get one element of the current object.
|
208
|
+
// Will assert() if there is no current object, or index i is negative.
|
209
|
+
C& operator[](std::ptrdiff_t i) const {
|
210
|
+
assert(i >= 0);
|
211
|
+
assert(array_ != NULL);
|
212
|
+
return array_[i];
|
213
|
+
}
|
214
|
+
|
215
|
+
// Get a pointer to the zeroth element of the current object.
|
216
|
+
// If there is no current object, return NULL.
|
217
|
+
C* get() const {
|
218
|
+
return array_;
|
219
|
+
}
|
220
|
+
|
221
|
+
// Comparison operators.
|
222
|
+
// These return whether a scoped_array and a raw pointer refer to
|
223
|
+
// the same array, not just to two different but equal arrays.
|
224
|
+
bool operator==(const C* p) const { return array_ == p; }
|
225
|
+
bool operator!=(const C* p) const { return array_ != p; }
|
226
|
+
|
227
|
+
// Swap two scoped arrays.
|
228
|
+
void swap(scoped_array& p2) {
|
229
|
+
C* tmp = array_;
|
230
|
+
array_ = p2.array_;
|
231
|
+
p2.array_ = tmp;
|
232
|
+
}
|
233
|
+
|
234
|
+
// Release an array.
|
235
|
+
// The return value is the current pointer held by this object.
|
236
|
+
// If this object holds a NULL pointer, the return value is NULL.
|
237
|
+
// After this operation, this object will hold a NULL pointer,
|
238
|
+
// and will not own the object any more.
|
239
|
+
C* release() {
|
240
|
+
C* retVal = array_;
|
241
|
+
array_ = NULL;
|
242
|
+
return retVal;
|
243
|
+
}
|
244
|
+
|
245
|
+
private:
|
246
|
+
C* array_;
|
247
|
+
|
248
|
+
// Forbid comparison of different scoped_array types.
|
249
|
+
template <class C2> bool operator==(scoped_array<C2> const& p2) const;
|
250
|
+
template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
|
251
|
+
|
252
|
+
// Disallow evil constructors
|
253
|
+
scoped_array(const scoped_array&);
|
254
|
+
void operator=(const scoped_array&);
|
255
|
+
};
|
256
|
+
|
257
|
+
// Free functions
|
258
|
+
template <class C>
|
259
|
+
inline void swap(scoped_array<C>& p1, scoped_array<C>& p2) {
|
260
|
+
p1.swap(p2);
|
261
|
+
}
|
262
|
+
|
263
|
+
template <class C>
|
264
|
+
inline bool operator==(const C* p1, const scoped_array<C>& p2) {
|
265
|
+
return p1 == p2.get();
|
266
|
+
}
|
267
|
+
|
268
|
+
template <class C>
|
269
|
+
inline bool operator==(const C* p1, const scoped_array<const C>& p2) {
|
270
|
+
return p1 == p2.get();
|
271
|
+
}
|
272
|
+
|
273
|
+
template <class C>
|
274
|
+
inline bool operator!=(const C* p1, const scoped_array<C>& p2) {
|
275
|
+
return p1 != p2.get();
|
276
|
+
}
|
277
|
+
|
278
|
+
template <class C>
|
279
|
+
inline bool operator!=(const C* p1, const scoped_array<const C>& p2) {
|
280
|
+
return p1 != p2.get();
|
281
|
+
}
|
282
|
+
|
283
|
+
// This class wraps the c library function free() in a class that can be
|
284
|
+
// passed as a template argument to scoped_ptr_malloc below.
|
285
|
+
class ScopedPtrMallocFree {
|
286
|
+
public:
|
287
|
+
inline void operator()(void* x) const {
|
288
|
+
free(x);
|
289
|
+
}
|
290
|
+
};
|
291
|
+
|
292
|
+
// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
|
293
|
+
// second template argument, the functor used to free the object.
|
294
|
+
|
295
|
+
template<class C, class FreeProc = ScopedPtrMallocFree>
|
296
|
+
class scoped_ptr_malloc {
|
297
|
+
public:
|
298
|
+
|
299
|
+
// The element type
|
300
|
+
typedef C element_type;
|
301
|
+
|
302
|
+
// Construction with no arguments sets ptr_ to NULL.
|
303
|
+
// There is no way to create an uninitialized scoped_ptr.
|
304
|
+
// The input parameter must be allocated with an allocator that matches the
|
305
|
+
// Free functor. For the default Free functor, this is malloc, calloc, or
|
306
|
+
// realloc.
|
307
|
+
explicit scoped_ptr_malloc(): ptr_(NULL) { }
|
308
|
+
|
309
|
+
// Construct with a C*, and provides an error with a D*.
|
310
|
+
template<class must_be_C>
|
311
|
+
explicit scoped_ptr_malloc(must_be_C* p): ptr_(p) { }
|
312
|
+
|
313
|
+
// Construct with a void*, such as you get from malloc.
|
314
|
+
explicit scoped_ptr_malloc(void *p): ptr_(static_cast<C*>(p)) { }
|
315
|
+
|
316
|
+
// Destructor. If there is a C object, call the Free functor.
|
317
|
+
~scoped_ptr_malloc() {
|
318
|
+
free_(ptr_);
|
319
|
+
}
|
320
|
+
|
321
|
+
// Reset. Calls the Free functor on the current owned object, if any.
|
322
|
+
// Then takes ownership of a new object, if given.
|
323
|
+
// this->reset(this->get()) works.
|
324
|
+
void reset(C* p = NULL) {
|
325
|
+
if (ptr_ != p) {
|
326
|
+
free_(ptr_);
|
327
|
+
ptr_ = p;
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
// Reallocates the existing pointer, and returns 'true' if
|
332
|
+
// the reallcation is succesfull. If the reallocation failed, then
|
333
|
+
// the pointer remains in its previous state.
|
334
|
+
//
|
335
|
+
// Note: this calls realloc() directly, even if an alternate 'free'
|
336
|
+
// functor is provided in the template instantiation.
|
337
|
+
bool try_realloc(size_t new_size) {
|
338
|
+
C* new_ptr = static_cast<C*>(realloc(ptr_, new_size));
|
339
|
+
if (new_ptr == NULL) {
|
340
|
+
return false;
|
341
|
+
}
|
342
|
+
ptr_ = new_ptr;
|
343
|
+
return true;
|
344
|
+
}
|
345
|
+
|
346
|
+
// Get the current object.
|
347
|
+
// operator* and operator-> will cause an assert() failure if there is
|
348
|
+
// no current object.
|
349
|
+
C& operator*() const {
|
350
|
+
assert(ptr_ != NULL);
|
351
|
+
return *ptr_;
|
352
|
+
}
|
353
|
+
|
354
|
+
C* operator->() const {
|
355
|
+
assert(ptr_ != NULL);
|
356
|
+
return ptr_;
|
357
|
+
}
|
358
|
+
|
359
|
+
C* get() const {
|
360
|
+
return ptr_;
|
361
|
+
}
|
362
|
+
|
363
|
+
// Comparison operators.
|
364
|
+
// These return whether a scoped_ptr_malloc and a plain pointer refer
|
365
|
+
// to the same object, not just to two different but equal objects.
|
366
|
+
// For compatibility with the boost-derived implementation, these
|
367
|
+
// take non-const arguments.
|
368
|
+
bool operator==(C* p) const {
|
369
|
+
return ptr_ == p;
|
370
|
+
}
|
371
|
+
|
372
|
+
bool operator!=(C* p) const {
|
373
|
+
return ptr_ != p;
|
374
|
+
}
|
375
|
+
|
376
|
+
// Swap two scoped pointers.
|
377
|
+
void swap(scoped_ptr_malloc & b) {
|
378
|
+
C* tmp = b.ptr_;
|
379
|
+
b.ptr_ = ptr_;
|
380
|
+
ptr_ = tmp;
|
381
|
+
}
|
382
|
+
|
383
|
+
// Release a pointer.
|
384
|
+
// The return value is the current pointer held by this object.
|
385
|
+
// If this object holds a NULL pointer, the return value is NULL.
|
386
|
+
// After this operation, this object will hold a NULL pointer,
|
387
|
+
// and will not own the object any more.
|
388
|
+
C* release() {
|
389
|
+
C* tmp = ptr_;
|
390
|
+
ptr_ = NULL;
|
391
|
+
return tmp;
|
392
|
+
}
|
393
|
+
|
394
|
+
private:
|
395
|
+
C* ptr_;
|
396
|
+
|
397
|
+
// no reason to use these: each scoped_ptr_malloc should have its own object
|
398
|
+
template <class C2, class GP>
|
399
|
+
bool operator==(scoped_ptr_malloc<C2, GP> const& p) const;
|
400
|
+
template <class C2, class GP>
|
401
|
+
bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const;
|
402
|
+
|
403
|
+
static FreeProc const free_;
|
404
|
+
|
405
|
+
// Disallow evil constructors
|
406
|
+
scoped_ptr_malloc(const scoped_ptr_malloc&);
|
407
|
+
void operator=(const scoped_ptr_malloc&);
|
408
|
+
};
|
409
|
+
|
410
|
+
template<class C, class FP>
|
411
|
+
FP const scoped_ptr_malloc<C, FP>::free_ = FP();
|
412
|
+
|
413
|
+
template<class C, class FP> inline
|
414
|
+
void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) {
|
415
|
+
a.swap(b);
|
416
|
+
}
|
417
|
+
|
418
|
+
template<class C, class FP> inline
|
419
|
+
bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
|
420
|
+
return p == b.get();
|
421
|
+
}
|
422
|
+
|
423
|
+
template<class C, class FP> inline
|
424
|
+
bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
|
425
|
+
return p != b.get();
|
426
|
+
}
|
427
|
+
|
428
|
+
#endif // BASE_SCOPED_PTR_H__
|