signal-ptr.t.cpp 8.18 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
 * Copyright 2010,
 * François Bleibel,
 * Olivier Stasse,
 *
 * CNRS/AIST
 *
 * This file is part of dynamic-graph.
 * dynamic-graph is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * dynamic-graph is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.  You should
 * have received a copy of the GNU Lesser General Public License along
 * with dynamic-graph.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Copyright 2010,
 * François Bleibel,
 * Olivier Stasse,
 *
 * CNRS/AIST
 *
Thomas Moulard's avatar
Thomas Moulard committed
28
 * This file is part of dynamic-graph.
Thomas Moulard's avatar
Thomas Moulard committed
29
30
31
32
33
34
35
36
37
38
39
40
 * dynamic-graph is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * dynamic-graph is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.  You should
 * have received a copy of the GNU Lesser General Public License along
 * with dynamic-graph.  If not, see <http://www.gnu.org/licenses/>.
 */

41
42
43
44
45
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Copyright Projet JRL-Japan, 2007
 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *
 * File:      SignalPtr.t.cpp
46
 * Project:   DYNAMIC-GRAPH
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 * Author:    Nicolas Mansard
 *
 * Version control
 * ===============
 *
 *  $Id$
 *
 * Description
 * ============
 *
 *
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/




#ifndef __SIGNAL_PTR_TCPP
#define __SIGNAL_PTR_TCPP

66
#include <dynamic-graph/signal-ptr.h>
67
68
69

#undef VP_TEMPLATE_DEBUG_MODE
#define VP_TEMPLATE_DEBUG_MODE 0
70
#include <dynamic-graph/debug.h>
71

72
namespace dynamicgraph {
73
74
75

template< class T,class Time >
bool SignalPtr<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
76
isAbstractPluged( void ) const
77
{
Thomas Moulard's avatar
Thomas Moulard committed
78
79
  return ( (NULL!=signalPtr)||(abstractTransmitter) );
}
80
81
82
83


template< class T,class Time >
Signal<T,Time>* SignalPtr<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
84
getPtr ( void )
85
86
87
88
{
  dgTDEBUGIN(25);
  if(! isPluged() )
    DG_THROW ExceptionSignal( ExceptionSignal::NOT_INITIALIZED,
89
				  "In SignalPtr: SIN ptr not set.",
90
91
				  " (in signal <%s>)",getName().c_str());
  dgTDEBUGOUT(25);
Thomas Moulard's avatar
Thomas Moulard committed
92
  return signalPtr;
93
94
95
96
97
98
99
100
101
102
103
}

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,
104
				    "In SignalPtr: SIN ptr not set.",
105
106
				    " (in signal <%s>)",getName().c_str()); }
  dgTDEBUGOUT(25);
Thomas Moulard's avatar
Thomas Moulard committed
107
  return signalPtr;
108
109
110
111
}

template< class T,class Time >
SignalBase<Time>* SignalPtr<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
112
getAbstractPtr ( void )
113
114
115
{
  if(! isAbstractPluged() )
    { DG_THROW ExceptionSignal( ExceptionSignal::NOT_INITIALIZED,
116
				    "In SignalPtr: SIN ptr not set.",
117
118
119
120
121
122
123
124
125
126
127
				    " (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,
128
				    "In SignalPtr: SIN ptr not set.",
129
130
131
132
133
134
135
				    " (in signal <%s>)",getName().c_str()); }
  if( NULL!=signalPtr ) return signalPtr;
  else return abstractTransmitter;
}

