• Mark Cogan's avatar
    [iOS] add CallableDispatcher() helper. · ca7ab7c6
    Mark Cogan authored
    Currently the usual way to turn a CommandDispatcher pointer into
    something that can have methods called on it is to static_cast() it
    into id<Protocols>, which compiles but has no runtime correct check.
    
    +conformsToProtocol: can't be used here, since that just checks a class
    interface, and doesn't work on a dynamic object like CommandDispatcher.
    
    This CL adds a function-like helper macro for use with CommandDispatcher.
    It does both typecasting and a runtime check that the dispatcher can
    handle the methods in the protocol.
    
    A macro is necessary to generate the correct static_cast expression that
    matches the protocol whose methods are being checked. Believe me, if
    there was another way to do this, I'd use it,
    
    The helper matches the behavior of ObjCCastStrict, returning nil and
    DCHECKing if the dispatcher doesn't handle the specified protocol.
    
    Note that while the dispatcher's startDispatchingToTarget:forProtocol:
    method doesn't handle protocols that the passed protocol conforms to,
    CallableDispatcher does, so composite protocols can be used when the
    callable needs to conform to multiple protocols.
    
    Change-Id: I49953ed89d76cbe6e714abec226161f927ebb60f
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1866751
    Commit-Queue: Mark Cogan <marq@chromium.org>
    Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#707888}
    ca7ab7c6
command_dispatcher.mm 4.91 KB