2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#ifndef CSCORE_HANDLE_H_
|
|
|
|
|
#define CSCORE_HANDLE_H_
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2021-09-17 12:28:12 -07:00
|
|
|
#include <wpi/Synchronization.h>
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#include "cscore_c.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
|
|
|
|
// Handle data layout:
|
|
|
|
|
// Bits 0-15: Handle index
|
2016-09-19 22:03:47 -07:00
|
|
|
// Bits 16-23: Parent index (property only)
|
2016-09-05 12:00:04 -07:00
|
|
|
// Bits 24-30: Type
|
|
|
|
|
|
|
|
|
|
class Handle {
|
|
|
|
|
public:
|
2018-07-27 22:12:30 -07:00
|
|
|
enum Type {
|
|
|
|
|
kUndefined = 0,
|
2021-09-17 12:28:12 -07:00
|
|
|
kProperty = wpi::kHandleTypeCSBase,
|
2018-07-27 22:12:30 -07:00
|
|
|
kSource,
|
|
|
|
|
kSink,
|
|
|
|
|
kListener,
|
2021-01-26 23:07:16 -08:00
|
|
|
kSinkProperty,
|
|
|
|
|
kListenerPoller
|
2018-07-27 22:12:30 -07:00
|
|
|
};
|
2016-09-05 12:00:04 -07:00
|
|
|
enum { kIndexMax = 0xffff };
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
Handle(CS_Handle handle) : m_handle(handle) {} // NOLINT
|
2016-09-05 12:00:04 -07:00
|
|
|
operator CS_Handle() const { return m_handle; }
|
|
|
|
|
|
|
|
|
|
Handle(int index, Type type) {
|
|
|
|
|
if (index < 0) {
|
|
|
|
|
m_handle = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_handle = ((static_cast<int>(type) & 0x7f) << 24) | (index & 0xffff);
|
|
|
|
|
}
|
|
|
|
|
Handle(int index, int property, Type type) {
|
|
|
|
|
if (index < 0 || property < 0) {
|
|
|
|
|
m_handle = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_handle = ((static_cast<int>(type) & 0x7f) << 24) |
|
2016-09-19 22:03:47 -07:00
|
|
|
((index & 0xff) << 16) | (property & 0xffff);
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetIndex() const { return static_cast<int>(m_handle) & 0xffff; }
|
|
|
|
|
Type GetType() const {
|
|
|
|
|
return static_cast<Type>((static_cast<int>(m_handle) >> 24) & 0xff);
|
|
|
|
|
}
|
|
|
|
|
bool IsType(Type type) const { return type == GetType(); }
|
|
|
|
|
int GetTypedIndex(Type type) const { return IsType(type) ? GetIndex() : -1; }
|
2016-09-19 22:03:47 -07:00
|
|
|
int GetParentIndex() const {
|
2018-07-27 22:12:30 -07:00
|
|
|
return (IsType(Handle::kProperty) || IsType(Handle::kSinkProperty))
|
|
|
|
|
? (static_cast<int>(m_handle) >> 16) & 0xff
|
|
|
|
|
: -1;
|
2016-09-19 22:03:47 -07:00
|
|
|
}
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
CS_Handle m_handle;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#endif // CSCORE_HANDLE_H_
|