SelectableObject.h 18.2 KB
Newer Older
1
2
//======================================================================================================================
//
3
//  This file is part of waLBerla. waLBerla is free software: you can
4
//  redistribute it and/or modify it under the terms of the GNU General Public
5
//  License as published by the Free Software Foundation, either version 3 of
6
//  the License, or (at your option) any later version.
7
8
9
10
//
//  waLBerla 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 General Public License
11
//  for more details.
12
//
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file SelectableObject.h
//! \ingroup core
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//
//======================================================================================================================

#pragma once

#include "core/debug/Debug.h"

#include <iostream>
#include <sstream>
#include <string>
#include <vector>


namespace walberla {
namespace selectable {



//**********************************************************************************************************************
/*!
*   \brief Abstract base class for storing and selecting objects of type T which are attached with selection attributes
*          of type A
*
*   The sole purpose of this class is to store selectable objects of type T, meaning objects which are attached with
*   additional selection attributes of type A, in order to later retrieve only those objects which match certain
*   requested attributes. For the retrieval (see functions "get") a selection object of type S is used (type S may or
*   may not be equal to type A).
*
*   Requirements for type A:
*
*      - objects of type A must be comparable with operator "!="
*      - A must implement operator "<<" for output via a std::ostream object
*
*   Additionally, it must be possible to store objects of both types T and A in a std::vector.
*
*   Objects can be added by calling the function "add" (for every object that is added an identifier string can be
*   provided) and be retrieved by calling "get". For additional information refer to the documentation of these
*   functions.
*
*   Every class that derives from SelectableObject must implement the function
*      \code void select( std::vector< size_t >& index, const S& selector ) const \endcode
*   After returning from the call to this function, the vector "index" must contain the index of every object in
*   "attributes_" that matches with the provided selector "selector" (for an actual implementation of SelectableObject
*   see "SetSelectableObject.h").
*/
//**********************************************************************************************************************

template< typename T, typename A, typename S >
class SelectableObject {

public:

   //**Iterators********************************************************************************************************
   /*! \name Iterators */
   //@{
   class const_iterator;
   friend class const_iterator;

   class iterator;
   friend class iterator;
   class iterator {

      friend class const_iterator;
      friend class SelectableObject;

   public:

      iterator( const iterator& it ) : selectable_( it.selectable_ ), index_( it.index_ ) {}

      iterator& operator++() { ++index_; return *this; }                            // prefix ++X
      iterator  operator++(int) { iterator it( *this ); operator++(); return it; }; // postfix X++

      bool operator==( const iterator& rhs ) const { return selectable_ == rhs.selectable_ && index_ == rhs.index_; }
      bool operator!=( const iterator& rhs ) const { return !operator==(rhs); }

      T *                        get() { WALBERLA_ASSERT_LESS( index_, selectable_->object_.size() );     return &(selectable_->object_[index_]); }
      const std::string & identifier() { WALBERLA_ASSERT_LESS( index_, selectable_->identifier_.size() ); return selectable_->identifier_[index_]; }

      T & operator*()  { WALBERLA_ASSERT_LESS( index_, selectable_->object_.size() ); return   selectable_->object_[index_]; }
      T * operator->() { WALBERLA_ASSERT_LESS( index_, selectable_->object_.size() ); return &(selectable_->object_[index_]); }

   private:

      iterator( SelectableObject* const selectable, const size_t index ) : selectable_( selectable ), index_( index ) {}

      SelectableObject * const selectable_;
      size_t index_;
   };

   class const_iterator {

      friend class SelectableObject;

   public:

      const_iterator( const       iterator& it ) : selectable_( it.selectable_ ), index_( it.index_ ) {}
      const_iterator( const const_iterator& it ) : selectable_( it.selectable_ ), index_( it.index_ ) {}

      const_iterator& operator++() { ++index_; return *this; }                                  // prefix ++X
      const_iterator  operator++(int) { const_iterator it( *this ); operator++(); return it; }; // postfix X++

      bool operator==( const const_iterator& rhs ) const { return selectable_ == rhs.selectable_ && index_ == rhs.index_; }
      bool operator!=( const const_iterator& rhs ) const { return !operator==(rhs); }

      const T *                  get() const { WALBERLA_ASSERT_LESS( index_, selectable_->object_.size() );     return &(selectable_->object_[index_]); }
      const std::string & identifier() const { WALBERLA_ASSERT_LESS( index_, selectable_->identifier_.size() ); return selectable_->identifier_[index_]; }

      const T & operator*()  const { WALBERLA_ASSERT_LESS( index_, selectable_->object_.size() ); return   selectable_->object_[index_]; }
      const T * operator->() const { WALBERLA_ASSERT_LESS( index_, selectable_->object_.size() ); return &(selectable_->object_[index_]); }

   private:

