libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moIODevicePlugin.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moIODevicePlugin.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 
30 *******************************************************************************/
31 
32 #include "moIODevicePlugin.h"
33 
34 #include "moArray.h"
35 moDefineDynamicArray(moIODevicePluginsArray)
36 
37 #include <iostream>
38 using namespace std;
39 
41 }
42 
43 
44 //===========================================
45 //
46 // moIODevicePlugin
47 //
48 //===========================================
49 
50 void moIODevicePlugin::Load(moText plugin_file)
51 {
52  name = plugin_file;
53  handle = moLoadPlugin(plugin_file);
54 
55  if(!handle) {
56  #if !defined(WIN32)
57  cerr << "Cannot open library: " << dlerror() << '\n';
58  #else
59  CHAR szBuf[80];
60  DWORD dw = GetLastError();
61  sprintf(szBuf, "%s failed: GetLastError returned %i\n",
62  (char*)plugin_file, (int)dw);
63 
64  moDebugManager::Error( "moIODevicePlugin::Load > Cannot open library: " + moText(szBuf) );
65 
66  #endif
67  }
68 
69  #if defined(_WIN32)
70  FARPROC farp;
71  farp = GetProcAddress(handle, "DestroyIODeviceFactory");
72  CreateIODeviceFactory = CreateIODeviceFactoryFunction(GetProcAddress(handle, "CreateIODeviceFactory"));
73  DestroyIODeviceFactory = DestroyIODeviceFactoryFunction(GetProcAddress(handle, "DestroyIODeviceFactory"));
74  #else
75  CreateIODeviceFactory = CreateIODeviceFactoryFunction(dlsym(handle, "CreateIODeviceFactory"));
76  DestroyIODeviceFactory = DestroyIODeviceFactoryFunction(dlsym(handle, "DestroyIODeviceFactory"));
77  #endif
78 
79  if(this->CreateIODeviceFactory!=NULL)
80  m_factory = this->CreateIODeviceFactory();
81 
82 }
83 
85 {
86  this->DestroyIODeviceFactory();
87 
88  CreateIODeviceFactory = NULL;
89  DestroyIODeviceFactory = NULL;
90 
91  moUnloadPlugin(handle);
92 }
93 
94 
96 
97  moIODevice **narray;
98 
99  if(m_factory!=NULL) {
100  moIODevice *pfx = m_factory->Create();
101 
102  if(pfx==NULL) return NULL;
103 
104  if(n==0) {//primera vez
105  array = new moIODevice* [1];
106  } else if(n>0) {//array dinamico
107  narray = new moIODevice* [n+1];//generamos el nuevo vacio
108  for(int i=0;i<n;i++) narray[i] = array[i];//copiamos el array
109  delete [] array;
110  array = narray;
111  }
112 
113  array[n] = pfx;
114  n++;
115  return pfx;
116  }
117  return NULL;
118 }
119 
121 
122  moIODevice **narray;
123  int i,j;
124 
125  if(m_factory!=NULL) {
126 
127  for(j=0;j<n;j++)
128  if(array[j]==iodevice) {
129 
130  m_factory->Destroy(iodevice);
131 
132  if(n==1) {//muere
133  delete [] array;
134  } else if(n>1) {//array dinamico
135  narray = new moIODevice* [n-1];//generamos el nuevo vacio
136  for(i=0;i<j;i++) narray[i] = array[i];//copiamos el array hasta el j(destruido)
137  for(i=j;i<(n-1);i++) narray[i] = array[i+1];//copiamos el array desde el j
138  delete [] array;
139  array = narray;
140  }
141  n--;
142  return true;
143  }
144  }
145  return false;
146 }
147 
148 
149 LIBMOLDEO_API moIODevice* moNewIODevice(moText effect_name, moIODevicePluginsArray &plugins)
150 {
151  // Creando el nombre complete del plugin(incluyendo ruta por defecto)
152  // a partir del name del efecto.
153  moText complete_name;
154 
155  if(!stricmp(effect_name, "nil")) return NULL;
156 
157  #if defined(_WIN32)
158  complete_name = moText(moDataManager::GetModulesDir()+ "/iodevices/") + (moText)effect_name;
159  #ifdef _DEBUG
160  complete_name+= moText("_d");
161  #endif
162  complete_name += moText(".dll");
163  #else
164  complete_name = moText(moDataManager::GetModulesDir()+ "/iodevices/libmoldeo_") + (moText)effect_name;
165  #ifdef _DEBUG
166  complete_name+= moText("d");
167  #endif
168  complete_name += moPluginExtension;
169  #endif
170 
171  // Indice del plugin que se utilizara para crear a este efecto.
172  int plg_index = -1;
173 
174  // First, revisa que el plugin ya no haya sido cargado antes.
175  for(MOuint i = 0; i < plugins.Count(); i++)
176  if(!stricmp(plugins[i]->GetName(), complete_name))
177  {
178  plg_index = i;
179  break;
180  }
181 
182  if(plg_index == -1)
183  {
184  // Es la primera vez que se intenta cargar este plugin. Se lo agrega al array.
185  // Falta control de errores(que pasa si la libreria no carga, etc?) :-)
186  plg_index = plugins.Count();
187  moIODevicePlugin *pplugin = new moIODevicePlugin(complete_name);
188  plugins.Add( pplugin );
189  }
190 
191  // El plugin crea al efecto!
192  if(plugins[plg_index]->m_factory!=NULL)
193  return plugins[plg_index]->Create();
194  else return NULL;
195 }
196 
197 
198 LIBMOLDEO_API bool moDeleteIODevice(moIODevice *IODevice, moIODevicePluginsArray &plugins)
199 {
200  // Creando el nombre complete del plugin(incluyendo ruta por defecto)
201  // a partir del nombre del efecto.
202  moText complete_name;
203 
204  if( IODevice->GetName() == "") return false;
205 
206  #if defined(_WIN32)
207  complete_name = moText("plugins/iodevices/") + moText(IODevice->GetName());
208  #ifdef _DEBUG
209  complete_name+= moText("d");
210  #endif
211  complete_name += moText(".dll");
212  #else
213  complete_name = moText("plugins/iodevices/libmoldeo_") + moText(IODevice->GetName());
214  #ifdef _DEBUG
215  complete_name+= moText("d");
216  #endif
217  complete_name += moPluginExtension;
218  #endif
219 
220  // Indice del plugin que se utilizara para crear a este efecto.
221  int plg_index = -1;
222 
223  // First, revisa que el plugin ya no haya sido cargado antes.
224  for(MOuint i = 0; i < plugins.Count(); i++)
225  if(!stricmp(plugins[i]->GetName(), complete_name))
226  {
227  plg_index = i;
228  break;
229  }
230 
231  if(plg_index == -1)
232  {
233  return false;
234  }
235 
236  bool res = plugins[plg_index]->Destroy(IODevice);
237 
239  if (res && plugins[plg_index]->n == 0) {
240  plugins[plg_index]->Unload();
241  plugins.Remove(plg_index);
242 
243  }
244 
245  return res;
246 }
247 
static void Error(moText p_text)
Anuncia un error.
const moText & GetName() const
void Load(moText plugin_file)
#define LIBMOLDEO_API
Definition: moTypes.h:180
clase de para manejar textos
Definition: moText.h:75
virtual ~moIODeviceFactory()
moIODevice * Create()
void moUnloadPlugin(MOpluginHandle &handle)
Definition: moBasePlugin.h:64
moText0 moText
Definition: moText.h:291
static const moText & GetModulesDir()
LIBMOLDEO_API bool moDeleteIODevice(moIODevice *IODevice, moIODevicePluginsArray &plugins)
moDefineDynamicArray(moIODevicePluginsArray) using namespace std
clase base para un plugin de dipositivos de E/S
#define MOuint
Definition: moTypes.h:387
LIBMOLDEO_API moIODevice * moNewIODevice(moText effect_name, moIODevicePluginsArray &plugins)
void(MO_PLG_ENTRY * DestroyIODeviceFactoryFunction)()
moIODeviceFactory *(MO_PLG_ENTRY * CreateIODeviceFactoryFunction)()
MOpluginHandle moLoadPlugin(moText fn)
Definition: moBasePlugin.h:55
bool Destroy(moIODevice *iodevice)