Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add const version of some array operation #37

Open
yanis-fourel opened this issue Jan 21, 2022 · 3 comments
Open

Add const version of some array operation #37

yanis-fourel opened this issue Jan 21, 2022 · 3 comments
Labels
++c Will be eventually fixed, using ++C

Comments

@yanis-fourel
Copy link
Collaborator

Some array/strings utility function cannot take a T const** as argument because they need to possibly reallocate and return the new pointer to the array.
For example:

// Appends the given string `string` to the end of the given string array `dest`.
t_char** StringArray_Add(t_char** dest, t_char const* str);

This forces T const** type array to be casted to T**.
One should be able to pass a T const** type array because the function does not modify the elements.

I suggest adding another version of these functions and wrapping them like so:

t_char const** StringArray_Add_Const(t_char const** dest, t_char const* str)
{
        // The current implementation of `StringArray_Add` which doesn't modify the content of strings anyway
    	t_char const**	result;
	t_uint		length;

	HANDLE_ERROR(NULLPOINTER, (dest == NULL), return (NULL);)
	HANDLE_ERROR(NULLPOINTER, (str  == NULL), return (NULL);)
	length = (StringArray_Length((t_char const**)dest));
	result = (t_char const**)Memory_Reallocate(dest, (length + 1) * sizeof(t_char*));
	HANDLE_ERROR(ALLOCFAILURE, (result == NULL), return (NULL);)
	result[length] = String_Duplicate(str);
	return (result);
}

t_char** StringArray_Add(t_char** dest, t_char const* str)
{
        return (t_char**)StringArray_Add_Const((t_char const**)dest, str);
}

Here is a list of all the functions I think this applies:

  • PointerArray_Add
  • PointerArray_Insert
  • PointerArray_Wedge
  • PointerArray_Replace
  • PointerArray_ReplaceFirst
  • PointerArray_ReplaceLast
  • PointerArray_Concat
  • PointerArray_Append
  • PointerArray_Prepend
  • PointerArray_Join
  • PointerArray_Find
  • PointerArray_Find_F
  • PointerArray_Iterate
  • PointerArray_Iterate_I
  • PointerArray_Map (need to double check if this applies)
  • PointerArray_Map_I (need to double check if this applies)
  • PointerArray_Filter
  • PointerArray_Filter_I
  • StringArray_Add
  • StringArray_Insert
  • StringArray_Wedge
  • StringArray_Append (that would imply we free some t_char const*, does that make sense?)
  • StringArray_Prepend (same ^^^)
  • StringArray_Iterate
  • StringArray_Iterate_I
@yanis-fourel yanis-fourel added the enhancement New feature or request label Jan 21, 2022
@yanis-fourel
Copy link
Collaborator Author

This would probably also remove some explicit casts from T** to T const* const*, for example when we want to call xxx_Length on these array

@LexouDuck
Copy link
Owner

I am wary of adding even more "overloads" of existing functions, especially when the only purpose is purely to avoid some benign explicit casts...

The way I see it, explicit-casting nested const pointers is a necessity that stems from a shortcoming of the C language - you'll notice that C++ does not have this limitation: https://stackoverflow.com/questions/5055655/double-pointer-const-correctness-warnings-in-c

I'm not exactly "against" the idea of this issue, but I simply feel that it is the kind of thing that will be better fixed when libccc is eventually ported to ++C - that way I won't have to change my code to be uglier and more unwieldy.

@LexouDuck LexouDuck added ++c Will be eventually fixed, using ++C and removed enhancement New feature or request labels Mar 25, 2022
@yanis-fourel
Copy link
Collaborator Author

okay, I trust you with how you want to use ++c to solve that.
In my head, the main purpose of this change was not to avoid some explicit casts, but more the correctness of calling something you won't modify const. Allowing not to explicitly cast pointers is just one side effect of that cleaner, more precise typing. I suspect there would be other code coherence improvement because this is what the real type is: a pointer to some t_char whose t_chars are not modified

I agree it does make the code a bit more heavier to change...

I'm curious about how you can solve that with ++c. Some kind of function overloading or more advanced macros/templating system?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
++c Will be eventually fixed, using ++C
Projects
None yet
Development

No branches or pull requests

2 participants