|
| 1 | +// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | +#include "silkit/capi/Parameters.h" |
| 10 | +#include "silkit/participant/exception.hpp" |
| 11 | + |
| 12 | +namespace SilKit { |
| 13 | +DETAIL_SILKIT_DETAIL_VN_NAMESPACE_BEGIN |
| 14 | +namespace Impl { |
| 15 | + |
| 16 | +class ParameterProvider |
| 17 | +{ |
| 18 | +public: |
| 19 | + inline ParameterProvider(); |
| 20 | + |
| 21 | + inline ~ParameterProvider() = default; |
| 22 | + |
| 23 | + inline auto GetStringParameter(SilKit_Participant* participant, Parameter parameter) -> std::string; |
| 24 | + |
| 25 | +private: |
| 26 | + using StringQueryFunction = SilKit_ReturnCode(SilKitCALL*)(void*, size_t*, SilKit_Participant*); |
| 27 | + inline auto QueryString(SilKit_Participant* participant, StringQueryFunction stringQueryFunction) -> std::string; |
| 28 | + |
| 29 | +}; |
| 30 | + |
| 31 | +} // namespace Impl |
| 32 | +DETAIL_SILKIT_DETAIL_VN_NAMESPACE_CLOSE |
| 33 | +} // namespace SilKit |
| 34 | + |
| 35 | + |
| 36 | +// ================================================================================ |
| 37 | +// Inline Implementations |
| 38 | +// ================================================================================ |
| 39 | + |
| 40 | +#include "silkit/detail/impl/ThrowOnError.hpp" |
| 41 | + |
| 42 | +namespace SilKit { |
| 43 | +DETAIL_SILKIT_DETAIL_VN_NAMESPACE_BEGIN |
| 44 | +namespace Impl { |
| 45 | + |
| 46 | +ParameterProvider::ParameterProvider() |
| 47 | +{ |
| 48 | +} |
| 49 | + |
| 50 | +auto ParameterProvider::QueryString(SilKit_Participant* participant, |
| 51 | + StringQueryFunction stringQueryFunction) -> std::string |
| 52 | +{ |
| 53 | + std::vector<char> buffer; |
| 54 | + size_t size = 0; |
| 55 | + |
| 56 | + { |
| 57 | + const auto returnCode = stringQueryFunction(nullptr, &size, participant); |
| 58 | + ThrowOnError(returnCode); |
| 59 | + } |
| 60 | + |
| 61 | + while (size > buffer.size()) |
| 62 | + { |
| 63 | + buffer.resize(size); |
| 64 | + const auto returnCode = stringQueryFunction(buffer.data(), &size, participant); |
| 65 | + ThrowOnError(returnCode); |
| 66 | + } |
| 67 | + |
| 68 | + return std::string{buffer.data()}; |
| 69 | +} |
| 70 | + |
| 71 | +auto ParameterProvider::GetStringParameter(SilKit_Participant* participant, Parameter parameter) -> std::string |
| 72 | +{ |
| 73 | + switch (parameter) |
| 74 | + { |
| 75 | + case Parameter::ParticipantName: |
| 76 | + return QueryString(participant, &SilKit_Participant_GetParticipantName); |
| 77 | + case Parameter::RegistryUri: |
| 78 | + return QueryString(participant, &SilKit_Participant_GetGetRegistryUri); |
| 79 | + default: |
| 80 | + throw SilKit::SilKitError("Unknown parameter."); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace Impl |
| 85 | +DETAIL_SILKIT_DETAIL_VN_NAMESPACE_CLOSE |
| 86 | +} // namespace SilKit |
0 commit comments