source: branches/with_autoconf/src/sc_object.cc @ 41

Last change on this file since 41 was 20, checked in by nipo, 16 years ago

Sync up with trunk changes

File size: 7.2 KB
RevLine 
[1]1/*------------------------------------------------------------\
2|                                                             |
3| Tool    :                  systemcass                       |
4|                                                             |
5| File    :                 sc_object.cc                      |
6|                                                             |
7| Author  :                 Buchmann Richard                  |
8|                                                             |
9| Date    :                   09_07_2004                      |
10|                                                             |
11\------------------------------------------------------------*/
12
13/*
14 * This file is part of the Disydent Project
15 * Copyright (C) Laboratoire LIP6 - Département ASIM
16 * Universite Pierre et Marie Curie
17 *
18 * Home page          : http://www-asim.lip6.fr/disydent
19 * E-mail             : mailto:richard.buchmann@lip6.fr
20 *
21 * This library is free software; you  can redistribute it and/or modify it
22 * under the terms  of the GNU Library General Public  License as published
23 * by the Free Software Foundation; either version 2 of the License, or (at
24 * your option) any later version.
25 *
26 * Disydent is distributed  in the hope  that it  will be
27 * useful, but WITHOUT  ANY WARRANTY; without even the  implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
29 * Public License for more details.
30 *
31 * You should have received a copy  of the GNU General Public License along
32 * with the GNU C Library; see the  file COPYING. If not, write to the Free
33 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 */
35
36
[20]37#include <cstdio>
38#include <cstring> //strdup
[1]39#include <string>
40#include <map>
41
[8]42#include <cassert>
[1]43#include "sc_object.h"
44//#include "sc_port.h"
45#include "internal.h"
46#include "sc_signal.h"
47#include "module_hierarchy.h"
[8]48#ifdef HAVE_CONFIG_H
49#include "config.h"
50#endif
[1]51
52using namespace std;
53
54namespace sc_core {
55
56static void
57gen_name (const char *prefix, string &s)
58{
59  s = prefix;
60  static int i = 0;
61  char ic[10];
62  sprintf (ic,"%d",i++);
63  s += ic;
64}
65// ----------------------------------------------------------------------------
66static void
67build_complete_name (const char *name, string &out)
68{
69        out = "";
70  module_name_stack_t::const_iterator i;
71  for (i = module_name_stack.begin (); i != module_name_stack.end (); ++i) {
72    const string &module_name = *i;
73    out += (module_name + ".");
74//    out += ".";
75  }
[8]76//  assert(name != NULL);
[1]77  if (name)
78    out += name;
79  else
80    out[out.length ()-1] = '\0';
81#if 0
82  cerr << "complete_name = " << out << endl;
83#endif
84}
85
86#if 0
87static
88void
89build_full_name_r (string &out, const sc_object *obj)
90{
91  if (obj == NULL)
92  {
93    out = "";
94  } else {
95    const sc_object *parent = obj->get_parent_object ();
96    build_full_name_r (out, parent);
97    out += obj->basename ();
98    out += ".";
99  }
100}
101
102static
103void
104build_full_name (string &out, const sc_object &obj)
105{
106  const sc_object *parent = obj.get_parent_object ();
107  build_full_name_r (out, parent);
108  out += obj.basename ();
109  cerr << "build_full_name = " << out << endl;
110}
111#endif
112
113typedef std::map<const sc_object* const,string> object2name_t;
114static object2name_t object2basename;
115static object2name_t object2fullname;
116
117struct object_infos_t {
118        const char *kind_string;
119};
120typedef std::map<const sc_object* const,object_infos_t> object2infos_t;
121static object2infos_t object2infos;
122
123// We initialize SC_BIND_PROXY_NIL there to make sure object2infos,
124// object2fullname, and object2basename are already initialized.
125// SC_BIND_PROXY_NIL should be declared into sc_module.cc but it prevents us
126// to force the initialization order.
127const char *SC_BIND_PROXY_NIL_string = "SC_BIND_PROXY_NIL";
128sc_bind_proxy SC_BIND_PROXY_NIL (SC_BIND_PROXY_NIL_string,NULL);
129
130// ----------------------------------------------------------------------------
131//  FUNCTION : sc_gen_unique_name
132//                                               
133// ----------------------------------------------------------------------------
134
135const char *
136sc_gen_unique_name (const char *basename_)
137{
138        string s;
139        gen_name (basename_,s);
140        return strdup (s.c_str ());
141}
142
143// ----------------------------------------------------------------------------
144//  CLASS : sc_object
145//                                               
146// ----------------------------------------------------------------------------
147
148const char* const sc_object::kind_string = "sc_object";
149
150void
151sc_object::set_kind (const char *k)
152{
153        object2infos[this].kind_string = k;
154}
155
156void
157sc_object::init ()
158{
159        set_kind ("sc_object");
160  add_child (*this);
161}
162
163sc_object::sc_object()
164{
165        string noname;
166        gen_name ("noname_",noname);
167#if 0
168  cerr << "object2basename[this] = " << noname << "\n";
169#endif
170  object2basename[this] = noname;
171  build_complete_name (noname.c_str(),object2fullname[this]);
172        init ();
173}
174
175sc_object::sc_object(const char *name_)
176{
177  const char *temp;
178  if (name_ == NULL) {
179    if (module_name_stack.empty())
180    {
181      cerr << "Internal error : module_name_stack is empty.";
182      exit (21092005);
183    }
184    string &module_name = module_name_stack.back ();
185    temp = module_name.c_str ();
186  } else {
187    temp = name_;
188  }
189#if 0
190  cerr << "object2basename[this] = " << temp << "\n";
191  cerr << "name temp = " << temp << endl;
192#endif
193  object2basename[this] = temp;
194  build_complete_name (name_,object2fullname[this]);
195        init ();
196}
197
198const char *
199sc_object::basename () const
200{
201  return object2basename[this].c_str ();
202}
203
204const char *
205sc_object::name () const
206{
207        object2name_t::iterator i = object2fullname.find (this);
[8]208#ifdef CONFIG_DEBUG
[1]209        if (i == object2fullname.end ()) {
210                cerr << "Internal error : can't find name of " << this << "\n";
211                exit (90);
212        }
213#endif
214  return i->second.c_str ();
215}
216 
217void
218sc_object::rename (const char* newname) const
219{
220/*
221        object2name_t::iterator i = object2fullname.find (this);
[8]222#ifdef CONFIG_DEBUG
[1]223        if (i == object2fullname.end ()) {
224                cerr << "Internal error : can't find name of " << this << "\n";
225                exit (90);
226        }
227#endif
228  i->second = newname;
229  object2basename[this] = newname;
230*/
231  object2basename[this] = newname;
232  build_complete_name (newname,object2fullname[this]);
233/*
234  const std::vector<sc_object*>& childs = get_child_objects();
235  std::vector<sc_object*>::const_iterator it;
236  for (it = childs.begin (); it != childs.end (); ++it)
237  {
238    string     out;
239    sc_object* obj = *it;
[8]240    assert(obj != NULL);
[1]241    build_full_name (out, *obj);
242  }
243*/
244}
245const char *sc_object::kind () const
246{
247        object2infos_t::iterator i = object2infos.find (this);
[8]248#ifdef CONFIG_DEBUG
[1]249        if (i == object2infos.end ()) {
250                cerr << "Internal error : can't find kind of " << this << "\n";
251                exit (90);
252        }
253#endif
254  return i->second.kind_string;
255}
256
257sc_object::~sc_object ()
258{
259  if (save_on_exit)
260  {
261    sc_save_simulation (save_on_exit);
262    save_on_exit = NULL;
263  }
264  object2fullname.erase (this);
265  object2basename.erase (this);
266  object2infos.erase    (this);
267}
268
269std::ostream& 
270operator << (std::ostream& os, const sc_object& obj)
271{
272        return os << obj.name ();
273}
274
275/* virtual */ 
276const std::vector<sc_object*>& 
277sc_object::get_child_objects() const
278{
279  return sc_core::get_child_objects (*this);
280}
281
282sc_object*
283sc_object::get_parent_object () const
284{
285  return sc_core::get_parent_object (*this);
286}
287
288} // end of sc_core namespace
289
Note: See TracBrowser for help on using the repository browser.