Tips for Abstract Factory, DLL Export, and Smart Pointers

This is a follow-up to one of my previous questions and I came up with a possible solution for my project and I need advice or guidance if I have the right.

Basically my project is a library to be used and compiled on both Linux and Windows, the Linux part is not a problem, its Windows.

My library is mostly classes, so 95% of the code is C ++. To provide better support and avoid name change issues, I'm going to use interfaces and factory methods to get instances of these classes rather than accessing them directly. I will also use extern "C" for factory functions ONLY to avoid changing the name.

So here are the questions:

  • I will be using export / import macros in factory functions, but then I need to add export / import (__declspec (dllexport / import)) to the header files of all classes that I wish to provide instances to include the base class interfaces? Or is it just exporting the factory function enough?

  • I was also thinking about using the smart pointer functions to automatically deallocate, a good idea or letting the user handle this? I was planning on using the autoptr template, which is part of the standard library and is available on both platforms?

  • Can anyone enlighten on which call should I use the convention? I know it is Microsoft specific, so I hope it doesn't interfere with Linux, so I have to leave it blank and let it be the default, which I think is _thiscall or something.

  • I am getting an error for this piece of code, I am not sure why, but it has to do with extern "C":

Mistake:

error: C function declaration 'std :: ostream & operator <(std :: ostream &, const MemInfo &) conflicts with | previous declaration 'std :: ostream & operator <(std :: ostream &, const SpeedInfo &) here |

error: C function declaration 'std :: ostream & operator <(std :: ostream &, const anal_result &) conflicts with previous declaration' std :: ostream & operator <(std :: ostream &, const MemInfo &) here |

... these error messages are repeated for all functions

Here is the code:

//WINLIB is my macro for the dllimport/export

extern "C"{

//operator overloading for stream operation
WINLIB std::ostream& operator<<(std::ostream &st, const SpeedInfo &si);
WINLIB std::ostream& operator<<(std::ostream &st, const MemInfo &mi);
WINLIB std::ostream& operator<<(std::ostream &st, const analyzed_result&);
WINLIB std::ostream& operator<<(std::ostream &st, const ustring_set&);
WINLIB std::ostream& operator<<(std::ostream &st, const speed_map&);
WINLIB std::ostream& operator<<(std::ostream &st, const mem_map&);

WINLIB SpeedInfo operator+(SpeedInfo &si, SpeedInfo &si2);
WINLIB SpeedInfo& operator+=(SpeedInfo &si, SpeedInfo &si2);
WINLIB SpeedInfo operator-(SpeedInfo &si, SpeedInfo &si2);
WINLIB SpeedInfo& operator-=(SpeedInfo &si, SpeedInfo &si2);

WINLIB MemInfo operator+(MemInfo &mi, MemInfo &mi2);
WINLIB MemInfo& operator+=(MemInfo &mi, MemInfo &mi2);
WINLIB MemInfo operator-(MemInfo &mi, MemInfo &mi2);
WINLIB MemInfo& operator-=(MemInfo &mi, MemInfo &mi2);
}

      

+2


source to share


2 answers


  • It is enough to export the factory function and include the headers for the classes in the user code.
  • If you don't depend on it, let the user choose the best tool for the job.
  • __ STDCALL
  • extern "C" gives you a c-style declaration scope, so you cannot overload it. see here . since win32 dll is c-style, you cannot export overloaded functions directly (think GetProcAddress()

    ).


+1


source


I can't say much about your factory idea (it's> 10 years ago that I did something like this), but regarding # 4, your error messages are:



If you declare something as extern "C"

, it means that, among other things, that mangling is disabled. But recoding a name is not just a nuisance, it is not. Name manipulation means that the compiler will manipulate the type names of the function arguments in the function names so that the compiler and linker can pass overloaded functions away from each other. If you declare them as extern "C"

, you are back to Earth C: the compiler cannot pass overloaded functions away from each other, and therefore overloading does not work.

+1


source







All Articles