signal-ptr.t.cpp 6.68 KB
Newer Older
1
2
3
4
5
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Copyright Projet JRL-Japan, 2007
 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *
 * File:      SignalPtr.t.cpp
6
 * Project:   DYNAMIC-GRAPH
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * Author:    Nicolas Mansard
 *
 * Version control
 * ===============
 *
 *  $Id$
 *
 * Description
 * ============
 *
 *
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/




#ifndef __SIGNAL_PTR_TCPP
#define __SIGNAL_PTR_TCPP

26
#include <dynamic-graph/signal-ptr.h>
27
28
29

#undef VP_TEMPLATE_DEBUG_MODE
#define VP_TEMPLATE_DEBUG_MODE 0
30
#include <dynamic-graph/debug.h>
31

32
namespace dynamicgraph {
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

template< class T,class Time >
bool SignalPtr<T,Time>::
isAbstractPluged( void ) const 
{
  return ( (NULL!=signalPtr)||(abstractTransmitter) ); 
} 


template< class T,class Time >
Signal<T,Time>* SignalPtr<T,Time>::
getPtr ( void ) 
{
  dgTDEBUGIN(25);
  if(! isPluged() )
    DG_THROW ExceptionSignal( ExceptionSignal::NOT_INITIALIZED,
49
				  "In SignalPtr: SIN ptr not set.",
50
51
52
53
54
55
56
57
58
59
60
61
62
63
				  " (in signal <%s>)",getName().c_str());
  dgTDEBUGOUT(25);
  return signalPtr; 
}

template< class T,class Time >
const Signal<T,Time>* SignalPtr<T,Time>::
getPtr ( void ) const
{
  dgTDEBUGIN(25) << SignalBase<Time>::name <<"("<< isPluged() <<")"
		  << this << "->"<<signalPtr <<std::endl;
  dgTDEBUGIN(25);
  if(! isPluged() )
    { DG_THROW ExceptionSignal( ExceptionSignal::NOT_INITIALIZED,
64
				    "In SignalPtr: SIN ptr not set.",
65
66
67
68
69
70
71
72
73
74
75
				    " (in signal <%s>)",getName().c_str()); }
  dgTDEBUGOUT(25);
  return signalPtr; 
}

template< class T,class Time >
SignalBase<Time>* SignalPtr<T,Time>::
getAbstractPtr ( void ) 
{
  if(! isAbstractPluged() )
    { DG_THROW ExceptionSignal( ExceptionSignal::NOT_INITIALIZED,
76
				    "In SignalPtr: SIN ptr not set.",
77
78
79
80
81
82
83
84
85
86
87
				    " (in signal <%s>)",getName().c_str()); }
  if( NULL!=signalPtr ) return signalPtr;
  else return abstractTransmitter;
}

template< class T,class Time >
const SignalBase<Time>* SignalPtr<T,Time>::
getAbstractPtr ( void ) const
{
  if(! isAbstractPluged() )
    { DG_THROW ExceptionSignal( ExceptionSignal::NOT_INITIALIZED,
88
				    "In SignalPtr: SIN ptr not set.",
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
				    " (in signal <%s>)",getName().c_str()); }
  if( NULL!=signalPtr ) return signalPtr;
  else return abstractTransmitter;
}

template< class T,class Time >
void SignalPtr<T,Time>::
plug( SignalBase<Time>* unknown_ref ) 
{
  dgTDEBUGIN(5);
  if(! unknown_ref )
    { 
      signalPtr = NULL;
      transmitAbstract = false;
      dgTDEBUGOUT(5);
      return;
    }

  dgTDEBUG(5) << "# In  T = "<< getName() <<" ="
	       <<typeid( Signal<T,Time>::Tcopy1 ) .name() << "{ "<<std::endl;

     
  Signal<T,Time> * ref = dynamic_cast< Signal<T,Time>* > (unknown_ref);
  if( NULL==ref )
    {
      try {
	unknown_ref->checkCompatibility(); 
      }
      catch( T* t )
	{
	  dgTDEBUG(25) << "Cast THROW ok."<< std::endl;
	  Signal<T,Time>::setReference( t );
	  transmitAbstract = true; abstractTransmitter = unknown_ref;
	  transmitAbstractData = t;
	}
      catch(...)
	{
126
	  dgTDEBUG(25) << "Fatal error."<< std::endl;
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	  transmitAbstract = false;
	  DG_THROW ExceptionSignal( ExceptionSignal::PLUG_IMPOSSIBLE,
					"Compl. Uncompatible types for plugin.",
					"(while trying to plug <%s> on <%s>).",
					unknown_ref->getName().c_str(),
					this->getName().c_str());
	}
    }
  else
    {
      dgTDEBUG(25) << "Cast ok."<< std::endl;
      transmitAbstract = false;
      signalPtr = ref; 
    }
  dgTDEBUGOUT(5);
}

template< class T,class Time >
bool SignalPtr<T,Time>::
needUpdate( const Time& t ) const
{ 
  if( (isAbstractPluged())&&(!autoref()) ){ return getAbstractPtr()->needUpdate(t);} 
  else return Signal<T,Time>::needUpdate(t);
}

template< class T,class Time >
const Time& SignalPtr<T,Time>::getTime( void ) const
{
  if(  (isAbstractPluged())&&(!autoref()) ){ return getAbstractPtr()->getTime();} 
  return Signal<T,Time>::getTime();
}


template< class T,class Time >
const T& SignalPtr<T,Time>::
operator()( const Time & t )
{
  return access(t); 
}

template< class T,class Time >
const T& SignalPtr<T,Time>::
access( const Time & t ) 
{
  dgTDEBUGIN(15);
  if( modeNoThrow&&(! isPluged())&&Signal<T,Time>::copyInit )
    {
      dgTDEBUGOUT(15);
      return Signal<T,Time>::accessCopy();
    }
  else if( autoref() )  
    {
      dgTDEBUGOUT(15);
      return Signal<T,Time>::access(t);
    }
  else if( transmitAbstract )
    {
      abstractTransmitter->recompute(t);
      dgTDEBUGOUT(15);
      return *transmitAbstractData;
    }
  else
    {
      dgTDEBUGOUT(15);
      return getPtr()->access(t);
    }
}
template< class T,class Time >
const T& SignalPtr<T,Time>::
accessCopy( void ) const
{
  if( modeNoThrow&&(! isPluged())&&Signal<T,Time>::copyInit )
    return Signal<T,Time>::accessCopy();
  else if( autoref() ) return Signal<T,Time>::accessCopy();
  else if( transmitAbstract ) return *transmitAbstractData;
  else return getPtr()->accessCopy();
}
template< class T, class Time>
std::ostream & SignalPtr<T,Time>::
writeGraph(std::ostream &os) const
{
  std::string LeaderLocalName;
  std::string LeaderNodeName;
  Signal<T,Time>::ExtractNodeAndLocalNames(LeaderLocalName,LeaderNodeName);
  if ( isAbstractPluged() && !autoref())
    {
      std::string itLocalName,itNodeName;
      getAbstractPtr()->ExtractNodeAndLocalNames(itLocalName,itNodeName);
      os << "\t\"" << itNodeName << "\" -> \"" << LeaderNodeName << "\"" << std::endl
	 << "\t [ headlabel = \"" << LeaderLocalName 
	 << "\" , taillabel = \"" << itLocalName 
	 << "\", fontsize=7, fontcolor=red ]" << std::endl;
      
    }
  return os;
}

template< class T,class Time >
std::ostream& SignalPtr<T,Time>::
display( std::ostream& os ) const
{
  dgTDEBUGIN(25) << SignalBase<Time>::name << this << "||" << isPluged() << "||"<<signalPtr;
  { Signal<T,Time>::display(os); }

  if( (isAbstractPluged())&&(!autoref()) ) 
    {
      // 	    dgTDEBUG(25) << "Display pointed."<<std::endl;
      // 	    getPtr()->display(os<<"PTR->"); 
235
      os << " -->-- PLUGGED";
236
237
238
    }
  else 
    { 
239
240
      if(! isAbstractPluged()) os << " UNPLUGGED";
      else if(autoref()) os << " AUTOPLUGGED";
241
242
243
244
245
246
247
248
    }
     
  dgTDEBUGOUT(25);
  return os;
}

template< class T,class Time >
std::ostream& SignalPtr<T,Time>::
249
displayDependencies( std::ostream& os,const int depth,
250
251
252
253
254
		     std::string space,
		     std::string next1,std::string next2 ) const
{
  dgTDEBUGIN(25);
  if( (isAbstractPluged())&&(!autoref()) )
255
    { getAbstractPtr()->displayDependencies(os,depth,space,next1+"-- "
256
257
258
				    +SignalBase<Time>::name+" -->",next2); }
  else 
    {
259
      SignalBase<Time>::displayDependencies(os,depth,space,next1,next2); 
260
261
262
263
264
    }
  dgTDEBUGOUT(25);
  return os;
}

265
} // namespace dynamicgraph
266
267
268
269
270
271
272
273
274

#endif // #ifndef __SIGNAL_PTR_TCPP

/*
 * Local variables:
 * c-basic-offset: 2
 * End:
 */