My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
module.cpp
Go to the documentation of this file.
1 /*
2  * module.cpp - module class implementation
3  *
4  * Copyright (C) 2008, 2009, 2010 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id: module.cpp 1826 2011-03-12 01:00:56Z ela $
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #include "netdefs.h"
33 #include "components.h"
34 #include "analyses.h"
35 #include "netdefs.h"
36 #include "module.h"
37 
38 // Global module hash.
40 
41 // Constructor creates an instance of the module class.
43  definition = NULL;
44  circreate = NULL;
45  anacreate = NULL;
46 }
47 
48 // Destructor deletes an instance of the module class.
50 }
51 
52 // Definitions which do not fit elsewhere.
53 static struct property_t props1[] = {
54  PROP_NO_PROP };
55 static struct property_t props2[] = {
56  { "Type", PROP_STR, { PROP_NO_VAL, "DEF1" }, PROP_NO_RANGE },
57  PROP_NO_PROP };
58 
61  props1, props1 };
62 
65  props1, props2 };
66 
67 // Registers an analysis object to the list of available modules.
69  analysis_creator_t create) {
70  module * m = new module ();
71  m->definition = define ();
72  m->anacreate = create;
73  modules.put ((char *) define()->type, m);
74 }
75 
76 // Registers a circuit object to the list of available modules.
78  circuit_creator_t create) {
79  module * m = new module ();
80  m->definition = define ();
81  m->circreate = create;
82  registerModule (define()->type, m);
83 }
84 
85 // Registers a miscellaneous object to the list of available modules.
87  module * m = new module ();
88  m->definition = define ();
89  registerModule (define()->type, m);
90 }
91 
92 /* Registers a miscellaneous structure just defined by a somple
93  define_t structure to the list of available modules. */
94 void module::registerModule (struct define_t * define) {
95  module * m = new module ();
96  m->definition = define;
97  registerModule (define->type, m);
98 }
99 
100 // Puts a module into the available module hash.
101 void module::registerModule (const char * type, module * m) {
102  if (modules.get ((char *) type) != NULL) {
103  logprint (LOG_ERROR, "module already registered: %s\n", type);
104  }
105  else {
106  modules.put ((char *) type, m);
107  }
108 }
109 
110 /* Returns the definition of a module specified by its type name if
111  such is existing and otherwise NULL. */
112 struct define_t * module::getModule (char * type) {
113  module * m = modules.get (type);
114  if (m != NULL) {
115  return m->definition;
116  }
117  return NULL;
118 }
119 
120 // Helper macros.
121 #define REGISTER_CIRCUIT(val) \
122  registerModule (&val::definition, &val::create)
123 #define REGISTER_ANALYSIS(val) \
124  registerModule (&val::definition, &val::create)
125 #define REGISTER_MISC(val) \
126  registerModule (&val::definition)
127 
128 // Global static module registration.
130 
131  // miscellaneous
132  registerModule (&miscdef1);
133  registerModule (&miscdef2);
136 
137  // circuit components
276 
277  // analyses
284 }
285 
286 // Global module unregistration.
289  for (it = qucs::hashiterator<module> (modules); *it; ++it) {
290  delete it.currentVal ();
291  }
292  modules.clear ();
293 }
294 
295 #if DEBUG
296 // header prefix
297 static const char * def_prefix =
298 "/*\n"
299 " * qucsdefs.h - netlist definitions for the Qucs netlists\n"
300 " *\n"
301 " * This is free software; you can redistribute it and/or modify\n"
302 " * it under the terms of the GNU General Public License as published by\n"
303 " * the Free Software Foundation; either version 2, or (at your option)\n"
304 " * any later version.\n"
305 " * \n"
306 " */\n"
307 "\n"
308 "#ifndef __QUCSDEFS_H__\n"
309 "#define __QUCSDEFS_H__\n";
310 
311 // header suffix
312 static const char * def_suffix =
313 "\n"
314 "#endif /* __QUCSDEFS_H__ */\n";
315 
316 // start of list of definitions
317 static const char * def_start =
318 "\n"
319 "// List of available components.\n"
320 "struct define_t qucs_definition_available[] =\n";
321 
322 // end of list entry
323 static const char * def_stop =
324 "\n"
325 "static struct define_t def_End = {\n"
326 " ((char *) 0), -1, 1, 0, 0, req_Def, opt_Def };\n";
327 
328 // Returns a compilable C-code string made from the given string.
329 static char * printstr (const char * str) {
330  static char txt[256];
331  int nostr = (str == PROP_NO_STR);
332  sprintf (txt, "%s%s%s",
333  (str && !nostr) ? "\"" : "",
334  str ? nostr ? "((char *) -1)" : str : "((char *) 0)",
335  (str && !nostr) ? "\"" : "");
336  return txt;
337 }
338 
339 // Prints a property list as compilable C-code.
340 static void printprop (const char * type, const char * prefix,
341  struct property_t * prop) {
342  const char * key;
343  const char ** str;
344  const char * txt;
345  fprintf (stdout, "static struct property_t %s_%s[] = {\n", prefix, type);
346  do {
347  key = prop->key;
348  fprintf (stdout, " { %s, %d, ", printstr (key), prop->type);
349  fprintf (stdout, "{ %g, %s }, ", prop->defaultval.d,
350  printstr (prop->defaultval.s));
351  fprintf (stdout, "{ '%c', %g, %g, '%c',\n",
352  prop->range.il, prop->range.l, prop->range.h, prop->range.ih);
353  fprintf (stdout, " {");
354  str = prop->range.str;
355  do {
356  txt = *str;
357  fprintf (stdout, " %s", printstr (txt));
358  if (txt) fprintf (stdout, ",");
359  str++;
360  }
361  while (txt != NULL);
362  fprintf (stdout, " } } }");
363  if (key) fprintf (stdout, ",");
364  fprintf (stdout, "\n");
365  prop++;
366  }
367  while (key != NULL);
368  fprintf (stdout, "};\n");
369 }
370 
371 /* The function emits a complete list of the registered component
372  definitions as compilable C-code. */
373 void module::print (void) {
374  fprintf (stdout, def_prefix);
376  for (it = qucs::hashiterator<module> (modules); *it; ++it) {
377  module * m = it.currentVal ();
378  struct define_t * def = m->definition;
379  fprintf (stdout, "\n");
380  printprop (def->type, "req", def->required);
381  fprintf (stdout, "\n");
382  printprop (def->type, "opt", def->optional);
383  fprintf (stdout, "\n");
384  fprintf (stdout, "static struct define_t def_%s = {\n", def->type);
385  fprintf (stdout, " %s, %d, %d, %d, %d, req_%s, opt_%s };\n",
386  printstr (def->type), def->nodes, def->action, def->substrate,
387  def->nonlinear, def->type, def->type);
388  }
389  fprintf (stdout, def_stop);
390  fprintf (stdout, def_start);
391  fprintf (stdout, "{\n");
392  for (it = qucs::hashiterator<module> (modules); *it; ++it) {
393  module * m = it.currentVal ();
394  struct define_t * def = m->definition;
395  fprintf (stdout, " def_%s,\n", def->type);
396  }
397  fprintf (stdout, " def_End\n");
398  fprintf (stdout, "};\n");
399  fprintf (stdout, def_suffix);
400 }
401 #endif /* DEBUG */