This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: filebuf::fd()


On Mon, Dec 10, 2001 at 04:49:06PM -0600, Loren James Rittle wrote:
> I have no idea if there is a flaw in my thinking but it seems when a
> type is unknown at the time an API is defined, we must use a template:
> 
> template <class HT>
> HT get_raw_handle (iostream& i);
> 
> Under this approach, the application attempts to get a handle of the
> type it needs with, e.g., get_raw_handle<FILE*> (cin).  An exception
> could be thrown if the handle used by the libstdc++-v3 implementation
> is not of that type (perhaps we could also allow conversion from FILE*
> to file descriptor).

** WARNING:  completely untested idea follows!
** Put on your sanity goggles now!

Rather than throwing an exception at runtime, we could provide explicit
specializations for the raw handles supported by any given scheme.  Then if
the user tried something that the current --enable-cstdio choice didn't
support, it would be a compile-time error.  Well, link-time.

  template<typename _CharT, typename _Traits>
    class basic_filebuf
    {
        ....

        template <typename HT>
          HT get_raw_handle();           // intentionally undefined, perhaps
    };

  template <typename _CharT, typename _Traits>
  template <typename HT>
    HT
    basic_filebuf<_CharT,_Traits>::get_raw_handle()
    {
        throw something nasty;           // or perhaps we define it after all
    }


Now, the stdio case can define specializations

    // stdio can define this case
    template <typename _CharT, typename _Traits>
    template <>
      FILE*
      basic_filebuf<_CharT,_Traits>::get_raw_handle()
      {
          return _M_file;
      }
  
    // and maybe also this one
    template <typename _CharT, typename _Traits>
    template <>
      int
      basic_filebuf<_CharT,_Traits>::get_raw_handle()
      {
          return fileno(_M_file);
      }

And the libio case can define its own

    // requires glibc 2.3.4.5.6
    template <typename _CharT, typename _Traits>
    template <>
      _g_funky
      basic_filebuf<_CharT,_Traits>::get_raw_handle()
      {
          return __something_weird();
      }

etc.  Then once installed, J. Nonportable User can write either

    FILE*  fp = simefilebuf.get_raw_handle<FILE*>();
    int    fd = simefilebuf.get_raw_handle<int>();

or

    FILE*  fp = somefilebuf.template get_raw_handle<FILE*>();
    int    fd = somefilebuf.template get_raw_handle<int>();

because I don't recall offhand how that syntax works, and a cursory look
over 14882 doesn't point it out.


> > If we're going to provide this as an extension in 3.1, we need to finalize
> > this before the end of Phase 2, six days from now.
> 
> I know some users want this feature but is any idea baked enough yet?

I'm thinking not, actually....


Phil

-- 
If ye love wealth greater than liberty, the tranquility of servitude greater
than the animating contest for freedom, go home and leave us in peace.  We seek
not your counsel, nor your arms.  Crouch down and lick the hand that feeds you;
and may posterity forget that ye were our countrymen.            - Samuel Adams


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]