| 49 | } |
| 50 | |
| 51 | const Device* CounterDirectory::RegisterDevice(const std::string& deviceName, |
| 52 | uint16_t cores, |
| 53 | const arm::pipe::Optional<std::string>& parentCategoryName) |
| 54 | { |
| 55 | // Check that the given device name is valid |
| 56 | if (deviceName.empty() || |
| 57 | !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(deviceName)) |
| 58 | { |
| 59 | throw arm::pipe::InvalidArgumentException("Trying to register a device with an invalid name"); |
| 60 | } |
| 61 | |
| 62 | // Check that a device with the given name is not already registered |
| 63 | if (IsDeviceRegistered(deviceName)) |
| 64 | { |
| 65 | throw arm::pipe::InvalidArgumentException(fmt::format( |
| 66 | "Trying to register a device already registered (\"{}\")", |
| 67 | deviceName)); |
| 68 | } |
| 69 | |
| 70 | // Check that a category with the given (optional) parent category name is already registered |
| 71 | if (parentCategoryName.has_value()) |
| 72 | { |
| 73 | // Get the (optional) parent category name |
| 74 | const std::string& parentCategoryNameValue = parentCategoryName.value(); |
| 75 | if (parentCategoryNameValue.empty()) |
| 76 | { |
| 77 | throw arm::pipe::InvalidArgumentException( |
| 78 | fmt::format("Trying to connect a device (name: \"{}\") to an invalid " |
| 79 | "parent category (name: \"{}\")", |
| 80 | deviceName, |
| 81 | parentCategoryNameValue)); |
| 82 | } |
| 83 | |
| 84 | // Check that the given parent category is already registered |
| 85 | auto categoryIt = FindCategory(parentCategoryNameValue); |
| 86 | if (categoryIt == m_Categories.end()) |
| 87 | { |
| 88 | throw arm::pipe::InvalidArgumentException( |
| 89 | fmt::format("Trying to connect a device (name: \"{}\") to a parent category that " |
| 90 | "is not registered (name: \"{}\")", |
| 91 | deviceName, |
| 92 | parentCategoryNameValue)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Get the device UID |
| 97 | uint16_t deviceUid = GetNextUid(); |
| 98 | |
| 99 | // Create the device |
| 100 | DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores); |
| 101 | ARM_PIPE_ASSERT(device); |
| 102 | |
| 103 | // Get the raw device pointer |
| 104 | const Device* devicePtr = device.get(); |
| 105 | ARM_PIPE_ASSERT(devicePtr); |
| 106 | |
| 107 | // Register the device |
| 108 | m_Devices.insert(std::make_pair(deviceUid, std::move(device))); |
no test coverage detected