Re: [PATCH 06/10] trailer: make trailer_info struct private

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2024.01.10 06:51, Linus Arver via GitGitGadget wrote:
> From: Linus Arver <linusa@xxxxxxxxxx>
> 
> In 13211ae23f (trailer: separate public from internal portion of
> trailer_iterator, 2023-09-09) we moved trailer_info behind an anonymous
> struct to discourage use by trailer.h API users. However it still left
> open the possibility of external use of trailer_info itself. Now that
> there are no external users of trailer_info, we can make this struct
> private.
> 
> Make this struct private by putting its definition inside trailer.c.
> This has two benefits:
> 
> (1) it makes the surface area of the public facing interface (trailer.h)
>     smaller, and
> 
> (2) external API users are unable to peer inside this struct (because it
>     is only ever exposed as an opaque pointer).
> 
> This change exposes some deficiencies in the API, mainly with regard to
> information about the location of the trailer block that was parsed.
> Expose new API functions to access this information (needed by
> builtin/interpret-trailers.c).
> 
> The idea in this patch to hide implementation details behind an "opaque
> pointer" is also known as the "pimpl" (pointer to implementation) idiom
> in C++ and is a common pattern in that language (where, for example,
> abstract classes only have pointers to concrete classes).
> 
> However, the original inspiration to use this idiom does not come from
> C++, but instead the book "C Interfaces and Implementations: Techniques
> for Creating Reusable Software" [1]. This book recommends opaque
> pointers as a good design principle for designing C libraries, using the
> term "interface" as the functions defined in *.h (header) files and
> "implementation" as the corresponding *.c file which define the
> interfaces.
> 
> The book says this about opaque pointers:
> 
>     ... clients can manipulate such pointers freely, but they can’t
>     dereference them; that is, they can’t look at the innards of the
>     structure pointed to by them. Only the implementation has that
>     privilege. Opaque pointers hide representation details and help
>     catch errors.
> 
> In our case, "struct trailer_info" is now hidden from clients, and the
> ways in which this opaque pointer can be used is limited to the richness
> of the trailer.h file. In other words, trailer.h exclusively controls
> exactly how "trailer_info" pointers are to be used.
> 
> [1] Hanson, David R. "C Interfaces and Implementations: Techniques for
>     Creating Reusable Software". Addison Wesley, 1997. p. 22
> 
> Signed-off-by: Linus Arver <linusa@xxxxxxxxxx>
> ---
>  builtin/interpret-trailers.c |  13 +--
>  trailer.c                    | 154 +++++++++++++++++++++++------------
>  trailer.h                    |  37 ++-------
>  3 files changed, 117 insertions(+), 87 deletions(-)

Looks like a pretty straightforward change. I think the only point worth
discussing is using the "pimpl" idiom. I think it's harder to see the
value because the trailer_info struct is fairly simple, but I can
definitely see this pattern being useful as we libify more complex parts
of Git where the struct internals have more complicated logic involved.
The pimpl pattern also seems like it will force us to think harder about
providing a useful interface, so I am in favor of using it here as
practice for future libification.




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux