]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
SetValue: Add various string-like type support
authorBreuninger Matthias (ETAS-DAP/XPC-Fe3) <Matthias.Breuninger@etas.com>
Fri, 31 Jan 2025 12:06:32 +0000 (13:06 +0100)
committerVincent Bernat <vincent@bernat.ch>
Tue, 4 Feb 2025 08:22:21 +0000 (09:22 +0100)
src/lib/lldpctl.hpp

index 4d8c038729e36837e7df953e497e2a599c0f0e51..c0cf3dc24e57bbf3d37ab705c801501ca92d543d 100644 (file)
@@ -90,6 +90,24 @@ class LldpErrCategory : public std::error_category {
        }
 };
 
+/**
+ * @brief Fallback type trait for checking against a const char array.
+ */
+template <typename T>
+struct is_const_char_array : std::false_type {};
+
+/**
+ * @brief Specialization of @p is_const_char_array for an actual array.
+ */
+template <std::size_t N>
+struct is_const_char_array<const char[N]> : std::true_type {};
+
+/**
+ * @brief Convenience constexpr for @p is_const_char_array value.
+ */
+template <typename T>
+inline constexpr bool is_const_char_array_v = is_const_char_array<T>::value;
+
 /**
  * @brief Wrapper class for @p lldpctl_atom_t with automatic lifetime management.
  */
@@ -252,7 +270,18 @@ class LldpAtom {
 
        template <typename T> void SetValue(lldpctl_key_t key, const T &data)
        {
-               if constexpr (std::is_same_v<T, std::optional<std::string>> ||
+               if constexpr (std::is_same_v<T, const char*> ||
+                   is_const_char_array<std::add_const_t<T>>::value ||
+                       std::is_same_v<T, std::nullptr_t>) {
+                       CHECK_LLDP_P(::lldpctl_atom_set_str(atom_, key,
+                                        data),
+                           conn_.get());
+               } else if constexpr (std::is_same_v<T, std::string> ||
+                   std::is_same_v<T, std::string_view>) {
+                       CHECK_LLDP_P(::lldpctl_atom_set_str(atom_, key,
+                                        data.data()),
+                           conn_.get());
+               } else if constexpr (std::is_same_v<T, std::optional<std::string>> ||
                    std::is_same_v<T, std::optional<std::string_view>>) {
                        CHECK_LLDP_P(::lldpctl_atom_set_str(atom_, key,
                                         data.has_value() ? data->data() : nullptr),