File: moduleloader.cc

package info (click to toggle)
wvstreams 4.6.1-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,420 kB
  • sloc: cpp: 64,196; ansic: 4,154; sh: 4,025; makefile: 546; perl: 402
file content (140 lines) | stat: -rw-r--r-- 3,504 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * XPLC - Cross-Platform Lightweight Components
 * Copyright (C) 2002-2004, Net Integration Technologies, Inc.
 * Copyright (C) 2002-2004, Pierre Phaneuf
 * Copyright (C) 2002-2004, Stphane Lajoie
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <assert.h>
#include <xplc/ICategoryManager.h>
#include <xplc/uuidops.h>
#include "moduleloader.h"
#include "loader.h"

UUID_MAP_BEGIN(ModuleLoader)
  UUID_MAP_ENTRY(IObject)
  UUID_MAP_ENTRY(IModuleLoader)
  UUID_MAP_END

UUID_MAP_BEGIN(Module)
  UUID_MAP_ENTRY(IObject)
  UUID_MAP_ENTRY(IServiceHandler)
  UUID_MAP_ENTRY(IModule)
  UUID_MAP_END

IModule* ModuleLoader::loadModule(const char* modulename) {
  return Module::loadModule(modulename);
}

Module* Module::loadModule(const char* modulename) {
  XPLC_ModuleInfo* moduleinfo = 0;
  void* dlh;
  const char* err;

  err = loaderOpen(modulename, &dlh);
  if(err)
    return NULL;

  void* symbol;
  err = loaderSymbol(dlh, "XPLC_Module", &symbol);
  moduleinfo = reinterpret_cast<XPLC_ModuleInfo*>(symbol);

  if(err
     || !moduleinfo
     || moduleinfo->magic != XPLC_MODULE_MAGIC) {
    loaderClose(dlh);
    return NULL;
  }

  switch((int)moduleinfo->version_major) {
#ifdef UNSTABLE
  case -1:
    /* nothing to do */
    break;
#endif
  default:
    loaderClose(dlh);
    return NULL;
  };

  return new Module(dlh, moduleinfo);
}

Module::Module(void* aHandle, const XPLC_ModuleInfo* aModuleInfo):
  handle(aHandle),
  moduleinfo(aModuleInfo)
{
  assert(moduleinfo);

  if(moduleinfo->categories) {
    IServiceManager* servmgr;
    IObject* obj;
    ICategoryManager* catmgr;
    const XPLC_CategoryEntry* entry;

    servmgr = XPLC_getServiceManager();
    assert(servmgr);

    obj = servmgr->getObject(XPLC_categoryManager);
    assert(obj);

    servmgr->release();

    catmgr = mutate<ICategoryManager>(obj);
    assert(catmgr);

    entry = moduleinfo->categories;
    while(entry->category != UUID_null && entry->uuid != UUID_null) {
      catmgr->registerComponent(entry->category, entry->uuid, entry->string);

      ++entry;
    }

    catmgr->release();
  }
}

IObject* Module::getObject(const UUID& cid) {
  const XPLC_ComponentEntry* entry = moduleinfo->components;
  IObject* obj = 0;

  if(!entry)
    return NULL;

  while(!obj && entry->uuid != UUID_null) {
    if(entry->uuid == cid)
      obj = entry->getObject();

    ++entry;
  }

  return obj;
}

Module::~Module() {
  /*
   * FIXME: Adding the conditional here is for future expansion, where
   * this class could be used for a non-dynamically loaded module. But
   * for some reason, the size of this file increases in an odd way
   * when it is added. This should be investigated.
   */
  if(handle)
    loaderClose(handle);
}