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