Boost logo

Boost Users :

From: Kevin Wheatley (yg-boost-users_at_[hidden])
Date: 2003-07-10 13:41:22


Hi,

I wondered if there are any examples of a templated handle class
within the boost code base I could look at?

I'm trying to hide both the implementation of the body class and that
of the template type, something like that below but my compiler is
complaining about partial specialization in the declaration of the
implementation class in handle.cpp

I know its not strictly boost related, but I don't get any of the
comp.lang.* groups.
Also before anybody suggests I should be using a smart pointer for the
impl variable I'd like to avoid having to introduce the boost library
to the user of the two .h files.

My goal was that the users would only need the *.h files and I could
provide fred.o fred2.o fred3.o etc. as new options for "fred" come
along.

I know that using the template doesn't appear to be getting me
anywhere towads my original goal (generally needing the implementation
to get the templated code to work!) but I'd like to see if I could at
least get the code to compile by studying anything similar as it may
have some use in other areas.

Thanks

Kevin

// -------------- fred.h
class fred
{
  public:
    void somefunction();
};

// -------------- handle.h

template <typename T>
class handle
{
  public:
    handle();
    ~handle();
    void callsomefunctionviaimpl();
  private:
    template <typename U> class handleimpl;
    handleimpl<T> *impl;
};

// -------------- fred.cpp
#include <iostream>
using namespace std;

#include "fred.h"

void fred::somefunction()
{
  cout << "fred::somefunction()" << endl;
}

// -------------- handle.cpp
#include <iostream>
using namespace std;

#include "handle.h"

template <class T, class U>
class handle<T>::handleimpl<U>
{
  public:
     void implfunctiononfred() { myfred.somefunction(); }
  private:
    static T myfred;
};

template <class T>
handle<T>::handle() : impl(new handle<T>::handleimpl<T>) { }

template <class T>
handle<T>::~handle() { delete impl; }

template <class T>
void handle<T>::callsomefunctionviaimpl()
{
  cout << "handle calling" << endl;
  impl->implfunctiononfred();
}

// ----------- Main code ----------------
#include "fred.h"
#include "handle.h"

int main(int argc, char** argv)
{
  handle<fred> foo;
  foo.callsomefunctionviaimpl();
  return 0;
}

-- 
| Kevin Wheatley                   | These are the opinions of |
| Senior Do-er of Technical Things | nobody and are not shared |
| Cinesite (Europe) Ltd            | by my employers           |

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net