template< class T,class Time >
void SignalPtr<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
136
plug( SignalBase<Time>* unknown_ref )
137
138
139
{
  dgTDEBUGIN(5);
  if(! unknown_ref )
Thomas Moulard's avatar
Thomas Moulard committed
140
    {
141
142
143
144
145
146
147
148
149
      signalPtr = NULL;
      transmitAbstract = false;
      dgTDEBUGOUT(5);
      return;
    }

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

Thomas Moulard's avatar
Thomas Moulard committed
150

151
152
153
154
  Signal<T,Time> * ref = dynamic_cast< Signal<T,Time>* > (unknown_ref);
  if( NULL==ref )
    {
      try {
Thomas Moulard's avatar
Thomas Moulard committed
155
	unknown_ref->checkCompatibility();
156
157
158
159
160
161
162
163
164
165
      }
      catch( T* t )
	{
	  dgTDEBUG(25) << "Cast THROW ok."<< std::endl;
	  Signal<T,Time>::setReference( t );
	  transmitAbstract = true; abstractTransmitter = unknown_ref;
	  transmitAbstractData = t;
	}
      catch(...)
	{
166
	  dgTDEBUG(25) << "Fatal error."<< std::endl;
167
168
169
170
171
172
173
174
175
176
177
178
	  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;
Thomas Moulard's avatar
Thomas Moulard committed
179
      signalPtr = ref;
180
181
182
183
184
185
186
    }
  dgTDEBUGOUT(5);
}

template< class T,class Time >
bool SignalPtr<T,Time>::
needUpdate( const Time& t ) const
Thomas Moulard's avatar
Thomas Moulard committed
187
188
{
  if( (isAbstractPluged())&&(!autoref()) ){ return getAbstractPtr()->needUpdate(t);}
189
190
191
192
193
194
  else return Signal<T,Time>::needUpdate(t);
}

template< class T,class Time >
const Time& SignalPtr<T,Time>::getTime( void ) const
{
Thomas Moulard's avatar
Thomas Moulard committed
195
  if(  (isAbstractPluged())&&(!autoref()) ){ return getAbstractPtr()->getTime();}
196
197
198
199
200
201
202
203
  return Signal<T,Time>::getTime();
}


template< class T,class Time >
const T& SignalPtr<T,Time>::
operator()( const Time & t )
{
Thomas Moulard's avatar
Thomas Moulard committed
204
  return access(t);
205
206
207
208
}

template< class T,class Time >
const T& SignalPtr<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
209
access( const Time & t )
210
211
212
213
214
215
216
{
  dgTDEBUGIN(15);
  if( modeNoThrow&&(! isPluged())&&Signal<T,Time>::copyInit )
    {
      dgTDEBUGOUT(15);
      return Signal<T,Time>::accessCopy();
    }
Thomas Moulard's avatar
Thomas Moulard committed
217
  else if( autoref() )
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    {
      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
Thomas Moulard's avatar
Thomas Moulard committed
256
257
	 << "\t [ headlabel = \"" << LeaderLocalName
	 << "\" , taillabel = \"" << itLocalName
258
	 << "\", fontsize=7, fontcolor=red ]" << std::endl;
Thomas Moulard's avatar
Thomas Moulard committed
259

260
261
262
263
264
265
266
267
268
269
270
    }
  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); }

Thomas Moulard's avatar
Thomas Moulard committed
271
  if( (isAbstractPluged())&&(!autoref()) )
272
273
    {
      // 	    dgTDEBUG(25) << "Display pointed."<<std::endl;
Thomas Moulard's avatar
Thomas Moulard committed
274
      // 	    getPtr()->display(os<<"PTR->");
275
      os << " -->-- PLUGGED";
276
    }
Thomas Moulard's avatar
Thomas Moulard committed
277
278
  else
    {
279
280
      if(! isAbstractPluged()) os << " UNPLUGGED";
      else if(autoref()) os << " AUTOPLUGGED";
281
    }
Thomas Moulard's avatar
Thomas Moulard committed
282

283
284
285
286
287
288
  dgTDEBUGOUT(25);
  return os;
}

template< class T,class Time >
std::ostream& SignalPtr<T,Time>::
289
displayDependencies( std::ostream& os,const int depth,
290
291
292
293
294
		     std::string space,
		     std::string next1,std::string next2 ) const
{
  dgTDEBUGIN(25);
  if( (isAbstractPluged())&&(!autoref()) )
295
    { getAbstractPtr()->displayDependencies(os,depth,space,next1+"-- "
296
				    +SignalBase<Time>::name+" -->",next2); }
Thomas Moulard's avatar
Thomas Moulard committed
297
  else
298
    {
Thomas Moulard's avatar
Thomas Moulard committed
299
      SignalBase<Time>::displayDependencies(os,depth,space,next1,next2);
300
301
302
303
304
    }
  dgTDEBUGOUT(25);
  return os;
}

305
} // namespace dynamicgraph
306
307
308
309
310
311
312
313
314

#endif // #ifndef __SIGNAL_PTR_TCPP

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