Autowired

Peter Ehrlich edited this page Jul 25, 2014 · 2 revisions

Autowired is a reference-counting smart pointer type based on std::shared_ptr which is used to make a delay-resolvable query against the current context for the existence of some particular type. If the type isn't in the current context at the time of the query, Autowired will hold null. If the enclosing context gains a member of the desired type a later time, Autowired will be updated after-the-fact to hold a reference to the newly introduced type.

A dual type to Autowired is AutoRequired. If the desired type cannot be found in a context at the time AutoRequired is instantiated, then that type will be created and injected into the context.

Rules for Autowiring

Autowired is both eager and delayed. If the current context contains the type that a user wants, it will receive a shared pointer to that type. Delayed injection results in delayed satisfaction of the type. The following demonstrates how this mechanism actually behaves:

    AutoCreateContext ctxt;
    CurrentContextPusher pshr(ctxt);
    Autowired<MyType> myType;

    ASSERT_FALSE(myType.IsAutowired());

    // At this point, myType is null.  The next line injects MyType into the
    // current context
    AutoRequired<MyType> myTypeInjected;

    // Now the earlier myType should be satisfied, all is right with the world
    ASSERT_TRUE(myType.IsAutowired());
    ASSERT_EQ(myTypeInjected, myType);
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.