Topic: hide C-header files throw NAMESPACES
Author: David R Tribble <david@tribble.com>
Date: 1999/10/28 Raw View
Raphael Bossek wrote:
>
> Hi all,
>
> i write here a framework for a C function library and have to include
> the old header files for function definitions i call. To prevent
> direct calls in our old sources i would like to hide the old function
> definitions throw a namespace. Now my question: Can header files be
> hiden [though] nampespaces (take a look at the following example)?
>
> File: oldlib.h
> // This file contains the old header files
>
> namespace OLDLIB {
> #include "mm.h"
> #include "mha.h"
> }
> // End
> End of file oldlib.h
>
> The function defined within mm.h, mha.h should be only be visible
> throw the namespace OLDLIB so i have to call for instance
> OLDLIB::open.
Yes, that's true in the C++ code than #includes oldlib.h. But
consider what the compiler does when you call OLDLIB::open() -
it produces a call for function OLDLIB::open(), not ::open(),
and the linker will not find this symbol in the old library.
(The name OLDLIB::open() will be mangled into something like
OLDLIB__open_Fi, instead of simply _open.)
OTOH, the old C library functions are probably already enclosed
within an extern "C" block, so name mangling might not occur. Or
it might; it depends on the compiler (I think).
namespace OLDLIB
{
extern "C" // Is this legal within a namespace?
{
int open(const char *fn, int m);
// Does this work?
}
}
Either way, I don't think this solution will work, at least not
portably.
-- David R. Tribble, david@tribble.com, http://www.david.tribble.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: 1999/10/25 Raw View
In article <EhwQOL0tYayTpfDcfd5gXiVG6Wlj@4ax.com> Raphael Bossek <raphael.bossek@solutions4linux.de> writes:
>i write here a framework for a C function library and have to include
>the old header files for function definitions i call. To prevent
>direct calls in our old sources i would like to hide the old function
>definitions throw a namespace. Now my question: Can header files be
>hiden throw nampespaces (take a look at the following example) ?
>
>File: oldlib.h
>// This file contains the old header files
>
>namespace OLDLIB {
> #include "mm.h"
> #include "mha.h"
>}
>// End
>End of file oldlib.h
>
>The function defined within mm.h, mha.h should be only be visible
>throw the namespace OLDLIB so i have to call for instance
>OLDLIB::open.
>
>One possible solutions could be to rewrite the whole old library but
>this library does not belong to us so we can not rewrite it. We also
>receive paches ect for it so if we would rewrite it we have to rewrite
>it every time we receive a patch and this is not aceptable at all.
>
>Any kind of help is welcome how to solve this problem ;)
The header files about are not added to the namespace.
However, some names within the header will be.
So, yes, if mha.h contains what would normally be ::open,
it would now be OLDLIB::open.
However, this does not prevent a user from #include'ing
mm.h and mha.h directly.
As well, once you OLDLIB open like above, you are saying
there is an OLDLIB:::open. This implies you'll need to go
back and recompile the lib. So perhaps you'll have to say
more about your situation for precise advise. As well,
check out a good C++ text for a namespace primer!
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- NOTE 4.2.42 BETAS NOW AVAILABLE
Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.com ***
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Salters <salters@lucent.com>
Date: 1999/10/27 Raw View
Raphael Bossek wrote:
> Hi all,
> i write here a framework for a C function library and have to include
> the old header files for function definitions i call. To prevent
> direct calls in our old sources i would like to hide the old function
> definitions throw a namespace. Now my question: Can header files be
> hiden throw nampespaces (take a look at the following example) ?
I think you mean "through", not anything involving exceptions...
> File: oldlib.h
> // This file contains the old header files
> namespace OLDLIB {
> #include "mm.h"
> #include "mha.h"
> }
> // End
> End of file oldlib.h
> The function defined within mm.h, mha.h should be only be visible
> throw the namespace OLDLIB so i have to call for instance
> OLDLIB::open.
Unfortunately, this is a partial solution. mm.h will be literally
included in oldlib.h at compile time. If you verify if that mm.h
contains only function prototypes, you're fine. Now, what could
happen is that mm.h is the API of mm.c, and mm.c includes (e.g.)
mm_errno. mm.h will include an extern mm_errno, which suddenly
turns into another variable: oldlib::mm_errno. Enter undefined
symbol.
Another danger is macro definitions. Macros don't obey namespace
scope.
I'm wondering how the namespace protects against direct calls.
It won't, as far as I see. OLDLIB::open is still a direct call.
> One possible solutions could be to rewrite the whole old library but
> this library does not belong to us so we can not rewrite it. We also
> receive paches ect for it so if we would rewrite it we have to rewrite
> it every time we receive a patch and this is not aceptable at all.
A simple solution is to write (inline) forwarding functions.
Include the old header file in a .cpp file, which calls
the functions in the header file:
<< oldlib.cpp >>
// This file contains the old header files
#include "mm.h"
#include "mha.h"
namespace OLDLIB {
void open() { ::open(); }
}
Users of the new interface use the header file:
<< oldlib.h >>
namespace OLDLIB {
void open();
}
They don't see anything in mm.h, but anything
*you* decide they need is made available.
HTH,
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Raphael Bossek <raphael.bossek@solutions4linux.de>
Date: 1999/10/25 Raw View
Hi all,
i write here a framework for a C function library and have to include
the old header files for function definitions i call. To prevent
direct calls in our old sources i would like to hide the old function
definitions throw a namespace. Now my question: Can header files be
hiden throw nampespaces (take a look at the following example) ?
File: oldlib.h
// This file contains the old header files
namespace OLDLIB {
#include "mm.h"
#include "mha.h"
}
// End
End of file oldlib.h
The function defined within mm.h, mha.h should be only be visible
throw the namespace OLDLIB so i have to call for instance
OLDLIB::open.
One possible solutions could be to rewrite the whole old library but
this library does not belong to us so we can not rewrite it. We also
receive paches ect for it so if we would rewrite it we have to rewrite
it every time we receive a patch and this is not aceptable at all.
Any kind of help is welcome how to solve this problem ;)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]