[ntcore] NetworkTables 4 (#3217)

This commit is contained in:
Peter Johnson
2022-10-08 10:01:31 -07:00
committed by GitHub
parent 90cfa00115
commit 77301b126c
380 changed files with 34573 additions and 22095 deletions

View File

@@ -0,0 +1,54 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <memory>
#include <utility>
#include <wpi/UidVector.h>
#include "Handle.h"
namespace nt {
// Utility wrapper class for our UidVectors
template <typename T, size_t Size>
class HandleMap : public wpi::UidVector<std::unique_ptr<T>, Size> {
public:
template <typename... Args>
T* Add(int inst, Args&&... args) {
auto i = this->emplace_back();
auto& it = (*this)[i];
it = std::make_unique<T>(Handle(inst, i, T::kType),
std::forward<Args>(args)...);
return it.get();
}
std::unique_ptr<T> Remove(NT_Handle handle) {
Handle h{handle};
if (!h.IsType(T::kType)) {
return {};
}
unsigned int i = h.GetIndex();
if (i >= this->size()) {
return {};
}
return this->erase(i);
}
T* Get(NT_Handle handle) {
Handle h{handle};
if (!h.IsType(T::kType)) {
return {};
}
unsigned int i = h.GetIndex();
if (i >= this->size()) {
return {};
}
return (*this)[i].get();
}
};
} // namespace nt