source: sources/src/sc_object.cc @ 5

Last change on this file since 5 was 1, checked in by buchmann, 17 years ago

Initial import from CVS repository

File size: 7.1 KB
Line 
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
37#include <stdio.h>
38#include <string>
39#include <map>
40
41#include "assert.h"
42#include "sc_object.h"
43//#include "sc_port.h"
44#include "internal.h"
45#include "sc_signal.h"
46#include "module_hierarchy.h"
47
48using namespace std;
49
50namespace sc_core {
51
52static void
53gen_name (const char *prefix, string &s)
54{
55  s = prefix;
56  static int i = 0;
57  char ic[10];
58  sprintf (ic,"%d",i++);
59  s += ic;
60}
61// ----------------------------------------------------------------------------
62static void
63build_complete_name (const char *name, string &out)
64{
65        out = "";
66  module_name_stack_t::const_iterator i;
67  for (i = module_name_stack.begin (); i != module_name_stack.end (); ++i) {
68    const string &module_name = *i;
69    out += (module_name + ".");
70//    out += ".";
71  }
72//  ASSERT(name != NULL);
73  if (name)
74    out += name;
75  else
76    out[out.length ()-1] = '\0';
77#if 0
78  cerr << "complete_name = " << out << endl;
79#endif
80}
81
82#if 0
83static
84void
85build_full_name_r (string &out, const sc_object *obj)
86{
87  if (obj == NULL)
88  {
89    out = "";
90  } else {
91    const sc_object *parent = obj->get_parent_object ();
92    build_full_name_r (out, parent);
93    out += obj->basename ();
94    out += ".";
95  }
96}
97
98static
99void
100build_full_name (string &out, const sc_object &obj)
101{
102  const sc_object *parent = obj.get_parent_object ();
103  build_full_name_r (out, parent);
104  out += obj.basename ();
105  cerr << "build_full_name = " << out << endl;
106}
107#endif
108
109typedef std::map<const sc_object* const,string> object2name_t;
110static object2name_t object2basename;
111static object2name_t object2fullname;
112
113struct object_infos_t {
114        const char *kind_string;
115};
116typedef std::map<const sc_object* const,object_infos_t> object2infos_t;
117static object2infos_t object2infos;
118
119// We initialize SC_BIND_PROXY_NIL there to make sure object2infos,
120// object2fullname, and object2basename are already initialized.
121// SC_BIND_PROXY_NIL should be declared into sc_module.cc but it prevents us
122// to force the initialization order.
123const char *SC_BIND_PROXY_NIL_string = "SC_BIND_PROXY_NIL";
124sc_bind_proxy SC_BIND_PROXY_NIL (SC_BIND_PROXY_NIL_string,NULL);
125
126// ----------------------------------------------------------------------------
127//  FUNCTION : sc_gen_unique_name
128//                                               
129// ----------------------------------------------------------------------------
130
131const char *
132sc_gen_unique_name (const char *basename_)
133{
134        string s;
135        gen_name (basename_,s);
136        return strdup (s.c_str ());
137}
138
139// ----------------------------------------------------------------------------
140//  CLASS : sc_object
141//                                               
142// ----------------------------------------------------------------------------
143
144const char* const sc_object::kind_string = "sc_object";
145
146void
147sc_object::set_kind (const char *k)
148{
149        object2infos[this].kind_string = k;
150}
151
152void
153sc_object::init ()
154{
155        set_kind ("sc_object");
156  add_child (*this);
157}
158
159sc_object::sc_object()
160{
161        string noname;
162        gen_name ("noname_",noname);
163#if 0
164  cerr << "object2basename[this] = " << noname << "\n";
165#endif
166  object2basename[this] = noname;
167  build_complete_name (noname.c_str(),object2fullname[this]);
168        init ();
169}
170
171sc_object::sc_object(const char *name_)
172{
173  const char *temp;
174  if (name_ == NULL) {
175    if (module_name_stack.empty())
176    {
177      cerr << "Internal error : module_name_stack is empty.";
178      exit (21092005);
179    }
180    string &module_name = module_name_stack.back ();
181    temp = module_name.c_str ();
182  } else {
183    temp = name_;
184  }
185#if 0
186  cerr << "object2basename[this] = " << temp << "\n";
187  cerr << "name temp = " << temp << endl;
188#endif
189  object2basename[this] = temp;
190  build_complete_name (name_,object2fullname[this]);
191        init ();
192}
193
194const char *
195sc_object::basename () const
196{
197  return object2basename[this].c_str ();
198}
199
200const char *
201sc_object::name () const
202{
203        object2name_t::iterator i = object2fullname.find (this);
204#ifdef DEBUG
205        if (i == object2fullname.end ()) {
206                cerr << "Internal error : can't find name of " << this << "\n";
207                exit (90);
208        }
209#endif
210  return i->second.c_str ();
211}
212 
213void
214sc_object::rename (const char* newname) const
215{
216/*
217        object2name_t::iterator i = object2fullname.find (this);
218#ifdef DEBUG
219        if (i == object2fullname.end ()) {
220                cerr << "Internal error : can't find name of " << this << "\n";
221                exit (90);
222        }
223#endif
224  i->second = newname;
225  object2basename[this] = newname;
226*/
227  object2basename[this] = newname;
228  build_complete_name (newname,object2fullname[this]);
229/*
230  const std::vector<sc_object*>& childs = get_child_objects();
231  std::vector<sc_object*>::const_iterator it;
232  for (it = childs.begin (); it != childs.end (); ++it)
233  {
234    string     out;
235    sc_object* obj = *it;
236    ASSERT(obj != NULL);
237    build_full_name (out, *obj);
238  }
239*/
240}
241const char *sc_object::kind () const
242{
243        object2infos_t::iterator i = object2infos.find (this);
244#ifdef DEBUG
245        if (i == object2infos.end ()) {
246                cerr << "Internal error : can't find kind of " << this << "\n";
247                exit (90);
248        }
249#endif
250  return i->second.kind_string;
251}
252
253sc_object::~sc_object ()
254{
255  if (save_on_exit)
256  {
257    sc_save_simulation (save_on_exit);
258    save_on_exit = NULL;
259  }
260  object2fullname.erase (this);
261  object2basename.erase (this);
262  object2infos.erase    (this);
263}
264
265std::ostream& 
266operator << (std::ostream& os, const sc_object& obj)
267{
268        return os << obj.name ();
269}
270
271/* virtual */ 
272const std::vector<sc_object*>& 
273sc_object::get_child_objects() const
274{
275  return sc_core::get_child_objects (*this);
276}
277
278sc_object*
279sc_object::get_parent_object () const
280{
281  return sc_core::get_parent_object (*this);
282}
283
284} // end of sc_core namespace
285
Note: See TracBrowser for help on using the repository browser.