Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
N_DEV_Algorithm.h
Go to the documentation of this file.
1 //-------------------------------------------------------------------------
2 // Copyright Notice
3 //
4 // Copyright 2002 Sandia Corporation. Under the terms
5 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
6 // Government retains certain rights in this software.
7 //
8 // Xyce(TM) Parallel Electrical Simulator
9 // Copyright (C) 2002-2014 Sandia Corporation
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //-------------------------------------------------------------------------
24 
25 //-------------------------------------------------------------------------
26 // Filename : $RCSfile: N_DEV_Algorithm.h,v $
27 //
28 // Purpose :
29 //
30 //
31 //
32 // Special Notes :
33 //
34 //
35 // Creator :
36 //
37 // Creation Date :
38 //
39 // Revision Information:
40 // ---------------------
41 //
42 // Revision Number: $Revision: 1.10 $
43 //
44 // Revision Date : $Date: 2014/04/23 13:32:01 $
45 //
46 // Current Owner : $Author: dgbaur $
47 //-------------------------------------------------------------------------
48 
49 #ifndef Xyce_N_DEV_Algorithm_h
50 #define Xyce_N_DEV_Algorithm_h
51 
52 #include <iterator>
53 #include <vector>
54 
55 #include <N_DEV_fwd.h>
56 #include <N_DEV_Device.h>
57 
58 namespace Xyce {
59 namespace Device {
60 
61 
62 // Ultimately making std::for_each work would be most awesome. But that will take some doing.
63 
64 //-----------------------------------------------------------------------------
65 // Function : forEachInstance
66 // Purpose :
67 // Special Notes :
68 // Scope : public
69 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
70 // Creation Date : Tue Apr 22 12:04:01 2014
71 //-----------------------------------------------------------------------------
72 ///
73 /// Call forEachInstance on object d, passing operator op
74 ///
75 /// @param d Object to call forEachInstance on
76 /// @param op Operator to call passing in DeviceInstance pointer
77 ///
78 template <class D, class Op>
79 void forEachInstance(const D &d, Op op) {
80  d.forEachInstance(op);
81 }
82 
83 
84 //-----------------------------------------------------------------------------
85 // Function : forEachModel
86 // Purpose :
87 // Special Notes :
88 // Scope : public
89 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
90 // Creation Date : Tue Apr 22 12:04:01 2014
91 //-----------------------------------------------------------------------------
92 ///
93 /// Call forEachModel on object d, passing operator op
94 ///
95 /// @param d Object to call forEachModel on
96 /// @param op Operator to call passing in DeviceModel pointer
97 ///
98 template <class D, class Op>
99 void forEachModel(const D &d, Op op) {
100  d.forEachModel(op);
101 }
102 
103 
104 //-----------------------------------------------------------------------------
105 // Function : getName
106 // Purpose :
107 // Special Notes :
108 // Scope : public
109 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
110 // Creation Date : Tue Apr 22 11:12:37 2014
111 //-----------------------------------------------------------------------------
112 ///
113 /// Returns the name of the specified object
114 ///
115 /// @param c object to return associated name
116 ///
117 /// @return name associated with the object
118 ///
119 template <class C>
120 const std::string &getName(const C *c);
121 
122 
123 //-----------------------------------------------------------------------------
124 // Class : DeviceInstanceOutIteratorOp
125 // Purpose :
126 // Special Notes :
127 // Scope : public
128 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
129 // Creation Date : Tue Apr 22 10:58:03 2014
130 //-----------------------------------------------------------------------------
131 ///
132 /// Operator to populate a container via an output iterator with device instances.
133 ///
134 template <class Out>
136 {
137  //-----------------------------------------------------------------------------
138  // Function : DeviceInstanceOutIteratorOp
139  // Purpose :
140  // Special Notes :
141  // Scope : public
142  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
143  // Creation Date : Tue Apr 22 10:59:27 2014
144  //-----------------------------------------------------------------------------
145  ///
146  /// Copies output iterator into operator
147  ///
149  : it_(it)
150  {}
151 
152  //-----------------------------------------------------------------------------
153  // Function : operator()
154  // Purpose :
155  // Special Notes :
156  // Scope : public
157  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
158  // Creation Date : Tue Apr 22 11:00:21 2014
159  //-----------------------------------------------------------------------------
160  ///
161  /// Copies instance pointer through output iterator
162  ///
163  virtual bool operator()(DeviceInstance *instance) {
164  (*it_)++ = instance;
165 
166  return true;
167  }
168 
169  Out it_; ///< Output iterator
170 };
171 
172 
173 //-----------------------------------------------------------------------------
174 // Class : DeviceModelOutIteratorOp
175 // Purpose :
176 // Special Notes :
177 // Scope : public
178 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
179 // Creation Date : Tue Apr 22 10:58:03 2014
180 //-----------------------------------------------------------------------------
181 ///
182 /// Operator to populate a container via an output iterator with device models.
183 ///
184 template <class Out>
186 {
187  //-----------------------------------------------------------------------------
188  // Function : DeviceModelOutIteratorOp
189  // Purpose :
190  // Special Notes :
191  // Scope : public
192  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
193  // Creation Date : Tue Apr 22 10:59:27 2014
194  //-----------------------------------------------------------------------------
195  ///
196  /// Copies output iterator into operator
197  ///
199  : it_(it)
200  {}
201 
202  //-----------------------------------------------------------------------------
203  // Function : operator()
204  // Purpose :
205  // Special Notes :
206  // Scope : public
207  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
208  // Creation Date : Tue Apr 22 11:00:21 2014
209  //-----------------------------------------------------------------------------
210  ///
211  /// Copies model pointer through output iterator
212  ///
213  virtual bool operator()(DeviceModel *model) {
214  (*it_)++ = model;
215 
216  return true;
217  }
218 
219  Out it_; ///< Output iterator
220 };
221 
222 
223 //-----------------------------------------------------------------------------
224 // Class : DeviceInstanceNameOp
225 // Purpose :
226 // Special Notes :
227 // Scope : public
228 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
229 // Creation Date : Tue Apr 22 10:58:03 2014
230 //-----------------------------------------------------------------------------
231 ///
232 /// Operator to populate a container of strings with the names of the device instances.
233 ///
234 template<class Out>
236 {
237  //-----------------------------------------------------------------------------
238  // Function : DeviceInstanceNameOp
239  // Purpose :
240  // Special Notes :
241  // Scope : public
242  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
243  // Creation Date : Tue Apr 22 10:59:27 2014
244  //-----------------------------------------------------------------------------
245  ///
246  /// Copies output iterator to it_
247  ///
249  : it_(it)
250  {}
251 
252  //-----------------------------------------------------------------------------
253  // Function : operator
254  // Purpose :
255  // Special Notes :
256  // Scope : public
257  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
258  // Creation Date : Tue Apr 22 12:08:10 2014
259  //-----------------------------------------------------------------------------
260  ///
261  /// Write the result of getName(instance) to the output iterator
262  ///
263  virtual bool operator()(DeviceInstance *device_instance) {
264  (*it_)++ = getName(device_instance);
265 
266  return true;
267  }
268 
269  Out it_; ///< Output iterator
270 };
271 
272 
273 //-----------------------------------------------------------------------------
274 // Function : MapOp
275 // Purpose :
276 // Special Notes :
277 // Scope : public
278 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
279 // Creation Date : Tue Apr 22 12:19:13 2014
280 //-----------------------------------------------------------------------------
281 ///
282 /// operator to populate a map from device instance name to device instance pointer
283 ///
284 template<class C>
285 struct MapOp : public DeviceInstanceOp
286 {
287  //-----------------------------------------------------------------------------
288  // Function : MapOp
289  // Purpose :
290  // Special Notes :
291  // Scope : public
292  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
293  // Creation Date : Tue Apr 22 12:19:57 2014
294  //-----------------------------------------------------------------------------
295  ///
296  /// Destination map reference into the operator
297  ///
298  /// @param map map to insert name to instance mapping
299  ///
300  MapOp(std::map<std::string, C *> &map)
301  : map_(map)
302  {}
303 
304  //-----------------------------------------------------------------------------
305  // Function : operator()
306  // Purpose :
307  // Special Notes :
308  // Scope : public
309  // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
310  // Creation Date : Tue Apr 22 12:21:47 2014
311  //-----------------------------------------------------------------------------
312  ///
313  ///
314  ///
315  virtual bool operator()(DeviceInstance *device_instance)
316  {
317  map_[getName(device_instance)] = static_cast<C *>(device_instance);
318  return true;
319  }
320 
321  std::map<std::string, C *> & map_;
322 };
323 
324 
325 //-----------------------------------------------------------------------------
326 // Function : getDeviceInstances
327 // Purpose :
328 // Special Notes :
329 // Scope : public
330 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
331 // Creation Date : Tue Apr 22 11:02:16 2014
332 //-----------------------------------------------------------------------------
333 ///
334 /// Calls forEachInstance() on d which iterates through all the instances copies them to the output iterator.
335 ///
336 /// @param d D to call forEachInstance
337 /// @param it output iterator
338 ///
339 template <class D, class Out>
340 void getDeviceInstances(const D &d, Out it) {
342 }
343 
344 
345 //-----------------------------------------------------------------------------
346 // Function : getDeviceModels
347 // Purpose :
348 // Special Notes :
349 // Scope : public
350 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
351 // Creation Date : Tue Apr 22 11:02:16 2014
352 //-----------------------------------------------------------------------------
353 ///
354 /// Calls forEachModel() on d which iterates through all the models copies them to the output iterator.
355 ///
356 /// @param d D to call forEachModel
357 /// @param it output iterator
358 ///
359 template <class D, class Out>
360 void getDeviceModels(const D &d, Out it)
361 {
363 }
364 
365 
366 //-----------------------------------------------------------------------------
367 // Function : getDeviceInstanceNames
368 // Purpose :
369 // Special Notes :
370 // Scope : public
371 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
372 // Creation Date : Tue Apr 22 12:22:16 2014
373 //-----------------------------------------------------------------------------
374 ///
375 /// Calls forEachInstance() on d which iterates through all the instance and copies the name to the output iterator.
376 ///
377 /// @param d D to call forEachInstance
378 /// @param it output iterator
379 ///
380 template<class D, class Out>
381 void getDeviceInstanceNames(const D &d, Out it)
382 {
384 }
385 
386 
387 //-----------------------------------------------------------------------------
388 // Function : mapDeviceInstances
389 // Purpose :
390 // Special Notes :
391 // Scope : public
392 // Creator : David G. Baur Raytheon Sandia National Laboratories 1355
393 // Creation Date : Tue Apr 22 12:22:22 2014
394 //-----------------------------------------------------------------------------
395 ///
396 /// Calls forEachInstance() on d which iterates through all the instances and inserts the device instance name to pointer mapping.
397 ///
398 /// @param d D to call forEachInstance
399 /// @param it output iterator
400 ///
401 template<class D, class X>
402 void mapDeviceInstances(const D &d, std::map<std::string, X *> &map) {
403  forEachInstance(d, MapOp<X>(map));
404 }
405 
406 } // namespace Device
407 } // namespace Xyce
408 
409 #endif // Xyce_N_DEV_Algorithm_h