This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [C++0x] a better <future>
2009/6/20 Jonathan Wakely:
>
> I'll do the check in tomorrow.
This is in now.
Here's a pretty printer patch I'm working on for futures, output looks like:
Breakpoint 1, main () at promise.cc:16
16 std::packaged_task<int(int)> task(inc);
(gdb) n
17 std::shared_future<int> f1 = task.get_future();
(gdb) p task
$1 = std::packaged_task (not ready, future not retrieved)
(gdb) n
18 task(2);
(gdb) p f1
$2 = std::shared_future (not ready, shares state with 1 other object)
(gdb) p task
$3 = std::packaged_task (not ready, shares state with 1 other object)
(gdb) n
19 int i = f1.get();
(gdb) p task
$4 = std::packaged_task (has value, shares state with 1 other object)
(gdb) p f1
$5 = std::shared_future (has value, shares state with 1 other object)
I want to improve the output for packaged_task, currently it only
prints the promise object, not the stored function object, which looks
like this:
$3 = {
_M_task = {<std::_Maybe_unary_or_binary_function<int, int>> =
{<std::unary_function<int, int>> = {<No data fields>}, <No data
fields>}, <std::_Function_base> = {static _M_max_size = 16, static
_M_max_align = 8, _M_functor = {_M_unused = {_M_object = 0x4019ed,
_M_const_object = 0x4019ed,
_M_function_pointer = 0x4019ed <inc(int)>, _M_member_pointer
= &virtual table offset 4200940, this adjustment 64971124},
_M_pod_data =
"ï\031@\000\000\000\000\000taï\003\000\000\000"},
_M_manager = 0x406137 <std::_Function_base::_Base_manager<int
(*)(int)>::_M_manager(std::_Any_data&, std::_Any_data const&,
std::_Manager_operation)>}, _M_invoker = 0x406110
<std::_Function_handler<int ()(int), int
(*)(int)>::_M_invoke(std::_Any_data const&, int)>},
_M_promise = std::promise (has value, shares state with 1 other object)}
which isn't very pretty :-)
Index: python/libstdcxx/v6/printers.py
===================================================================
--- python/libstdcxx/v6/printers.py (revision 148762)
+++ python/libstdcxx/v6/printers.py (working copy)
@@ -545,6 +545,65 @@ class Tr1UnorderedMapPrinter:
def display_hint (self):
return 'map'
+class StdFutureStatePtrPrinter:
+ "Print a std::shared_ptr<_Future_state>"
+
+ def __init__ (self, typename, rtype, val):
+ self.typename = typename
+ self.rtype = rtype
+ self.val = val
+
+ def to_string (self):
+ if self.val['_M_ptr'] == 0:
+ return "%s (no associated state)" % self.typename
+ usecount = self.val['_M_refcount']['_M_pi']['_M_use_count']
+ sharing = "shares state with %d other object" % (usecount-1)
+ if (usecount != 2):
+ sharing = sharing + "s"
+ state = self.val['_M_ptr'].dereference()
+ if state['_M_retrieved']['_M_i'] == 0:
+ sharing = "future not retrieved"
+ resptr = state['_M_result']['_M_t']['_M_head_impl']
+ if resptr == 0:
+ return "%s (not ready, %s)" % (self.typename, sharing)
+ resultbase = resptr.dereference()
+ exceptptr = resultbase['_M_error']['_M_exception_object']
+ if exceptptr != 0:
+ return "%s (has exception, %s)" % (self.typename, sharing)
+ return "%s (has value, %s)" % (self.typename, sharing)
+
+class StdPromisePrinter:
+ "Print a std::promise"
+
+ def __init__ (self, val):
+ self.val = val
+
+ def to_string (self):
+ resulttype = self.val.type.template_argument(0)
+ return StdFutureStatePtrPrinter("std::promise", resulttype, self.val['_M_future']).to_string()
+
+class StdFuturePrinter:
+ "Print a std::unique_future or std::shared_future"
+
+ def __init__ (self, typename, val):
+ self.typename = typename
+ self.val = val
+
+ def to_string (self):
+ resulttype = self.val.type.template_argument(0)
+ return StdFutureStatePtrPrinter(self.typename, resulttype, self.val['_M_state']).to_string()
+
+class StdPackagedTaskPrinter:
+ "Print a std::packaged_task"
+
+ def __init__ (self, val):
+ self.val = val
+
+ def to_string (self):
+ resulttype = self.val['_M_promise'].type.template_argument(0)
+ return StdFutureStatePtrPrinter("std::packaged_task", resulttype, self.val['_M_promise']['_M_future']).to_string()
+
+
def register_libstdcxx_printers (obj):
"Register libstdc++ pretty-printers with objfile Obj."
@@ -611,6 +670,10 @@ def build_libstdcxx_dictionary ():
pretty_printers_dict[re.compile('^std::unordered_set<.*>$')] = lambda val: Tr1UnorderedSetPrinter ('std::unordered_set', val)
pretty_printers_dict[re.compile('^std::unordered_multimap<.*>$')] = lambda val: Tr1UnorderedMapPrinter ('std::unordered_multimap', val)
pretty_printers_dict[re.compile('^std::unordered_multiset<.*>$')] = lambda val: Tr1UnorderedSetPrinter ('std::unordered_multiset', val)
+ pretty_printers_dict[re.compile('^std::promise<.*>$')] = StdPromisePrinter
+ pretty_printers_dict[re.compile('^std::packaged_task<.*>$')] = StdPackagedTaskPrinter
+ pretty_printers_dict[re.compile('^std::unique_future<.*>$')] = lambda val: StdFuturePrinter ('std::unique_future', val)
+ pretty_printers_dict[re.compile('^std::shared_future<.*>$')] = lambda val: StdFuturePrinter ('std::shared_future', val)
pretty_printers_dict[re.compile('^std::tr1::shared_ptr<.*>$')] = lambda val: StdPointerPrinter ('std::tr1::shared_ptr', val)
pretty_printers_dict[re.compile('^std::tr1::weak_ptr<.*>$')] = lambda val: StdPointerPrinter ('std::tr1::weak_ptr', val)