Skip to content

Commit fe3ae4d

Browse files
committed
Add ancestor count concept
Useful in cases where we want to enumerate contexts and keep track of how deep we are, as in the case of printing a tree representation of contexts.
1 parent d5742f3 commit fe3ae4d

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/autowiring/CoreContext.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ static thread_specific_ptr<std::shared_ptr<CoreContext>> autoCurrentContext;
5555
CoreContext::CoreContext(const std::shared_ptr<CoreContext>& pParent, t_childList::iterator backReference, auto_id sigilType) :
5656
m_pParent(pParent),
5757
m_backReference(backReference),
58-
m_sigilType(sigilType),
58+
SigilType(sigilType),
59+
AncestorCount(pParent ? pParent->AncestorCount + 1 : 0),
5960
m_stateBlock(std::make_shared<CoreContextStateBlock>(pParent ? pParent->m_stateBlock : nullptr))
6061
{}
6162

src/autowiring/CoreContext.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,19 @@ class CoreContext:
147147
/// \sa AutoGlobalContext, GlobalCoreContext
148148
static std::shared_ptr<CoreContext> GetGlobal(void);
149149

150+
// The number of ancestors of this context. The global context is defined to have zero ancestors.
151+
const size_t AncestorCount = 0;
152+
153+
// Sigil type, used during bolting
154+
const auto_id SigilType;
155+
150156
protected:
151157
// A pointer to the parent context
152158
const std::shared_ptr<CoreContext> m_pParent;
153159

154160
// Back-referencing iterator which refers to ourselves in our parent's child list:
155161
const t_childList::iterator m_backReference;
156162

157-
// Sigil type, used during bolting
158-
const auto_id m_sigilType;
159-
160163
// State block for this context:
161164
std::shared_ptr<autowiring::CoreContextStateBlock> m_stateBlock;
162165

@@ -394,7 +397,7 @@ class CoreContext:
394397
/// The number of child contexts of this context.
395398
size_t GetChildCount(void) const;
396399
/// The type used as a sigil when creating this class, if any.
397-
auto_id GetSigilType(void) const { return m_sigilType; }
400+
auto_id GetSigilType(void) const { return SigilType; }
398401
/// The Context iterator for the parent context's children, pointing to this context.
399402
t_childList::iterator GetBackReference(void) const { return m_backReference; }
400403
/// A shared reference to the parent context of this context.
@@ -411,7 +414,7 @@ class CoreContext:
411414

412415
/// True if the sigil type of this CoreContext matches the specified sigil type.
413416
template<class Sigil>
414-
bool Is(void) const { return m_sigilType == auto_id_t<Sigil>{}; }
417+
bool Is(void) const { return SigilType == auto_id_t<Sigil>{}; }
415418

416419
/// <summary>
417420
/// The first child in the set of this context's children.

src/autowiring/test/CoreContextTest.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,20 @@ TEST_F(CoreContextTest, SimultaneousMultiInject) {
698698
// Verify that there's no config block still present
699699
ASSERT_NO_THROW(ctxt->Config.Get("c"));
700700
}
701+
702+
703+
TEST_F(CoreContextTest, AncestorCount) {
704+
AutoCurrentContext ctxt;
705+
706+
size_t ancestorCount = 0;
707+
for (std::shared_ptr<CoreContext> cur = ctxt->GetParentContext(); cur; cur = cur->GetParentContext())
708+
ancestorCount++;
709+
ASSERT_EQ(ancestorCount, ctxt->AncestorCount) << "Manual traversal of the ancestor count did not match the annotated count";
710+
711+
auto child1 = ctxt->Create<void>();
712+
auto child2 = child1->Create<void>();
713+
auto child3 = child2->Create<void>();
714+
ASSERT_EQ(ctxt->AncestorCount + 1, child1->AncestorCount);
715+
ASSERT_EQ(ctxt->AncestorCount + 2, child2->AncestorCount);
716+
ASSERT_EQ(ctxt->AncestorCount + 3, child3->AncestorCount);
717+
}

src/autowiring/test/GlobalInitTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void GlobalInitTest::SetUp(void) {
1515

1616
void GlobalInitTest::TearDown(void) {
1717
std::shared_ptr<GlobalCoreContext> glbl = AutoGlobalContext();
18+
ASSERT_EQ(0U, glbl->AncestorCount) << "Global context ancestor count must be exactly zero";
1819

1920
{
2021
// Always drop the global context when tests are done

0 commit comments

Comments
 (0)