C++ API Reference for Intel® Data Analytics Acceleration Library 2018 Update 2

error_handling.h
1 
2 /*******************************************************************************
3 * Copyright 2014-2018 Intel Corporation
4 * All Rights Reserved.
5 *
6 * If this software was obtained under the Intel Simplified Software License,
7 * the following terms apply:
8 *
9 * The source code, information and material ("Material") contained herein is
10 * owned by Intel Corporation or its suppliers or licensors, and title to such
11 * Material remains with Intel Corporation or its suppliers or licensors. The
12 * Material contains proprietary information of Intel or its suppliers and
13 * licensors. The Material is protected by worldwide copyright laws and treaty
14 * provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed or disclosed
16 * in any way without Intel's prior express written permission. No license under
17 * any patent, copyright or other intellectual property rights in the Material
18 * is granted to or conferred upon you, either expressly, by implication,
19 * inducement, estoppel or otherwise. Any license under such intellectual
20 * property rights must be express and approved by Intel in writing.
21 *
22 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 * notice or any other notice embedded in Materials by Intel or Intel's
24 * suppliers or licensors in any way.
25 *
26 *
27 * If this software was obtained under the Apache License, Version 2.0 (the
28 * "License"), the following terms apply:
29 *
30 * You may not use this file except in compliance with the License. You may
31 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 *
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 *
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41 
42 /*
43 //++
44 // Handling errors in Intel(R) DAAL.
45 //--
46 */
47 
48 #ifndef __ERROR_HANDLING__
49 #define __ERROR_HANDLING__
50 
51 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
52 #include <exception>
53 #endif
54 
55 #include "daal_string.h"
56 #include "error_indexes.h"
57 #include "error_id.h"
58 #include "services/collection.h"
59 
60 namespace daal
61 {
62 namespace services
63 {
64 namespace interface1
65 {
70 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
71 
76 struct DAAL_EXPORT Exception : std::exception
77 {
78 public:
83  Exception(const char *description) : _description(description) {};
84 
89  virtual const char *what() const throw() { return _description.c_str(); };
90 
91 #ifndef cxx11
92 
95  virtual ~Exception() throw() {}
96 #endif
97 
102  static Exception getException(const String &description)
103  {
104  String d(description);
105  return Exception(d.c_str());
106  }
107 
112  static Exception getException(const char *description)
113  {
114  return Exception(description);
115  }
116 
117 private:
118  String _description;
119 };
120 
121 #endif
122 
123 } // namespace interface1
124 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
125 using interface1::Exception;
126 #endif
127 
128 namespace interface1
129 {
130 
135 class DAAL_EXPORT Error
136 {
137 public:
138  DAAL_NEW_DELETE();
139 
144  Error(const ErrorID id = NoErrorMessageFound);
145 
150  Error(const Error &e);
151 
153  ~Error();
154 
159  ErrorID id() const { return _id; }
160 
165  void setId(ErrorID id)
166  {
167  _id = id;
168  }
169 
174  const char *description() const;
175 
182  Error &addIntDetail(ErrorDetailID id, int value);
183 
190  Error &addDoubleDetail(ErrorDetailID id, double value);
191 
198  Error &addStringDetail(ErrorDetailID id, const String &value);
199 
204  const ErrorDetail *details() const { return _details; }
205 
210  static SharedPtr<Error> create(ErrorID id);
211 
212  static SharedPtr<Error> create(ErrorID id, ErrorDetailID det, int value);
213 
214  static SharedPtr<Error> create(ErrorID id, ErrorDetailID det, const String &value);
215 
216 protected:
223  Error &addDetail(ErrorDetail *detail);
224 
225 private:
226  ErrorID _id;
227  ErrorDetail *_details;
228 };
229 typedef SharedPtr<Error> ErrorPtr;
230 
235 class DAAL_EXPORT KernelErrorCollection : private Collection<SharedPtr<Error> >
236 {
237 public:
238  DAAL_NEW_DELETE();
239 
240  typedef Collection<SharedPtr<Error> > super;
241 
245  KernelErrorCollection() : _description(0) {}
246 
251  KernelErrorCollection(const KernelErrorCollection &other);
252 
258  Error &add(const ErrorID &id);
259 
264  void add(const ErrorPtr &e);
265 
270  void add(const services::SharedPtr<KernelErrorCollection> &e);
271 
276  void add(const KernelErrorCollection &e);
277 
282  bool isEmpty() const { return size() == 0; }
283 
288  size_t size() const;
289 
295  Error *at(size_t index);
296 
302  const Error *at(size_t index) const;
303 
309  Error *operator[](size_t index);
310 
316  const Error *operator[](size_t index) const;
317 
321  virtual ~KernelErrorCollection();
322 
327  const char *getDescription() const;
328 
329 private:
330  mutable char *_description;
331 };
332 typedef SharedPtr<KernelErrorCollection> KernelErrorCollectionPtr;
333 
338 class DAAL_EXPORT ErrorCollection
339 {
340 public:
341  DAAL_NEW_DELETE();
342 
346  ErrorCollection() : _errors(new KernelErrorCollection()), _canThrow(true){}
347 
352  ErrorCollection(const ErrorCollection &o) : _errors(o.getErrors()), _canThrow(o._canThrow) {}
353 
354 
359  explicit ErrorCollection(const KernelErrorCollection &errors) : _errors(new KernelErrorCollection(errors)), _canThrow(true) {}
360 
365  void add(const ErrorID &id)
366  {
367  _errors->add(id);
368 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
369  if(_canThrow)
370  throw Exception::getException(getDescription());
371 #endif
372  }
373 
378  void add(const ErrorPtr &e)
379  {
380  _errors->add(e);
381 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
382  if(_canThrow)
383  throw Exception::getException(getDescription());
384 #endif
385  }
386 
391  void add(const ErrorCollection &e)
392  {
393  if(!e.isEmpty())
394  {
395  _errors->add(e.getErrors());
396 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
397  if(_canThrow)
398  throw Exception::getException(getDescription());
399 #endif
400  }
401  }
402 
403 
408  void add(const KernelErrorCollectionPtr &e)
409  {
410  if(!e->isEmpty())
411  {
412  _errors->add(e);
413 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
414  if(_canThrow)
415  throw Exception::getException(getDescription());
416 #endif
417  }
418  }
419 
424  size_t size() const
425  {
426  return _errors->size();
427  }
428 
433  bool isEmpty() const
434  {
435  return _errors->isEmpty();
436  }
437 
441  virtual ~ErrorCollection() {}
442 
447  const KernelErrorCollectionPtr &getErrors() const
448  {
449  return _errors;
450  }
451 
456  const char *getDescription() const { return _errors->getDescription(); }
457 
462  bool canThrow() const
463  {
464  return _canThrow;
465  }
466 
472  bool setCanThrow(bool bOn)
473  {
474  bool bVal = _canThrow;
475  _canThrow = bOn;
476  return bVal;
477  }
478 
479 private:
480  KernelErrorCollectionPtr _errors;
481  bool _canThrow;
482 };
483 typedef SharedPtr<ErrorCollection> ErrorCollectionPtr;
491 class DAAL_EXPORT Status
492 {
493 public:
497  Status() : _impl(0){}
502  Status(ErrorID id);
507  Status(const ErrorPtr& e);
508 
512  Status(const Status& other);
513 
517  ~Status();
518 
523  bool ok() const { return !_impl; }
524 
529  operator bool() const { return ok(); }
530 
536  Status& add(ErrorID id);
537 
543  Status& add(const ErrorPtr& e);
544 
550  Status& add(const Status& other);
551 
557  Status& operator |=(const Status& other) { return add(other); }
558 
564  Status& operator =(const Status& other);
565 
570  const char* getDescription() const;
571 
576  void clear();
577 
581  DAAL_DEPRECATED Status(const KernelErrorCollection& e);
585  DAAL_DEPRECATED Status(const ErrorCollection& e);
586 
590  ErrorCollectionPtr getCollection() const;
591 
592 private:
593  void* _impl; //pointer to the collection of errors with reference counting
594 };
595 
596 inline const Status& throwIfPossible(const Status& s)
597 {
598 #if (!defined(DAAL_NOTHROW_EXCEPTIONS))
599  if(!s.ok())
600  throw services::Exception::getException(s.getDescription());
601 #endif
602  return s;
603 }
604 
605 } // namespace interface1
606 using interface1::Error;
607 using interface1::KernelErrorCollection;
608 using interface1::ErrorCollection;
609 using interface1::ErrorPtr;
610 using interface1::KernelErrorCollectionPtr;
611 using interface1::ErrorCollectionPtr;
612 using interface1::Status;
613 using interface1::throwIfPossible;
614 
615 }
616 };
617 #endif
daal::services::interface1::ErrorDetail
Base for error detail classes.
Definition: error_id.h:70
daal::services::interface1::ErrorCollection::add
void add(const ErrorPtr &e)
Definition: error_handling.h:378
daal::services::NoErrorMessageFound
Definition: error_indexes.h:427
daal::services::interface1::Status::getDescription
const char * getDescription() const
daal::services::interface1::Error::details
const ErrorDetail * details() const
Definition: error_handling.h:204
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:491
daal
Definition: algorithm_base_common.h:57
daal::services::interface1::Exception::~Exception
virtual ~Exception()
Definition: error_handling.h:95
daal::services::interface1::Exception::what
virtual const char * what() const
Definition: error_handling.h:89
daal::services::interface1::ErrorCollection::~ErrorCollection
virtual ~ErrorCollection()
Definition: error_handling.h:441
daal::services::interface1::Status::Status
Status()
Definition: error_handling.h:497
daal::services::interface1::ErrorCollection::canThrow
bool canThrow() const
Definition: error_handling.h:462
daal::services::interface1::Error
Class that represents an error.
Definition: error_handling.h:135
daal::services::interface1::ErrorCollection::add
void add(const KernelErrorCollectionPtr &e)
Definition: error_handling.h:408
daal::services::interface1::Error::id
ErrorID id() const
Definition: error_handling.h:159
daal::services::interface1::ErrorCollection
Class that represents an error collection.
Definition: error_handling.h:338
daal::services::interface1::ErrorCollection::setCanThrow
bool setCanThrow(bool bOn)
Definition: error_handling.h:472
daal::services::ErrorID
ErrorID
Definition: error_indexes.h:92
daal::services::interface1::Exception::getException
static Exception getException(const char *description)
Definition: error_handling.h:112
daal::services::interface1::SharedPtr< Error >
daal::services::interface1::ErrorCollection::add
void add(const ErrorCollection &e)
Definition: error_handling.h:391
daal::services::interface1::String::c_str
const char * c_str() const
daal::services::interface1::Exception::Exception
Exception(const char *description)
Definition: error_handling.h:83
daal::services::interface1::ErrorCollection::size
size_t size() const
Definition: error_handling.h:424
daal::services::interface1::KernelErrorCollection::KernelErrorCollection
KernelErrorCollection()
Definition: error_handling.h:245
daal::services::ErrorDetailID
ErrorDetailID
Definition: error_indexes.h:65
daal::services::interface1::Exception
Class that represents an exception.
Definition: error_handling.h:76
daal::services::interface1::ErrorCollection::getDescription
const char * getDescription() const
Definition: error_handling.h:456
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection(const ErrorCollection &o)
Definition: error_handling.h:352
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection()
Definition: error_handling.h:346
daal::services::interface1::KernelErrorCollection::isEmpty
bool isEmpty() const
Definition: error_handling.h:282
daal::services::interface1::ErrorCollection::add
void add(const ErrorID &id)
Definition: error_handling.h:365
daal::services::interface1::ErrorCollection::isEmpty
bool isEmpty() const
Definition: error_handling.h:433
daal::algorithms::math::abs::value
Definition: abs_types.h:112
daal::services::interface1::ErrorCollection::ErrorCollection
ErrorCollection(const KernelErrorCollection &errors)
Definition: error_handling.h:359
daal::services::interface1::ErrorCollection::getErrors
const KernelErrorCollectionPtr & getErrors() const
Definition: error_handling.h:447
daal::services::interface1::Exception::getException
static Exception getException(const String &description)
Definition: error_handling.h:102
daal::services::interface1::String
Class that implements functionality of the string, an object that represents a sequence of characters...
Definition: daal_string.h:69
daal::services::interface1::Error::setId
void setId(ErrorID id)
Definition: error_handling.h:165
daal::services::interface1::Status::ok
bool ok() const
Definition: error_handling.h:523
daal::services::interface1::KernelErrorCollection
Class that represents a kernel error collection (collection that cannot throw exceptions) ...
Definition: error_handling.h:235
daal::services::interface1::Collection
Class that implements functionality of the Collection container.
Definition: collection.h:69

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