M
Michael Wojcik
Please use proper attribution lines (like the one above). I wrote the
twice-quoted text below.
An aside: the C language specification does not use the term
"signature", and I would recommend avoiding it when discussing C,
as it has a specific meaning in some languages (particularly ones
that support overloading) that is not applicable to C.
It depends on the function in question and the design of the library
interface, but in general I create a new function with one or more
additional parameters, and refactor the old function to call the
new function, with appropriate values for the new parameter(s) to
indicate that the old behavior should be used.
For example, say the first version of the library includes a
factory-pattern function for an ADT used by the library. The
interface might be something like:
struct XxxThing;
struct XxxThing *XxxCreateThing(void);
In the next version, I find it useful to have an enhanced factory
that performs some optional processing. The new interface is:
struct XxxThing;
enum XxxCTOption
{
XxxOPT_NONE = 0,
XxxOPT_SOMETHING,
XxxOPT_ANOTHER
};
struct XxxThing *XxxCreateThing(void);
struct XxxThing *XxxCreateThingEx(enum XxxCTOption Opt);
and in the implementation:
struct XxxThing *XxxCreateThing(void)
{
return XxxCreateThingEx(XxxOPT_NONE);
}
That's certainly not ideal - for one thing, it'd be better if the
name "XxxCreateThingEx" were more descriptive, but since this is
hypothetical I don't know what a "Thing" is or what the options
actually do - but it's a start. It'd be better if the original
XxxCreateThing had taken, say, a "void *Reserved" parameter which
could be used for other purposes in future versions.
--
Michael Wojcik (e-mail address removed)
Maybe, but it can't compete with _SNA Formats_ for intricate plot
twists. "This format is used only when byte 5, bit 1 is set to 1
(i.e., when generalized PIU trace data is included)" - brilliant!
twice-quoted text below.
I think is possible (and frequent) to have a new version of a existing
function with a new parameter in his signature.
An aside: the C language specification does not use the term
"signature", and I would recommend avoiding it when discussing C,
as it has a specific meaning in some languages (particularly ones
that support overloading) that is not applicable to C.
what way you propose to achieve such change mantaining
backward compatibility?
It depends on the function in question and the design of the library
interface, but in general I create a new function with one or more
additional parameters, and refactor the old function to call the
new function, with appropriate values for the new parameter(s) to
indicate that the old behavior should be used.
For example, say the first version of the library includes a
factory-pattern function for an ADT used by the library. The
interface might be something like:
struct XxxThing;
struct XxxThing *XxxCreateThing(void);
In the next version, I find it useful to have an enhanced factory
that performs some optional processing. The new interface is:
struct XxxThing;
enum XxxCTOption
{
XxxOPT_NONE = 0,
XxxOPT_SOMETHING,
XxxOPT_ANOTHER
};
struct XxxThing *XxxCreateThing(void);
struct XxxThing *XxxCreateThingEx(enum XxxCTOption Opt);
and in the implementation:
struct XxxThing *XxxCreateThing(void)
{
return XxxCreateThingEx(XxxOPT_NONE);
}
That's certainly not ideal - for one thing, it'd be better if the
name "XxxCreateThingEx" were more descriptive, but since this is
hypothetical I don't know what a "Thing" is or what the options
actually do - but it's a start. It'd be better if the original
XxxCreateThing had taken, say, a "void *Reserved" parameter which
could be used for other purposes in future versions.
--
Michael Wojcik (e-mail address removed)
Maybe, but it can't compete with _SNA Formats_ for intricate plot
twists. "This format is used only when byte 5, bit 1 is set to 1
(i.e., when generalized PIU trace data is included)" - brilliant!