SDL_ttf not taking const pointers?

Is there a reason SDL_ttf’s various query functions (for example,
TTF_FontHeight or TTF_FontLineSkip) don’t take const pointers to
TTF_Font structs? They take non-const pointers, but are they actually
modifying the struct itself?

Can these be changed to const pointers? This would allow for cleaner
setups using these fonts - having the ability to pass const font
poitners without worrying (much) about someone changing them.

If this is the wrong place for SDL_ttf questions, where is that place?

Thanks.–
Steaphan Greene
GPG public key: http://www.cs.binghamton.edu/~sgreene/gpg.key.txt
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060329/6b00f905/attachment.pgp

I was going to reply that SDL_ttf is a C (not C++) library and probably doesn’t
use “const”…

However, I looked at the code and it does use “const” in several places (at
least for const char pointers).

Also, here is the source for TTF_FontHeight():

int TTF_FontHeight(TTF_Font *font)
{
return(font->height);
}

Obviously, “const” would work fine in this case. Maybe you should try to
contact the SDL_ttf author. You might even download the source and take a
crack at the changes yourself…

John Giors
Game Programmer
Pandemic Studios> Message: 7

Date: Wed, 29 Mar 2006 22:40:03 -0500
From: Stea Greene
Subject: [SDL] SDL_ttf not taking const pointers?
To: sdl at libsdl.org
Message-ID: <20060330034003.GB11669 at cs.binghamton.edu>
Content-Type: text/plain; charset=“us-ascii”

Is there a reason SDL_ttf’s various query functions (for example,
TTF_FontHeight or TTF_FontLineSkip) don’t take const pointers to
TTF_Font structs? They take non-const pointers, but are they actually
modifying the struct itself?

Can these be changed to const pointers? This would allow for cleaner
setups using these fonts - having the ability to pass const font
poitners without worrying (much) about someone changing them.

If this is the wrong place for SDL_ttf questions, where is that place?

Thanks.


Steaphan Greene
GPG public key: http://www.cs.binghamton.edu/~sgreene/gpg.key.txt


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Obviously, “const” would work fine in this case. Maybe you should try to
contact the SDL_ttf author. You might even download the source and take a
crack at the changes yourself…

That was my plan. If nobody did it for me, and nobody say “no, it will
misalign the whole planet if you did that”, my next step was to post a
patch. I’ll do that this weekend.On Fri, Mar 31, 2006 at 01:56:49PM -0800, John Giors wrote:


Steaphan Greene
GPG public key: http://www.cs.binghamton.edu/~sgreene/gpg.key.txt
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060331/42052eec/attachment.pgp

Changing a function argument from T* to T const* will not
impact any C functions calling it. It also won’t impact
the function itself, unless of course the function is using
the pointer in a non-const way.

The change may impact C++, but only if this would result
in an ambiguous overload (which is very unlikely in this
context).

Unfortunately, there is a small snowball effect. The function
may be passing the pointer to a function which itself is
not correctly declaring an argument const that should be,
so that function will have to be modified as well.

Changing the return type of a function to T const*
may have a large snowball effect. It can break lots of
code which was forced to accept a T* previously, and
thus cannot accept a T const*. For example

int *id(int *pi) { return pi; } 
int *p = &x; // some int x somewhere
int *q;
q = id(p);

Now if you change the argument to int const*, the function
return is wrong, so you have to make

int const *id(int const *pi) { return pi; }

And now

q = id(p); // ERROR assign int const* to int*q

The bottom line here is that changing pointers to use
const can snowball unless it is restricted to function
arguments.

The semantics of const are unfortunately not very useful:
the conversion T* --> T const* allows aliasing and thereby
make it impossible to cache pointed at values on the assumption
that they cannot change, which basically defeats its purpose.On Fri, 2006-03-31 at 17:18 -0500, Stea Greene wrote:

On Fri, Mar 31, 2006 at 01:56:49PM -0800, John Giors wrote:

Obviously, “const” would work fine in this case. Maybe you should try to
contact the SDL_ttf author. You might even download the source and take a
crack at the changes yourself…

That was my plan. If nobody did it for me, and nobody say “no, it will
misalign the whole planet if you did that”, my next step was to post a
patch. I’ll do that this weekend.


John Skaller
Felix, successor to C++: http://felix.sf.net

Changing a function argument from T* to T const* will not impact any C
functions calling it. It also won’t impact the function itself, unless
of course the function is using the pointer in a non-const way.

Sure it will. If they are declared const, then other functions with
only const TTF_Font pointers can call it with these. Otherwise, they
can not, and all the pointers must be non-const, even if a function
should never modify them.

I’m not suggesting anything major, just something like the attached
patch (to current CVS of SDL_ttf). While I think it would be useful to
go through and see if it’s possible to constify the TTF_Font pointers to
functions like _Render and _Size, that’s more work than I was
thinking at the moment. Though, if people think it would be helpful,
maybe I will see what I can do.

I’ll see what Sam/Ryan says.

Changing the return type of a function to T const* may have a large
snowball effect. It can break lots of code which was forced to accept
a T* previously, and thus cannot accept a T const*. For example

Yeah, wasn’t gonna go there. Bad eats.On Sat, Apr 01, 2006 at 01:25:24PM +1100, skaller wrote:


Steaphan Greene
GPG public key: http://www.cs.binghamton.edu/~sgreene/gpg.key.txt
-------------- next part --------------
A non-text attachment was scrubbed…
Name: SDL-ttf-sgreene-constify-1.0.patch.gz
Type: application/octet-stream
Size: 997 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060403/a96e1afa/attachment.obj
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060403/a96e1afa/attachment.pgp