File: | tools/lldb/source/Commands/CommandObjectExpression.cpp |
Warning: | line 505, column 15 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | ||||
10 | // C Includes | |||
11 | // C++ Includes | |||
12 | // Other libraries and framework includes | |||
13 | #include "llvm/ADT/STLExtras.h" | |||
14 | #include "llvm/ADT/StringRef.h" | |||
15 | ||||
16 | // Project includes | |||
17 | #include "CommandObjectExpression.h" | |||
18 | #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h" | |||
19 | #include "lldb/Core/Debugger.h" | |||
20 | #include "lldb/Core/Value.h" | |||
21 | #include "lldb/Core/ValueObjectVariable.h" | |||
22 | #include "lldb/DataFormatters/ValueObjectPrinter.h" | |||
23 | #include "lldb/Expression/DWARFExpression.h" | |||
24 | #include "lldb/Expression/REPL.h" | |||
25 | #include "lldb/Expression/UserExpression.h" | |||
26 | #include "lldb/Host/Host.h" | |||
27 | #include "lldb/Host/OptionParser.h" | |||
28 | #include "lldb/Interpreter/CommandInterpreter.h" | |||
29 | #include "lldb/Interpreter/CommandReturnObject.h" | |||
30 | #include "lldb/Interpreter/OptionArgParser.h" | |||
31 | #include "lldb/Symbol/ObjectFile.h" | |||
32 | #include "lldb/Symbol/Variable.h" | |||
33 | #include "lldb/Target/Language.h" | |||
34 | #include "lldb/Target/Process.h" | |||
35 | #include "lldb/Target/StackFrame.h" | |||
36 | #include "lldb/Target/Target.h" | |||
37 | #include "lldb/Target/Thread.h" | |||
38 | ||||
39 | using namespace lldb; | |||
40 | using namespace lldb_private; | |||
41 | ||||
42 | CommandObjectExpression::CommandOptions::CommandOptions() : OptionGroup() {} | |||
43 | ||||
44 | CommandObjectExpression::CommandOptions::~CommandOptions() = default; | |||
45 | ||||
46 | static constexpr OptionEnumValueElement g_description_verbosity_type[] = { | |||
47 | {eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", | |||
48 | "Only show the description string"}, | |||
49 | {eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", | |||
50 | "Show the full output, including persistent variable's name and type"} }; | |||
51 | ||||
52 | static constexpr OptionEnumValues DescriptionVerbosityTypes() { | |||
53 | return OptionEnumValues(g_description_verbosity_type); | |||
54 | } | |||
55 | ||||
56 | static constexpr OptionDefinition g_expression_options[] = { | |||
57 | // clang-format off | |||
58 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "all-threads", 'a', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Should we run all threads if the execution doesn't complete on one thread."}, | |||
59 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "ignore-breakpoints", 'i', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Ignore breakpoint hits while running expressions"}, | |||
60 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "timeout", 't', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeUnsignedInteger, "Timeout value (in microseconds) for running the expression."}, | |||
61 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "unwind-on-error", 'u', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Clean up program state if the expression causes a crash, or raises a signal. " | |||
62 | "Note, unlike gdb hitting a breakpoint is controlled by another option (-i)."}, | |||
63 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "debug", 'g', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "When specified, debug the JIT code by setting a breakpoint on the first instruction " | |||
64 | "and forcing breakpoints to not be ignored (-i0) and no unwinding to happen on error (-u0)."}, | |||
65 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "language", 'l', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeLanguage, "Specifies the Language to use when parsing the expression. If not set the target.language " | |||
66 | "setting is used." }, | |||
67 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "apply-fixits", 'X', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeLanguage, "If true, simple fix-it hints will be automatically applied to the expression." }, | |||
68 | {LLDB_OPT_SET_1(1U << 0), false, "description-verbosity", 'v', OptionParser::eOptionalArgument, nullptr, DescriptionVerbosityTypes(), 0, eArgTypeDescriptionVerbosity, "How verbose should the output of this expression be, if the object description is asked for."}, | |||
69 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "top-level", 'p', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Interpret the expression as a complete translation unit, without injecting it into the local " | |||
70 | "context. Allows declaration of persistent, top-level entities without a $ prefix."}, | |||
71 | {LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1), false, "allow-jit", 'j', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Controls whether the expression can fall back to being JITted if it's not supported by " | |||
72 | "the interpreter (defaults to true)."} | |||
73 | // clang-format on | |||
74 | }; | |||
75 | ||||
76 | Status CommandObjectExpression::CommandOptions::SetOptionValue( | |||
77 | uint32_t option_idx, llvm::StringRef option_arg, | |||
78 | ExecutionContext *execution_context) { | |||
79 | Status error; | |||
80 | ||||
81 | const int short_option = GetDefinitions()[option_idx].short_option; | |||
82 | ||||
83 | switch (short_option) { | |||
84 | case 'l': | |||
85 | language = Language::GetLanguageTypeFromString(option_arg); | |||
86 | if (language == eLanguageTypeUnknown) | |||
87 | error.SetErrorStringWithFormat( | |||
88 | "unknown language type: '%s' for expression", | |||
89 | option_arg.str().c_str()); | |||
90 | break; | |||
91 | ||||
92 | case 'a': { | |||
93 | bool success; | |||
94 | bool result; | |||
95 | result = OptionArgParser::ToBoolean(option_arg, true, &success); | |||
96 | if (!success) | |||
97 | error.SetErrorStringWithFormat( | |||
98 | "invalid all-threads value setting: \"%s\"", | |||
99 | option_arg.str().c_str()); | |||
100 | else | |||
101 | try_all_threads = result; | |||
102 | } break; | |||
103 | ||||
104 | case 'i': { | |||
105 | bool success; | |||
106 | bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); | |||
107 | if (success) | |||
108 | ignore_breakpoints = tmp_value; | |||
109 | else | |||
110 | error.SetErrorStringWithFormat( | |||
111 | "could not convert \"%s\" to a boolean value.", | |||
112 | option_arg.str().c_str()); | |||
113 | break; | |||
114 | } | |||
115 | ||||
116 | case 'j': { | |||
117 | bool success; | |||
118 | bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); | |||
119 | if (success) | |||
120 | allow_jit = tmp_value; | |||
121 | else | |||
122 | error.SetErrorStringWithFormat( | |||
123 | "could not convert \"%s\" to a boolean value.", | |||
124 | option_arg.str().c_str()); | |||
125 | break; | |||
126 | } | |||
127 | ||||
128 | case 't': | |||
129 | if (option_arg.getAsInteger(0, timeout)) { | |||
130 | timeout = 0; | |||
131 | error.SetErrorStringWithFormat("invalid timeout setting \"%s\"", | |||
132 | option_arg.str().c_str()); | |||
133 | } | |||
134 | break; | |||
135 | ||||
136 | case 'u': { | |||
137 | bool success; | |||
138 | bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); | |||
139 | if (success) | |||
140 | unwind_on_error = tmp_value; | |||
141 | else | |||
142 | error.SetErrorStringWithFormat( | |||
143 | "could not convert \"%s\" to a boolean value.", | |||
144 | option_arg.str().c_str()); | |||
145 | break; | |||
146 | } | |||
147 | ||||
148 | case 'v': | |||
149 | if (option_arg.empty()) { | |||
150 | m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; | |||
151 | break; | |||
152 | } | |||
153 | m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) | |||
154 | OptionArgParser::ToOptionEnum( | |||
155 | option_arg, GetDefinitions()[option_idx].enum_values, 0, error); | |||
156 | if (!error.Success()) | |||
157 | error.SetErrorStringWithFormat( | |||
158 | "unrecognized value for description-verbosity '%s'", | |||
159 | option_arg.str().c_str()); | |||
160 | break; | |||
161 | ||||
162 | case 'g': | |||
163 | debug = true; | |||
164 | unwind_on_error = false; | |||
165 | ignore_breakpoints = false; | |||
166 | break; | |||
167 | ||||
168 | case 'p': | |||
169 | top_level = true; | |||
170 | break; | |||
171 | ||||
172 | case 'X': { | |||
173 | bool success; | |||
174 | bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); | |||
175 | if (success) | |||
176 | auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; | |||
177 | else | |||
178 | error.SetErrorStringWithFormat( | |||
179 | "could not convert \"%s\" to a boolean value.", | |||
180 | option_arg.str().c_str()); | |||
181 | break; | |||
182 | } | |||
183 | ||||
184 | default: | |||
185 | error.SetErrorStringWithFormat("invalid short option character '%c'", | |||
186 | short_option); | |||
187 | break; | |||
188 | } | |||
189 | ||||
190 | return error; | |||
191 | } | |||
192 | ||||
193 | void CommandObjectExpression::CommandOptions::OptionParsingStarting( | |||
194 | ExecutionContext *execution_context) { | |||
195 | auto process_sp = | |||
196 | execution_context ? execution_context->GetProcessSP() : ProcessSP(); | |||
197 | if (process_sp) { | |||
198 | ignore_breakpoints = process_sp->GetIgnoreBreakpointsInExpressions(); | |||
199 | unwind_on_error = process_sp->GetUnwindOnErrorInExpressions(); | |||
200 | } else { | |||
201 | ignore_breakpoints = true; | |||
202 | unwind_on_error = true; | |||
203 | } | |||
204 | ||||
205 | show_summary = true; | |||
206 | try_all_threads = true; | |||
207 | timeout = 0; | |||
208 | debug = false; | |||
209 | language = eLanguageTypeUnknown; | |||
210 | m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; | |||
211 | auto_apply_fixits = eLazyBoolCalculate; | |||
212 | top_level = false; | |||
213 | allow_jit = true; | |||
214 | } | |||
215 | ||||
216 | llvm::ArrayRef<OptionDefinition> | |||
217 | CommandObjectExpression::CommandOptions::GetDefinitions() { | |||
218 | return llvm::makeArrayRef(g_expression_options); | |||
219 | } | |||
220 | ||||
221 | CommandObjectExpression::CommandObjectExpression( | |||
222 | CommandInterpreter &interpreter) | |||
223 | : CommandObjectRaw( | |||
224 | interpreter, "expression", "Evaluate an expression on the current " | |||
225 | "thread. Displays any returned value " | |||
226 | "with LLDB's default formatting.", | |||
227 | "", eCommandProcessMustBePaused | eCommandTryTargetAPILock), | |||
228 | IOHandlerDelegate(IOHandlerDelegate::Completion::Expression), | |||
229 | m_option_group(), m_format_options(eFormatDefault), | |||
230 | m_repl_option(LLDB_OPT_SET_1(1U << 0), false, "repl", 'r', "Drop into REPL", false, | |||
231 | true), | |||
232 | m_command_options(), m_expr_line_count(0), m_expr_lines() { | |||
233 | SetHelpLong( | |||
234 | R"( | |||
235 | Single and multi-line expressions: | |||
236 | ||||
237 | )" | |||
238 | " The expression provided on the command line must be a complete expression \ | |||
239 | with no newlines. To evaluate a multi-line expression, \ | |||
240 | hit a return after an empty expression, and lldb will enter the multi-line expression editor. \ | |||
241 | Hit return on an empty line to end the multi-line expression." | |||
242 | ||||
243 | R"( | |||
244 | ||||
245 | Timeouts: | |||
246 | ||||
247 | )" | |||
248 | " If the expression can be evaluated statically (without running code) then it will be. \ | |||
249 | Otherwise, by default the expression will run on the current thread with a short timeout: \ | |||
250 | currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ | |||
251 | and resumed with all threads running. You can use the -a option to disable retrying on all \ | |||
252 | threads. You can use the -t option to set a shorter timeout." | |||
253 | R"( | |||
254 | ||||
255 | User defined variables: | |||
256 | ||||
257 | )" | |||
258 | " You can define your own variables for convenience or to be used in subsequent expressions. \ | |||
259 | You define them the same way you would define variables in C. If the first character of \ | |||
260 | your user defined variable is a $, then the variable's value will be available in future \ | |||
261 | expressions, otherwise it will just be available in the current expression." | |||
262 | R"( | |||
263 | ||||
264 | Continuing evaluation after a breakpoint: | |||
265 | ||||
266 | )" | |||
267 | " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ | |||
268 | you are done with your investigation, you can either remove the expression execution frames \ | |||
269 | from the stack with \"thread return -x\" or if you are still interested in the expression result \ | |||
270 | you can issue the \"continue\" command and the expression evaluation will complete and the \ | |||
271 | expression result will be available using the \"thread.completed-expression\" key in the thread \ | |||
272 | format." | |||
273 | ||||
274 | R"( | |||
275 | ||||
276 | Examples: | |||
277 | ||||
278 | expr my_struct->a = my_array[3] | |||
279 | expr -f bin -- (index * 8) + 5 | |||
280 | expr unsigned int $foo = 5 | |||
281 | expr char c[] = \"foo\"; c[0])"); | |||
282 | ||||
283 | CommandArgumentEntry arg; | |||
284 | CommandArgumentData expression_arg; | |||
285 | ||||
286 | // Define the first (and only) variant of this arg. | |||
287 | expression_arg.arg_type = eArgTypeExpression; | |||
288 | expression_arg.arg_repetition = eArgRepeatPlain; | |||
289 | ||||
290 | // There is only one variant this argument could be; put it into the argument | |||
291 | // entry. | |||
292 | arg.push_back(expression_arg); | |||
293 | ||||
294 | // Push the data for the first argument into the m_arguments vector. | |||
295 | m_arguments.push_back(arg); | |||
296 | ||||
297 | // Add the "--format" and "--gdb-format" | |||
298 | m_option_group.Append(&m_format_options, | |||
299 | OptionGroupFormat::OPTION_GROUP_FORMAT | | |||
300 | OptionGroupFormat::OPTION_GROUP_GDB_FMT, | |||
301 | LLDB_OPT_SET_1(1U << 0)); | |||
302 | m_option_group.Append(&m_command_options); | |||
303 | m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL0xFFFFFFFFU, | |||
304 | LLDB_OPT_SET_1(1U << 0) | LLDB_OPT_SET_2(1U << 1)); | |||
305 | m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL0xFFFFFFFFU, LLDB_OPT_SET_3(1U << 2)); | |||
306 | m_option_group.Finalize(); | |||
307 | } | |||
308 | ||||
309 | CommandObjectExpression::~CommandObjectExpression() = default; | |||
310 | ||||
311 | Options *CommandObjectExpression::GetOptions() { return &m_option_group; } | |||
312 | ||||
313 | int CommandObjectExpression::HandleCompletion(CompletionRequest &request) { | |||
314 | EvaluateExpressionOptions options; | |||
315 | options.SetCoerceToId(m_varobj_options.use_objc); | |||
316 | options.SetLanguage(m_command_options.language); | |||
317 | options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever); | |||
318 | options.SetAutoApplyFixIts(false); | |||
319 | options.SetGenerateDebugInfo(false); | |||
320 | ||||
321 | // We need a valid execution context with a frame pointer for this | |||
322 | // completion, so if we don't have one we should try to make a valid | |||
323 | // execution context. | |||
324 | if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) | |||
325 | m_interpreter.UpdateExecutionContext(nullptr); | |||
326 | ||||
327 | // This didn't work, so let's get out before we start doing things that | |||
328 | // expect a valid frame pointer. | |||
329 | if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) | |||
330 | return 0; | |||
331 | ||||
332 | ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); | |||
333 | ||||
334 | Target *target = exe_ctx.GetTargetPtr(); | |||
335 | ||||
336 | if (!target) | |||
337 | target = GetDummyTarget(); | |||
338 | ||||
339 | if (!target) | |||
340 | return 0; | |||
341 | ||||
342 | unsigned cursor_pos = request.GetRawCursorPos(); | |||
343 | llvm::StringRef code = request.GetRawLine(); | |||
344 | ||||
345 | const std::size_t original_code_size = code.size(); | |||
346 | ||||
347 | // Remove the first token which is 'expr' or some alias/abbreviation of that. | |||
348 | code = llvm::getToken(code).second.ltrim(); | |||
349 | OptionsWithRaw args(code); | |||
350 | code = args.GetRawPart(); | |||
351 | ||||
352 | // The position where the expression starts in the command line. | |||
353 | assert(original_code_size >= code.size())((original_code_size >= code.size()) ? static_cast<void > (0) : __assert_fail ("original_code_size >= code.size()" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Commands/CommandObjectExpression.cpp" , 353, __PRETTY_FUNCTION__)); | |||
354 | std::size_t raw_start = original_code_size - code.size(); | |||
355 | ||||
356 | // Check if the cursor is actually in the expression string, and if not, we | |||
357 | // exit. | |||
358 | // FIXME: We should complete the options here. | |||
359 | if (cursor_pos < raw_start) | |||
360 | return 0; | |||
361 | ||||
362 | // Make the cursor_pos again relative to the start of the code string. | |||
363 | assert(cursor_pos >= raw_start)((cursor_pos >= raw_start) ? static_cast<void> (0) : __assert_fail ("cursor_pos >= raw_start", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Commands/CommandObjectExpression.cpp" , 363, __PRETTY_FUNCTION__)); | |||
364 | cursor_pos -= raw_start; | |||
365 | ||||
366 | auto language = exe_ctx.GetFrameRef().GetLanguage(); | |||
367 | ||||
368 | Status error; | |||
369 | lldb::UserExpressionSP expr(target->GetUserExpressionForLanguage( | |||
370 | code, llvm::StringRef(), language, UserExpression::eResultTypeAny, | |||
371 | options, error)); | |||
372 | if (error.Fail()) | |||
373 | return 0; | |||
374 | ||||
375 | expr->Complete(exe_ctx, request, cursor_pos); | |||
376 | return request.GetNumberOfMatches(); | |||
377 | } | |||
378 | ||||
379 | static lldb_private::Status | |||
380 | CanBeUsedForElementCountPrinting(ValueObject &valobj) { | |||
381 | CompilerType type(valobj.GetCompilerType()); | |||
382 | CompilerType pointee; | |||
383 | if (!type.IsPointerType(&pointee)) | |||
384 | return Status("as it does not refer to a pointer"); | |||
385 | if (pointee.IsVoidType()) | |||
386 | return Status("as it refers to a pointer to void"); | |||
387 | return Status(); | |||
388 | } | |||
389 | ||||
390 | bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, | |||
391 | Stream *output_stream, | |||
392 | Stream *error_stream, | |||
393 | CommandReturnObject *result) { | |||
394 | // Don't use m_exe_ctx as this might be called asynchronously after the | |||
395 | // command object DoExecute has finished when doing multi-line expression | |||
396 | // that use an input reader... | |||
397 | ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); | |||
398 | ||||
399 | Target *target = exe_ctx.GetTargetPtr(); | |||
400 | ||||
401 | if (!target) | |||
402 | target = GetDummyTarget(); | |||
403 | ||||
404 | if (target) { | |||
405 | lldb::ValueObjectSP result_valobj_sp; | |||
406 | bool keep_in_memory = true; | |||
407 | StackFrame *frame = exe_ctx.GetFramePtr(); | |||
408 | ||||
409 | EvaluateExpressionOptions options; | |||
410 | options.SetCoerceToId(m_varobj_options.use_objc); | |||
411 | options.SetUnwindOnError(m_command_options.unwind_on_error); | |||
412 | options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints); | |||
413 | options.SetKeepInMemory(keep_in_memory); | |||
414 | options.SetUseDynamic(m_varobj_options.use_dynamic); | |||
415 | options.SetTryAllThreads(m_command_options.try_all_threads); | |||
416 | options.SetDebug(m_command_options.debug); | |||
417 | options.SetLanguage(m_command_options.language); | |||
418 | options.SetExecutionPolicy( | |||
419 | m_command_options.allow_jit | |||
420 | ? EvaluateExpressionOptions::default_execution_policy | |||
421 | : lldb_private::eExecutionPolicyNever); | |||
422 | ||||
423 | bool auto_apply_fixits; | |||
424 | if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) | |||
425 | auto_apply_fixits = target->GetEnableAutoApplyFixIts(); | |||
426 | else | |||
427 | auto_apply_fixits = | |||
428 | m_command_options.auto_apply_fixits == eLazyBoolYes ? true : false; | |||
429 | ||||
430 | options.SetAutoApplyFixIts(auto_apply_fixits); | |||
431 | ||||
432 | if (m_command_options.top_level) | |||
433 | options.SetExecutionPolicy(eExecutionPolicyTopLevel); | |||
434 | ||||
435 | // If there is any chance we are going to stop and want to see what went | |||
436 | // wrong with our expression, we should generate debug info | |||
437 | if (!m_command_options.ignore_breakpoints || | |||
438 | !m_command_options.unwind_on_error) | |||
439 | options.SetGenerateDebugInfo(true); | |||
440 | ||||
441 | if (m_command_options.timeout > 0) | |||
442 | options.SetTimeout(std::chrono::microseconds(m_command_options.timeout)); | |||
443 | else | |||
444 | options.SetTimeout(llvm::None); | |||
445 | ||||
446 | ExpressionResults success = target->EvaluateExpression( | |||
447 | expr, frame, result_valobj_sp, options, &m_fixed_expression); | |||
448 | ||||
449 | // We only tell you about the FixIt if we applied it. The compiler errors | |||
450 | // will suggest the FixIt if it parsed. | |||
451 | if (error_stream && !m_fixed_expression.empty() && | |||
452 | target->GetEnableNotifyAboutFixIts()) { | |||
453 | if (success == eExpressionCompleted) | |||
454 | error_stream->Printf( | |||
455 | " Fix-it applied, fixed expression was: \n %s\n", | |||
456 | m_fixed_expression.c_str()); | |||
457 | } | |||
458 | ||||
459 | if (result_valobj_sp) { | |||
460 | Format format = m_format_options.GetFormat(); | |||
461 | ||||
462 | if (result_valobj_sp->GetError().Success()) { | |||
463 | if (format != eFormatVoid) { | |||
464 | if (format != eFormatDefault) | |||
465 | result_valobj_sp->SetFormat(format); | |||
466 | ||||
467 | if (m_varobj_options.elem_count > 0) { | |||
468 | Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); | |||
469 | if (error.Fail()) { | |||
470 | result->AppendErrorWithFormat( | |||
471 | "expression cannot be used with --element-count %s\n", | |||
472 | error.AsCString("")); | |||
473 | result->SetStatus(eReturnStatusFailed); | |||
474 | return false; | |||
475 | } | |||
476 | } | |||
477 | ||||
478 | DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( | |||
479 | m_command_options.m_verbosity, format)); | |||
480 | options.SetVariableFormatDisplayLanguage( | |||
481 | result_valobj_sp->GetPreferredDisplayLanguage()); | |||
482 | ||||
483 | result_valobj_sp->Dump(*output_stream, options); | |||
484 | ||||
485 | if (result) | |||
486 | result->SetStatus(eReturnStatusSuccessFinishResult); | |||
487 | } | |||
488 | } else { | |||
489 | if (result_valobj_sp->GetError().GetError() == | |||
490 | UserExpression::kNoResult) { | |||
491 | if (format != eFormatVoid && | |||
492 | m_interpreter.GetDebugger().GetNotifyVoid()) { | |||
493 | error_stream->PutCString("(void)\n"); | |||
494 | } | |||
495 | ||||
496 | if (result) | |||
497 | result->SetStatus(eReturnStatusSuccessFinishResult); | |||
498 | } else { | |||
499 | const char *error_cstr = result_valobj_sp->GetError().AsCString(); | |||
500 | if (error_cstr && error_cstr[0]) { | |||
501 | const size_t error_cstr_len = strlen(error_cstr); | |||
502 | const bool ends_with_newline = | |||
503 | error_cstr[error_cstr_len - 1] == '\n'; | |||
504 | if (strstr(error_cstr, "error:") != error_cstr) | |||
505 | error_stream->PutCString("error: "); | |||
| ||||
506 | error_stream->Write(error_cstr, error_cstr_len); | |||
507 | if (!ends_with_newline) | |||
508 | error_stream->EOL(); | |||
509 | } else { | |||
510 | error_stream->PutCString("error: unknown error\n"); | |||
511 | } | |||
512 | ||||
513 | if (result) | |||
514 | result->SetStatus(eReturnStatusFailed); | |||
515 | } | |||
516 | } | |||
517 | } | |||
518 | } else { | |||
519 | error_stream->Printf("error: invalid execution context for expression\n"); | |||
520 | return false; | |||
521 | } | |||
522 | ||||
523 | return true; | |||
524 | } | |||
525 | ||||
526 | void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, | |||
527 | std::string &line) { | |||
528 | io_handler.SetIsDone(true); | |||
529 | // StreamSP output_stream = | |||
530 | // io_handler.GetDebugger().GetAsyncOutputStream(); | |||
531 | // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); | |||
532 | StreamFileSP output_sp(io_handler.GetOutputStreamFile()); | |||
533 | StreamFileSP error_sp(io_handler.GetErrorStreamFile()); | |||
| ||||
534 | ||||
535 | EvaluateExpression(line.c_str(), output_sp.get(), error_sp.get()); | |||
536 | if (output_sp) | |||
537 | output_sp->Flush(); | |||
538 | if (error_sp) | |||
539 | error_sp->Flush(); | |||
540 | } | |||
541 | ||||
542 | bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, | |||
543 | StringList &lines) { | |||
544 | // An empty lines is used to indicate the end of input | |||
545 | const size_t num_lines = lines.GetSize(); | |||
546 | if (num_lines > 0 && lines[num_lines - 1].empty()) { | |||
547 | // Remove the last empty line from "lines" so it doesn't appear in our | |||
548 | // resulting input and return true to indicate we are done getting lines | |||
549 | lines.PopBack(); | |||
550 | return true; | |||
551 | } | |||
552 | return false; | |||
553 | } | |||
554 | ||||
555 | void CommandObjectExpression::GetMultilineExpression() { | |||
556 | m_expr_lines.clear(); | |||
557 | m_expr_line_count = 0; | |||
558 | ||||
559 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); | |||
560 | bool color_prompt = debugger.GetUseColor(); | |||
561 | const bool multiple_lines = true; // Get multiple lines | |||
562 | IOHandlerSP io_handler_sp( | |||
563 | new IOHandlerEditline(debugger, IOHandler::Type::Expression, | |||
564 | "lldb-expr", // Name of input reader for history | |||
565 | llvm::StringRef(), // No prompt | |||
566 | llvm::StringRef(), // Continuation prompt | |||
567 | multiple_lines, color_prompt, | |||
568 | 1, // Show line numbers starting at 1 | |||
569 | *this)); | |||
570 | ||||
571 | StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); | |||
572 | if (output_sp) { | |||
573 | output_sp->PutCString( | |||
574 | "Enter expressions, then terminate with an empty line to evaluate:\n"); | |||
575 | output_sp->Flush(); | |||
576 | } | |||
577 | debugger.PushIOHandler(io_handler_sp); | |||
578 | } | |||
579 | ||||
580 | bool CommandObjectExpression::DoExecute(llvm::StringRef command, | |||
581 | CommandReturnObject &result) { | |||
582 | m_fixed_expression.clear(); | |||
583 | auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); | |||
584 | m_option_group.NotifyOptionParsingStarting(&exe_ctx); | |||
585 | ||||
586 | if (command.empty()) { | |||
587 | GetMultilineExpression(); | |||
588 | return result.Succeeded(); | |||
589 | } | |||
590 | ||||
591 | OptionsWithRaw args(command); | |||
592 | llvm::StringRef expr = args.GetRawPart(); | |||
593 | ||||
594 | if (args.HasArgs()) { | |||
595 | if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx)) | |||
596 | return false; | |||
597 | ||||
598 | if (m_repl_option.GetOptionValue().GetCurrentValue()) { | |||
599 | Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); | |||
600 | if (target) { | |||
601 | // Drop into REPL | |||
602 | m_expr_lines.clear(); | |||
603 | m_expr_line_count = 0; | |||
604 | ||||
605 | Debugger &debugger = target->GetDebugger(); | |||
606 | ||||
607 | // Check if the LLDB command interpreter is sitting on top of a REPL | |||
608 | // that launched it... | |||
609 | if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, | |||
610 | IOHandler::Type::REPL)) { | |||
611 | // the LLDB command interpreter is sitting on top of a REPL that | |||
612 | // launched it, so just say the command interpreter is done and | |||
613 | // fall back to the existing REPL | |||
614 | m_interpreter.GetIOHandler(false)->SetIsDone(true); | |||
615 | } else { | |||
616 | // We are launching the REPL on top of the current LLDB command | |||
617 | // interpreter, so just push one | |||
618 | bool initialize = false; | |||
619 | Status repl_error; | |||
620 | REPLSP repl_sp(target->GetREPL(repl_error, m_command_options.language, | |||
621 | nullptr, false)); | |||
622 | ||||
623 | if (!repl_sp) { | |||
624 | initialize = true; | |||
625 | repl_sp = target->GetREPL(repl_error, m_command_options.language, | |||
626 | nullptr, true); | |||
627 | if (!repl_error.Success()) { | |||
628 | result.SetError(repl_error); | |||
629 | return result.Succeeded(); | |||
630 | } | |||
631 | } | |||
632 | ||||
633 | if (repl_sp) { | |||
634 | if (initialize) { | |||
635 | repl_sp->SetCommandOptions(m_command_options); | |||
636 | repl_sp->SetFormatOptions(m_format_options); | |||
637 | repl_sp->SetValueObjectDisplayOptions(m_varobj_options); | |||
638 | } | |||
639 | ||||
640 | IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); | |||
641 | ||||
642 | io_handler_sp->SetIsDone(false); | |||
643 | ||||
644 | debugger.PushIOHandler(io_handler_sp); | |||
645 | } else { | |||
646 | repl_error.SetErrorStringWithFormat( | |||
647 | "Couldn't create a REPL for %s", | |||
648 | Language::GetNameForLanguageType(m_command_options.language)); | |||
649 | result.SetError(repl_error); | |||
650 | return result.Succeeded(); | |||
651 | } | |||
652 | } | |||
653 | } | |||
654 | } | |||
655 | // No expression following options | |||
656 | else if (expr.empty()) { | |||
657 | GetMultilineExpression(); | |||
658 | return result.Succeeded(); | |||
659 | } | |||
660 | } | |||
661 | ||||
662 | Target *target = GetSelectedOrDummyTarget(); | |||
663 | if (EvaluateExpression(expr, &(result.GetOutputStream()), | |||
664 | &(result.GetErrorStream()), &result)) { | |||
665 | ||||
666 | if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) { | |||
667 | CommandHistory &history = m_interpreter.GetCommandHistory(); | |||
668 | // FIXME: Can we figure out what the user actually typed (e.g. some alias | |||
669 | // for expr???) | |||
670 | // If we can it would be nice to show that. | |||
671 | std::string fixed_command("expression "); | |||
672 | if (args.HasArgs()) { | |||
673 | // Add in any options that might have been in the original command: | |||
674 | fixed_command.append(args.GetArgStringWithDelimiter()); | |||
675 | fixed_command.append(m_fixed_expression); | |||
676 | } else | |||
677 | fixed_command.append(m_fixed_expression); | |||
678 | history.AppendString(fixed_command); | |||
679 | } | |||
680 | // Increment statistics to record this expression evaluation success. | |||
681 | target->IncrementStats(StatisticKind::ExpressionSuccessful); | |||
682 | return true; | |||
683 | } | |||
684 | ||||
685 | // Increment statistics to record this expression evaluation failure. | |||
686 | target->IncrementStats(StatisticKind::ExpressionFailure); | |||
687 | result.SetStatus(eReturnStatusFailed); | |||
688 | return false; | |||
689 | } |
1 | // shared_ptr and weak_ptr implementation -*- C++ -*- |
2 | |
3 | // Copyright (C) 2007-2016 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library 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 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
26 | |
27 | // shared_count.hpp |
28 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. |
29 | |
30 | // shared_ptr.hpp |
31 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. |
32 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
33 | |
34 | // weak_ptr.hpp |
35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
36 | |
37 | // enable_shared_from_this.hpp |
38 | // Copyright (C) 2002 Peter Dimov |
39 | |
40 | // Distributed under the Boost Software License, Version 1.0. (See |
41 | // accompanying file LICENSE_1_0.txt or copy at |
42 | // http://www.boost.org/LICENSE_1_0.txt) |
43 | |
44 | /** @file |
45 | * This is an internal header file, included by other library headers. |
46 | * Do not attempt to use it directly. @headername{memory} |
47 | */ |
48 | |
49 | #ifndef _SHARED_PTR_H1 |
50 | #define _SHARED_PTR_H1 1 |
51 | |
52 | #include <bits/shared_ptr_base.h> |
53 | |
54 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
55 | { |
56 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
57 | |
58 | /** |
59 | * @addtogroup pointer_abstractions |
60 | * @{ |
61 | */ |
62 | |
63 | /// 20.7.2.2.11 shared_ptr I/O |
64 | template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp> |
65 | inline std::basic_ostream<_Ch, _Tr>& |
66 | operator<<(std::basic_ostream<_Ch, _Tr>& __os, |
67 | const __shared_ptr<_Tp, _Lp>& __p) |
68 | { |
69 | __os << __p.get(); |
70 | return __os; |
71 | } |
72 | |
73 | /// 20.7.2.2.10 shared_ptr get_deleter |
74 | template<typename _Del, typename _Tp, _Lock_policy _Lp> |
75 | inline _Del* |
76 | get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept |
77 | { |
78 | #if __cpp_rtti199711L |
79 | return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); |
80 | #else |
81 | return 0; |
82 | #endif |
83 | } |
84 | |
85 | |
86 | /** |
87 | * @brief A smart pointer with reference-counted copy semantics. |
88 | * |
89 | * The object pointed to is deleted when the last shared_ptr pointing to |
90 | * it is destroyed or reset. |
91 | */ |
92 | template<typename _Tp> |
93 | class shared_ptr : public __shared_ptr<_Tp> |
94 | { |
95 | template<typename _Ptr> |
96 | using _Convertible |
97 | = typename enable_if<is_convertible<_Ptr, _Tp*>::value>::type; |
98 | |
99 | public: |
100 | /** |
101 | * @brief Construct an empty %shared_ptr. |
102 | * @post use_count()==0 && get()==0 |
103 | */ |
104 | constexpr shared_ptr() noexcept |
105 | : __shared_ptr<_Tp>() { } |
106 | |
107 | shared_ptr(const shared_ptr&) noexcept = default; |
108 | |
109 | /** |
110 | * @brief Construct a %shared_ptr that owns the pointer @a __p. |
111 | * @param __p A pointer that is convertible to element_type*. |
112 | * @post use_count() == 1 && get() == __p |
113 | * @throw std::bad_alloc, in which case @c delete @a __p is called. |
114 | */ |
115 | template<typename _Tp1> |
116 | explicit shared_ptr(_Tp1* __p) |
117 | : __shared_ptr<_Tp>(__p) { } |
118 | |
119 | /** |
120 | * @brief Construct a %shared_ptr that owns the pointer @a __p |
121 | * and the deleter @a __d. |
122 | * @param __p A pointer. |
123 | * @param __d A deleter. |
124 | * @post use_count() == 1 && get() == __p |
125 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
126 | * |
127 | * Requirements: _Deleter's copy constructor and destructor must |
128 | * not throw |
129 | * |
130 | * __shared_ptr will release __p by calling __d(__p) |
131 | */ |
132 | template<typename _Tp1, typename _Deleter> |
133 | shared_ptr(_Tp1* __p, _Deleter __d) |
134 | : __shared_ptr<_Tp>(__p, __d) { } |
135 | |
136 | /** |
137 | * @brief Construct a %shared_ptr that owns a null pointer |
138 | * and the deleter @a __d. |
139 | * @param __p A null pointer constant. |
140 | * @param __d A deleter. |
141 | * @post use_count() == 1 && get() == __p |
142 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
143 | * |
144 | * Requirements: _Deleter's copy constructor and destructor must |
145 | * not throw |
146 | * |
147 | * The last owner will call __d(__p) |
148 | */ |
149 | template<typename _Deleter> |
150 | shared_ptr(nullptr_t __p, _Deleter __d) |
151 | : __shared_ptr<_Tp>(__p, __d) { } |
152 | |
153 | /** |
154 | * @brief Construct a %shared_ptr that owns the pointer @a __p |
155 | * and the deleter @a __d. |
156 | * @param __p A pointer. |
157 | * @param __d A deleter. |
158 | * @param __a An allocator. |
159 | * @post use_count() == 1 && get() == __p |
160 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
161 | * |
162 | * Requirements: _Deleter's copy constructor and destructor must |
163 | * not throw _Alloc's copy constructor and destructor must not |
164 | * throw. |
165 | * |
166 | * __shared_ptr will release __p by calling __d(__p) |
167 | */ |
168 | template<typename _Tp1, typename _Deleter, typename _Alloc> |
169 | shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a) |
170 | : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { } |
171 | |
172 | /** |
173 | * @brief Construct a %shared_ptr that owns a null pointer |
174 | * and the deleter @a __d. |
175 | * @param __p A null pointer constant. |
176 | * @param __d A deleter. |
177 | * @param __a An allocator. |
178 | * @post use_count() == 1 && get() == __p |
179 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
180 | * |
181 | * Requirements: _Deleter's copy constructor and destructor must |
182 | * not throw _Alloc's copy constructor and destructor must not |
183 | * throw. |
184 | * |
185 | * The last owner will call __d(__p) |
186 | */ |
187 | template<typename _Deleter, typename _Alloc> |
188 | shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
189 | : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { } |
190 | |
191 | // Aliasing constructor |
192 | |
193 | /** |
194 | * @brief Constructs a %shared_ptr instance that stores @a __p |
195 | * and shares ownership with @a __r. |
196 | * @param __r A %shared_ptr. |
197 | * @param __p A pointer that will remain valid while @a *__r is valid. |
198 | * @post get() == __p && use_count() == __r.use_count() |
199 | * |
200 | * This can be used to construct a @c shared_ptr to a sub-object |
201 | * of an object managed by an existing @c shared_ptr. |
202 | * |
203 | * @code |
204 | * shared_ptr< pair<int,int> > pii(new pair<int,int>()); |
205 | * shared_ptr<int> pi(pii, &pii->first); |
206 | * assert(pii.use_count() == 2); |
207 | * @endcode |
208 | */ |
209 | template<typename _Tp1> |
210 | shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p) noexcept |
211 | : __shared_ptr<_Tp>(__r, __p) { } |
212 | |
213 | /** |
214 | * @brief If @a __r is empty, constructs an empty %shared_ptr; |
215 | * otherwise construct a %shared_ptr that shares ownership |
216 | * with @a __r. |
217 | * @param __r A %shared_ptr. |
218 | * @post get() == __r.get() && use_count() == __r.use_count() |
219 | */ |
220 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
221 | shared_ptr(const shared_ptr<_Tp1>& __r) noexcept |
222 | : __shared_ptr<_Tp>(__r) { } |
223 | |
224 | /** |
225 | * @brief Move-constructs a %shared_ptr instance from @a __r. |
226 | * @param __r A %shared_ptr rvalue. |
227 | * @post *this contains the old value of @a __r, @a __r is empty. |
228 | */ |
229 | shared_ptr(shared_ptr&& __r) noexcept |
230 | : __shared_ptr<_Tp>(std::move(__r)) { } |
231 | |
232 | /** |
233 | * @brief Move-constructs a %shared_ptr instance from @a __r. |
234 | * @param __r A %shared_ptr rvalue. |
235 | * @post *this contains the old value of @a __r, @a __r is empty. |
236 | */ |
237 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
238 | shared_ptr(shared_ptr<_Tp1>&& __r) noexcept |
239 | : __shared_ptr<_Tp>(std::move(__r)) { } |
240 | |
241 | /** |
242 | * @brief Constructs a %shared_ptr that shares ownership with @a __r |
243 | * and stores a copy of the pointer stored in @a __r. |
244 | * @param __r A weak_ptr. |
245 | * @post use_count() == __r.use_count() |
246 | * @throw bad_weak_ptr when __r.expired(), |
247 | * in which case the constructor has no effect. |
248 | */ |
249 | template<typename _Tp1> |
250 | explicit shared_ptr(const weak_ptr<_Tp1>& __r) |
251 | : __shared_ptr<_Tp>(__r) { } |
252 | |
253 | #if _GLIBCXX_USE_DEPRECATED1 |
254 | template<typename _Tp1> |
255 | shared_ptr(std::auto_ptr<_Tp1>&& __r); |
256 | #endif |
257 | |
258 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
259 | // 2399. shared_ptr's constructor from unique_ptr should be constrained |
260 | template<typename _Tp1, typename _Del, typename |
261 | = _Convertible<typename unique_ptr<_Tp1, _Del>::pointer>> |
262 | shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r) |
263 | : __shared_ptr<_Tp>(std::move(__r)) { } |
264 | |
265 | /** |
266 | * @brief Construct an empty %shared_ptr. |
267 | * @post use_count() == 0 && get() == nullptr |
268 | */ |
269 | constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { } |
270 | |
271 | shared_ptr& operator=(const shared_ptr&) noexcept = default; |
272 | |
273 | template<typename _Tp1> |
274 | shared_ptr& |
275 | operator=(const shared_ptr<_Tp1>& __r) noexcept |
276 | { |
277 | this->__shared_ptr<_Tp>::operator=(__r); |
278 | return *this; |
279 | } |
280 | |
281 | #if _GLIBCXX_USE_DEPRECATED1 |
282 | template<typename _Tp1> |
283 | shared_ptr& |
284 | operator=(std::auto_ptr<_Tp1>&& __r) |
285 | { |
286 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
287 | return *this; |
288 | } |
289 | #endif |
290 | |
291 | shared_ptr& |
292 | operator=(shared_ptr&& __r) noexcept |
293 | { |
294 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
295 | return *this; |
296 | } |
297 | |
298 | template<class _Tp1> |
299 | shared_ptr& |
300 | operator=(shared_ptr<_Tp1>&& __r) noexcept |
301 | { |
302 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
303 | return *this; |
304 | } |
305 | |
306 | template<typename _Tp1, typename _Del> |
307 | shared_ptr& |
308 | operator=(std::unique_ptr<_Tp1, _Del>&& __r) |
309 | { |
310 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
311 | return *this; |
312 | } |
313 | |
314 | private: |
315 | // This constructor is non-standard, it is used by allocate_shared. |
316 | template<typename _Alloc, typename... _Args> |
317 | shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
318 | _Args&&... __args) |
319 | : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...) |
320 | { } |
321 | |
322 | template<typename _Tp1, typename _Alloc, typename... _Args> |
323 | friend shared_ptr<_Tp1> |
324 | allocate_shared(const _Alloc& __a, _Args&&... __args); |
325 | |
326 | // This constructor is non-standard, it is used by weak_ptr::lock(). |
327 | shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) |
328 | : __shared_ptr<_Tp>(__r, std::nothrow) { } |
329 | |
330 | friend class weak_ptr<_Tp>; |
331 | }; |
332 | |
333 | // 20.7.2.2.7 shared_ptr comparisons |
334 | template<typename _Tp1, typename _Tp2> |
335 | inline bool |
336 | operator==(const shared_ptr<_Tp1>& __a, |
337 | const shared_ptr<_Tp2>& __b) noexcept |
338 | { return __a.get() == __b.get(); } |
339 | |
340 | template<typename _Tp> |
341 | inline bool |
342 | operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
343 | { return !__a; } |
344 | |
345 | template<typename _Tp> |
346 | inline bool |
347 | operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
348 | { return !__a; } |
349 | |
350 | template<typename _Tp1, typename _Tp2> |
351 | inline bool |
352 | operator!=(const shared_ptr<_Tp1>& __a, |
353 | const shared_ptr<_Tp2>& __b) noexcept |
354 | { return __a.get() != __b.get(); } |
355 | |
356 | template<typename _Tp> |
357 | inline bool |
358 | operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
359 | { return (bool)__a; } |
360 | |
361 | template<typename _Tp> |
362 | inline bool |
363 | operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
364 | { return (bool)__a; } |
365 | |
366 | template<typename _Tp1, typename _Tp2> |
367 | inline bool |
368 | operator<(const shared_ptr<_Tp1>& __a, |
369 | const shared_ptr<_Tp2>& __b) noexcept |
370 | { |
371 | typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT; |
372 | return std::less<_CT>()(__a.get(), __b.get()); |
373 | } |
374 | |
375 | template<typename _Tp> |
376 | inline bool |
377 | operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
378 | { return std::less<_Tp*>()(__a.get(), nullptr); } |
379 | |
380 | template<typename _Tp> |
381 | inline bool |
382 | operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
383 | { return std::less<_Tp*>()(nullptr, __a.get()); } |
384 | |
385 | template<typename _Tp1, typename _Tp2> |
386 | inline bool |
387 | operator<=(const shared_ptr<_Tp1>& __a, |
388 | const shared_ptr<_Tp2>& __b) noexcept |
389 | { return !(__b < __a); } |
390 | |
391 | template<typename _Tp> |
392 | inline bool |
393 | operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
394 | { return !(nullptr < __a); } |
395 | |
396 | template<typename _Tp> |
397 | inline bool |
398 | operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
399 | { return !(__a < nullptr); } |
400 | |
401 | template<typename _Tp1, typename _Tp2> |
402 | inline bool |
403 | operator>(const shared_ptr<_Tp1>& __a, |
404 | const shared_ptr<_Tp2>& __b) noexcept |
405 | { return (__b < __a); } |
406 | |
407 | template<typename _Tp> |
408 | inline bool |
409 | operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
410 | { return std::less<_Tp*>()(nullptr, __a.get()); } |
411 | |
412 | template<typename _Tp> |
413 | inline bool |
414 | operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
415 | { return std::less<_Tp*>()(__a.get(), nullptr); } |
416 | |
417 | template<typename _Tp1, typename _Tp2> |
418 | inline bool |
419 | operator>=(const shared_ptr<_Tp1>& __a, |
420 | const shared_ptr<_Tp2>& __b) noexcept |
421 | { return !(__a < __b); } |
422 | |
423 | template<typename _Tp> |
424 | inline bool |
425 | operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
426 | { return !(__a < nullptr); } |
427 | |
428 | template<typename _Tp> |
429 | inline bool |
430 | operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
431 | { return !(nullptr < __a); } |
432 | |
433 | template<typename _Tp> |
434 | struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>> |
435 | { }; |
436 | |
437 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
438 | template<typename _Tp> |
439 | inline void |
440 | swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept |
441 | { __a.swap(__b); } |
442 | |
443 | // 20.7.2.2.9 shared_ptr casts. |
444 | template<typename _Tp, typename _Tp1> |
445 | inline shared_ptr<_Tp> |
446 | static_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept |
447 | { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); } |
448 | |
449 | template<typename _Tp, typename _Tp1> |
450 | inline shared_ptr<_Tp> |
451 | const_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept |
452 | { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); } |
453 | |
454 | template<typename _Tp, typename _Tp1> |
455 | inline shared_ptr<_Tp> |
456 | dynamic_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept |
457 | { |
458 | if (_Tp* __p = dynamic_cast<_Tp*>(__r.get())) |
459 | return shared_ptr<_Tp>(__r, __p); |
460 | return shared_ptr<_Tp>(); |
461 | } |
462 | |
463 | |
464 | /** |
465 | * @brief A smart pointer with weak semantics. |
466 | * |
467 | * With forwarding constructors and assignment operators. |
468 | */ |
469 | template<typename _Tp> |
470 | class weak_ptr : public __weak_ptr<_Tp> |
471 | { |
472 | template<typename _Ptr> |
473 | using _Convertible |
474 | = typename enable_if<is_convertible<_Ptr, _Tp*>::value>::type; |
475 | |
476 | public: |
477 | constexpr weak_ptr() noexcept = default; |
478 | |
479 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
480 | weak_ptr(const shared_ptr<_Tp1>& __r) noexcept |
481 | : __weak_ptr<_Tp>(__r) { } |
482 | |
483 | weak_ptr(const weak_ptr&) noexcept = default; |
484 | |
485 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
486 | weak_ptr(const weak_ptr<_Tp1>& __r) noexcept |
487 | : __weak_ptr<_Tp>(__r) { } |
488 | |
489 | weak_ptr(weak_ptr&&) noexcept = default; |
490 | |
491 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
492 | weak_ptr(weak_ptr<_Tp1>&& __r) noexcept |
493 | : __weak_ptr<_Tp>(std::move(__r)) { } |
494 | |
495 | weak_ptr& |
496 | operator=(const weak_ptr& __r) noexcept = default; |
497 | |
498 | template<typename _Tp1> |
499 | weak_ptr& |
500 | operator=(const weak_ptr<_Tp1>& __r) noexcept |
501 | { |
502 | this->__weak_ptr<_Tp>::operator=(__r); |
503 | return *this; |
504 | } |
505 | |
506 | template<typename _Tp1> |
507 | weak_ptr& |
508 | operator=(const shared_ptr<_Tp1>& __r) noexcept |
509 | { |
510 | this->__weak_ptr<_Tp>::operator=(__r); |
511 | return *this; |
512 | } |
513 | |
514 | weak_ptr& |
515 | operator=(weak_ptr&& __r) noexcept = default; |
516 | |
517 | template<typename _Tp1> |
518 | weak_ptr& |
519 | operator=(weak_ptr<_Tp1>&& __r) noexcept |
520 | { |
521 | this->__weak_ptr<_Tp>::operator=(std::move(__r)); |
522 | return *this; |
523 | } |
524 | |
525 | shared_ptr<_Tp> |
526 | lock() const noexcept |
527 | { return shared_ptr<_Tp>(*this, std::nothrow); } |
528 | }; |
529 | |
530 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
531 | template<typename _Tp> |
532 | inline void |
533 | swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept |
534 | { __a.swap(__b); } |
535 | |
536 | |
537 | /// Primary template owner_less |
538 | template<typename _Tp> |
539 | struct owner_less; |
540 | |
541 | /// Partial specialization of owner_less for shared_ptr. |
542 | template<typename _Tp> |
543 | struct owner_less<shared_ptr<_Tp>> |
544 | : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>> |
545 | { }; |
546 | |
547 | /// Partial specialization of owner_less for weak_ptr. |
548 | template<typename _Tp> |
549 | struct owner_less<weak_ptr<_Tp>> |
550 | : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>> |
551 | { }; |
552 | |
553 | /** |
554 | * @brief Base class allowing use of member function shared_from_this. |
555 | */ |
556 | template<typename _Tp> |
557 | class enable_shared_from_this |
558 | { |
559 | protected: |
560 | constexpr enable_shared_from_this() noexcept { } |
561 | |
562 | enable_shared_from_this(const enable_shared_from_this&) noexcept { } |
563 | |
564 | enable_shared_from_this& |
565 | operator=(const enable_shared_from_this&) noexcept |
566 | { return *this; } |
567 | |
568 | ~enable_shared_from_this() { } |
569 | |
570 | public: |
571 | shared_ptr<_Tp> |
572 | shared_from_this() |
573 | { return shared_ptr<_Tp>(this->_M_weak_this); } |
574 | |
575 | shared_ptr<const _Tp> |
576 | shared_from_this() const |
577 | { return shared_ptr<const _Tp>(this->_M_weak_this); } |
578 | |
579 | private: |
580 | template<typename _Tp1> |
581 | void |
582 | _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept |
583 | { _M_weak_this._M_assign(__p, __n); } |
584 | |
585 | template<typename _Tp1, typename _Tp2> |
586 | friend void |
587 | __enable_shared_from_this_helper(const __shared_count<>&, |
588 | const enable_shared_from_this<_Tp1>*, |
589 | const _Tp2*) noexcept; |
590 | |
591 | mutable weak_ptr<_Tp> _M_weak_this; |
592 | }; |
593 | |
594 | template<typename _Tp1, typename _Tp2> |
595 | inline void |
596 | __enable_shared_from_this_helper(const __shared_count<>& __pn, |
597 | const enable_shared_from_this<_Tp1>* |
598 | __pe, const _Tp2* __px) noexcept |
599 | { |
600 | if (__pe != nullptr) |
601 | __pe->_M_weak_assign(const_cast<_Tp2*>(__px), __pn); |
602 | } |
603 | |
604 | /** |
605 | * @brief Create an object that is owned by a shared_ptr. |
606 | * @param __a An allocator. |
607 | * @param __args Arguments for the @a _Tp object's constructor. |
608 | * @return A shared_ptr that owns the newly created object. |
609 | * @throw An exception thrown from @a _Alloc::allocate or from the |
610 | * constructor of @a _Tp. |
611 | * |
612 | * A copy of @a __a will be used to allocate memory for the shared_ptr |
613 | * and the new object. |
614 | */ |
615 | template<typename _Tp, typename _Alloc, typename... _Args> |
616 | inline shared_ptr<_Tp> |
617 | allocate_shared(const _Alloc& __a, _Args&&... __args) |
618 | { |
619 | return shared_ptr<_Tp>(_Sp_make_shared_tag(), __a, |
620 | std::forward<_Args>(__args)...); |
621 | } |
622 | |
623 | /** |
624 | * @brief Create an object that is owned by a shared_ptr. |
625 | * @param __args Arguments for the @a _Tp object's constructor. |
626 | * @return A shared_ptr that owns the newly created object. |
627 | * @throw std::bad_alloc, or an exception thrown from the |
628 | * constructor of @a _Tp. |
629 | */ |
630 | template<typename _Tp, typename... _Args> |
631 | inline shared_ptr<_Tp> |
632 | make_shared(_Args&&... __args) |
633 | { |
634 | typedef typename std::remove_const<_Tp>::type _Tp_nc; |
635 | return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(), |
636 | std::forward<_Args>(__args)...); |
637 | } |
638 | |
639 | /// std::hash specialization for shared_ptr. |
640 | template<typename _Tp> |
641 | struct hash<shared_ptr<_Tp>> |
642 | : public __hash_base<size_t, shared_ptr<_Tp>> |
643 | { |
644 | size_t |
645 | operator()(const shared_ptr<_Tp>& __s) const noexcept |
646 | { return std::hash<_Tp*>()(__s.get()); } |
647 | }; |
648 | |
649 | // @} group pointer_abstractions |
650 | |
651 | _GLIBCXX_END_NAMESPACE_VERSION |
652 | } // namespace |
653 | |
654 | #endif // _SHARED_PTR_H |
1 | // shared_ptr and weak_ptr implementation details -*- C++ -*- |
2 | |
3 | // Copyright (C) 2007-2016 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library 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 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
26 | |
27 | // shared_count.hpp |
28 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. |
29 | |
30 | // shared_ptr.hpp |
31 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. |
32 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
33 | |
34 | // weak_ptr.hpp |
35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
36 | |
37 | // enable_shared_from_this.hpp |
38 | // Copyright (C) 2002 Peter Dimov |
39 | |
40 | // Distributed under the Boost Software License, Version 1.0. (See |
41 | // accompanying file LICENSE_1_0.txt or copy at |
42 | // http://www.boost.org/LICENSE_1_0.txt) |
43 | |
44 | /** @file bits/shared_ptr_base.h |
45 | * This is an internal header file, included by other library headers. |
46 | * Do not attempt to use it directly. @headername{memory} |
47 | */ |
48 | |
49 | #ifndef _SHARED_PTR_BASE_H1 |
50 | #define _SHARED_PTR_BASE_H1 1 |
51 | |
52 | #include <typeinfo> |
53 | #include <bits/allocated_ptr.h> |
54 | #include <ext/aligned_buffer.h> |
55 | |
56 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
57 | { |
58 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
59 | |
60 | #if _GLIBCXX_USE_DEPRECATED1 |
61 | template<typename> class auto_ptr; |
62 | #endif |
63 | |
64 | /** |
65 | * @brief Exception possibly thrown by @c shared_ptr. |
66 | * @ingroup exceptions |
67 | */ |
68 | class bad_weak_ptr : public std::exception |
69 | { |
70 | public: |
71 | virtual char const* what() const noexcept; |
72 | |
73 | virtual ~bad_weak_ptr() noexcept; |
74 | }; |
75 | |
76 | // Substitute for bad_weak_ptr object in the case of -fno-exceptions. |
77 | inline void |
78 | __throw_bad_weak_ptr() |
79 | { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr())(__builtin_abort()); } |
80 | |
81 | using __gnu_cxx::_Lock_policy; |
82 | using __gnu_cxx::__default_lock_policy; |
83 | using __gnu_cxx::_S_single; |
84 | using __gnu_cxx::_S_mutex; |
85 | using __gnu_cxx::_S_atomic; |
86 | |
87 | // Empty helper class except when the template argument is _S_mutex. |
88 | template<_Lock_policy _Lp> |
89 | class _Mutex_base |
90 | { |
91 | protected: |
92 | // The atomic policy uses fully-fenced builtins, single doesn't care. |
93 | enum { _S_need_barriers = 0 }; |
94 | }; |
95 | |
96 | template<> |
97 | class _Mutex_base<_S_mutex> |
98 | : public __gnu_cxx::__mutex |
99 | { |
100 | protected: |
101 | // This policy is used when atomic builtins are not available. |
102 | // The replacement atomic operations might not have the necessary |
103 | // memory barriers. |
104 | enum { _S_need_barriers = 1 }; |
105 | }; |
106 | |
107 | template<_Lock_policy _Lp = __default_lock_policy> |
108 | class _Sp_counted_base |
109 | : public _Mutex_base<_Lp> |
110 | { |
111 | public: |
112 | _Sp_counted_base() noexcept |
113 | : _M_use_count(1), _M_weak_count(1) { } |
114 | |
115 | virtual |
116 | ~_Sp_counted_base() noexcept |
117 | { } |
118 | |
119 | // Called when _M_use_count drops to zero, to release the resources |
120 | // managed by *this. |
121 | virtual void |
122 | _M_dispose() noexcept = 0; |
123 | |
124 | // Called when _M_weak_count drops to zero. |
125 | virtual void |
126 | _M_destroy() noexcept |
127 | { delete this; } |
128 | |
129 | virtual void* |
130 | _M_get_deleter(const std::type_info&) noexcept = 0; |
131 | |
132 | void |
133 | _M_add_ref_copy() |
134 | { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); } |
135 | |
136 | void |
137 | _M_add_ref_lock(); |
138 | |
139 | bool |
140 | _M_add_ref_lock_nothrow(); |
141 | |
142 | void |
143 | _M_release() noexcept |
144 | { |
145 | // Be race-detector-friendly. For more info see bits/c++config. |
146 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count); |
147 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1) |
148 | { |
149 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count); |
150 | _M_dispose(); |
151 | // There must be a memory barrier between dispose() and destroy() |
152 | // to ensure that the effects of dispose() are observed in the |
153 | // thread that runs destroy(). |
154 | // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html |
155 | if (_Mutex_base<_Lp>::_S_need_barriers) |
156 | { |
157 | __atomic_thread_fence (__ATOMIC_ACQ_REL4); |
158 | } |
159 | |
160 | // Be race-detector-friendly. For more info see bits/c++config. |
161 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
162 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, |
163 | -1) == 1) |
164 | { |
165 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
166 | _M_destroy(); |
167 | } |
168 | } |
169 | } |
170 | |
171 | void |
172 | _M_weak_add_ref() noexcept |
173 | { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); } |
174 | |
175 | void |
176 | _M_weak_release() noexcept |
177 | { |
178 | // Be race-detector-friendly. For more info see bits/c++config. |
179 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
180 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) |
181 | { |
182 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
183 | if (_Mutex_base<_Lp>::_S_need_barriers) |
184 | { |
185 | // See _M_release(), |
186 | // destroy() must observe results of dispose() |
187 | __atomic_thread_fence (__ATOMIC_ACQ_REL4); |
188 | } |
189 | _M_destroy(); |
190 | } |
191 | } |
192 | |
193 | long |
194 | _M_get_use_count() const noexcept |
195 | { |
196 | // No memory barrier is used here so there is no synchronization |
197 | // with other threads. |
198 | return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED0); |
199 | } |
200 | |
201 | private: |
202 | _Sp_counted_base(_Sp_counted_base const&) = delete; |
203 | _Sp_counted_base& operator=(_Sp_counted_base const&) = delete; |
204 | |
205 | _Atomic_word _M_use_count; // #shared |
206 | _Atomic_word _M_weak_count; // #weak + (#shared != 0) |
207 | }; |
208 | |
209 | template<> |
210 | inline void |
211 | _Sp_counted_base<_S_single>:: |
212 | _M_add_ref_lock() |
213 | { |
214 | if (_M_use_count == 0) |
215 | __throw_bad_weak_ptr(); |
216 | ++_M_use_count; |
217 | } |
218 | |
219 | template<> |
220 | inline void |
221 | _Sp_counted_base<_S_mutex>:: |
222 | _M_add_ref_lock() |
223 | { |
224 | __gnu_cxx::__scoped_lock sentry(*this); |
225 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
226 | { |
227 | _M_use_count = 0; |
228 | __throw_bad_weak_ptr(); |
229 | } |
230 | } |
231 | |
232 | template<> |
233 | inline void |
234 | _Sp_counted_base<_S_atomic>:: |
235 | _M_add_ref_lock() |
236 | { |
237 | // Perform lock-free add-if-not-zero operation. |
238 | _Atomic_word __count = _M_get_use_count(); |
239 | do |
240 | { |
241 | if (__count == 0) |
242 | __throw_bad_weak_ptr(); |
243 | // Replace the current counter value with the old value + 1, as |
244 | // long as it's not changed meanwhile. |
245 | } |
246 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
247 | true, __ATOMIC_ACQ_REL4, |
248 | __ATOMIC_RELAXED0)); |
249 | } |
250 | |
251 | template<> |
252 | inline bool |
253 | _Sp_counted_base<_S_single>:: |
254 | _M_add_ref_lock_nothrow() |
255 | { |
256 | if (_M_use_count == 0) |
257 | return false; |
258 | ++_M_use_count; |
259 | return true; |
260 | } |
261 | |
262 | template<> |
263 | inline bool |
264 | _Sp_counted_base<_S_mutex>:: |
265 | _M_add_ref_lock_nothrow() |
266 | { |
267 | __gnu_cxx::__scoped_lock sentry(*this); |
268 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
269 | { |
270 | _M_use_count = 0; |
271 | return false; |
272 | } |
273 | return true; |
274 | } |
275 | |
276 | template<> |
277 | inline bool |
278 | _Sp_counted_base<_S_atomic>:: |
279 | _M_add_ref_lock_nothrow() |
280 | { |
281 | // Perform lock-free add-if-not-zero operation. |
282 | _Atomic_word __count = _M_get_use_count(); |
283 | do |
284 | { |
285 | if (__count == 0) |
286 | return false; |
287 | // Replace the current counter value with the old value + 1, as |
288 | // long as it's not changed meanwhile. |
289 | } |
290 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
291 | true, __ATOMIC_ACQ_REL4, |
292 | __ATOMIC_RELAXED0)); |
293 | return true; |
294 | } |
295 | |
296 | template<> |
297 | inline void |
298 | _Sp_counted_base<_S_single>::_M_add_ref_copy() |
299 | { ++_M_use_count; } |
300 | |
301 | template<> |
302 | inline void |
303 | _Sp_counted_base<_S_single>::_M_release() noexcept |
304 | { |
305 | if (--_M_use_count == 0) |
306 | { |
307 | _M_dispose(); |
308 | if (--_M_weak_count == 0) |
309 | _M_destroy(); |
310 | } |
311 | } |
312 | |
313 | template<> |
314 | inline void |
315 | _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept |
316 | { ++_M_weak_count; } |
317 | |
318 | template<> |
319 | inline void |
320 | _Sp_counted_base<_S_single>::_M_weak_release() noexcept |
321 | { |
322 | if (--_M_weak_count == 0) |
323 | _M_destroy(); |
324 | } |
325 | |
326 | template<> |
327 | inline long |
328 | _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept |
329 | { return _M_use_count; } |
330 | |
331 | |
332 | // Forward declarations. |
333 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
334 | class __shared_ptr; |
335 | |
336 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
337 | class __weak_ptr; |
338 | |
339 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
340 | class __enable_shared_from_this; |
341 | |
342 | template<typename _Tp> |
343 | class shared_ptr; |
344 | |
345 | template<typename _Tp> |
346 | class weak_ptr; |
347 | |
348 | template<typename _Tp> |
349 | struct owner_less; |
350 | |
351 | template<typename _Tp> |
352 | class enable_shared_from_this; |
353 | |
354 | template<_Lock_policy _Lp = __default_lock_policy> |
355 | class __weak_count; |
356 | |
357 | template<_Lock_policy _Lp = __default_lock_policy> |
358 | class __shared_count; |
359 | |
360 | |
361 | // Counted ptr with no deleter or allocator support |
362 | template<typename _Ptr, _Lock_policy _Lp> |
363 | class _Sp_counted_ptr final : public _Sp_counted_base<_Lp> |
364 | { |
365 | public: |
366 | explicit |
367 | _Sp_counted_ptr(_Ptr __p) noexcept |
368 | : _M_ptr(__p) { } |
369 | |
370 | virtual void |
371 | _M_dispose() noexcept |
372 | { delete _M_ptr; } |
373 | |
374 | virtual void |
375 | _M_destroy() noexcept |
376 | { delete this; } |
377 | |
378 | virtual void* |
379 | _M_get_deleter(const std::type_info&) noexcept |
380 | { return nullptr; } |
381 | |
382 | _Sp_counted_ptr(const _Sp_counted_ptr&) = delete; |
383 | _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete; |
384 | |
385 | private: |
386 | _Ptr _M_ptr; |
387 | }; |
388 | |
389 | template<> |
390 | inline void |
391 | _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { } |
392 | |
393 | template<> |
394 | inline void |
395 | _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { } |
396 | |
397 | template<> |
398 | inline void |
399 | _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { } |
400 | |
401 | template<int _Nm, typename _Tp, |
402 | bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)> |
403 | struct _Sp_ebo_helper; |
404 | |
405 | /// Specialization using EBO. |
406 | template<int _Nm, typename _Tp> |
407 | struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp |
408 | { |
409 | explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { } |
410 | |
411 | static _Tp& |
412 | _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); } |
413 | }; |
414 | |
415 | /// Specialization not using EBO. |
416 | template<int _Nm, typename _Tp> |
417 | struct _Sp_ebo_helper<_Nm, _Tp, false> |
418 | { |
419 | explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { } |
420 | |
421 | static _Tp& |
422 | _S_get(_Sp_ebo_helper& __eboh) |
423 | { return __eboh._M_tp; } |
424 | |
425 | private: |
426 | _Tp _M_tp; |
427 | }; |
428 | |
429 | // Support for custom deleter and/or allocator |
430 | template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp> |
431 | class _Sp_counted_deleter final : public _Sp_counted_base<_Lp> |
432 | { |
433 | class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc> |
434 | { |
435 | typedef _Sp_ebo_helper<0, _Deleter> _Del_base; |
436 | typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base; |
437 | |
438 | public: |
439 | _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
440 | : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a) |
441 | { } |
442 | |
443 | _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); } |
444 | _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); } |
445 | |
446 | _Ptr _M_ptr; |
447 | }; |
448 | |
449 | public: |
450 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>; |
451 | |
452 | // __d(__p) must not throw. |
453 | _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept |
454 | : _M_impl(__p, __d, _Alloc()) { } |
455 | |
456 | // __d(__p) must not throw. |
457 | _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
458 | : _M_impl(__p, __d, __a) { } |
459 | |
460 | ~_Sp_counted_deleter() noexcept { } |
461 | |
462 | virtual void |
463 | _M_dispose() noexcept |
464 | { _M_impl._M_del()(_M_impl._M_ptr); } |
465 | |
466 | virtual void |
467 | _M_destroy() noexcept |
468 | { |
469 | __allocator_type __a(_M_impl._M_alloc()); |
470 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
471 | this->~_Sp_counted_deleter(); |
472 | } |
473 | |
474 | virtual void* |
475 | _M_get_deleter(const std::type_info& __ti) noexcept |
476 | { |
477 | #if __cpp_rtti199711L |
478 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
479 | // 2400. shared_ptr's get_deleter() should use addressof() |
480 | return __ti == typeid(_Deleter) |
481 | ? std::__addressof(_M_impl._M_del()) |
482 | : nullptr; |
483 | #else |
484 | return nullptr; |
485 | #endif |
486 | } |
487 | |
488 | private: |
489 | _Impl _M_impl; |
490 | }; |
491 | |
492 | // helpers for make_shared / allocate_shared |
493 | |
494 | struct _Sp_make_shared_tag { }; |
495 | |
496 | template<typename _Tp, typename _Alloc, _Lock_policy _Lp> |
497 | class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp> |
498 | { |
499 | class _Impl : _Sp_ebo_helper<0, _Alloc> |
500 | { |
501 | typedef _Sp_ebo_helper<0, _Alloc> _A_base; |
502 | |
503 | public: |
504 | explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { } |
505 | |
506 | _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); } |
507 | |
508 | __gnu_cxx::__aligned_buffer<_Tp> _M_storage; |
509 | }; |
510 | |
511 | public: |
512 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>; |
513 | |
514 | template<typename... _Args> |
515 | _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args) |
516 | : _M_impl(__a) |
517 | { |
518 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
519 | // 2070. allocate_shared should use allocator_traits<A>::construct |
520 | allocator_traits<_Alloc>::construct(__a, _M_ptr(), |
521 | std::forward<_Args>(__args)...); // might throw |
522 | } |
523 | |
524 | ~_Sp_counted_ptr_inplace() noexcept { } |
525 | |
526 | virtual void |
527 | _M_dispose() noexcept |
528 | { |
529 | allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr()); |
530 | } |
531 | |
532 | // Override because the allocator needs to know the dynamic type |
533 | virtual void |
534 | _M_destroy() noexcept |
535 | { |
536 | __allocator_type __a(_M_impl._M_alloc()); |
537 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
538 | this->~_Sp_counted_ptr_inplace(); |
539 | } |
540 | |
541 | // Sneaky trick so __shared_ptr can get the managed pointer |
542 | virtual void* |
543 | _M_get_deleter(const std::type_info& __ti) noexcept |
544 | { |
545 | #if __cpp_rtti199711L |
546 | if (__ti == typeid(_Sp_make_shared_tag)) |
547 | return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr()); |
548 | #endif |
549 | return nullptr; |
550 | } |
551 | |
552 | private: |
553 | _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); } |
554 | |
555 | _Impl _M_impl; |
556 | }; |
557 | |
558 | |
559 | template<_Lock_policy _Lp> |
560 | class __shared_count |
561 | { |
562 | public: |
563 | constexpr __shared_count() noexcept : _M_pi(0) |
564 | { } |
565 | |
566 | template<typename _Ptr> |
567 | explicit |
568 | __shared_count(_Ptr __p) : _M_pi(0) |
569 | { |
570 | __tryif (true) |
571 | { |
572 | _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p); |
573 | } |
574 | __catch(...)if (false) |
575 | { |
576 | delete __p; |
577 | __throw_exception_again; |
578 | } |
579 | } |
580 | |
581 | template<typename _Ptr, typename _Deleter> |
582 | __shared_count(_Ptr __p, _Deleter __d) |
583 | : __shared_count(__p, std::move(__d), allocator<void>()) |
584 | { } |
585 | |
586 | template<typename _Ptr, typename _Deleter, typename _Alloc> |
587 | __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0) |
588 | { |
589 | typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type; |
590 | __tryif (true) |
591 | { |
592 | typename _Sp_cd_type::__allocator_type __a2(__a); |
593 | auto __guard = std::__allocate_guarded(__a2); |
594 | _Sp_cd_type* __mem = __guard.get(); |
595 | ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a)); |
596 | _M_pi = __mem; |
597 | __guard = nullptr; |
598 | } |
599 | __catch(...)if (false) |
600 | { |
601 | __d(__p); // Call _Deleter on __p. |
602 | __throw_exception_again; |
603 | } |
604 | } |
605 | |
606 | template<typename _Tp, typename _Alloc, typename... _Args> |
607 | __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a, |
608 | _Args&&... __args) |
609 | : _M_pi(0) |
610 | { |
611 | typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type; |
612 | typename _Sp_cp_type::__allocator_type __a2(__a); |
613 | auto __guard = std::__allocate_guarded(__a2); |
614 | _Sp_cp_type* __mem = __guard.get(); |
615 | ::new (__mem) _Sp_cp_type(std::move(__a), |
616 | std::forward<_Args>(__args)...); |
617 | _M_pi = __mem; |
618 | __guard = nullptr; |
619 | } |
620 | |
621 | #if _GLIBCXX_USE_DEPRECATED1 |
622 | // Special case for auto_ptr<_Tp> to provide the strong guarantee. |
623 | template<typename _Tp> |
624 | explicit |
625 | __shared_count(std::auto_ptr<_Tp>&& __r); |
626 | #endif |
627 | |
628 | // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee. |
629 | template<typename _Tp, typename _Del> |
630 | explicit |
631 | __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0) |
632 | { |
633 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
634 | // 2415. Inconsistency between unique_ptr and shared_ptr |
635 | if (__r.get() == nullptr) |
636 | return; |
637 | |
638 | using _Ptr = typename unique_ptr<_Tp, _Del>::pointer; |
639 | using _Del2 = typename conditional<is_reference<_Del>::value, |
640 | reference_wrapper<typename remove_reference<_Del>::type>, |
641 | _Del>::type; |
642 | using _Sp_cd_type |
643 | = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>; |
644 | using _Alloc = allocator<_Sp_cd_type>; |
645 | using _Alloc_traits = allocator_traits<_Alloc>; |
646 | _Alloc __a; |
647 | _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1); |
648 | _Alloc_traits::construct(__a, __mem, __r.release(), |
649 | __r.get_deleter()); // non-throwing |
650 | _M_pi = __mem; |
651 | } |
652 | |
653 | // Throw bad_weak_ptr when __r._M_get_use_count() == 0. |
654 | explicit __shared_count(const __weak_count<_Lp>& __r); |
655 | |
656 | // Does not throw if __r._M_get_use_count() == 0, caller must check. |
657 | explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t); |
658 | |
659 | ~__shared_count() noexcept |
660 | { |
661 | if (_M_pi != nullptr) |
662 | _M_pi->_M_release(); |
663 | } |
664 | |
665 | __shared_count(const __shared_count& __r) noexcept |
666 | : _M_pi(__r._M_pi) |
667 | { |
668 | if (_M_pi != 0) |
669 | _M_pi->_M_add_ref_copy(); |
670 | } |
671 | |
672 | __shared_count& |
673 | operator=(const __shared_count& __r) noexcept |
674 | { |
675 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
676 | if (__tmp != _M_pi) |
677 | { |
678 | if (__tmp != 0) |
679 | __tmp->_M_add_ref_copy(); |
680 | if (_M_pi != 0) |
681 | _M_pi->_M_release(); |
682 | _M_pi = __tmp; |
683 | } |
684 | return *this; |
685 | } |
686 | |
687 | void |
688 | _M_swap(__shared_count& __r) noexcept |
689 | { |
690 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
691 | __r._M_pi = _M_pi; |
692 | _M_pi = __tmp; |
693 | } |
694 | |
695 | long |
696 | _M_get_use_count() const noexcept |
697 | { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; } |
698 | |
699 | bool |
700 | _M_unique() const noexcept |
701 | { return this->_M_get_use_count() == 1; } |
702 | |
703 | void* |
704 | _M_get_deleter(const std::type_info& __ti) const noexcept |
705 | { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; } |
706 | |
707 | bool |
708 | _M_less(const __shared_count& __rhs) const noexcept |
709 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
710 | |
711 | bool |
712 | _M_less(const __weak_count<_Lp>& __rhs) const noexcept |
713 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
714 | |
715 | // Friend function injected into enclosing namespace and found by ADL |
716 | friend inline bool |
717 | operator==(const __shared_count& __a, const __shared_count& __b) noexcept |
718 | { return __a._M_pi == __b._M_pi; } |
719 | |
720 | private: |
721 | friend class __weak_count<_Lp>; |
722 | |
723 | _Sp_counted_base<_Lp>* _M_pi; |
724 | }; |
725 | |
726 | |
727 | template<_Lock_policy _Lp> |
728 | class __weak_count |
729 | { |
730 | public: |
731 | constexpr __weak_count() noexcept : _M_pi(nullptr) |
732 | { } |
733 | |
734 | __weak_count(const __shared_count<_Lp>& __r) noexcept |
735 | : _M_pi(__r._M_pi) |
736 | { |
737 | if (_M_pi != nullptr) |
738 | _M_pi->_M_weak_add_ref(); |
739 | } |
740 | |
741 | __weak_count(const __weak_count& __r) noexcept |
742 | : _M_pi(__r._M_pi) |
743 | { |
744 | if (_M_pi != nullptr) |
745 | _M_pi->_M_weak_add_ref(); |
746 | } |
747 | |
748 | __weak_count(__weak_count&& __r) noexcept |
749 | : _M_pi(__r._M_pi) |
750 | { __r._M_pi = nullptr; } |
751 | |
752 | ~__weak_count() noexcept |
753 | { |
754 | if (_M_pi != nullptr) |
755 | _M_pi->_M_weak_release(); |
756 | } |
757 | |
758 | __weak_count& |
759 | operator=(const __shared_count<_Lp>& __r) noexcept |
760 | { |
761 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
762 | if (__tmp != nullptr) |
763 | __tmp->_M_weak_add_ref(); |
764 | if (_M_pi != nullptr) |
765 | _M_pi->_M_weak_release(); |
766 | _M_pi = __tmp; |
767 | return *this; |
768 | } |
769 | |
770 | __weak_count& |
771 | operator=(const __weak_count& __r) noexcept |
772 | { |
773 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
774 | if (__tmp != nullptr) |
775 | __tmp->_M_weak_add_ref(); |
776 | if (_M_pi != nullptr) |
777 | _M_pi->_M_weak_release(); |
778 | _M_pi = __tmp; |
779 | return *this; |
780 | } |
781 | |
782 | __weak_count& |
783 | operator=(__weak_count&& __r) noexcept |
784 | { |
785 | if (_M_pi != nullptr) |
786 | _M_pi->_M_weak_release(); |
787 | _M_pi = __r._M_pi; |
788 | __r._M_pi = nullptr; |
789 | return *this; |
790 | } |
791 | |
792 | void |
793 | _M_swap(__weak_count& __r) noexcept |
794 | { |
795 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
796 | __r._M_pi = _M_pi; |
797 | _M_pi = __tmp; |
798 | } |
799 | |
800 | long |
801 | _M_get_use_count() const noexcept |
802 | { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; } |
803 | |
804 | bool |
805 | _M_less(const __weak_count& __rhs) const noexcept |
806 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
807 | |
808 | bool |
809 | _M_less(const __shared_count<_Lp>& __rhs) const noexcept |
810 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
811 | |
812 | // Friend function injected into enclosing namespace and found by ADL |
813 | friend inline bool |
814 | operator==(const __weak_count& __a, const __weak_count& __b) noexcept |
815 | { return __a._M_pi == __b._M_pi; } |
816 | |
817 | private: |
818 | friend class __shared_count<_Lp>; |
819 | |
820 | _Sp_counted_base<_Lp>* _M_pi; |
821 | }; |
822 | |
823 | // Now that __weak_count is defined we can define this constructor: |
824 | template<_Lock_policy _Lp> |
825 | inline |
826 | __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r) |
827 | : _M_pi(__r._M_pi) |
828 | { |
829 | if (_M_pi != nullptr) |
830 | _M_pi->_M_add_ref_lock(); |
831 | else |
832 | __throw_bad_weak_ptr(); |
833 | } |
834 | |
835 | // Now that __weak_count is defined we can define this constructor: |
836 | template<_Lock_policy _Lp> |
837 | inline |
838 | __shared_count<_Lp>:: |
839 | __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) |
840 | : _M_pi(__r._M_pi) |
841 | { |
842 | if (_M_pi != nullptr) |
843 | if (!_M_pi->_M_add_ref_lock_nothrow()) |
844 | _M_pi = nullptr; |
845 | } |
846 | |
847 | // Support for enable_shared_from_this. |
848 | |
849 | // Friend of __enable_shared_from_this. |
850 | template<_Lock_policy _Lp, typename _Tp1, typename _Tp2> |
851 | void |
852 | __enable_shared_from_this_helper(const __shared_count<_Lp>&, |
853 | const __enable_shared_from_this<_Tp1, |
854 | _Lp>*, const _Tp2*) noexcept; |
855 | |
856 | // Friend of enable_shared_from_this. |
857 | template<typename _Tp1, typename _Tp2> |
858 | void |
859 | __enable_shared_from_this_helper(const __shared_count<>&, |
860 | const enable_shared_from_this<_Tp1>*, |
861 | const _Tp2*) noexcept; |
862 | |
863 | template<_Lock_policy _Lp> |
864 | inline void |
865 | __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept |
866 | { } |
867 | |
868 | |
869 | template<typename _Tp, _Lock_policy _Lp> |
870 | class __shared_ptr |
871 | { |
872 | template<typename _Ptr> |
873 | using _Convertible |
874 | = typename enable_if<is_convertible<_Ptr, _Tp*>::value>::type; |
875 | |
876 | public: |
877 | typedef _Tp element_type; |
878 | |
879 | constexpr __shared_ptr() noexcept |
880 | : _M_ptr(0), _M_refcount() |
881 | { } |
882 | |
883 | template<typename _Tp1> |
884 | explicit __shared_ptr(_Tp1* __p) |
885 | : _M_ptr(__p), _M_refcount(__p) |
886 | { |
887 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) |
888 | static_assert( !is_void<_Tp1>::value, "incomplete type" ); |
889 | static_assert( sizeof(_Tp1) > 0, "incomplete type" ); |
890 | __enable_shared_from_this_helper(_M_refcount, __p, __p); |
891 | } |
892 | |
893 | template<typename _Tp1, typename _Deleter> |
894 | __shared_ptr(_Tp1* __p, _Deleter __d) |
895 | : _M_ptr(__p), _M_refcount(__p, __d) |
896 | { |
897 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) |
898 | // TODO requires _Deleter CopyConstructible and __d(__p) well-formed |
899 | __enable_shared_from_this_helper(_M_refcount, __p, __p); |
900 | } |
901 | |
902 | template<typename _Tp1, typename _Deleter, typename _Alloc> |
903 | __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a) |
904 | : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a)) |
905 | { |
906 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) |
907 | // TODO requires _Deleter CopyConstructible and __d(__p) well-formed |
908 | __enable_shared_from_this_helper(_M_refcount, __p, __p); |
909 | } |
910 | |
911 | template<typename _Deleter> |
912 | __shared_ptr(nullptr_t __p, _Deleter __d) |
913 | : _M_ptr(0), _M_refcount(__p, __d) |
914 | { } |
915 | |
916 | template<typename _Deleter, typename _Alloc> |
917 | __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
918 | : _M_ptr(0), _M_refcount(__p, __d, std::move(__a)) |
919 | { } |
920 | |
921 | template<typename _Tp1> |
922 | __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept |
923 | : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws |
924 | { } |
925 | |
926 | __shared_ptr(const __shared_ptr&) noexcept = default; |
927 | __shared_ptr& operator=(const __shared_ptr&) noexcept = default; |
928 | ~__shared_ptr() = default; |
929 | |
930 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
931 | __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
932 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
933 | { } |
934 | |
935 | __shared_ptr(__shared_ptr&& __r) noexcept |
936 | : _M_ptr(__r._M_ptr), _M_refcount() |
937 | { |
938 | _M_refcount._M_swap(__r._M_refcount); |
939 | __r._M_ptr = 0; |
940 | } |
941 | |
942 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
943 | __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept |
944 | : _M_ptr(__r._M_ptr), _M_refcount() |
945 | { |
946 | _M_refcount._M_swap(__r._M_refcount); |
947 | __r._M_ptr = 0; |
948 | } |
949 | |
950 | template<typename _Tp1> |
951 | explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r) |
952 | : _M_refcount(__r._M_refcount) // may throw |
953 | { |
954 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) |
955 | |
956 | // It is now safe to copy __r._M_ptr, as |
957 | // _M_refcount(__r._M_refcount) did not throw. |
958 | _M_ptr = __r._M_ptr; |
959 | } |
960 | |
961 | // If an exception is thrown this constructor has no effect. |
962 | template<typename _Tp1, typename _Del, typename |
963 | = _Convertible<typename unique_ptr<_Tp1, _Del>::pointer>> |
964 | __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r) |
965 | : _M_ptr(__r.get()), _M_refcount() |
966 | { |
967 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) |
968 | auto __raw = _S_raw_ptr(__r.get()); |
969 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
970 | __enable_shared_from_this_helper(_M_refcount, __raw, __raw); |
971 | } |
972 | |
973 | #if _GLIBCXX_USE_DEPRECATED1 |
974 | // Postcondition: use_count() == 1 and __r.get() == 0 |
975 | template<typename _Tp1> |
976 | __shared_ptr(std::auto_ptr<_Tp1>&& __r); |
977 | #endif |
978 | |
979 | constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { } |
980 | |
981 | template<typename _Tp1> |
982 | __shared_ptr& |
983 | operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
984 | { |
985 | _M_ptr = __r._M_ptr; |
986 | _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw |
987 | return *this; |
988 | } |
989 | |
990 | #if _GLIBCXX_USE_DEPRECATED1 |
991 | template<typename _Tp1> |
992 | __shared_ptr& |
993 | operator=(std::auto_ptr<_Tp1>&& __r) |
994 | { |
995 | __shared_ptr(std::move(__r)).swap(*this); |
996 | return *this; |
997 | } |
998 | #endif |
999 | |
1000 | __shared_ptr& |
1001 | operator=(__shared_ptr&& __r) noexcept |
1002 | { |
1003 | __shared_ptr(std::move(__r)).swap(*this); |
1004 | return *this; |
1005 | } |
1006 | |
1007 | template<class _Tp1> |
1008 | __shared_ptr& |
1009 | operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept |
1010 | { |
1011 | __shared_ptr(std::move(__r)).swap(*this); |
1012 | return *this; |
1013 | } |
1014 | |
1015 | template<typename _Tp1, typename _Del> |
1016 | __shared_ptr& |
1017 | operator=(std::unique_ptr<_Tp1, _Del>&& __r) |
1018 | { |
1019 | __shared_ptr(std::move(__r)).swap(*this); |
1020 | return *this; |
1021 | } |
1022 | |
1023 | void |
1024 | reset() noexcept |
1025 | { __shared_ptr().swap(*this); } |
1026 | |
1027 | template<typename _Tp1> |
1028 | void |
1029 | reset(_Tp1* __p) // _Tp1 must be complete. |
1030 | { |
1031 | // Catch self-reset errors. |
1032 | __glibcxx_assert(__p == 0 || __p != _M_ptr); |
1033 | __shared_ptr(__p).swap(*this); |
1034 | } |
1035 | |
1036 | template<typename _Tp1, typename _Deleter> |
1037 | void |
1038 | reset(_Tp1* __p, _Deleter __d) |
1039 | { __shared_ptr(__p, __d).swap(*this); } |
1040 | |
1041 | template<typename _Tp1, typename _Deleter, typename _Alloc> |
1042 | void |
1043 | reset(_Tp1* __p, _Deleter __d, _Alloc __a) |
1044 | { __shared_ptr(__p, __d, std::move(__a)).swap(*this); } |
1045 | |
1046 | // Allow class instantiation when _Tp is [cv-qual] void. |
1047 | typename std::add_lvalue_reference<_Tp>::type |
1048 | operator*() const noexcept |
1049 | { |
1050 | __glibcxx_assert(_M_ptr != 0); |
1051 | return *_M_ptr; |
1052 | } |
1053 | |
1054 | _Tp* |
1055 | operator->() const noexcept |
1056 | { |
1057 | _GLIBCXX_DEBUG_PEDASSERT(_M_ptr != 0); |
1058 | return _M_ptr; |
1059 | } |
1060 | |
1061 | _Tp* |
1062 | get() const noexcept |
1063 | { return _M_ptr; } |
1064 | |
1065 | explicit operator bool() const // never throws |
1066 | { return _M_ptr == 0 ? false : true; } |
1067 | |
1068 | bool |
1069 | unique() const noexcept |
1070 | { return _M_refcount._M_unique(); } |
1071 | |
1072 | long |
1073 | use_count() const noexcept |
1074 | { return _M_refcount._M_get_use_count(); } |
1075 | |
1076 | void |
1077 | swap(__shared_ptr<_Tp, _Lp>& __other) noexcept |
1078 | { |
1079 | std::swap(_M_ptr, __other._M_ptr); |
1080 | _M_refcount._M_swap(__other._M_refcount); |
1081 | } |
1082 | |
1083 | template<typename _Tp1> |
1084 | bool |
1085 | owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const |
1086 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1087 | |
1088 | template<typename _Tp1> |
1089 | bool |
1090 | owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const |
1091 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1092 | |
1093 | #if __cpp_rtti199711L |
1094 | protected: |
1095 | // This constructor is non-standard, it is used by allocate_shared. |
1096 | template<typename _Alloc, typename... _Args> |
1097 | __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
1098 | _Args&&... __args) |
1099 | : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a, |
1100 | std::forward<_Args>(__args)...) |
1101 | { |
1102 | // _M_ptr needs to point to the newly constructed object. |
1103 | // This relies on _Sp_counted_ptr_inplace::_M_get_deleter. |
1104 | void* __p = _M_refcount._M_get_deleter(typeid(__tag)); |
1105 | _M_ptr = static_cast<_Tp*>(__p); |
1106 | __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr); |
1107 | } |
1108 | #else |
1109 | template<typename _Alloc> |
1110 | struct _Deleter |
1111 | { |
1112 | void operator()(typename _Alloc::value_type* __ptr) |
1113 | { |
1114 | __allocated_ptr<_Alloc> __guard{ _M_alloc, __ptr }; |
1115 | allocator_traits<_Alloc>::destroy(_M_alloc, __guard.get()); |
1116 | } |
1117 | _Alloc _M_alloc; |
1118 | }; |
1119 | |
1120 | template<typename _Alloc, typename... _Args> |
1121 | __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
1122 | _Args&&... __args) |
1123 | : _M_ptr(), _M_refcount() |
1124 | { |
1125 | typedef typename allocator_traits<_Alloc>::template |
1126 | rebind_traits<typename std::remove_cv<_Tp>::type> __traits; |
1127 | _Deleter<typename __traits::allocator_type> __del = { __a }; |
1128 | auto __guard = std::__allocate_guarded(__del._M_alloc); |
1129 | auto __ptr = __guard.get(); |
1130 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1131 | // 2070. allocate_shared should use allocator_traits<A>::construct |
1132 | __traits::construct(__del._M_alloc, __ptr, |
1133 | std::forward<_Args>(__args)...); |
1134 | __guard = nullptr; |
1135 | __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc); |
1136 | _M_refcount._M_swap(__count); |
1137 | _M_ptr = __ptr; |
1138 | __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr); |
1139 | } |
1140 | #endif |
1141 | |
1142 | template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc, |
1143 | typename... _Args> |
1144 | friend __shared_ptr<_Tp1, _Lp1> |
1145 | __allocate_shared(const _Alloc& __a, _Args&&... __args); |
1146 | |
1147 | // This constructor is used by __weak_ptr::lock() and |
1148 | // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t). |
1149 | __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) |
1150 | : _M_refcount(__r._M_refcount, std::nothrow) |
1151 | { |
1152 | _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr; |
1153 | } |
1154 | |
1155 | friend class __weak_ptr<_Tp, _Lp>; |
1156 | |
1157 | private: |
1158 | void* |
1159 | _M_get_deleter(const std::type_info& __ti) const noexcept |
1160 | { return _M_refcount._M_get_deleter(__ti); } |
1161 | |
1162 | template<typename _Tp1> |
1163 | static _Tp1* |
1164 | _S_raw_ptr(_Tp1* __ptr) |
1165 | { return __ptr; } |
1166 | |
1167 | template<typename _Tp1> |
1168 | static auto |
1169 | _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr)) |
1170 | { return std::__addressof(*__ptr); } |
1171 | |
1172 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
1173 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
1174 | |
1175 | template<typename _Del, typename _Tp1, _Lock_policy _Lp1> |
1176 | friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept; |
1177 | |
1178 | _Tp* _M_ptr; // Contained pointer. |
1179 | __shared_count<_Lp> _M_refcount; // Reference counter. |
1180 | }; |
1181 | |
1182 | |
1183 | // 20.7.2.2.7 shared_ptr comparisons |
1184 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1185 | inline bool |
1186 | operator==(const __shared_ptr<_Tp1, _Lp>& __a, |
1187 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1188 | { return __a.get() == __b.get(); } |
1189 | |
1190 | template<typename _Tp, _Lock_policy _Lp> |
1191 | inline bool |
1192 | operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1193 | { return !__a; } |
1194 | |
1195 | template<typename _Tp, _Lock_policy _Lp> |
1196 | inline bool |
1197 | operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1198 | { return !__a; } |
1199 | |
1200 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1201 | inline bool |
1202 | operator!=(const __shared_ptr<_Tp1, _Lp>& __a, |
1203 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1204 | { return __a.get() != __b.get(); } |
1205 | |
1206 | template<typename _Tp, _Lock_policy _Lp> |
1207 | inline bool |
1208 | operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1209 | { return (bool)__a; } |
1210 | |
1211 | template<typename _Tp, _Lock_policy _Lp> |
1212 | inline bool |
1213 | operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1214 | { return (bool)__a; } |
1215 | |
1216 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1217 | inline bool |
1218 | operator<(const __shared_ptr<_Tp1, _Lp>& __a, |
1219 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1220 | { |
1221 | typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT; |
1222 | return std::less<_CT>()(__a.get(), __b.get()); |
1223 | } |
1224 | |
1225 | template<typename _Tp, _Lock_policy _Lp> |
1226 | inline bool |
1227 | operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1228 | { return std::less<_Tp*>()(__a.get(), nullptr); } |
1229 | |
1230 | template<typename _Tp, _Lock_policy _Lp> |
1231 | inline bool |
1232 | operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1233 | { return std::less<_Tp*>()(nullptr, __a.get()); } |
1234 | |
1235 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1236 | inline bool |
1237 | operator<=(const __shared_ptr<_Tp1, _Lp>& __a, |
1238 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1239 | { return !(__b < __a); } |
1240 | |
1241 | template<typename _Tp, _Lock_policy _Lp> |
1242 | inline bool |
1243 | operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1244 | { return !(nullptr < __a); } |
1245 | |
1246 | template<typename _Tp, _Lock_policy _Lp> |
1247 | inline bool |
1248 | operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1249 | { return !(__a < nullptr); } |
1250 | |
1251 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1252 | inline bool |
1253 | operator>(const __shared_ptr<_Tp1, _Lp>& __a, |
1254 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1255 | { return (__b < __a); } |
1256 | |
1257 | template<typename _Tp, _Lock_policy _Lp> |
1258 | inline bool |
1259 | operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1260 | { return std::less<_Tp*>()(nullptr, __a.get()); } |
1261 | |
1262 | template<typename _Tp, _Lock_policy _Lp> |
1263 | inline bool |
1264 | operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1265 | { return std::less<_Tp*>()(__a.get(), nullptr); } |
1266 | |
1267 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1268 | inline bool |
1269 | operator>=(const __shared_ptr<_Tp1, _Lp>& __a, |
1270 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1271 | { return !(__a < __b); } |
1272 | |
1273 | template<typename _Tp, _Lock_policy _Lp> |
1274 | inline bool |
1275 | operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1276 | { return !(__a < nullptr); } |
1277 | |
1278 | template<typename _Tp, _Lock_policy _Lp> |
1279 | inline bool |
1280 | operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1281 | { return !(nullptr < __a); } |
1282 | |
1283 | template<typename _Sp> |
1284 | struct _Sp_less : public binary_function<_Sp, _Sp, bool> |
1285 | { |
1286 | bool |
1287 | operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept |
1288 | { |
1289 | typedef typename _Sp::element_type element_type; |
1290 | return std::less<element_type*>()(__lhs.get(), __rhs.get()); |
1291 | } |
1292 | }; |
1293 | |
1294 | template<typename _Tp, _Lock_policy _Lp> |
1295 | struct less<__shared_ptr<_Tp, _Lp>> |
1296 | : public _Sp_less<__shared_ptr<_Tp, _Lp>> |
1297 | { }; |
1298 | |
1299 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
1300 | template<typename _Tp, _Lock_policy _Lp> |
1301 | inline void |
1302 | swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept |
1303 | { __a.swap(__b); } |
1304 | |
1305 | // 20.7.2.2.9 shared_ptr casts |
1306 | |
1307 | // The seemingly equivalent code: |
1308 | // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get())) |
1309 | // will eventually result in undefined behaviour, attempting to |
1310 | // delete the same object twice. |
1311 | /// static_pointer_cast |
1312 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1313 | inline __shared_ptr<_Tp, _Lp> |
1314 | static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1315 | { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); } |
1316 | |
1317 | // The seemingly equivalent code: |
1318 | // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get())) |
1319 | // will eventually result in undefined behaviour, attempting to |
1320 | // delete the same object twice. |
1321 | /// const_pointer_cast |
1322 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1323 | inline __shared_ptr<_Tp, _Lp> |
1324 | const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1325 | { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); } |
1326 | |
1327 | // The seemingly equivalent code: |
1328 | // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get())) |
1329 | // will eventually result in undefined behaviour, attempting to |
1330 | // delete the same object twice. |
1331 | /// dynamic_pointer_cast |
1332 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1333 | inline __shared_ptr<_Tp, _Lp> |
1334 | dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1335 | { |
1336 | if (_Tp* __p = dynamic_cast<_Tp*>(__r.get())) |
1337 | return __shared_ptr<_Tp, _Lp>(__r, __p); |
1338 | return __shared_ptr<_Tp, _Lp>(); |
1339 | } |
1340 | |
1341 | |
1342 | template<typename _Tp, _Lock_policy _Lp> |
1343 | class __weak_ptr |
1344 | { |
1345 | template<typename _Ptr> |
1346 | using _Convertible |
1347 | = typename enable_if<is_convertible<_Ptr, _Tp*>::value>::type; |
1348 | |
1349 | public: |
1350 | typedef _Tp element_type; |
1351 | |
1352 | constexpr __weak_ptr() noexcept |
1353 | : _M_ptr(nullptr), _M_refcount() |
1354 | { } |
1355 | |
1356 | __weak_ptr(const __weak_ptr&) noexcept = default; |
1357 | |
1358 | ~__weak_ptr() = default; |
1359 | |
1360 | // The "obvious" converting constructor implementation: |
1361 | // |
1362 | // template<typename _Tp1> |
1363 | // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) |
1364 | // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws |
1365 | // { } |
1366 | // |
1367 | // has a serious problem. |
1368 | // |
1369 | // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr) |
1370 | // conversion may require access to *__r._M_ptr (virtual inheritance). |
1371 | // |
1372 | // It is not possible to avoid spurious access violations since |
1373 | // in multithreaded programs __r._M_ptr may be invalidated at any point. |
1374 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
1375 | __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept |
1376 | : _M_refcount(__r._M_refcount) |
1377 | { _M_ptr = __r.lock().get(); } |
1378 | |
1379 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
1380 | __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1381 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
1382 | { } |
1383 | |
1384 | __weak_ptr(__weak_ptr&& __r) noexcept |
1385 | : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount)) |
1386 | { __r._M_ptr = nullptr; } |
1387 | |
1388 | template<typename _Tp1, typename = _Convertible<_Tp1*>> |
1389 | __weak_ptr(__weak_ptr<_Tp1, _Lp>&& __r) noexcept |
1390 | : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount)) |
1391 | { __r._M_ptr = nullptr; } |
1392 | |
1393 | __weak_ptr& |
1394 | operator=(const __weak_ptr& __r) noexcept = default; |
1395 | |
1396 | template<typename _Tp1> |
1397 | __weak_ptr& |
1398 | operator=(const __weak_ptr<_Tp1, _Lp>& __r) noexcept |
1399 | { |
1400 | _M_ptr = __r.lock().get(); |
1401 | _M_refcount = __r._M_refcount; |
1402 | return *this; |
1403 | } |
1404 | |
1405 | template<typename _Tp1> |
1406 | __weak_ptr& |
1407 | operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1408 | { |
1409 | _M_ptr = __r._M_ptr; |
1410 | _M_refcount = __r._M_refcount; |
1411 | return *this; |
1412 | } |
1413 | |
1414 | __weak_ptr& |
1415 | operator=(__weak_ptr&& __r) noexcept |
1416 | { |
1417 | _M_ptr = __r._M_ptr; |
1418 | _M_refcount = std::move(__r._M_refcount); |
1419 | __r._M_ptr = nullptr; |
1420 | return *this; |
1421 | } |
1422 | |
1423 | template<typename _Tp1> |
1424 | __weak_ptr& |
1425 | operator=(__weak_ptr<_Tp1, _Lp>&& __r) noexcept |
1426 | { |
1427 | _M_ptr = __r.lock().get(); |
1428 | _M_refcount = std::move(__r._M_refcount); |
1429 | __r._M_ptr = nullptr; |
1430 | return *this; |
1431 | } |
1432 | |
1433 | __shared_ptr<_Tp, _Lp> |
1434 | lock() const noexcept |
1435 | { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); } |
1436 | |
1437 | long |
1438 | use_count() const noexcept |
1439 | { return _M_refcount._M_get_use_count(); } |
1440 | |
1441 | bool |
1442 | expired() const noexcept |
1443 | { return _M_refcount._M_get_use_count() == 0; } |
1444 | |
1445 | template<typename _Tp1> |
1446 | bool |
1447 | owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const |
1448 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1449 | |
1450 | template<typename _Tp1> |
1451 | bool |
1452 | owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const |
1453 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1454 | |
1455 | void |
1456 | reset() noexcept |
1457 | { __weak_ptr().swap(*this); } |
1458 | |
1459 | void |
1460 | swap(__weak_ptr& __s) noexcept |
1461 | { |
1462 | std::swap(_M_ptr, __s._M_ptr); |
1463 | _M_refcount._M_swap(__s._M_refcount); |
1464 | } |
1465 | |
1466 | private: |
1467 | // Used by __enable_shared_from_this. |
1468 | void |
1469 | _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept |
1470 | { |
1471 | if (use_count() == 0) |
1472 | { |
1473 | _M_ptr = __ptr; |
1474 | _M_refcount = __refcount; |
1475 | } |
1476 | } |
1477 | |
1478 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
1479 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
1480 | friend class __enable_shared_from_this<_Tp, _Lp>; |
1481 | friend class enable_shared_from_this<_Tp>; |
1482 | |
1483 | _Tp* _M_ptr; // Contained pointer. |
1484 | __weak_count<_Lp> _M_refcount; // Reference counter. |
1485 | }; |
1486 | |
1487 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
1488 | template<typename _Tp, _Lock_policy _Lp> |
1489 | inline void |
1490 | swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept |
1491 | { __a.swap(__b); } |
1492 | |
1493 | template<typename _Tp, typename _Tp1> |
1494 | struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool> |
1495 | { |
1496 | bool |
1497 | operator()(const _Tp& __lhs, const _Tp& __rhs) const |
1498 | { return __lhs.owner_before(__rhs); } |
1499 | |
1500 | bool |
1501 | operator()(const _Tp& __lhs, const _Tp1& __rhs) const |
1502 | { return __lhs.owner_before(__rhs); } |
1503 | |
1504 | bool |
1505 | operator()(const _Tp1& __lhs, const _Tp& __rhs) const |
1506 | { return __lhs.owner_before(__rhs); } |
1507 | }; |
1508 | |
1509 | template<typename _Tp, _Lock_policy _Lp> |
1510 | struct owner_less<__shared_ptr<_Tp, _Lp>> |
1511 | : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>> |
1512 | { }; |
1513 | |
1514 | template<typename _Tp, _Lock_policy _Lp> |
1515 | struct owner_less<__weak_ptr<_Tp, _Lp>> |
1516 | : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>> |
1517 | { }; |
1518 | |
1519 | |
1520 | template<typename _Tp, _Lock_policy _Lp> |
1521 | class __enable_shared_from_this |
1522 | { |
1523 | protected: |
1524 | constexpr __enable_shared_from_this() noexcept { } |
1525 | |
1526 | __enable_shared_from_this(const __enable_shared_from_this&) noexcept { } |
1527 | |
1528 | __enable_shared_from_this& |
1529 | operator=(const __enable_shared_from_this&) noexcept |
1530 | { return *this; } |
1531 | |
1532 | ~__enable_shared_from_this() { } |
1533 | |
1534 | public: |
1535 | __shared_ptr<_Tp, _Lp> |
1536 | shared_from_this() |
1537 | { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); } |
1538 | |
1539 | __shared_ptr<const _Tp, _Lp> |
1540 | shared_from_this() const |
1541 | { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); } |
1542 | |
1543 | private: |
1544 | template<typename _Tp1> |
1545 | void |
1546 | _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept |
1547 | { _M_weak_this._M_assign(__p, __n); } |
1548 | |
1549 | template<_Lock_policy _Lp1, typename _Tp1, typename _Tp2> |
1550 | friend void |
1551 | __enable_shared_from_this_helper(const __shared_count<_Lp1>&, |
1552 | const __enable_shared_from_this<_Tp1, |
1553 | _Lp1>*, const _Tp2*) noexcept; |
1554 | |
1555 | mutable __weak_ptr<_Tp, _Lp> _M_weak_this; |
1556 | }; |
1557 | |
1558 | template<_Lock_policy _Lp1, typename _Tp1, typename _Tp2> |
1559 | inline void |
1560 | __enable_shared_from_this_helper(const __shared_count<_Lp1>& __pn, |
1561 | const __enable_shared_from_this<_Tp1, |
1562 | _Lp1>* __pe, |
1563 | const _Tp2* __px) noexcept |
1564 | { |
1565 | if (__pe != nullptr) |
1566 | __pe->_M_weak_assign(const_cast<_Tp2*>(__px), __pn); |
1567 | } |
1568 | |
1569 | template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args> |
1570 | inline __shared_ptr<_Tp, _Lp> |
1571 | __allocate_shared(const _Alloc& __a, _Args&&... __args) |
1572 | { |
1573 | return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a, |
1574 | std::forward<_Args>(__args)...); |
1575 | } |
1576 | |
1577 | template<typename _Tp, _Lock_policy _Lp, typename... _Args> |
1578 | inline __shared_ptr<_Tp, _Lp> |
1579 | __make_shared(_Args&&... __args) |
1580 | { |
1581 | typedef typename std::remove_const<_Tp>::type _Tp_nc; |
1582 | return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(), |
1583 | std::forward<_Args>(__args)...); |
1584 | } |
1585 | |
1586 | /// std::hash specialization for __shared_ptr. |
1587 | template<typename _Tp, _Lock_policy _Lp> |
1588 | struct hash<__shared_ptr<_Tp, _Lp>> |
1589 | : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>> |
1590 | { |
1591 | size_t |
1592 | operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept |
1593 | { return std::hash<_Tp*>()(__s.get()); } |
1594 | }; |
1595 | |
1596 | _GLIBCXX_END_NAMESPACE_VERSION |
1597 | } // namespace |
1598 | |
1599 | #endif // _SHARED_PTR_BASE_H |