C++ API Reference for Intel® Data Analytics Acceleration Library 2019 Update 4

error_handling.h
1 
2 /*******************************************************************************
3 * Copyright 2014-2019 Intel Corporation.
4 *
5 * This software and the related documents are Intel copyrighted materials, and
6 * your use of them is governed by the express license under which they were
7 * provided to you (License). Unless the License provides otherwise, you may not
8 * use, modify, copy, publish, distribute, disclose or transmit this software or
9 * the related documents without Intel's prior written permission.
10 *
11 * This software and the related documents are provided as is, with no express
12 * or implied warranties, other than those that are expressly stated in the
13 * License.
14 *******************************************************************************/
15 
16 /*
17 //++
18 // Handling errors in Intel(R) DAAL.
19 //--
20 */
21 
22 #ifndef __ERROR_HANDLING__
23 #define __ERROR_HANDLING__
24 
25 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
26 #include <exception>
27 #endif
28 
29 #include "daal_string.h"
30 #include "error_indexes.h"
31 #include "error_id.h"
32 #include "services/collection.h"
33 
34 namespace daal
35 {
36 namespace services
37 {
38 namespace interface1
39 {
44 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
45 
50 struct DAAL_EXPORT Exception : std::exception
51 {
52 public:
57  Exception(const char *description) : _description(description) {};
58 
63  virtual const char *what() const throw() { return _description.c_str(); };
64 
65 #ifndef cxx11
66 
69  virtual ~Exception() throw() {}
70 #endif
71 
76  static Exception getException(const String &description)
77  {
78  String d(description);
79  return Exception(d.c_str());
80  }
81 
86  static Exception getException(const char *description)
87  {
88  return Exception(description);
89  }
90 
91 private:
92  String _description;
93 };
94 
95 #endif
96 
97 } // namespace interface1
98 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
99 using interface1::Exception;
100 #endif
101 
102 namespace interface1
103 {
104 
109 class DAAL_EXPORT Error
110 {
111 public:
112  DAAL_NEW_DELETE();
113 
118  Error(const ErrorID id = NoErrorMessageFound);
119 
124  Error(const Error &e);
125 
127  ~Error();
128 
133  ErrorID id() const { return _id; }
134 
139  void setId(ErrorID id)
140  {
141  _id = id;
142  }
143 
148  const char *description() const;
149 
156  Error &addIntDetail(ErrorDetailID id, int value);
157 
164  Error &addDoubleDetail(ErrorDetailID id, double value);
165 
172  Error &addStringDetail(ErrorDetailID id, const String &value);
173 
178  const ErrorDetail *details() const { return _details; }
179 
184  static SharedPtr<Error> create(ErrorID id);
185 
186  static SharedPtr<Error> create(ErrorID id, ErrorDetailID det, int value);
187 
188  static SharedPtr<Error> create(ErrorID id, ErrorDetailID det, const String &value);
189 
190 protected:
197  Error &addDetail(ErrorDetail *detail);
198 
199 private:
200  ErrorID _id;
201  ErrorDetail *_details;
202 };
203 typedef SharedPtr<Error> ErrorPtr;
204 
209 class DAAL_EXPORT KernelErrorCollection : private Collection<SharedPtr<Error> >
210 {
211 public:
212  DAAL_NEW_DELETE();
213 
214  typedef Collection<SharedPtr<Error> > super;
215 
219  KernelErrorCollection() : _description(0) {}
220 
225  KernelErrorCollection(const KernelErrorCollection &other);
226 
232  Error &add(const ErrorID &id);
233 
238  void add(const ErrorPtr &e);
239 
244  void add(const services::SharedPtr<KernelErrorCollection> &e);
245 
250  void add(const KernelErrorCollection &e);
251 
256  bool isEmpty() const { return size() == 0; }
257 
262  size_t size() const;
263 
269  Error *at(size_t index);
270 
276  const Error *at(size_t index) const;
277 
283  Error *operator[](size_t index);
284 
290  const Error *operator[](size_t index) const;
291 
295  virtual ~KernelErrorCollection();
296 
301  const char *getDescription() const;
302 
303 private:
304  mutable char *_description;
305 };
306 typedef SharedPtr<KernelErrorCollection> KernelErrorCollectionPtr;
307 
312 class DAAL_EXPORT ErrorCollection
313 {
314 public:
315  DAAL_NEW_DELETE();
316 
320  ErrorCollection() : _errors(new KernelErrorCollection()), _canThrow(true){}
321 
326  ErrorCollection(const ErrorCollection &o) : _errors(o.getErrors()), _canThrow(o._canThrow) {}
327 
328 
333  explicit ErrorCollection(const KernelErrorCollection &errors) : _errors(new KernelErrorCollection(errors)), _canThrow(true) {}
334 
339  void add(const ErrorID &id)
340  {
341  _errors->add(id);
342 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
343  if(_canThrow)
344  throw Exception::getException(getDescription());
345 #endif
346  }
347 
352  void add(const ErrorPtr &e)
353  {
354  _errors->add(e);
355 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
356  if(_canThrow)
357  throw Exception::getException(getDescription());
358 #endif
359  }
360 
365  void add(const ErrorCollection &e)
366  {
367  if(!e.isEmpty())
368  {
369  _errors->add(e.getErrors());
370 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
371  if(_canThrow)
372  throw Exception::getException(getDescription());
373 #endif
374  }
375  }
376 
377 
382  void add(const KernelErrorCollectionPtr &e)
383  {
384  if(!e->isEmpty())
385  {
386  _errors->add(e);
387 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
388  if(_canThrow)
389  throw Exception::getException(getDescription());
390 #endif
391  }
392  }
393 
398  size_t size() const
399  {
400  return _errors->size();
401  }
402 
407  bool isEmpty() const
408  {
409  return _errors->isEmpty();
410  }
411 
415  virtual ~ErrorCollection() {}
416 
421  const KernelErrorCollectionPtr &getErrors() const
422  {
423  return _errors;
424  }
425 
430  const char *getDescription() const { return _errors->getDescription(); }
431 
436  bool canThrow() const
437  {
438  return _canThrow;
439  }
440 
446  bool setCanThrow(bool bOn)
447  {
448  bool bVal = _canThrow;
449  _canThrow = bOn;
450  return bVal;
451  }
452 
453 private:
454  KernelErrorCollectionPtr _errors;
455  bool _canThrow;
456 };
457 typedef SharedPtr<ErrorCollection> ErrorCollectionPtr;
465 class DAAL_EXPORT Status
466 {
467 public:
471  Status() : _impl(0){}
476  Status(ErrorID id);
481  Status(const ErrorPtr& e);
482 
486  Status(const Status& other);
487 
491  ~Status();
492 
497  bool ok() const { return !_impl; }
498 
503  operator bool() const { return ok(); }
504 
510  Status& add(ErrorID id);
511 
517  Status& add(const ErrorPtr& e);
518 
524  Status& add(const Status& other);
525 
531  Status& operator |=(const Status& other) { return add(other); }
532 
538  Status& operator =(const Status& other);
539 
544  const char* getDescription() const;
545 
550  void clear();
551 
555  DAAL_DEPRECATED Status(const KernelErrorCollection& e);
559  DAAL_DEPRECATED Status(const ErrorCollection& e);
560 
564  ErrorCollectionPtr getCollection() const;
565 
566 private:
567  void* _impl; //pointer to the collection of errors with reference counting
568 };
569 
570 inline const Status& throwIfPossible(const Status& s)
571 {
572 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
573  if(!s.ok())
574  throw services::Exception::getException(s.getDescription());
575 #endif
576  return s;
577 }
578 
579 } // namespace interface1
580 using interface1::Error;
581 using interface1::KernelErrorCollection;
582 using interface1::ErrorCollection;
583 using interface1::ErrorPtr;
584 using interface1::KernelErrorCollectionPtr;
585 using interface1::ErrorCollectionPtr;
586 using interface1::Status;
587 using interface1::throwIfPossible;
588 
589 }
590 };
591 #endif
daal::services::interface1::ErrorDetail
Base for error detail classes.
Definition: error_id.h:44
daal::services::interface1::ErrorCollection::add
void add(const ErrorPtr &e)
Definition: error_handling.h:352
daal::services::NoErrorMessageFound
Definition: error_indexes.h:406
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:465
daal
Definition: algorithm_base_common.h:31
daal::services::interface1::Exception::~Exception
virtual ~Exception()
Definition: error_handling.h:69
daal::services::interface1::ErrorCollection::getErrors
const KernelErrorCollectionPtr & getErrors() const
Definition: error_handling.h:421
daal::services::interface1::Status::ok
bool ok() const
Definition: error_handling.h:497
daal::services::interface1::ErrorCollection::~ErrorCollection
virtual ~ErrorCollection()
Definition: error_handling.h:415
daal::services::interface1::Status::Status
Status()
Definition: error_handling.h:471
daal::services::interface1::ErrorCollection::getDescription
const char * getDescription() const
Definition: error_handling.h:430
daal::services::interface1::Error
Class that represents an error.
Definition: error_handling.h:109
daal::services::interface1::ErrorCollection::add
void add(const KernelErrorCollectionPtr &e)
Definition: error_handling.h:382
daal::services::interface1::ErrorCollection
Class that represents an error collection.
Definition: error_handling.h:312
daal::services::interface1::ErrorCollection::setCanThrow
bool setCanThrow(bool bOn)
Definition: error_handling.h:446
daal::services::interface1::Error::details
const ErrorDetail * details() const
Definition: error_handling.h:178
daal::services::ErrorID
ErrorID
Definition: error_indexes.h:66
daal::services::interface1::Exception::getException
static Exception getException(const char *description)
Definition: error_handling.h:86
daal::services::interface1::SharedPtr< Error >
daal::services::interface1::ErrorCollection::add
void add(const ErrorCollection &e)
Definition: error_handling.h:365
daal::services::interface1::KernelErrorCollection::isEmpty
bool isEmpty() const
Definition: error_handling.h:256
daal::services::interface1::Exception::Exception
Exception(const char *description)
Definition: error_handling.h:57
daal::services::interface1::ErrorCollection::size
size_t size() const
Definition: error_handling.h:398
daal::services::interface1::KernelErrorCollection::KernelErrorCollection
KernelErrorCollection()
Definition: error_handling.h:219
daal::services::ErrorDetailID
ErrorDetailID
Definition: error_indexes.h:39
daal::services::interface1::ErrorCollection::isEmpty
bool isEmpty() const
Definition: error_handling.h:407
daal::services::interface1::Exception
Class that represents an exception.
Definition: error_handling.h:50
daal::services::interface1::String::c_str
const char * c_str() const
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection(const ErrorCollection &o)
Definition: error_handling.h:326
daal::services::interface1::Error::id
ErrorID id() const
Definition: error_handling.h:133
daal::services::interface1::ErrorCollection::canThrow
bool canThrow() const
Definition: error_handling.h:436
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection()
Definition: error_handling.h:320
daal::services::interface1::Exception::what
virtual const char * what() const
Definition: error_handling.h:63
daal::services::interface1::ErrorCollection::add
void add(const ErrorID &id)
Definition: error_handling.h:339
daal::algorithms::math::abs::value
Definition: abs_types.h:86
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection(const KernelErrorCollection &errors)
Definition: error_handling.h:333
daal::services::interface1::Exception::getException
static Exception getException(const String &description)
Definition: error_handling.h:76
daal::services::interface1::String
Class that implements functionality of the string, an object that represents a sequence of characters...
Definition: daal_string.h:44
daal::services::interface1::Error::setId
void setId(ErrorID id)
Definition: error_handling.h:139
daal::services::interface1::KernelErrorCollection
Class that represents a kernel error collection (collection that cannot throw exceptions) ...
Definition: error_handling.h:209
daal::services::interface1::Collection
Class that implements functionality of the Collection container.
Definition: collection.h:43

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