1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  sc_vector.cpp - A vector of named (SystemC) objects (modules, ports, channels)
23
24  Original Author: Philipp A. Hartmann, OFFIS
25
26  CHANGE LOG AT END OF FILE
27 *****************************************************************************/
28
29
30#include "sc_vector.h"
31
32#include "sysc/utils/sc_hash.h"
33#include "sysc/utils/sc_list.h"
34#include "sysc/utils/sc_utils_ids.h"
35
36#include "sysc/kernel/sc_simcontext.h"
37#include "sysc/kernel/sc_object_manager.h"
38
39#include <sstream>
40
41namespace sc_core {
42
43sc_vector_base::sc_vector_base()
44  : sc_object( sc_gen_unique_name("vector") )
45  , vec_()
46  , objs_vec_()
47{}
48
49std::vector< sc_object* > const &
50sc_vector_base::get_elements() const
51{
52  if( !objs_vec_ )
53    objs_vec_ = new std::vector< sc_object* >;
54
55  if( objs_vec_->size() || !size() )
56    return *objs_vec_;
57
58  objs_vec_->reserve( size() );
59  for( const_iterator it=begin(); it != end(); ++it )
60    if( sc_object* obj = object_cast(*it) )
61      objs_vec_->push_back( obj );
62
63  return *objs_vec_;
64}
65
66sc_object*
67sc_vector_base::implicit_cast( ... ) const
68{
69  SC_REPORT_ERROR( SC_ID_VECTOR_NONOBJECT_ELEMENTS_, name() );
70  return NULL;
71}
72
73void
74sc_vector_base::check_index( size_type i ) const
75{
76  if( i>=size() )
77  {
78    std::stringstream str;
79    str << name()
80        << "[" << i << "] >= size() = " << size();
81    SC_REPORT_ERROR( SC_ID_OUT_OF_BOUNDS_, str.str().c_str() );
82  }
83}
84
85bool
86sc_vector_base::check_init( size_type n ) const
87{
88  if ( !n )
89    return false;
90
91  if( size() ) // already filled
92  {
93    std::stringstream str;
94    str << name()
95        << ", size=" << size()
96        << ", requested size=" << n;
97    SC_REPORT_ERROR( SC_ID_VECTOR_INIT_CALLED_TWICE_
98                   , str.str().c_str() );
99    return false;
100  }
101
102  sc_simcontext* simc = simcontext();
103  sc_assert( simc == sc_get_curr_simcontext() );
104
105  sc_object* parent_p = simc->active_object();
106  if( parent_p != get_parent_object() )
107  {
108    std::stringstream str;
109    str << name() << ": expected "
110        << ( get_parent_object()
111              ? get_parent_object()->name() : "<top-level>" )
112        << ", got "
113        << ( parent_p ? parent_p->name() : "<top-level>" );
114
115    SC_REPORT_ERROR( SC_ID_VECTOR_INIT_INVALID_CONTEXT_
116                   , str.str().c_str() );
117    return false;
118  }
119
120  return true;
121}
122
123void
124sc_vector_base::report_empty_bind( const char* kind_, bool dst_empty_ ) const
125{
126  std::stringstream str;
127
128  str << "target `" << name() << "' "
129      << "(" << kind_ << ") ";
130
131  if( !size() ) {
132    str << "not initialised yet";
133  } else if ( dst_empty_ ) {
134    str << "empty range given";
135  } else {
136    str << "empty destination range given";
137  }
138
139  SC_REPORT_WARNING( SC_ID_VECTOR_BIND_EMPTY_, str.str().c_str() );
140}
141
142std::string
143sc_vector_base::make_name( const char* prefix, size_type /* idx */ )
144{
145  // TODO: How to handle name clashes due to interleaved vector
146  //       creation and init()?
147  //       sc_vector< foo > v1, v2;
148  //       v1.name() == "vector", v2.name() == "vector_0"
149  //       v1.init( 1 ); -> v1[0].name() == "vector_0" -> clash
150  return sc_gen_unique_name( prefix );
151}
152
153} // namespace sc_core
154
155// $Log: sc_vector.cpp,v $
156// Revision 1.6  2011/08/26 20:46:20  acg
157//  Andy Goodrich: moved the modification log to the end of the file to
158//  eliminate source line number skew when check-ins are done.
159//
160// Revision 1.5  2011/04/01 22:35:19  acg
161//  Andy Goodrich: spelling fix.
162//
163// Revision 1.4  2011/03/28 13:03:09  acg
164//  Andy Goodrich: Philipp's latest update.
165//
166// Revision 1.3  2011/03/23 16:16:28  acg
167//  Philipp A. Hartmann: rebase implementation on void*
168//      - supports virtual inheritance from sc_object again
169//      - build up get_elements result on demand
170//      - still requires element type to be derived from sc_object
171//
172// Revision 1.2  2011/02/14 17:54:25  acg
173//  Andy Goodrich: Philipp's addition of early bind checks.
174//
175// Revision 1.1  2011/02/13 21:54:14  acg
176//  Andy Goodrich: turn on embedding of cvs log records.
177
178// Taf!
179