|
| 1 | +// Copyright 2023 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef RMW__CHECK_TYPE_IDENTIFIERS_MATCH_H_ |
| 16 | +#define RMW__CHECK_TYPE_IDENTIFIERS_MATCH_H_ |
| 17 | + |
| 18 | +#include <string.h> |
| 19 | + |
| 20 | +#include "rcutils/snprintf.h" |
| 21 | + |
| 22 | +#include "rmw/allocators.h" |
| 23 | +#include "rmw/error_handling.h" |
| 24 | +#include "rmw/impl/config.h" // For RMW_AVOID_MEMORY_ALLOCATION |
| 25 | + |
| 26 | +#if RMW_AVOID_MEMORY_ALLOCATION |
| 27 | +#define RMW_CHECK_TYPE_IDENTIFIERS_MATCH(ElementName, ElementTypeID, ExpectedTypeID, OnFailure) \ |
| 28 | + do { \ |
| 29 | + if (ElementTypeID != ExpectedTypeID) { \ |
| 30 | + char __msg[1024]; \ |
| 31 | + int ret = rcutils_snprintf( \ |
| 32 | + __msg, sizeof(__msg), \ |
| 33 | + #ElementName " implementation '%s'(%p) does not match rmw implementation '%s'(%p)", \ |
| 34 | + ElementTypeID, (const void *)ElementTypeID, \ |
| 35 | + ExpectedTypeID, (const void *)ExpectedTypeID); \ |
| 36 | + if (ret < 0) { \ |
| 37 | + static const char error_msg[] = \ |
| 38 | + "RMW_CHECK_TYPE_IDENTIFIERS_MATCH(): rcutils_snprintf() failed"; \ |
| 39 | + memmove(__msg, error_msg, sizeof(error_msg)); \ |
| 40 | + } \ |
| 41 | + RMW_SET_ERROR_MSG(__msg); \ |
| 42 | + OnFailure; \ |
| 43 | + } \ |
| 44 | + } while(0) |
| 45 | +#else // RMW_AVOID_MEMORY_ALLOCATION |
| 46 | +#define RMW_CHECK_TYPE_IDENTIFIERS_MATCH(ElementName, ElementTypeID, ExpectedTypeID, OnFailure) \ |
| 47 | + do { \ |
| 48 | + if (ElementTypeID != ExpectedTypeID) { \ |
| 49 | + int __bytes_that_would_have_been_written = rcutils_snprintf( \ |
| 50 | + NULL, 0, \ |
| 51 | + #ElementName " implementation '%s'(%p) does not match rmw implementation '%s'(%p)", \ |
| 52 | + ElementTypeID, (const void *)ElementTypeID, \ |
| 53 | + ExpectedTypeID, (const void *)ExpectedTypeID); \ |
| 54 | + if (__bytes_that_would_have_been_written < 0) { \ |
| 55 | + RMW_SET_ERROR_MSG( \ |
| 56 | + "RMW_CHECK_TYPE_IDENTIFIERS_MATCH(): rcutils_snprintf(NULL, 0, ...) failed"); \ |
| 57 | + OnFailure; \ |
| 58 | + } else { \ |
| 59 | + char * __msg = \ |
| 60 | + (char *)rmw_allocate(__bytes_that_would_have_been_written + 1); \ |
| 61 | + if (NULL == __msg) { \ |
| 62 | + RMW_SET_ERROR_MSG( \ |
| 63 | + "RMW_CHECK_TYPE_IDENTIFIERS_MATCH(): rmw_allocate() failed"); \ |
| 64 | + } else { \ |
| 65 | + int ret = rcutils_snprintf( \ |
| 66 | + __msg, __bytes_that_would_have_been_written + 1, \ |
| 67 | + #ElementName " implementation '%s'(%p) does not match rmw implementation '%s'(%p)", \ |
| 68 | + ElementTypeID, (const void *)ElementTypeID, \ |
| 69 | + ExpectedTypeID, (const void *)ExpectedTypeID); \ |
| 70 | + if (ret < 0) { \ |
| 71 | + RMW_SET_ERROR_MSG( \ |
| 72 | + "RMW_CHECK_TYPE_IDENTIFIERS_MATCH(): rcutils_snprintf() failed"); \ |
| 73 | + } else { \ |
| 74 | + RMW_SET_ERROR_MSG(__msg); \ |
| 75 | + } \ |
| 76 | + } \ |
| 77 | + rmw_free(__msg); \ |
| 78 | + OnFailure; \ |
| 79 | + } \ |
| 80 | + } \ |
| 81 | + } while(0) |
| 82 | +#endif // RMW_AVOID_MEMORY_ALLOCATION |
| 83 | + |
| 84 | +#endif // RMW__CHECK_TYPE_IDENTIFIERS_MATCH_H_ |
0 commit comments