Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

TestInterface.cpp

By: a guest on May 14th, 2012  |  syntax: C++  |  size: 2.14 KB  |  views: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include "TestInterface.h"
  4.  
  5. // I have put the logOneLine as a global to this file for clarity's sake, and simplified it.
  6. void logOneLine (const char *caller, const char *msg) {
  7.     static FILE *log    = 0;
  8.     log                 = fopen("TestDLL.log", log ? "at" : "wt");
  9.     fprintf             (log, "%s: %s\n", caller, msg);
  10.     fclose              (log);
  11. }
  12. // Macro logging to ease reading the code.
  13. #define Log(x)          logOneLine(__func__, (x));
  14.  
  15. // I added a constructor to TestInterface, visible in the log.
  16. TestInterface::TestInterface () {
  17.     Log                 ("...");
  18. }
  19. TestInterface::~TestInterface() {
  20.     Log                 ("...");
  21. }
  22.  
  23. // You have to tell the compiler that you are exporting this class too (see the DECLSPEC definition in the header).
  24. class DECLSPEC Test : public TestInterface {
  25.    public:
  26.       /**/          Test        (const bool, const int);
  27.       virtual       ~Test       ();
  28.       virtual bool  get         (bool &flag, int &num);
  29.       virtual bool  set         (const bool flag, const int num);
  30.    private:
  31.       bool          f_flag;
  32.       int           f_num;
  33. };
  34.  
  35. // You don't need to re-specify the calling convention nor the declspec here, as it is implicitelly done in the above.
  36. Test::Test (const bool flag, const int num)
  37. // Also, attention, you have to initialize your parent class and your members, in that order:
  38. : TestInterface(), f_flag(flag), f_num(num) {
  39.    Log          ("...");
  40.    char buff    [255];
  41.    sprintf      (buff, "flag=%s, num=%d", flag ? "true" : "false", num);
  42.    Log          (buff);
  43. }
  44. Test::~Test () {
  45.    Log          ("...");
  46. }
  47. // Accessors.
  48. bool Test::get (bool &flag, int &num) {
  49.    flag         = f_flag;
  50.    num          = f_num;
  51.    return       true;
  52. }
  53. bool Test::set (const bool flag, const int num) {
  54.    f_flag       = flag;
  55.    f_num        = num;
  56.    return       true;
  57. }
  58.  
  59. // Factory stuff.
  60. TestInterface* Instanciate (const bool flag, const int num) {
  61.     Log         ("...");
  62.     return      new Test(flag, num);
  63. }
  64. void Destroy (TestInterface *f) {
  65.     Log         ("...");
  66.     delete      f;
  67. }