mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-15 04:50:20 +01:00
glib-aux: add nm_ref_string_reset_str_upcast() helper
This commit is contained in:
parent
39c308f370
commit
28018d6985
1 changed files with 43 additions and 4 deletions
|
|
@ -164,24 +164,63 @@ nm_ref_string_unref_upcast(const char *str)
|
|||
nm_ref_string_unref(NM_REF_STRING_UPCAST(str));
|
||||
}
|
||||
|
||||
/**
|
||||
* nm_ref_string_reset_str_upcast:
|
||||
* @ptr: the destination pointer that gets updated.
|
||||
* @str: the new string to be set.
|
||||
*
|
||||
* @ptr is a location (destination pointer) of an "upcast" NMRefString.
|
||||
* That is, it holds either %NULL or some ((NMRefString *) rstr)->str.
|
||||
* In other words, @ptr holds an NMRefString which you could get via
|
||||
* NM_REF_STRING_UPCAST(*ptr).
|
||||
* This function resets @ptr to point to a NMRefString equal to @str.
|
||||
*
|
||||
* Returns: %TRUE if the pointer changed and %FALSE if the value was
|
||||
* already set to a string equal to @str.
|
||||
*/
|
||||
static inline gboolean
|
||||
nm_ref_string_reset_str_upcast(const char **ptr, const char *str)
|
||||
{
|
||||
NMRefString *rstr;
|
||||
gsize l;
|
||||
|
||||
nm_assert(ptr);
|
||||
|
||||
if (!str)
|
||||
return nm_clear_pointer(ptr, nm_ref_string_unref_upcast);
|
||||
|
||||
rstr = NM_REF_STRING_UPCAST(*ptr);
|
||||
|
||||
l = strlen(str);
|
||||
|
||||
if (rstr && rstr->len == l && (rstr->str == str || memcmp(rstr->str, str, l) == 0))
|
||||
return FALSE;
|
||||
|
||||
*ptr = nm_ref_string_new_len(str, l)->str;
|
||||
nm_ref_string_unref(rstr);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static inline gboolean
|
||||
nm_ref_string_reset_str(NMRefString **ptr, const char *str)
|
||||
{
|
||||
nm_auto_ref_string NMRefString *rstr = NULL;
|
||||
gsize l;
|
||||
NMRefString *rstr;
|
||||
gsize l;
|
||||
|
||||
nm_assert(ptr);
|
||||
|
||||
if (!str)
|
||||
return nm_clear_pointer(ptr, nm_ref_string_unref);
|
||||
|
||||
rstr = *ptr;
|
||||
|
||||
l = strlen(str);
|
||||
|
||||
if ((*ptr) && (*ptr)->len == l && ((*ptr)->str == str || memcmp((*ptr)->str, str, l) == 0))
|
||||
if (rstr && rstr->len == l && (rstr->str == str || memcmp(rstr->str, str, l) == 0))
|
||||
return FALSE;
|
||||
|
||||
rstr = *ptr;
|
||||
*ptr = nm_ref_string_new_len(str, l);
|
||||
nm_ref_string_unref(rstr);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue