Re: [Re: [libxml++] UTF8 support]



On Thu, Mar 20, 2003 at 09:44:20AM -0500, Stefan Seefeld wrote:

> >- -------
> >
> >template<typename String_t>
> >class Node {  // or maybe BaseNode
> >	public:
> >		String_t get_content();
> >}
> >
> >namespace qt {
> >	typedef xmlpp::Node<QString> Node;
> >}
> >
> >namespace glib {
> >	typedef xmlpp::Node<Glib::ustring> Node;
> >}
> 
> This will instantiate the actual types, and thus require the full
> template code to be seen. That's what Murray objects to. In order
> to hide the templates you really have to use compiler firewalls 
> ('pimpls'), which requires more than a 'typedef': it requires
> real wrapper classes. (you can't forward declare a typedef...).

Nothing will get instantiated by this, typedefs only introduce a new
name for a type, they don't instatiate anything. As long as the
template had been forward declared before the typedef it would be ok.
In the code below (which compiles if I replace the two string types with
std::string, I don't have the QString or glib::ustring headers here to
test it) the typedefs are given before seeing the body of the Node
template.

--------------------------
// node.h
// declares typedefs for common string types and defines class interface
// included by users who want to use Node<T>, qt::Node or glib::Node

#include <glibmm/ustring.h>   // or whatever the right header is
#include <qt/qstring.h>       // or whatever the right header is

namespace xmlpp {

    template<typename StringT> class Node;   // fwd decl of template

    namespace qt {
        typedef Node<QString> Node;  // typedef without seeing template
    }

    namespace glib {
        typedef Node<glib::ustring> Node;  // another typedef
    }

    // now give template definition, but no function definitions
    template<typename StringT>
    class Node {
    public:
        Node();
        void makeSomeNoise() const;
    };
}
--------------------------

--------------------------
// node.tcc
// template definitions for xmlpp::Node<StringT>
// Only included by users for "uncommon" string types,
// otherwise only used in lib

#include "node.h"
#include <iostream>

template <typename StringT>
xmlpp::Node<StringT>::Node()
{
}

template <typename StringT>
void
xmlpp::Node<StringT>::makeSomeNoise() const
{
    std::cerr << "RAAAAA!\n";
}
--------------------------

--------------------------
// node.cc
// explicit instantiations of template with common string types
// never seen by users, compiled in lib

#include "node.tcc"

template class xmlpp::Node<::QString>;
template class xmlpp::Node<::glib::String>;
--------------------------


libxml++.a would contain node.o, built from node.cc, which includes
the node.tcc and node.h headers.

Users would include node.h for the common string types, and if they want
to use a string type not supported directly they'd have to include
node.tcc to see the bodies of the functions.

I'm not proposing this as the way to go, (there are still the issues of
introducing deps on the two string types, and of binary-incompatible
libs if they use different string types), I'm just trying to clear up what
seem to be some misunderstandings about the language features.

jon


-- 
"UNIX was not designed to stop you from doing stupid things,
 because that would also stop you from doing clever things."
	- Doug Gwyn




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