      const_iterator( const SelectableObject* const selectable, const size_t index ) : selectable_( selectable ), index_( index ) {}

      const SelectableObject * const selectable_;
      size_t index_;
   };
   //@}
   //*******************************************************************************************************************



Sebastian Eibl's avatar
Sebastian Eibl committed
141
   virtual ~SelectableObject() = default;
142
143
144
145
146
147
148
149
150

   void add( const T& object, const A& attributes, const std::string& identifier = std::string() );

   iterator       begin()       { return       iterator( this, 0 ); }
   const_iterator begin() const { return const_iterator( this, 0 ); }

   iterator       end()       { return       iterator( this, object_.size() ); }
   const_iterator end() const { return const_iterator( this, object_.size() ); }

151
152
   size_t getNumberOfMatching( const S& selector ) const;

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
   size_t get(              T& object, const S& selector ) const;
   void   get( std::vector<T>& object, const S& selector ) const;

   size_t get(              T& object,                std::string& identifier, const S& selector ) const;
   void   get( std::vector<T>& object, std::vector< std::string >& identifier, const S& selector ) const;

         T* getUnique( const S& selector );
   const T* getUnique( const S& selector ) const;
         T* getUnique( const S& selector, std::string & identifierOut );
   const T* getUnique( const S& selector, std::string & identifierOut ) const;

          void        toStream( std::ostream& os ) const;
   inline std::string toString() const;

   size_t size() const { WALBERLA_ASSERT_EQUAL( object_.size(), identifier_.size() );   return object_.size();  }
   bool  empty() const { WALBERLA_ASSERT_EQUAL( object_.empty(), identifier_.empty() ); return object_.empty(); }

private:

   //*******************************************************************************************************************
   /*!
   *   Every class that derives from SelectableObject must implement this function. After returning from the call to
   *   this function, the vector "index" must contain the index of every object in "attributes_" that matches with the
   *   provided selector "selector".
   */
   //*******************************************************************************************************************
   virtual void select( std::vector< size_t >& index, const S& selector ) const = 0;

   std::vector< T >           object_;
   std::vector< std::string > identifier_;

protected:

