NCBI C++ ToolKit
object_list.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: object_list.cpp 39100 2017-07-28 16:42:32Z katargir $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
36 
37 template<class T> class CObjectVector
38  : public ncbi::CObject, public std::vector<T>
39 {
40 public:
41  typedef std::vector<T> TVectorBase;
42 
44 
45  CObjectVector( int _Count ) : TVectorBase( _Count ) {}
46 
47  CObjectVector( int _Count, const T& _Val ) : TVectorBase( _Count, _Val ) {}
48 };
49 
52 
53 const vector<int>& CObjectList::_CColumn::IntVector() const
54 {
55  _ASSERT( m_Type == eInteger );
56 
57  return *dynamic_cast<const CObjectVector<int>*>( m_ValuesRef.GetPointer() );
58 }
59 
61 {
62  _ASSERT( m_Type == eInteger );
63 
64  return *dynamic_cast<CObjectVector<int>*>( m_ValuesRef.GetPointer() );
65 }
66 
68 {
69  _ASSERT( m_Type == eInteger );
70 
71  if( m_ValuesRef.IsNull() ){
72  m_ValuesRef.Reset( new CObjectVector<int>( size ) );
73  } else {
74  dynamic_cast<CObjectVector<int>*>( m_ValuesRef.GetPointer() )->resize( size );
75  }
76 }
77 
78 const vector<double>& CObjectList::_CColumn::DoubleVector() const
79 {
80  _ASSERT( m_Type == eDouble );
81 
82  return *dynamic_cast<const CObjectVector<double>*>( m_ValuesRef.GetPointer() );
83 }
84 
86 {
87  _ASSERT( m_Type == eDouble );
88 
89  return *dynamic_cast<CObjectVector<double>*>( m_ValuesRef.GetPointer() );
90 }
91 
93 {
94  _ASSERT( m_Type == eDouble );
95 
96  if( m_ValuesRef.IsNull() ){
97  m_ValuesRef.Reset( new CObjectVector<double>( size ) );
98  } else {
99  dynamic_cast<CObjectVector<double>*>( m_ValuesRef.GetPointer() )->resize( size );
100  }
101 }
102 
103 const vector<string>& CObjectList::_CColumn::StringVector() const
104 {
105  _ASSERT( m_Type == eString );
106 
107  return *dynamic_cast<const CObjectVector<string>*>( m_ValuesRef.GetPointer() );
108 }
109 
111 {
112  _ASSERT( m_Type == eString );
113 
114  return *dynamic_cast<CObjectVector<string>*>( m_ValuesRef.GetPointer() );
115 }
116 
118 {
119  _ASSERT( m_Type == eString );
120 
121  if( m_ValuesRef.IsNull() ){
122  m_ValuesRef.Reset( new CObjectVector<string>( size ) );
123  } else {
124  dynamic_cast<CObjectVector<string>*>( m_ValuesRef.GetPointer() )->resize( size );
125  }
126 }
127 
128 /*
129 vector< CRef<CObject> >& CObjectList::_CColumn::GetVectorObject()
130 {
131  _ASSERT( m_Type == eObject );
132 
133  return *dynamic_cast<CObjectVector< CRef<CObject> >*>( m_ValuesRef.GetPointer() );
134 }
135 
136 const vector< CRef<CObject> >& CObjectList::_CColumn::GetVectorObject() const
137 {
138  _ASSERT( m_Type == eObject );
139 
140  return *dynamic_cast<const CObjectVector< CRef<CObject> >*>( m_ValuesRef.GetPointer() );
141 }
142 */
143 
144 ///////////////////////////////////////////////////////////////////////////////
145 /// CObjectList
146 
147 void CObjectList::x_AssertValidLabel( int col ) const
148 {
149  if( col < 0 || col >= GetNumObjectLabels() ){
150  _ASSERT(false);
151  NCBI_THROW( CObjectListException, eUnknown, "Invalid column index" );
152  }
153 }
154 
155 void CObjectList::x_AssertValidColumn( int col ) const
156 {
157  if( col < 0 || col >= GetNumColumns() ){
158  _ASSERT(false);
159  NCBI_THROW( CObjectListException, eUnknown, "Invalid column index" );
160  }
161 }
162 
164 {
165  if( row < 0 || row >= GetNumRows() ){
166  _ASSERT(false);
167  NCBI_THROW( CObjectListException, eUnknown, "Invalid row index" );
168  }
169 }
170 
171 void CObjectList::x_AssertValid( int col, int row ) const
172 {
173  x_AssertValidColumn( col );
175 }
176 
178  : m_StartIx( -1 )
179 {
180  x_Init();
181 }
182 
183 
185  : m_Objects( ob_list.m_Objects )
186  , m_Scopes( ob_list.m_Scopes )
187  , m_ExtraColName( ob_list.m_ExtraColName )
188  , m_ExtraColType( ob_list.m_ExtraColType )
189  , m_StartIx( ob_list.m_StartIx )
190 {
191  // columns are deep copied
192 
193  ITERATE( TColumns, it, ob_list.m_Columns ){
194  _CColumn new_col(it->m_Type, it->m_Name);
195  switch (it->m_Type) {
196  case eString:
197  new_col.StringVector((int)it->StringVector().size());
198  new_col.StringVector() = it->StringVector();
199  break;
200 
201  case eInteger:
202  new_col.IntVector((int)it->IntVector().size());
203  new_col.IntVector() = it->IntVector();
204  break;
205 
206  case eDouble:
207  new_col.DoubleVector((int)it->DoubleVector().size());
208  new_col.DoubleVector() = it->DoubleVector();
209  break;
210 
211  default:
212  _ASSERT(false);
213  NCBI_THROW( CObjectListException, eUnknown, "Invalid column type" );
214  }
215  m_Columns.push_back( new_col );
216  }
217 }
218 
220 {
221  //x_Clear();
222 }
223 
225 {
226  ClearColumns();
227  ClearRows();
228 
230  ClearColStartIx();
231 }
232 
233 
234 /// initializers
236 {
237  x_Clear();
238 
239  AddObjectLabel( "Label", CLabel::eContent );
241  AddObjectLabel( "Subtype", CLabel::eUserSubtype );
242  AddObjectLabel( "NCBI Type", CLabel::eType );
243  AddObjectLabel( "Description", CLabel::eDescription );
244 }
245 
247 {
248  x_Init();
249 
250  size_t num_objects = objects.size();
251  m_Objects.resize( num_objects );
252  m_Scopes.resize( num_objects );
253 
254  for( size_t i = 0; i < num_objects; i++ ) {
256  m_Objects[i].Reset( const_cast<CObject*>(sc.object.GetPointer()) );
257  m_Scopes[i].Reset( const_cast<CScope*>(sc.scope.GetPointer()) );
258  }
259 }
260 
262 {
263  x_Clear();
264 }
265 
267 {
268  m_Scopes.clear();
269  m_Objects.clear();
270 
272  _CColumn& column = *it;
273 
274  switch( column.m_Type ){
275  case eString:
276  column.StringVector().clear();
277  break;
278 
279  case eInteger:
280  column.IntVector().clear();
281  break;
282 
283  case eDouble:
284  column.DoubleVector().clear();
285  break;
286 
287  default:
288  _ASSERT(false);
289  NCBI_THROW( CObjectListException, eUnknown, "Invalid column type" );
290  }
291  }
292 
293 }
294 
295 template<class C> void AppendContainer( C& c1, const C& c2 )
296 {
297  c1.insert( c1.end(), c2.begin(), c2.end() );
298 }
299 
300 
301 void CObjectList::Append( const CObjectList& list )
302 {
303  _ASSERT( m_Columns.size() == list.m_Columns.size() );
304 
307 
308  for( size_t i = 0; i < m_Columns.size(); i++) {
309  _CColumn& col_1 = m_Columns[i];
310  const _CColumn& col_2 = list.m_Columns[i];
311 
312  _ASSERT( col_1.m_Type == col_2.m_Type );
313 
314  switch( col_1.m_Type ){
315  case eString:
316  AppendContainer( col_1.StringVector(), col_2.StringVector() );
317  break;
318  case eInteger:
319  AppendContainer( col_1.IntVector(), col_2.IntVector() );
320  break;
321  case eDouble:
322  AppendContainer( col_1.DoubleVector(), col_2.DoubleVector() );
323  break;
324  default:
325  _ASSERT( false ); // not supported
326  }
327  }
328 }
329 
330 int CObjectList::AddColumn( EColumnType type, const string& name, int col )
331 {
332  if( col != -1 ){
333  x_AssertValidColumn( col );
334  }
335 
336  int size = GetNumRows();
337 
338  _CColumn column( type, name );
339 
340  switch( type ){
341  case eString:
342  column.StringVector( size );
343  break;
344 
345  case eInteger:
346  column.IntVector( size );
347  break;
348 
349  case eDouble:
350  column.DoubleVector( size );
351  break;
352 
353  default:
354  _ASSERT(false);
355  NCBI_THROW( CObjectListException, eUnknown, "Invalid column type" );
356  }
357 
358  if( col == -1 ){
359  m_Columns.push_back( column );
360  return (int)m_Columns.size() - 1;
361 
362  } else {
363  m_Columns.insert( m_Columns.begin() + col, column );
364  return col;
365  }
366 }
367 
368 
369 void CObjectList::DeleteColumn( int i_col )
370 {
371  //bool valid_index = i_col > 1 && i_col < GetColumnsCount();
372  //_VERIFY(valid_index);
373  _ASSERT(false); // not implemented
374 }
375 
376 
377 int CObjectList::AddRow( CObject* obj, CScope* scope )
378 {
379  m_Objects.emplace_back(obj);
380  m_Scopes.emplace_back(scope);
381 
382  int num_rows = (int)m_Objects.size();
384  _CColumn& column = *it;
385 
386  switch( column.m_Type ){
387  case eString:
388  column.StringVector( num_rows );
389  break;
390 
391  case eInteger:
392  column.IntVector( num_rows );
393  break;
394 
395  case eDouble:
396  column.DoubleVector( num_rows );
397  break;
398 
399  default:
400  _ASSERT(false);
401  NCBI_THROW( CObjectListException, eUnknown, "Invalid column type" );
402  }
403  }
404 
405  return num_rows - 1;
406 }
407 
408 
410 {
411  return (int)m_Columns.size();
412 }
413 
414 
416 {
417  x_AssertValidColumn( col );
418 
419  const _CColumn& column = m_Columns[col];
420  switch( column.m_Type ){
421  case eString:
422  case eInteger:
423  case eDouble:
424  return column.m_Type;
425 
426  default:
427  break;
428  }
429 
430  _ASSERT(false);
431  return eInvalid;
432 }
433 
434 
435 const string& CObjectList::GetColumnName( int col ) const
436 {
437  x_AssertValidColumn( col );
438 
439  return m_Columns[col].m_Name;
440 }
441 
442 
444 {
445  return (int)m_Objects.size();
446 }
447 
448 
450 {
452 
453  return m_Objects[row].GetPointer();
454 }
455 
456 
458 {
460 
461  m_Objects[row].Reset( &obj );
462 }
463 
464 
465 const CObject* CObjectList::GetObject( int row ) const
466 {
468 
469  return m_Objects[row].GetPointer();
470 }
471 
472 
474 {
476 
477  return m_Scopes[row].GetPointer();
478 }
479 
480 
481 const CScope* CObjectList::GetScope( int row ) const
482 {
484 
485  return m_Scopes[row].GetPointer();
486 }
487 
488 
490 {
492 
493  m_Scopes[row].Reset( &sc );
494 }
495 
496 
497 const string& CObjectList::GetString( int col, int row ) const
498 {
499  x_AssertValid( col, row );
500 
501  return m_Columns[col].StringVector()[row];
502 }
503 
504 
505 void CObjectList::SetString( int col, int row, const string& val )
506 {
507  x_AssertValid( col, row );
508 
509  m_Columns[col].StringVector()[row] = val;
510 }
511 
512 
513 int CObjectList::GetInteger( int col, int row ) const
514 {
515  x_AssertValid( col, row );
516 
517  return m_Columns[col].IntVector()[row];
518 }
519 
520 
521 void CObjectList::SetInteger( int col, int row, int val )
522 {
523  x_AssertValid( col, row );
524 
525  m_Columns[col].IntVector()[row] = val;
526 }
527 
528 
529 double CObjectList::GetDouble(int col, int row) const
530 {
531  x_AssertValid( col, row );
532 
533  return m_Columns[col].DoubleVector()[row];
534 }
535 
536 
537 void CObjectList::SetDouble(int col, int row, double val)
538 {
539  x_AssertValid( col, row );
540 
541  m_Columns[col].DoubleVector()[row] = val;
542 }
543 
544 string CObjectList::GetObjectLabelName( int col ) const
545 {
546  x_AssertValidLabel( col );
547 
548  return m_ExtraColName[col];
549 }
550 
552 {
553  x_AssertValidLabel( col );
554 
555  return m_ExtraColType[col];
556 }
557 
558 
559 void CObjectList::SetObjectLabel( int col, const string& name, CLabel::ELabelType type )
560 {
561  if( col < 0 ){
562  col = GetNumObjectLabels();
563  }
564 
565  m_ExtraColName.insert( m_ExtraColName.begin() + col, name );
566  m_ExtraColType.insert( m_ExtraColType.begin() + col, type );
567 }
568 
570 {
571  if( col < 0 ){
572  col = GetNumObjectLabels() -1;
573  }
574 
575  m_ExtraColName.erase( m_ExtraColName.begin() + col );
576  m_ExtraColType.erase( m_ExtraColType.begin() + col );
577 }
578 
CObjectListException.
Definition: object_list.hpp:50
CObjectList Data structure representing a list of CObjects with associated Scopes and other optional ...
Definition: object_list.hpp:63
void RemoveObjectLabel(int col)
void x_Clear()
void x_AssertValidLabel(int col) const
CObjectList.
void SetScope(int row, objects::CScope &sc)
double GetDouble(int col, int row) const
void SetDouble(int col, int row, double val)
const string & GetColumnName(int col) const
virtual ~CObjectList()
int AddRow(CObject *obj, objects::CScope *scope)
EColumnType GetColumnType(int col) const
void AddObjectLabel(const string &name, CLabel::ELabelType type)
void x_AssertValidRow(int row) const
vector< CRef< objects::CScope > > m_Scopes
Objects column.
void Init(TConstScopedObjects &objects)
initializers
void x_AssertValidColumn(int col) const
CLabel::ELabelType GetObjectLabelType(int col) const
void SetString(int col, int row, const string &val)
void x_Init()
removes all rows and non-standard columns
const string & GetString(int col, int row) const
void ClearObjectLabels()
void ClearColumns()
removes all data rows (i.e. objects)
objects::CScope * GetScope(int row)
vector< CRef< CObject > > m_Objects
all columns must be of the same size
string GetObjectLabelName(int col) const
void x_AssertValid(int col, int row) const
void SetInteger(int col, int row, int val)
void Append(const CObjectList &list)
adds rows from the given list, the lists must have identical sets of columns
void ClearRows()
vector< string > m_ExtraColName
all columns
int GetNumObjectLabels() const
access to a set of extra columns
int AddColumn(EColumnType type, const string &name, int col=-1)
void ClearColStartIx()
int GetNumRows() const
vector< _CColumn > TColumns
Scopes column, may contain NULLs.
vector< CLabel::ELabelType > m_ExtraColType
int GetInteger(int col, int row) const
TColumns m_Columns
void DeleteColumn(int col)
void SetObjectLabel(int col, const string &name, CLabel::ELabelType type)
int GetNumColumns() const
access to columns (attribute, indexes)
CObjectVector(int _Count)
Definition: object_list.cpp:45
CObjectVector(int _Count, const T &_Val)
Definition: object_list.cpp:47
std::vector< T > TVectorBase
Definition: object_list.cpp:41
CObject –.
Definition: ncbiobj.hpp:180
CScope –.
Definition: scope.hpp:92
#define C(s)
Definition: common.h:231
#define T(s)
Definition: common.h:230
static const char * column
Definition: stats.c:23
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
CRef< objects::CScope > scope
Definition: objects.hpp:53
CConstRef< CObject > object
Definition: objects.hpp:52
ELabelType
Definition: label.hpp:60
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
@ eType
Definition: label.hpp:65
@ eUserSubtype
Definition: label.hpp:64
@ eContent
Definition: label.hpp:62
@ eDescription
Definition: label.hpp:68
@ eUserType
Definition: label.hpp:63
TObjectType * GetPointer(void) const THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:1684
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
TObject & SetObject(void)
Assign a value to Object data member.
const TObject & GetObject(void) const
Get the Object member data.
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
void resize(vector< SMethodDef > &container)
USING_SCOPE(objects)
void AppendContainer(C &c1, const C &c2)
#define row(bind, expected)
Definition: string_bind.c:73
const vector< int > & IntVector() const
Definition: object_list.cpp:53
const vector< string > & StringVector() const
const vector< double > & DoubleVector() const
Definition: object_list.cpp:78
CRef< CObject > m_ValuesRef
actually it is a reference to wxArrayString, wxArrayInt, or wxArrayDouble
Definition: object_list.hpp:86
EColumnType m_Type
actually column can be of types eString, eInteger, eDouble
Definition: object_list.hpp:83
Definition: type.c:6
#define _ASSERT
Modified on Fri May 24 14:58:19 2024 by modify_doxy.py rev. 669887