Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:29:55+00:00 2026-06-12T15:29:55+00:00

I have had a template for some time which wrappers a C library FILE*.

  • 0

I have had a template for some time which wrappers a C library FILE*. It’s a fairly classic implementation of a shared pointer to a wrapper class for the FILE*. The reasoning behind using my own custom shared pointer is to provide free function replacements for some of the C library FILE* free functions, in order to allow me to do a drop-in replacement of legacy code that works with FILE*.

The implementation that I have uses an inner wrapper that guarantees that when it is deleted, the owned FILE* is closed. RAII.

However, I’ve come into the need to create a similar system to handle the case where I want the underlying FILE* to be flushed & truncated, rather than closed when the last FILE* holder is destroyed. That is to say, I have an open FILE* of the original guaranteed-to-close type, but wish to hand out an unowned copy of the FILE* to another object that is going to guarantee that when it’s last instance is destroyed, that it will flush & truncate the FILE* rather than close it, hence leaving me with the underlying FILE* in an open state, but with the contents of the stream flushed to disk (and the file size reflecting only valid contents).

I have solved this trivially for compile time polymorphism. But I need some way to supply runtime polymorphism, and I really don’t want to put yet-another-layer-of-indirection into this scenario (i.e. if I used a polymorphic pointer to either an auto-close or auto-flush FILE* wrapper, I’d be golden – but I really want to keep the same depth I have now and hide the polymorphism inside the custom shared pointer implementation).

Basically, if I have a:

template <class WrapperT>  
class FilePointerT  
{  
public:
  // omitted: create, destroy, manipulate the underlying wrappered FILE*  
private:
  WrapperT * m_pFileWrapper;  
  ReferenceCount m_rc;  
}

Obviously, tons of details omitted. Suffice it to say that when the last one of these objects is deleted, it deletes the last m_pFileWrapper (in fact, if I were rewriting this code, I’d probably use a boost::shared_ptr).

Regardless, the real issue here is I am stumped on how to have a FilePointerT<WrapperT> whose WrapperT can vary, but can then be used in code as if they were all the same (which, after all, they are, since the implementation of WrapperT has zero affect on the structure and interface of FilePointerT (essentially a pimpl).

What can I declare that can possibly hold any FilePointerT<WrapperT> for any WrapperT?

Or, how can I change the definition of FilePointerT in order to allow me to supply specific WrapperT?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:29:57+00:00Added an answer on June 12, 2026 at 3:29 pm

    For what it’s worth, what I ended up doing is to embed the wrapper into the FilePointer class, instead of making it part of its type.

    class FilePointer
    {
    public:
        // create using a file wrapper (which will handle policy issues)
        FilePointer(FileWrapper * pfw) : m_pFileWrapper(pfw) { }
    
    protected:
        FileWrapper *   m_pFileWrapper; // wrapper has close/flush policy
        ReferenceCount  m_references;   // reference count
    };
    

    Then the file pointer just delegates the real work to the wrapper, and the wrapper implements the needed policy, and code can be written to consume FilePointer(s).

    There are obviously other ways to do this, but that’s what I went with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a tabBarApplication that I have as a template which has a UINavigation
From time to time I have to write some simple summarized reports using Django.
I have a page which contains some search fields to query a database, and
I had some memory problems due to Xcode's template for a UIPageViewController caching all
I have had an online archive service for over a year now. Unfortunately, I
I have had to compile my app for the 4.0 sdk to run on
I have had a persistent problem with Visual Studio 2010 and any framework version
I have had this problem crop up a few times and I can't figure
I have had a look all over, but it doesn't seem as though my
I have had several situations when i would like to do that. This could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.