MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SingleMsg.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2010 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include "header.h"
11 #include "SingleMsg.h"
12 
13 // Initializing static variables
15 vector< SingleMsg* > SingleMsg::msg_;
16 
18 // Here is the SingleMsg code
20 
21 SingleMsg::SingleMsg( const Eref& e1, const Eref& e2, unsigned int msgIndex)
22  : Msg( ObjId( managerId_, (msgIndex != 0 ) ? msgIndex: msg_.size() ),
23  e1.element(), e2.element() ),
24  i1_( e1.dataIndex() ),
25  i2_( e2.dataIndex() ),
26  f2_( e2.fieldIndex() )
27 {
28  if ( msgIndex == 0 )
29  {
30  msg_.push_back( this );
31  return;
32  }
33  else if ( msg_.size() <= msgIndex )
34  {
35  msg_.resize( msgIndex + 1 );
36  }
37  msg_[ msgIndex ] = this;
38 }
39 
41 {
42  assert( mid_.dataIndex < msg_.size() );
43  msg_[ mid_.dataIndex ] = 0; // ensure deleted ptr isn't reused.
44 }
45 
46 Eref SingleMsg::firstTgt( const Eref& src ) const
47 {
48  if ( src.element() == e1_ )
49  return Eref( e2_, i2_, f2_ );
50  else if ( src.element() == e2_ )
51  return Eref( e1_, i1_ );
52  return Eref( 0, 0 );
53 }
54 
55 void SingleMsg::sources( vector< vector< Eref > >& v ) const
56 {
57  v.clear();
58  v.resize( e2_->numData() );
59  v[i2_].resize( 1, Eref( e1_, i1_ ) );
60 }
61 
62 void SingleMsg::targets( vector< vector< Eref > >& v ) const
63 {
64  v.clear();
65  v.resize( e1_->numData() );
66  v[i1_].resize( 1, Eref( e2_, i2_, f2_ ) );
67 }
68 
69 
70 
71 /*
72 bool SingleMsg::isMsgHere( const Qinfo& q ) const
73 {
74  if ( q.isForward() )
75  return ( i1_ == q.srcIndex() );
76  else
77  return ( i2_ == q.srcIndex() );
78 }
79 */
80 
82 {
83  return i1_;
84 }
85 
87 {
88  return i2_;
89 }
90 
92 {
93  return SingleMsg::managerId_;
94 }
95 
97 {
98  if ( f.element() == e1() )
99  {
100  if ( f.dataIndex == i1_ )
101  return ObjId( e2()->id(), i2_ );
102  }
103  else if ( f.element() == e2() )
104  {
105  if ( f.dataIndex == i2_ )
106  return ObjId( e1()->id(), i1_ );
107  }
108  return ObjId( 0, BADINDEX );
109 }
110 
111 Msg* SingleMsg::copy( Id origSrc, Id newSrc, Id newTgt,
112  FuncId fid, unsigned int b, unsigned int n ) const
113 {
114  const Element* orig = origSrc.element();
115  if ( n <= 1 )
116  {
117  SingleMsg* ret = 0;
118  if ( orig == e1() )
119  {
120  ret = new SingleMsg( Eref( newSrc.element(), i1_ ),
121  Eref( newTgt.element(), i2_, f2_ ), 0 );
122  ret->e1()->addMsgAndFunc( ret->mid(), fid, b );
123  }
124  else if ( orig == e2() )
125  {
126  ret = new SingleMsg( Eref( newTgt.element(), i1_ ),
127  Eref( newSrc.element(), i2_, f2_ ), 0 );
128  ret->e2()->addMsgAndFunc( ret->mid(), fid, b );
129  }
130  else
131  {
132  assert( 0 );
133  }
134  return ret;
135  }
136  else
137  {
138  // Here we need a SliceMsg which goes from one 2-d array to another.
139  cout << "Error: SingleMsg::copy: SliceMsg not yet implemented\n";
140  return 0;
141  }
142 }
143 
145 // Here we set up the MsgManager portion of the class.
147 
149 {
151  // Field definitions.
153  static ValueFinfo< SingleMsg, DataId > index1(
154  "i1",
155  "Index of source object.",
158  );
159  static ValueFinfo< SingleMsg, DataId > index2(
160  "i2",
161  "Index of dest object.",
164  );
165 
166  static Finfo* singleMsgFinfos[] =
167  {
168  &index1, // value
169  &index2, // value
170  };
171 
172  static Dinfo< short > dinfo;
173  static Cinfo singleMsgCinfo (
174  "SingleMsg", // name
175  Msg::initCinfo(), // base class
176  singleMsgFinfos,
177  sizeof( singleMsgFinfos ) / sizeof( Finfo* ), // num Fields
178  &dinfo
179  );
180 
181  return &singleMsgCinfo;
182 }
183 
185 
186 
188 {
189  return i1_;
190 }
191 
193 {
194  i1_ = di;
195  e1()->markRewired();
196  e2()->markRewired();
197 }
198 
200 {
201  return i2_;
202 }
203 
205 {
206  i2_ = di;
207  e1()->markRewired();
208  e2()->markRewired();
209 }
210 
211 void SingleMsg::setTargetField( unsigned int f )
212 {
213  f2_ = f;
214  e1()->markRewired();
215 }
216 
217 unsigned int SingleMsg::getTargetField() const
218 {
219  return f2_;
220 }
221 
223 unsigned int SingleMsg::numMsg()
224 {
225  return msg_.size();
226 }
227 
229 char* SingleMsg::lookupMsg( unsigned int index )
230 {
231  assert( index < msg_.size() );
232  return reinterpret_cast< char* >( msg_[index] );
233 }
void markRewired()
Definition: Element.cpp:706
void setI2(DataId di)
Definition: SingleMsg.cpp:204
ObjId findOtherEnd(ObjId end) const
Definition: SingleMsg.cpp:96
Element * e2() const
Definition: Msg.h:68
ObjId mid() const
Definition: Msg.h:106
DataId i2() const
Definition: SingleMsg.cpp:86
Eref firstTgt(const Eref &src) const
Definition: SingleMsg.cpp:46
Element * element() const
Synonym for Id::operator()()
Definition: Id.cpp:113
Definition: Dinfo.h:60
void setI1(DataId di)
Definition: SingleMsg.cpp:192
Id managerId() const
Definition: SingleMsg.cpp:91
void addMsgAndFunc(ObjId mid, FuncId fid, BindIndex bindIndex)
Definition: Element.cpp:128
Definition: ObjId.h:20
Element * element() const
Definition: Eref.h:42
static char dataIndex[]
Definition: mfield.cpp:406
DataId i1_
Definition: SingleMsg.h:58
void sources(vector< vector< Eref > > &v) const
Definition: SingleMsg.cpp:55
DataId getI1() const
Definition: SingleMsg.cpp:187
DataId i1() const
Definition: SingleMsg.cpp:81
static vector< SingleMsg * > msg_
Definition: SingleMsg.h:62
Definition: Eref.h:26
virtual unsigned int numData() const =0
Returns number of data entries across all nodes.
unsigned int getTargetField() const
Definition: SingleMsg.cpp:217
Definition: Msg.h:18
void targets(vector< vector< Eref > > &v) const
Definition: SingleMsg.cpp:62
Element * e1_
Index of this Msg on the msg_ vector.
Definition: Msg.h:180
ObjId mid_
Definition: Msg.h:178
Element * e1() const
Definition: Msg.h:61
Element * element() const
Definition: ObjId.cpp:124
DataId i2_
Definition: SingleMsg.h:59
static const Cinfo * initCinfo()
Definition: SingleMsg.cpp:148
static const Cinfo * singleMsgCinfo
Definition: SingleMsg.cpp:184
Msg * copy(Id origSrc, Id newSrc, Id newTgt, FuncId fid, unsigned int b, unsigned int n) const
Definition: SingleMsg.cpp:111
Definition: Id.h:17
Element * e2_
Element 1 attached to Msg.
Definition: Msg.h:181
void setTargetField(unsigned int f)
Definition: SingleMsg.cpp:211
static unsigned int numMsg()
Msg lookup functions.
Definition: SingleMsg.cpp:223
unsigned int FuncId
Definition: header.h:42
unsigned int f2_
Definition: SingleMsg.h:60
SingleMsg(const Eref &e1, const Eref &e2, unsigned int msgIndex)
Definition: SingleMsg.cpp:21
const unsigned int BADINDEX
Used by ObjId and Eref.
Definition: consts.cpp:25
unsigned int DataId
Definition: header.h:47
Definition: Cinfo.h:18
static char * lookupMsg(unsigned int index)
Static function for Msg access.
Definition: SingleMsg.cpp:229
static Id managerId_
Definition: SingleMsg.h:61
DataId getI2() const
Definition: SingleMsg.cpp:199
static const Cinfo * initCinfo()
Definition: Msg.cpp:165
unsigned int dataIndex
Definition: ObjId.h:99
Definition: Finfo.h:12