~ubuntu-branches/ubuntu/oneiric/libclaw/oneiric

« back to all changes in this revision

Viewing changes to examples/dynamic_library/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge
  • Date: 2008-05-17 15:36:57 UTC
  • Revision ID: james.westby@ubuntu.com-20080517153657-0b1204j754ykoz48
Tags: upstream-1.5.2b
ImportĀ upstreamĀ versionĀ 1.5.2b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Example program for the claw::dynamic_library class.
 
3
*/
 
4
 
 
5
#include <iostream>
 
6
#include <claw/dynamic_library.hpp>
 
7
 
 
8
#ifdef _WIN32
 
9
# define DLLEXPORT __declspec(dllexport)
 
10
#else
 
11
# define DLLEXPORT
 
12
#endif
 
13
 
 
14
typedef void (*function_type)( const std::string& s );
 
15
 
 
16
extern "C"
 
17
DLLEXPORT void in_program( const std::string& s )
 
18
{
 
19
  std::cout << "in_program : " << s << std::endl;
 
20
}
 
21
 
 
22
int main( int argc, char** argv )
 
23
{
 
24
  if ( argc < 2 )
 
25
    {
 
26
      std::cout << argv[0] << " method_name...\n";
 
27
      std::cout << "\tThe first method_name is searched in the current "
 
28
                << "program\n";
 
29
      std::cout << "\tThe next arguments are searched in the ./libfunc.so "
 
30
                << "shared library." << std::endl;
 
31
    }
 
32
  else
 
33
    {
 
34
      /* Load the library. */
 
35
#ifdef _WIN32
 
36
      claw::dynamic_library dl( "./libfunc.dll" );
 
37
#else
 
38
      claw::dynamic_library dl( "./libfunc.so" );
 
39
#endif
 
40
 
 
41
      /* Load the current program. */
 
42
      claw::dynamic_library current_prog( argv[0], true );
 
43
 
 
44
      function_type f;
 
45
 
 
46
      /* Search in the current program. */
 
47
      if ( current_prog.have_symbol( argv[1] ) )
 
48
        {
 
49
          f = current_prog.get_symbol<function_type>( argv[1] );
 
50
          f( "The string passed to a method in the current program" );
 
51
        }
 
52
      else
 
53
        std::cout << "Symbol '" << argv[1]
 
54
                  << "' not found in the current program." << std::endl;
 
55
 
 
56
      /* We try to find the next symbols passed as arguments */
 
57
      for (int i=2; i<argc; ++i)
 
58
        if ( dl.have_symbol( argv[i] ) )
 
59
          {
 
60
            f = dl.get_symbol<function_type>( argv[i] );
 
61
 
 
62
            f( "The string passed to the method" );
 
63
          }
 
64
        else
 
65
          std::cout << "Symbol '" << argv[i] << "' not found." << std::endl;
 
66
    }
 
67
 
 
68
  return 0;
 
69
}