C++ API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

error_handling.h
1 
2 /*******************************************************************************
3 * Copyright 2014-2020 Intel Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *******************************************************************************/
17 
18 /*
19 //++
20 // Handling errors in Intel(R) DAAL.
21 //--
22 */
23 
24 #ifndef __ERROR_HANDLING__
25 #define __ERROR_HANDLING__
26 
27 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
28 #include <exception>
29 #endif
30 
31 #include "daal_string.h"
32 #include "error_indexes.h"
33 #include "error_id.h"
34 #include "services/collection.h"
35 
36 namespace daal
37 {
38 namespace services
39 {
40 namespace interface1
41 {
46 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
47 
52 struct DAAL_EXPORT Exception : std::exception
53 {
54 public:
59  Exception(const char *description) : _description(description) {};
60 
65  virtual const char *what() const throw() { return _description.c_str(); };
66 
67 #ifndef cxx11
68 
71  virtual ~Exception() throw() {}
72 #endif
73 
78  static Exception getException(const String &description)
79  {
80  String d(description);
81  return Exception(d.c_str());
82  }
83 
88  static Exception getException(const char *description)
89  {
90  return Exception(description);
91  }
92 
93 private:
94  String _description;
95 };
96 
97 #endif
98 
99 } // namespace interface1
100 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
101 using interface1::Exception;
102 #endif
103 
104 namespace interface1
105 {
106 
111 class DAAL_EXPORT Error
112 {
113 public:
114  DAAL_NEW_DELETE();
115 
120  Error(const ErrorID id = NoErrorMessageFound);
121 
126  Error(const Error &e);
127 
129  ~Error();
130 
135  ErrorID id() const { return _id; }
136 
141  void setId(ErrorID id)
142  {
143  _id = id;
144  }
145 
150  const char *description() const;
151 
158  Error &addIntDetail(ErrorDetailID id, int value);
159 
166  Error &addDoubleDetail(ErrorDetailID id, double value);
167 
174  Error &addStringDetail(ErrorDetailID id, const String &value);
175 
180  const ErrorDetail *details() const { return _details; }
181 
186  static SharedPtr<Error> create(ErrorID id);
187 
188  static SharedPtr<Error> create(ErrorID id, ErrorDetailID det, int value);
189 
190  static SharedPtr<Error> create(ErrorID id, ErrorDetailID det, const String &value);
191 
192 protected:
199  Error &addDetail(ErrorDetail *detail);
200 
201 private:
202  ErrorID _id;
203  ErrorDetail *_details;
204 };
205 typedef SharedPtr<Error> ErrorPtr;
206 
211 class DAAL_EXPORT KernelErrorCollection : private Collection<SharedPtr<Error> >
212 {
213 public:
214  DAAL_NEW_DELETE();
215 
216  typedef Collection<SharedPtr<Error> > super;
217 
221  KernelErrorCollection() : _description(0) {}
222 
227  KernelErrorCollection(const KernelErrorCollection &other);
228 
234  Error &add(const ErrorID &id);
235 
240  void add(const ErrorPtr &e);
241 
246  void add(const services::SharedPtr<KernelErrorCollection> &e);
247 
252  void add(const KernelErrorCollection &e);
253 
258  bool isEmpty() const { return size() == 0; }
259 
264  size_t size() const;
265 
271  Error *at(size_t index);
272 
278  const Error *at(size_t index) const;
279 
285  Error *operator[](size_t index);
286 
292  const Error *operator[](size_t index) const;
293 
297  virtual ~KernelErrorCollection();
298 
303  const char *getDescription() const;
304 
305 private:
306  mutable char *_description;
307 };
308 typedef SharedPtr<KernelErrorCollection> KernelErrorCollectionPtr;
309 
314 class DAAL_EXPORT ErrorCollection
315 {
316 public:
317  DAAL_NEW_DELETE();
318 
322  ErrorCollection() : _errors(new KernelErrorCollection()), _canThrow(true){}
323 
328  ErrorCollection(const ErrorCollection &o) : _errors(o.getErrors()), _canThrow(o._canThrow) {}
329 
330 
335  explicit ErrorCollection(const KernelErrorCollection &errors) : _errors(new KernelErrorCollection(errors)), _canThrow(true) {}
336 
341  void add(const ErrorID &id)
342  {
343  _errors->add(id);
344 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
345  if(_canThrow)
346  throw Exception::getException(getDescription());
347 #endif
348  }
349 
354  void add(const ErrorPtr &e)
355  {
356  _errors->add(e);
357 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
358  if(_canThrow)
359  throw Exception::getException(getDescription());
360 #endif
361  }
362 
367  void add(const ErrorCollection &e)
368  {
369  if(!e.isEmpty())
370  {
371  _errors->add(e.getErrors());
372 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
373  if(_canThrow)
374  throw Exception::getException(getDescription());
375 #endif
376  }
377  }
378 
379 
384  void add(const KernelErrorCollectionPtr &e)
385  {
386  if(!e->isEmpty())
387  {
388  _errors->add(e);
389 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
390  if(_canThrow)
391  throw Exception::getException(getDescription());
392 #endif
393  }
394  }
395 
400  size_t size() const
401  {
402  return _errors->size();
403  }
404 
409  bool isEmpty() const
410  {
411  return _errors->isEmpty();
412  }
413 
417  virtual ~ErrorCollection() {}
418 
423  const KernelErrorCollectionPtr &getErrors() const
424  {
425  return _errors;
426  }
427 
432  const char *getDescription() const { return _errors->getDescription(); }
433 
438  bool canThrow() const
439  {
440  return _canThrow;
441  }
442 
448  bool setCanThrow(bool bOn)
449  {
450  bool bVal = _canThrow;
451  _canThrow = bOn;
452  return bVal;
453  }
454 
455 private:
456  KernelErrorCollectionPtr _errors;
457  bool _canThrow;
458 };
459 typedef SharedPtr<ErrorCollection> ErrorCollectionPtr;
467 class DAAL_EXPORT Status
468 {
469 public:
473  Status() : _impl(0){}
478  Status(ErrorID id);
483  Status(const ErrorPtr& e);
484 
488  Status(const Status& other);
489 
493  ~Status();
494 
499  bool ok() const { return !_impl; }
500 
505  operator bool() const { return ok(); }
506 
512  Status& add(ErrorID id);
513 
519  Status& add(const ErrorPtr& e);
520 
526  Status& add(const Status& other);
527 
533  Status& operator |=(const Status& other) { return add(other); }
534 
540  Status& operator =(const Status& other);
541 
546  const char* getDescription() const;
547 
552  void clear();
553 
557  DAAL_DEPRECATED Status(const KernelErrorCollection& e);
561  DAAL_DEPRECATED Status(const ErrorCollection& e);
562 
566  ErrorCollectionPtr getCollection() const;
567 
568 private:
569  void* _impl; //pointer to the collection of errors with reference counting
570 };
571 
572 inline const Status& throwIfPossible(const Status& s)
573 {
574 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
575  if(!s.ok())
576  throw services::Exception::getException(s.getDescription());
577 #endif
578  return s;
579 }
580 
581 } // namespace interface1
582 using interface1::Error;
583 using interface1::KernelErrorCollection;
584 using interface1::ErrorCollection;
585 using interface1::ErrorPtr;
586 using interface1::KernelErrorCollectionPtr;
587 using interface1::ErrorCollectionPtr;
588 using interface1::Status;
589 using interface1::throwIfPossible;
590 namespace internal {
591 inline Status checkForNullByteInjection(const char *begin, const char *const end)
592 {
593  for (; begin != end; ++begin)
594  {
595  if (*begin == '\0')
596  {
597  return Status(ErrorNullByteInjection);
598  }
599  }
600  return Status();
601 }
602 }
603 }
604 };
605 #endif
daal::services::interface1::Status
Class that holds the results of API calls. In case of API routine failure it contains the list of err...
Definition: error_handling.h:467
daal::services::interface1::Collection
Class that implements functionality of the Collection container.
Definition: collection.h:45
daal::services::interface1::KernelErrorCollection::isEmpty
bool isEmpty() const
Definition: error_handling.h:258
daal::services::interface1::KernelErrorCollection
Class that represents a kernel error collection (collection that cannot throw exceptions) ...
Definition: error_handling.h:211
daal::services::interface1::Error::details
const ErrorDetail * details() const
Definition: error_handling.h:180
daal::services::interface1::ErrorCollection::isEmpty
bool isEmpty() const
Definition: error_handling.h:409
daal::services::interface1::ErrorCollection::add
void add(const ErrorCollection &e)
Definition: error_handling.h:367
daal::services::interface1::ErrorCollection::add
void add(const KernelErrorCollectionPtr &e)
Definition: error_handling.h:384
daal::services::interface1::Exception::getException
static Exception getException(const String &description)
Definition: error_handling.h:78
daal::services::interface1::Error::setId
void setId(ErrorID id)
Definition: error_handling.h:141
daal::services::interface1::ErrorCollection::add
void add(const ErrorID &id)
Definition: error_handling.h:341
daal::services::interface1::ErrorCollection::canThrow
bool canThrow() const
Definition: error_handling.h:438
daal::services::interface1::Exception::what
virtual const char * what() const
Definition: error_handling.h:65
daal::services::interface1::ErrorCollection::~ErrorCollection
virtual ~ErrorCollection()
Definition: error_handling.h:417
daal::services::interface1::Status::Status
Status()
Definition: error_handling.h:473
daal::services::interface1::ErrorDetail
Base for error detail classes.
Definition: error_id.h:46
daal::services::ErrorNullByteInjection
Definition: error_indexes.h:394
daal::services::interface1::KernelErrorCollection::KernelErrorCollection
KernelErrorCollection()
Definition: error_handling.h:221
daal::services::interface1::String::c_str
const char * c_str() const
daal::services::interface1::Exception::getException
static Exception getException(const char *description)
Definition: error_handling.h:88
daal::services::interface1::Error::id
ErrorID id() const
Definition: error_handling.h:135
daal::services::interface1::SharedPtr< Error >
daal::services::interface1::ErrorCollection::getDescription
const char * getDescription() const
Definition: error_handling.h:432
daal::services::NoErrorMessageFound
Definition: error_indexes.h:418
daal::services::interface1::ErrorCollection::setCanThrow
bool setCanThrow(bool bOn)
Definition: error_handling.h:448
daal::services::interface1::ErrorCollection::size
size_t size() const
Definition: error_handling.h:400
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection()
Definition: error_handling.h:322
daal::services::interface1::Error
Class that represents an error.
Definition: error_handling.h:111
daal::services::interface1::Exception::Exception
Exception(const char *description)
Definition: error_handling.h:59
daal::services::interface1::ErrorCollection::add
void add(const ErrorPtr &e)
Definition: error_handling.h:354
daal::services::interface1::Exception::~Exception
virtual ~Exception()
Definition: error_handling.h:71
daal::services::interface1::ErrorCollection::getErrors
const KernelErrorCollectionPtr & getErrors() const
Definition: error_handling.h:423
daal::services::interface1::Exception
Class that represents an exception.
Definition: error_handling.h:52
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection(const ErrorCollection &o)
Definition: error_handling.h:328
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection(const KernelErrorCollection &errors)
Definition: error_handling.h:335
daal::services::interface1::String
Class that implements functionality of the string, an object that represents a sequence of characters...
Definition: daal_string.h:46
daal::algorithms::math::abs::value
Definition: abs_types.h:88
daal::services::interface1::Status::ok
bool ok() const
Definition: error_handling.h:499
daal::services::interface1::ErrorCollection
Class that represents an error collection.
Definition: error_handling.h:314
daal::services::ErrorID
ErrorID
Definition: error_indexes.h:68
daal::services::ErrorDetailID
ErrorDetailID
Definition: error_indexes.h:41

For more complete information about compiler optimizations, see our Optimization Notice.