Revert "pldm: use std::expected for instance ID allocation"
This reverts commit 70262ed7bf854b25d4b65628bc3c892ddfe9380f.
Change-Id: I1f54d6b60bf3cfa9f00764140eec6167e1e182d6
Signed-off-by: Manojkiran Eda <manojkiran.eda@gmail.com>
diff --git a/common/instance_id.hpp b/common/instance_id.hpp
index 2a29f82..40de1f1 100644
--- a/common/instance_id.hpp
+++ b/common/instance_id.hpp
@@ -5,70 +5,12 @@
#include <cerrno>
#include <cstdint>
#include <exception>
-#include <expected>
#include <string>
#include <system_error>
namespace pldm
{
-/**
- * @class InstanceIdError
- * @brief Exception for PLDM instance ID allocation and management errors.
- */
-class InstanceIdError : public std::exception
-{
- public:
- InstanceIdError(const InstanceIdError&) noexcept = default;
- InstanceIdError(InstanceIdError&&) noexcept = default;
- InstanceIdError& operator=(const InstanceIdError&) noexcept = default;
- InstanceIdError& operator=(InstanceIdError&&) noexcept = default;
- ~InstanceIdError() noexcept override = default;
-
- /** @brief Construct with an error code. */
- explicit InstanceIdError(int rc) : rc_(rc), msg_(rcToMsg(rc)) {}
-
- /** @brief Construct with an error code and a string message (copied). */
- InstanceIdError(int rc, const std::string& m) : rc_(rc), msg_(m) {}
-
- /** @brief Construct with an error code and a string message (moved). */
- InstanceIdError(int rc, std::string&& m) : rc_(rc), msg_(std::move(m)) {}
-
- /** @brief Get the error code. */
- int rc() const noexcept
- {
- return rc_;
- }
-
- /** @brief Get the error message. */
- const std::string& msg() const noexcept
- {
- return msg_;
- }
-
- /** @brief Convert an error code to a message. */
- static std::string rcToMsg(int rc)
- {
- switch (rc)
- {
- case -EAGAIN:
- return "No free instance ids";
- default:
- return std::system_category().message(rc);
- }
- }
-
- /** @brief Get the error message (for std::exception). */
- const char* what() const noexcept override
- {
- return msg_.c_str();
- }
-
- private:
- int rc_;
- std::string msg_;
-};
-
/** @class InstanceId
* @brief Implementation of PLDM instance id as per DSP0240 v1.0.0
*/
@@ -111,19 +53,22 @@
/** @brief Allocate an instance ID for the given terminus
* @param[in] tid - the terminus ID the instance ID is associated with
- * @return - PLDM instance id on success, or InstanceIdError on failure
+ * @return - PLDM instance id or -EAGAIN if there are no available instance
+ * IDs
*/
- std::expected<uint8_t, InstanceIdError> next(uint8_t tid)
+ uint8_t next(uint8_t tid)
{
uint8_t id;
int rc = pldm_instance_id_alloc(pldmInstanceIdDb, tid, &id);
+ if (rc == -EAGAIN)
+ {
+ throw std::runtime_error("No free instance ids");
+ }
+
if (rc)
{
- return std::unexpected(
- InstanceIdError{rc, "Failed to allocate instance ID for EID " +
- std::to_string(tid) + ": " +
- InstanceIdError::rcToMsg(rc)});
+ throw std::system_category().default_error_condition(rc);
}
return id;