[locator] adding Get<Type> to InstanceLocator (#3714)

This commit changes how the objects in OpenThread access each other.
It adds a template `Get<Type>()` method in `InstanceLocator`. This
method returns a reference to a given `Type` object belonging to the
OpenThread instance (e.g. `Get<MeshForwarder>()` returns a reference
to `MeshForwarder` object on the OpenThread instance). The
`InstanceLocator` is used as base class of all OpenThread classes so
every class can easily access any other object. This commit also
changes how the main instance is retrieved in `InstanceLocator` for
the single-instance case. The method `GetInstance()` directly uses the
raw buffer `gInstanceRaw`. This change helps make the `GetInstance()`
and in turn all `Get<Type>()` methods in-line. This commit also
removes the existing getters across all classes to use the new
`Get<Type>()` model.
This commit is contained in:
Abtin Keshavarzian
2019-04-02 09:07:25 -07:00
committed by Jonathan Hui
parent 9dfa4e2b31
commit 8f112eeb5a
120 changed files with 1846 additions and 2508 deletions
+7 -9
View File
@@ -38,7 +38,6 @@ namespace ot {
ot::Instance * sInstance;
Ip6::Ip6 * sIp6;
ThreadNetif * sThreadNetif;
Lowpan::Lowpan *sLowpan;
void TestIphcVector::GetCompressedStream(uint8_t *aIphc, uint16_t &aIphcLength)
@@ -110,7 +109,7 @@ void TestIphcVector::GetUncompressedStream(Message &aMessage)
static void Init()
{
otMeshLocalPrefix meshLocalPrefix = {{0xfd, 0x00, 0xca, 0xfe, 0xfa, 0xce, 0x12, 0x34}};
sThreadNetif->GetMle().SetMeshLocalPrefix(meshLocalPrefix);
sInstance->Get<Mle::MleRouter>().SetMeshLocalPrefix(meshLocalPrefix);
// Emulate global prefixes with contextes.
uint8_t mockNetworkData[] = {
@@ -128,12 +127,12 @@ static void Init()
0x02, 0x40 // Context ID = 2, C = FALSE
};
Message *message = sInstance->GetMessagePool().New(Message::kTypeIp6, 0);
Message *message = sInstance->Get<MessagePool>().New(Message::kTypeIp6, 0);
VerifyOrQuit(message != NULL, "6lo: Ip6::NewMessage failed");
SuccessOrQuit(message->Append(mockNetworkData, sizeof(mockNetworkData)), "6lo: Message::Append failed");
sThreadNetif->GetNetworkDataLeader().SetNetworkData(0, 0, true, *message, 0);
sInstance->Get<NetworkData::Leader>().SetNetworkData(0, 0, true, *message, 0);
}
/**
@@ -180,7 +179,7 @@ static void Test(TestIphcVector &aVector, bool aCompress, bool aDecompress)
{
Lowpan::BufferWriter buffer(result, 127);
VerifyOrQuit((message = sInstance->GetMessagePool().New(Message::kTypeIp6, 0)) != NULL,
VerifyOrQuit((message = sInstance->Get<MessagePool>().New(Message::kTypeIp6, 0)) != NULL,
"6lo: Ip6::NewMessage failed");
aVector.GetUncompressedStream(*message);
@@ -210,7 +209,7 @@ static void Test(TestIphcVector &aVector, bool aCompress, bool aDecompress)
if (aDecompress)
{
VerifyOrQuit((message = sInstance->GetMessagePool().New(Message::kTypeIp6, 0)) != NULL,
VerifyOrQuit((message = sInstance->Get<MessagePool>().New(Message::kTypeIp6, 0)) != NULL,
"6lo: Ip6::NewMessage failed");
int decompressedBytes =
@@ -1754,9 +1753,8 @@ void TestLowpanIphc(void)
VerifyOrQuit(sInstance != NULL, "NULL instance");
sIp6 = &sInstance->GetIp6();
sThreadNetif = &sInstance->GetThreadNetif();
sLowpan = &sThreadNetif->GetLowpan();
sIp6 = &sInstance->Get<Ip6::Ip6>();
sLowpan = &sInstance->Get<Lowpan::Lowpan>();
Init();