]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
fix: Error category handling
authorBreuninger Matthias (ETAS-ICA/XPC-Fe3) <matthias.breuninger@etas.com>
Tue, 5 Aug 2025 14:38:16 +0000 (16:38 +0200)
committerBreuninger Matthias (ETAS-ICA/XPC-Fe3) <matthias.breuninger@etas.com>
Tue, 5 Aug 2025 14:38:16 +0000 (16:38 +0200)
A std::error_code stores the error category only by reference, hence, we must use an object in static storage.
Add lldpcli::make_error_code for convenience similar to std::make_error_code.

src/lib/lldpctl.hpp

index e2e301a7ea203d3ba7dba03a8ef4d01b056a7a3d..9d69fdbdb21f3386dc16926015f2698040f19bd3 100644 (file)
 
 /* *** exported interfaces ****************************************************/
 
-namespace lldpcli {
-namespace literals {
-/**
- * @brief Operator to define std::byte literals.
- *
- * Example: auto byte{ 0x01_b };
- */
-consteval std::byte operator"" _b(unsigned long long int value)
+namespace
 {
-       return static_cast<std::byte>(value);
-}
-} // namespace literals
-
 /**
- * @brief LLDP error category.
+ * @brief LLDP error category. Don't use this class directly, intead, use @ref lldpcli::make_error_code.
  */
 class LldpErrCategory : public std::error_category {
     public:
@@ -90,6 +79,32 @@ class LldpErrCategory : public std::error_category {
        }
 };
 
+const LldpErrCategory lldp_err_category{};
+
+} // namespace
+
+namespace lldpcli {
+
+/**
+ * Convenience function to wrap an LLDP error code in a @p std::error_code.
+ */
+inline std::error_code make_error_code( lldpctl_error_t e )
+{
+    return { static_cast<int>( e ), lldp_err_category };
+}
+
+namespace literals {
+/**
+ * @brief Operator to define std::byte literals.
+ *
+ * Example: auto byte{ 0x01_b };
+ */
+consteval std::byte operator"" _b(unsigned long long int value)
+{
+       return static_cast<std::byte>(value);
+}
+} // namespace literals
+
 /**
  * @brief Fallback type trait for checking against a const char array.
  */