From: <sv...@ww...> - 2005-03-12 05:28:51
|
Author: mkrose Date: 2005-03-11 21:28:45 -0800 (Fri, 11 Mar 2005) New Revision: 1492 Modified: trunk/CSP/SimData/CHANGES.current trunk/CSP/SimData/Include/SimData/Ref.h Log: Add Ref casting functions to test for campatible downcasts without throwing an exception. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1492 Modified: trunk/CSP/SimData/CHANGES.current =================================================================== --- trunk/CSP/SimData/CHANGES.current 2005-03-12 05:24:58 UTC (rev 1491) +++ trunk/CSP/SimData/CHANGES.current 2005-03-12 05:28:45 UTC (rev 1492) @@ -5,6 +5,9 @@ * Add a method to DateZulu to get the time offset relative to another DateZulu instance in seconds. + * Add Ref casting functions to test for campatible downcasts without + throwing an exception. + 2005-02-16: delta * For some reasons, vs 2005 needs some template(!) classes to be exported whereas vs 2003 doesn't allow it... Back to a version Modified: trunk/CSP/SimData/Include/SimData/Ref.h =================================================================== --- trunk/CSP/SimData/Include/SimData/Ref.h 2005-03-12 05:24:58 UTC (rev 1491) +++ trunk/CSP/SimData/Include/SimData/Ref.h 2005-03-12 05:28:45 UTC (rev 1492) @@ -297,6 +297,38 @@ return p.isNull() || dynamic_cast<CLASS*>(p.get()) != 0; } + /** Try to assignment from a potentially incompatible reference. Returns + * true on success, or false if the pointers are incompatible. This + * reference will be set to null if the assignment fails (or the other + * reference is null). + */ + template <class Q> + bool tryAssign(Ref<Q> const & p) { + _rebind(p.get(), false); + return (valid() || p.isNull()); + } + + /** Try to assignment from a potentially incompatible LinkBase. Returns + * true on success, or false if the pointers are incompatible. This + * reference will be set to null if the assignment fails (or the other + * reference is null). + */ + bool tryAssign(LinkBase const & p) { + _rebind(p._get(), false); + return (valid() || p.isNull()); + } + + /** Try to assignment from a potentially incompatible pointer. Returns + * true on success, or false if the pointers are incompatible. This + * reference will be set to null if the assignment fails (or the other + * reference is null). + */ + template <class Q> + bool tryAssign(Q *p) { + _rebind(p, false); + return (valid() || (p == 0)); + } + /** Comparison with other simdata pointers. * XXX the base class NonCopyable is used here instead of CLASS since * swig generates bad code if CLASS is const (const const). @@ -321,11 +353,11 @@ * @throws ConversionError if downcasting from an incompatible type. */ template <class Q> - void _rebind(Q* ptr) { + void _rebind(Q* ptr, bool throw_on_fail=true) { if (ptr) ptr->_incref(); _unbind(); _reference = dynamic_cast<CLASS*>(ptr); - if (_reference == 0 && ptr != 0) { + if (_reference == 0 && ptr != 0 && throw_on_fail) { _log_reference_conversion_error(); throw ConversionError(); } |