Modeler Extensions Framework
DataHelper.h
Go to the documentation of this file.
1 //============================================================================
2 // Licensed Materials - Property of IBM
3 //
4 // IBM SPSS Products: Modeler
5 //
6 // (C) Copyright IBM Corp. 1994, 2014
7 //
8 // US Government Users Restricted Rights - Use, duplication or disclosure restricted
9 // by GSA ADP Schedule Contract with IBM Corp.
10 //============================================================================
11 /*
12  Modeler Extensions Framework C++ Helpers (DataHelper.h)
13 */
14 
15 #ifndef DATA_HELPER_H
16 #define DATA_HELPER_H
17 
18 #include "clemext.h"
19 
20 #include "BufferHelper.h"
21 
22 #include <string.h>
23 #include <string>
24 #include <vector>
25 
26 namespace ClemextCppHelper {
27 
30 class DataRecord {
31 
32 public:
35  struct DataValue {
36 
37  enum {
42  };
43  unsigned typeCode_;
44  union {
48  } val_;
49  bool isNull_;
50 
54  typeCode_ = CLEMEXT_TYPE_UNKNOWN;
55  isNull_ = true;
56  }
57 
61  void clearValue() {
62  if (typeCode_ == CLEMEXT_TYPE_STRING)
63  delete val_.stringValue_;
64  typeCode_ = CLEMEXT_TYPE_UNKNOWN;
65  isNull_ = true;
66  }
67 
73  void setValue(const CLEMEXTInteger &v, bool isNull) {
74  if (typeCode_ == CLEMEXT_TYPE_STRING)
75  delete val_.stringValue_;
76  typeCode_ = CLEMEXT_TYPE_INTEGER;
77  isNull_ = isNull;
78  val_.integerValue_ = v;
79  }
80 
86  void setValue(const CLEMEXTReal &v, bool isNull) {
87  if (typeCode_ == CLEMEXT_TYPE_STRING)
88  delete val_.stringValue_;
89  typeCode_ = CLEMEXT_TYPE_REAL;
90  isNull_ = isNull;
91  val_.realValue_ = v;
92  }
93 
99  void setValue(const char *v, bool isNull) {
100  isNull_ = isNull;
101  if (typeCode_ != CLEMEXT_TYPE_STRING) {
102  val_.stringValue_ = new BufferHelper;
103  }
104 
105  if (!isNull) {
106  size_t reqd_sz = strlen(v)+1;
107  if (val_.stringValue_->operator size_t() < reqd_sz) {
108  val_.stringValue_->resize(reqd_sz);
109  }
110  strcpy(val_.stringValue_->operator char *(),v);
111  }
112  typeCode_ = CLEMEXT_TYPE_STRING;
113  }
114  };
115 
118  std::vector<DataValue> values_;
119 
124  void clear() {
125  std::vector<DataValue>::iterator it = values_.begin();
126  while(it != values_.end()) {
127  (*it).clearValue();
128  it++;
129  }
130  }
131 
136  size_t size() {
137  return values_.size();
138  }
139 
145  clear();
146  }
147 };
148 
149 
154 class DataHelper {
155 
157  CLEMEXTIterator *iter_;
158 
160  DataRecord record_;
161 
163  bool passThrough_;
164 
166  int retrieved_;
167 
169  void *value_;
170  size_t value_length_;
171 
172 public:
173 
176 
177  CLEMEXTStatus status_;
178  CLEMEXTErrorCode errorCode_;
179  std::string apiFnName_;
180 
181  public:
182  DataHelperException(CLEMEXTStatus status, CLEMEXTErrorCode errorCode, const char *apiFnName) {
183  status_ = status;
184  errorCode_ = errorCode;
185  apiFnName_ = apiFnName_;
186  }
187 
189  status_ = 0;
190  errorCode_ = 0;
191  apiFnName_ = "";
192  }
193 
195  return status_;
196  }
197 
199  return errorCode_;
200  }
201 
202  std::string getApiFnName() {
203  return apiFnName_;
204  }
205 
206  };
207 
212  DataHelper(CLEMEXTIterator *iter, bool passThrough=true);
213 
215  ~DataHelper();
216 
218  void clear();
219 
223  bool nextRecord();
224 
231  bool rewind();
232 
239  template<class T> void getInputValue(size_t index, T &val, bool &isNull) {
240  CLEMEXTErrorCode errorCode;
241 
242  CLEMEXTStatus status = iter_->getRecordValue(iter_->target,index,&value_,&value_length_,&errorCode);
243  if (status != CLEMEXT_OK) {
244  throw DataHelperException(status,errorCode,"getRecordValue");
245  }
246  if (value_ != NULL) {
247  if (value_length_ != sizeof(T)) {
248  throw DataHelperException();
249  }
250  val = *(T *)value_;
251  isNull = false;
252  } else {
253  isNull = true;
254  }
255  }
256 
263  void getInputStringValue(size_t index, const char *&val, bool &isNull);
264 
271  template<class T> void setOutputValue(size_t index, const T &val, bool isNull) {
272  if (record_.values_.size() <= index) {
273  record_.values_.resize(index+1,DataRecord::DataValue());
274  }
275 
276  record_.values_[index].setValue(val,isNull);
277  }
278 
285  void setOutputStringValue(size_t index, const char *val, bool isNull);
286 
300  CLEMEXTStatus getRecordValue(size_t index, void **value, size_t *value_len, CLEMEXTErrorCode *errorCode);
301 
302 
308  int getNumberRecordsRetrieved() { return retrieved_; };
309 };
310 
311 } // namespace CLEMEXTCppHelper
312 
313 #endif
union ClemextCppHelper::DataRecord::DataValue::@1 val_
void setValue(const char *v, bool isNull)
Definition: DataHelper.h:99
double CLEMEXTReal
Definition: clemext.h:111
#define CLEMEXT_OK
Definition: clemext.h:44
long long CLEMEXTInteger
Definition: clemext.h:116
std::vector< DataValue > values_
Definition: DataHelper.h:118
void getInputStringValue(size_t index, const char *&val, bool &isNull)
void setOutputStringValue(size_t index, const char *val, bool isNull)
CLEMEXTStatus getRecordValue(size_t index, void **value, size_t *value_len, CLEMEXTErrorCode *errorCode)
int CLEMEXTErrorCode
Definition: clemext.h:75
void setValue(const CLEMEXTInteger &v, bool isNull)
Definition: DataHelper.h:73
void setOutputValue(size_t index, const T &val, bool isNull)
Definition: DataHelper.h:271
void getInputValue(size_t index, T &val, bool &isNull)
Definition: DataHelper.h:239
void setValue(const CLEMEXTReal &v, bool isNull)
Definition: DataHelper.h:86
DataHelper(CLEMEXTIterator *iter, bool passThrough=true)
DataHelperException(CLEMEXTStatus status, CLEMEXTErrorCode errorCode, const char *apiFnName)
Definition: DataHelper.h:182
unsigned CLEMEXTStatus
Definition: clemext.h:42