   std::vector< A >           attributes_;

}; // class SelectableObject



//**********************************************************************************************************************
/*!
*   This function is used to add an object together with its selection attributes stored in "attributes". Optionally,
*   an identifier string can be provided which is used during output (see "toStream" and "toString"). In debug mode,
*   this function triggers an assertion and fails if another object with the same selection attributes already exists.
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
void SelectableObject<T,A,S>::add( const T& object, const A& attributes, const std::string& identifier ) {

   object_.push_back( object );
   identifier_.push_back( identifier.empty() ? std::string( "[anonymous]" ) : identifier );
   attributes_.push_back( attributes );
}

207
208
209
210
211
212
213
214
215
216
217
//**********************************************************************************************************************
/*!
*   Returns the number of objects matching the specified "selector".
 */
//**********************************************************************************************************************
template< typename T, typename A, typename S >
size_t SelectableObject<T,A,S>::getNumberOfMatching( const S& selector ) const {

   std::vector< size_t > index;

   select( index, selector );
218

219
220
221
222
223
224
   if( !index.empty() ) {
      WALBERLA_ASSERT_LESS( index[0], object_.size() );
   }

   return index.size();
}
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

//**********************************************************************************************************************
/*!
*   This function can be used to retrieve the one object whose attributes match with "selector". Depending on "selector"
*   and the actual implementation of the function "select", no object, one object, or multiple objects may be found. If
*   only one object is found, it is returned via the parameter "object". If multiple objects are found, the first object
*   whose attributes match is returned via "object". In any case, the number of objects that match with "selector" is
*   returned by this function - and only if the return value is equal to '1' exactly one object was found and stored in
*   "object".
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
size_t SelectableObject<T,A,S>::get( T& object, const S& selector ) const {

   std::vector< size_t > index;

   select( index, selector );

Sebastian Eibl's avatar
Sebastian Eibl committed
243
   if( !index.empty() ) {
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
      WALBERLA_ASSERT_LESS( index[0], object_.size() );
      object = object_[ index[0] ];
   }

   return index.size();
}



//**********************************************************************************************************************
/*!
*   This function can be used to retrieve all objects whose attributes match with the selector "selector". The objects
*   are returned using the provided vector "objects" (this function may return none, exactly one, ore multiple objects
*   depending on "selector" and the actual implementation of the function "select").
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
void SelectableObject<T,A,S>::get( std::vector<T>& object, const S& selector ) const {

   std::vector< size_t > index;

   select( index, selector );

   for( size_t i = 0; i != index.size(); ++i ) {
      WALBERLA_ASSERT_LESS( index[i], object_.size() );
      object.push_back( object_[ index[i] ] );
   }
}



//**********************************************************************************************************************
/*!
*   This function can be used to retrieve the one object whose attributes match with "selector". Depending on "selector"
*   and the actual implementation of the function "select", no object, one object, or multiple objects may be found. If
*   only one object is found, it is returned via the parameter "object". If multiple objects are found, the first object
*   whose attributes match is returned via "object". In any case, the number of objects that match with "selector" is
*   returned by this function - and only if the return value is equal to '1' exactly one object was found and stored in
*   "object". Additionally, the corresponding identifier is also returned via the parameter "identifier".
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
size_t SelectableObject<T,A,S>::get( T& object, std::string& identifier, const S& selector ) const {

   std::vector< size_t > index;

   select( index, selector );

Sebastian Eibl's avatar
Sebastian Eibl committed
292
   if( !index.empty() ) {
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
      WALBERLA_ASSERT_LESS( index[0], object_.size() );
      object = object_[ index[0] ];
      identifier = identifier_[ index[0] ];
   }

   return index.size();
}



//**********************************************************************************************************************
/*!
*   This function can be used to retrieve all objects whose attributes match with the selector "selector". The objects
*   are returned using the provided vector "objects" (this function may return none, exactly one, ore multiple objects
*   depending on "selector" and the actual implementation of the function "select"). Additionally, for every object that
*   is found, its corresponding identifier is also returned via "identifier".
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
void SelectableObject<T,A,S>::get( std::vector<T>& object, std::vector< std::string >& identifier, const S& selector ) const {

   std::vector< size_t > index;

   select( index, selector );

   for( size_t i = 0; i != index.size(); ++i ) {
      WALBERLA_ASSERT_LESS( index[i], object_.size() );
      object.push_back( object_[ index[i] ] );
      identifier.push_back( identifier_[ index[i] ] );
   }
}



//**********************************************************************************************************************
/*!
*   This function can be used to retrieve a pointer (!) to the object whose attributes match with "selector". Depending
*   on "selector" and the actual implementation of the function "select", no object, one object, or multiple objects may
*   be found. If no objects or multiple objects are found, NULL is returned.
*   Attention: The pointer may get invalidated by subsequent calls to the member function "add".
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
const T* SelectableObject<T,A,S>::getUnique( const S& selector ) const {

   std::vector< size_t > index;

   select( index, selector );

   if( index.size() == 1 ) {
      WALBERLA_ASSERT_LESS( index[0], object_.size() );
      return &(object_[ index[0] ]);
   }

Sebastian Eibl's avatar
Sebastian Eibl committed
347
   return nullptr;
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
}


//**********************************************************************************************************************
/*!
*   Non-const version of getUnique().
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
T* SelectableObject<T,A,S>::getUnique( const S& selector ) {

   const SelectableObject<T,A,S>& const_this = *this;
   return const_cast<T*>( const_this.getUnique( selector ) );
}



//**********************************************************************************************************************
/*!
*   This function is similar to getUnique(const S &selector) but takes an additional output parameter
*   identifier, which is set to the string identifier of the selected object, if a non-zero pointer is
*   returned, otherwise the identifier string is not changed
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
const T* SelectableObject<T,A,S>::getUnique( const S& selector, std::string & identifier ) const {

   std::vector< size_t > index;

   select( index, selector );

   if( index.size() == 1 ) {
      WALBERLA_ASSERT_LESS( index[0], object_.size() );
      identifier = identifier_[ index[0] ];
      return &(object_[ index[0] ]);
   }

Sebastian Eibl's avatar
Sebastian Eibl committed
385
   return nullptr;
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
}


//**********************************************************************************************************************
/*!
*   Non-const version of getUnique(const S&, std::string& ).
*/
//**********************************************************************************************************************
template< typename T, typename A, typename S >
T* SelectableObject<T,A,S>::getUnique( const S& selector, std::string & identifier ) {

   const SelectableObject<T,A,S>& const_this = *this;
   return const_cast<T*>( const_this.getUnique( selector,identifier ) );
}





template< typename T, typename A, typename S >
void SelectableObject<T,A,S>::toStream( std::ostream& os ) const {

   WALBERLA_ASSERT_EQUAL( identifier_.size(), attributes_.size() );

   os << "[ ";

   for( size_t i = 0; i != identifier_.size(); ++i )
      os << "( " << identifier_[i] << " : " << attributes_[i] << ( ( i+1 == identifier_.size() ) ? " ) " : " ), " );

   os << "]";
}



template< typename T, typename A, typename S >
inline std::string SelectableObject<T,A,S>::toString() const {

   std::ostringstream oss;
   toStream( oss );

   return oss.str();
}



//////////////////////
// Global Functions //
//////////////////////



template< typename T, typename A, typename S >
inline std::ostream& operator<<( std::ostream& os, const SelectableObject<T,A,S>& selectableObject ) {

   selectableObject.toStream( os );
   return os;
}



} // namespace selectable
} // namespace walberla