TensorRT/parsers/caffe/caffeMacros.h源碼研讀

TensorRT/parsers/caffe/caffeMacros.h源碼研讀

  • 前言
  • TensorRT/parsers/caffe/caffeMacros.h
  • \_MSC\_VER,\_\_FUNCTION\_\_,\_\_func__
  • multiline macro
  • #x
  • 參考連結

前言

這個檔案裡定義了幾個macro function,這些macro將在TensorRT/parsers/caffe/caffeParser/caffeParser.h裡被使用。

TensorRT/parsers/caffe/caffeMacros.h

/*
 * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TRT_CAFFE_PARSER_MACROS_H
#define TRT_CAFFE_PARSER_MACROS_H
#ifdef _MSC_VER
#define FN_NAME __FUNCTION__
#else
#define FN_NAME __func__
#endif

//檢查ptr是否為nullptr,如果是則輸出錯誤訊息
#define CHECK_NULL(ptr)                                                                                                \
    if ((ptr) == nullptr)                                                                                              \
    {                                                                                                                  \
        std::cout << "Error: input " << #ptr << " is NULL in " << FN_NAME << std::endl;                                \
        return;                                                                                                        \
    }
//同上,為回傳nullptr的版本
#define CHECK_NULL_RET_NULL(ptr)                                                                                       \
    if ((ptr) == nullptr)                                                                                              \
    {                                                                                                                  \
        std::cout << "Error: input " << #ptr << " is NULL in " << FN_NAME << std::endl;                                \
        return nullptr;                                                                                                \
    }
//同上,為回傳val的版本
#define CHECK_NULL_RET_VAL(ptr, val)                                                                                   \
    if ((ptr) == nullptr)                                                                                              \
    {                                                                                                                  \
        std::cout << "Error: input " << #ptr << " is NULL in " << FN_NAME << std::endl;                                \
        return val;                                                                                                    \
    }

#include "parserUtils.h"
//輸出錯誤訊息message,並回傳ret
//"CaffeParser: "是RETURN_AND_LOG_ERROR_IMPL函數的參數parserName
#define RETURN_AND_LOG_ERROR(ret, message) RETURN_AND_LOG_ERROR_IMPL(ret, message, "CaffeParser: ")
#endif // TRT_CAFFE_PARSER_MACROS_H

思考一下,為何要將以上這些功能定義為macro而不是一般的函數呢?

兩者的差別在於:macro在預處理過後會被直接替換到代碼裡。因此macro裡的return將會導致程序直接跳出該代碼段所在的函數;如果是一般的函數,其return只能跳出該函數本身,而無法進一步跳出該函數所在的函數。

_MSC_VER,__FUNCTION__,__func__

這個檔案裡用到了_MSC_VER__FUNCTION____func__等macro,詳見C predefined macros __FILE__,__LINE__,__func__。

multiline macro

在定義CHECK_NULLCHECK_NULL_RET_NULLCHECK_NULL_RET_VAL三個函數時,除末行的每行末尾都加上了\,這是什麼意思呢?詳見C multiline macro。

#x

TensorRT/parsers/caffe/caffeMacros.h的macro函數CHECK_NULL裡,用到了#ptr,詳見C macro裡的#x。

參考連結

C predefined macros __FILE__,__LINE__,__func__

C multiline macro

C macro裡的#x

你可能感兴趣的:(TensorRT源碼研讀筆記)