Viskores  1.0
ErrorMessageBuffer.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_exec_internal_ErrorMessageBuffer_h
19 #define viskores_exec_internal_ErrorMessageBuffer_h
20 
21 #include <viskores/Types.h>
22 
23 namespace viskores
24 {
25 namespace exec
26 {
27 namespace internal
28 {
29 
41 class VISKORES_ALWAYS_EXPORT ErrorMessageBuffer
42 {
43 public:
44  VISKORES_EXEC_CONT ErrorMessageBuffer()
45  : MessageBuffer(nullptr)
46  , MessageBufferSize(0)
47  {
48  }
49 
51  ErrorMessageBuffer(char* messageBuffer, viskores::Id bufferSize)
52  : MessageBuffer(messageBuffer)
53  , MessageBufferSize(bufferSize)
54  {
55  }
56 
57  VISKORES_EXEC void RaiseError(const char* message) const
58  {
59  // Only raise the error if one has not been raised yet. This check is not
60  // guaranteed to work across threads. However, chances are that if two or
61  // more threads simultaneously pass this test, they will be writing the
62  // same error, which is fine. Even in the much less likely case that two
63  // threads simultaneously write different error messages, the worst case is
64  // that you get a mangled message. That's not good (and it's what we are
65  // trying to avoid), but it's not critical.
66  if (this->IsErrorRaised())
67  {
68  return;
69  }
70 
71  // Safely copy message into array.
72  for (viskores::Id index = 0; index < this->MessageBufferSize; index++)
73  {
74  this->MessageBuffer[index] = message[index];
75  if (message[index] == '\0')
76  {
77  break;
78  }
79  }
80 
81  // Make sure message is null terminated.
82  this->MessageBuffer[this->MessageBufferSize - 1] = '\0';
83  }
84 
85  VISKORES_EXEC_CONT bool IsErrorRaised() const
86  {
87  if (this->MessageBufferSize > 0)
88  {
89  return (this->MessageBuffer[0] != '\0');
90  }
91  else
92  {
93  // If there is no buffer set, then always report an error.
94  return true;
95  }
96  }
97 
98 private:
99  char* MessageBuffer;
100  viskores::Id MessageBufferSize;
101 };
102 }
103 }
104 } // namespace viskores::exec::internal
105 
106 #endif // viskores_exec_internal_ErrorMessageBuffer_h
Types.h
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
VISKORES_EXEC
#define VISKORES_EXEC
Definition: ExportMacros.h:59