mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-15 17:23:33 +08:00
TransferIDRegistry - grouping by data type kind instead of transfer type
This commit is contained in:
@@ -27,6 +27,12 @@ enum TransferType
|
||||
NUM_TRANSFER_TYPES = 4
|
||||
};
|
||||
|
||||
enum DataTypeKind
|
||||
{
|
||||
DATA_TYPE_KIND_SERVICE,
|
||||
DATA_TYPE_KIND_MESSAGE,
|
||||
NUM_DATA_TYPE_KINDS
|
||||
};
|
||||
|
||||
class TransferID
|
||||
{
|
||||
|
||||
@@ -19,18 +19,18 @@ class TransferIDRegistry
|
||||
public:
|
||||
struct Key
|
||||
{
|
||||
TransferType transfer_type;
|
||||
DataTypeKind data_type_kind;
|
||||
uint16_t data_type_id;
|
||||
uint8_t node_id;
|
||||
|
||||
Key()
|
||||
: transfer_type(TransferType(0))
|
||||
: data_type_kind(DataTypeKind(0))
|
||||
, data_type_id(0)
|
||||
, node_id(NODE_ID_INVALID)
|
||||
{ }
|
||||
|
||||
Key(uint8_t node_id, TransferType transfer_type, uint16_t data_type_id)
|
||||
: transfer_type(transfer_type)
|
||||
Key(uint8_t node_id, DataTypeKind data_type_kind, uint16_t data_type_id)
|
||||
: data_type_kind(data_type_kind)
|
||||
, data_type_id(data_type_id)
|
||||
, node_id(node_id)
|
||||
{ }
|
||||
@@ -105,7 +105,7 @@ private:
|
||||
void compact(IAllocator* allocator);
|
||||
};
|
||||
|
||||
List lists_by_transfer_type_[NUM_TRANSFER_TYPES];
|
||||
List lists_by_data_type_kind_[NUM_DATA_TYPE_KINDS];
|
||||
IAllocator* const allocator_;
|
||||
|
||||
public:
|
||||
@@ -128,9 +128,9 @@ public:
|
||||
template <typename Predicate>
|
||||
void removeWhere(Predicate predicate)
|
||||
{
|
||||
for (int transfer_type = 0; transfer_type < NUM_TRANSFER_TYPES; transfer_type++)
|
||||
for (int data_type_kind = 0; data_type_kind < NUM_DATA_TYPE_KINDS; data_type_kind++)
|
||||
{
|
||||
StorageEntryGroup* p = lists_by_transfer_type_[transfer_type].getHead();
|
||||
StorageEntryGroup* p = lists_by_data_type_kind_[data_type_kind].getHead();
|
||||
while (p)
|
||||
{
|
||||
for (int i = 0; i < StorageEntryGroup::NUM_ENTRIES; i++)
|
||||
@@ -138,7 +138,7 @@ public:
|
||||
const StorageEntry* const entry = p->entries + i;
|
||||
if (!entry->isEmpty())
|
||||
{
|
||||
const Key key(entry->node_id, TransferType(transfer_type), entry->data_type_id);
|
||||
const Key key(entry->node_id, DataTypeKind(data_type_kind), entry->data_type_id);
|
||||
const bool result = predicate(key, static_cast<const Entry&>(*entry));
|
||||
if (result)
|
||||
p->entries[i] = StorageEntry();
|
||||
@@ -146,7 +146,7 @@ public:
|
||||
}
|
||||
p = p->getNextListNode();
|
||||
}
|
||||
lists_by_transfer_type_[transfer_type].compact(allocator_);
|
||||
lists_by_data_type_kind_[data_type_kind].compact(allocator_);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -101,33 +101,33 @@ void TransferIDRegistry::List::compact(IAllocator* allocator)
|
||||
*/
|
||||
TransferIDRegistry::Entry* TransferIDRegistry::access(const Key& key)
|
||||
{
|
||||
if (key.node_id > NODE_ID_MAX || key.transfer_type >= NUM_TRANSFER_TYPES)
|
||||
if (key.node_id > NODE_ID_MAX || key.data_type_kind >= NUM_DATA_TYPE_KINDS)
|
||||
{
|
||||
assert(0);
|
||||
return NULL;
|
||||
}
|
||||
return static_cast<Entry*>(lists_by_transfer_type_[key.transfer_type].find(key));
|
||||
return static_cast<Entry*>(lists_by_data_type_kind_[key.data_type_kind].find(key));
|
||||
}
|
||||
|
||||
bool TransferIDRegistry::create(const Key& key, const Entry& entry)
|
||||
{
|
||||
if (key.node_id > NODE_ID_MAX || key.transfer_type >= NUM_TRANSFER_TYPES)
|
||||
if (key.node_id > NODE_ID_MAX || key.data_type_kind >= NUM_DATA_TYPE_KINDS)
|
||||
{
|
||||
assert(0);
|
||||
return false;
|
||||
}
|
||||
return lists_by_transfer_type_[key.transfer_type].create(
|
||||
return lists_by_data_type_kind_[key.data_type_kind].create(
|
||||
StorageEntry(key.node_id, key.data_type_id, entry), allocator_);
|
||||
}
|
||||
|
||||
void TransferIDRegistry::remove(const Key& key)
|
||||
{
|
||||
if (key.node_id > NODE_ID_MAX || key.transfer_type >= NUM_TRANSFER_TYPES)
|
||||
if (key.node_id > NODE_ID_MAX || key.data_type_kind >= NUM_DATA_TYPE_KINDS)
|
||||
{
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
lists_by_transfer_type_[key.transfer_type].remove(key, allocator_);
|
||||
lists_by_data_type_kind_[key.data_type_kind].remove(key, allocator_);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ TEST(TransferIDRegistry, Basic)
|
||||
{
|
||||
using uavcan::Frame;
|
||||
using uavcan::TransferID;
|
||||
using uavcan::TransferType;
|
||||
using uavcan::DataTypeKind;
|
||||
using uavcan::TransferIDRegistry;
|
||||
typedef TransferIDRegistry::Key Key;
|
||||
typedef TransferIDRegistry::Entry Entry;
|
||||
@@ -32,7 +32,7 @@ TEST(TransferIDRegistry, Basic)
|
||||
|
||||
TransferIDRegistry reg(&poolmgr);
|
||||
|
||||
ASSERT_EQ(NULL, reg.access(Key(0, uavcan::MESSAGE_BROADCAST, 0)));
|
||||
ASSERT_EQ(NULL, reg.access(Key(0, uavcan::DATA_TYPE_KIND_MESSAGE, 0)));
|
||||
|
||||
static const int NUM_ITEMS = 100; // Just to make sure it will be enough
|
||||
Key keys[NUM_ITEMS];
|
||||
@@ -44,7 +44,7 @@ TEST(TransferIDRegistry, Basic)
|
||||
{
|
||||
keys[i].data_type_id = i * (Frame::DATA_TYPE_ID_MAX / NUM_ITEMS);
|
||||
keys[i].node_id = i * (uavcan::NODE_ID_MAX / NUM_ITEMS);
|
||||
keys[i].transfer_type = TransferType(i % uavcan::NUM_TRANSFER_TYPES);
|
||||
keys[i].data_type_kind = DataTypeKind(i % uavcan::NUM_DATA_TYPE_KINDS);
|
||||
|
||||
entries[i].timestamp = i * 10000000;
|
||||
entries[i].transfer_id = TransferID(i % TransferID::MAX);
|
||||
|
||||
Reference in New Issue
Block a user