source: sources/src/sc_object.cc @ 26

Last change on this file since 26 was 17, checked in by buchmann, 16 years ago

Fix :

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