StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StRecord.cxx
1 /*
2  * StRecord.cpp
3  *
4  * Created on Aug 20, 2007
5  *
6  * Author: Zhengqiang Liang (Wayne State University)
7  * Valeri Fine (Brookhaven National Laboratory)
8  * Jerome Lauret (Brookhaven National Laboratory)
9  * Stephen Tramer (Tech-X Corp.)
10  *
11  *
12  * This file is part of the UCM project funded under an SBIR
13  * Copyright (c) 2007-2008 STAR Collaboration - Brookhaven National Laboratory
14  *
15  * @(#)cpp/api:$Id: StRecord.cxx,v 1.2 2010/03/30 20:05:36 fine Exp $
16  *
17  *
18  *
19  * This is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * STAR Scheduler is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with STAR Scheduler; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32  */
33 
34 #include <string>
35 #include <iostream>
36 
37 #include "StDbFieldI.h"
38 #include "StRecord.h"
39 #include "StDataException.h"
40 
41 using namespace TxLogging;
42 using namespace std;
43 //________________________________________________________________________
44 StRecord::StRecord() : fFields()
45 {
46 }
47 
48 //________________________________________________________________________
49 StRecord::StRecord(const FieldList &fields) : fFields(fields) {
50  // If the length is 0, the record is already in
51  // an unusable state so throwing an exception is kosher.
52  if ( fields.empty() ) {
53  throw StDataException("Attempted to create an empty record",
54  StDataException::RECORD);
55  }
56  // TODO: Deep copy?
57 }
58 
59 
60 //________________________________________________________________________
61 StRecord::StRecord(const StRecord &r) : fFields() {
62  copy(r);
63 }
64 
65 //________________________________________________________________________
67  if (!fFields.empty()) { }
68 }
69 
70 
71 //________________________________________________________________________
73  if ( this != &r ) {
74  copy(r);
75  }
76  return *this;
77 }
78 
79 
80 //________________________________________________________________________
81 void StRecord::copy(const StRecord& r) {
82  if (!fFields.empty()) clearList();
83 
84  // Deep copy field list
85  for ( FieldList::const_iterator iter = r.getFields().begin();
86  iter != r.getFields().end();
87  iter++ ) {
88  fFields.push_back(new StDbFieldI(**iter));
89  }
90 }
91 
92 
93 //________________________________________________________________________
94 void StRecord::clearList() {
95  for (FieldList::iterator iter = fFields.begin();
96  iter != fFields.end();
97  iter++) {
98  delete *iter;
99  }
100  fFields.clear();
101 }
102 
103 
104 //________________________________________________________________________
105 const char*StRecord::toString() const {
106  static std::string recordStr;
107  recordStr="";
108 
109  for (FieldList::const_iterator iter = fFields.begin();
110  iter != fFields.end();
111  ++iter) {
112  recordStr += string("|") + (*iter)->fieldAsString();
113  }
114  recordStr += "|";
115  return recordStr.c_str() ;
116 }
117 
118 //________________________________________________________________________
119 const FieldList& StRecord::getFields() const {
120  return fFields;
121 }
122 
123 //________________________________________________________________________
125  return fFields;
126 }
127 
128 //________________________________________________________________________
129 StDbFieldI* StRecord::getField(const char *name) const
130 {
131  StDbFieldI* field = 0;
132  for (FieldList::const_iterator iter = fFields.begin();
133  !field && (iter != fFields.end()) ;
134  iter++) {
135  if (string(name) == (*iter)->getName()) field = *iter;
136  }
137  return field;
138 }
139 
140 
141 //________________________________________________________________________
142 StDbFieldI* StRecord::getField(unsigned int col) const
143 {
144  return
145  ( col > 0 && col <= fFields.size() ) ? fFields[col-1] : 0;
146 }
147 //________________________________________________________________________
148 const RecordList &StRecord::getRecords() const{
149  return fRecords;
150 }
151 
152 //________________________________________________________________________
154  return fRecords;
155 }
156 
157 //________________________________________________________________________
158 void StRecord::removeField(const char *name) {
159  for (FieldList::iterator iter = fFields.begin();
160  iter != fFields.end();
161  iter++) {
162  if (string(name) == (*iter)->getName()) {
163  fFields.erase(iter);
164  break;
165  }
166  }
167 }
168 
169 
170 //________________________________________________________________________
171 void StRecord::removeField(int col)
172 {
173  int i;
174  FieldList::iterator iter;
175 
176  for (iter = fFields.begin(), i=0; iter != fFields.end(); i++, iter++)
177  {
178  if ( i == col ) {
179  fFields.erase(iter);
180  break;
181  }
182  }
183 }
184 
185 //________________________________________________________________________
186 void StRecord::printHeader() const
187 {
188  FieldList::const_iterator it = fFields.begin();
189  for (; it != fFields.end(); ++it) {
190  cout << (*it)->getName() << " | " ;
191  }
192  cout << endl;
193  for (; it != fFields.end(); ++it) {
194  cout << (*it)->getTypeAsString() << " | " ;
195  }
196  cout << endl;
197 }
198 
199 //________________________________________________________________________
200 void StRecord::print() const
201 {
202  FieldList::const_iterator it = fFields.begin();
203  for (; it != fFields.end(); ++it) {
204  cout << (*it)->getValueAsString() << " | " ;
205  }
206  cout << endl;
207 }
StDbFieldI * getField(const char *name) const
Definition: StRecord.cxx:129
TxLogging::RecordList & getRecords()
Definition: StRecord.cxx:153
const StRecord & operator=(const StRecord &)
Copy constructor.
Definition: StRecord.cxx:72
void removeField(const char *name)
Definition: StRecord.cxx:158
virtual ~StRecord()
Assignment.
Definition: StRecord.cxx:66
const char * toString() const
Deconstructor.
Definition: StRecord.cxx:105
TxLogging::FieldList & getFields()
Definition: StRecord.cxx:124