본문 바로가기
C++/C++ Common

함수 결과 처리 코드 간소

by doublerabbits 2024. 5. 29.

원래 내용

ENUM_VALUE enum_value = TestFunction();
switch (enum_value)
{
    case ENUM_VALUE::SUCC;
        LogNormal("SUCC");
        return enum_value;
    case ENUM_VALUE::FAIL;
        LogError("FAIL");
        return enum_value;
    case ENUM_VALUE::ABORT;
        LogInfo("ABORT");
        return enum_value;
    case ENUM_VALUE::RETRY;
        LogInfo("RETRY");
        return enum_value;
}

 

 

 

 

매크로 이용

#include <iostream>
enum class ENUM_VALUE { SUCC, FAIL, ABORT, RETRY };
ENUM_VALUE TestFunction() {
    // Example function for demonstration
    return ENUM_VALUE::SUCC;
}
void LogNormal(const std::string& message) {
    std::cout << "[NORMAL] " << message << std::endl;
}
void LogError(const std::string& message) {
    std::cout << "[ERROR] " << message << std::endl;
}
void LogInfo(const std::string& message) {
    std::cout << "[INFO] " << message << std::endl;
}
#define HANDLE_CASE(value, logFunc) \
    case ENUM_VALUE::value:         \
        logFunc(#value);            \
        return enum_value;
ENUM_VALUE ProcessEnumValue(ENUM_VALUE enum_value) {
    switch (enum_value) {
        HANDLE_CASE(SUCC, LogNormal)
        HANDLE_CASE(FAIL, LogError)
        HANDLE_CASE(ABORT, LogInfo)
        HANDLE_CASE(RETRY, LogInfo)
        default:
            return enum_value; // Handle unexpected values
    }
}
int main() {
    ENUM_VALUE enum_value = TestFunction();
    ProcessEnumValue(enum_value);
    return 0;
}

 

 

템플릿 이용

#include <iostream>
#include <unordered_map>
#include <functional>
enum class ENUM_VALUE { SUCC, FAIL, ABORT, RETRY };
ENUM_VALUE TestFunction() {
    // Example function for demonstration
    return ENUM_VALUE::SUCC;
}
void LogNormal(const std::string& message) {
    std::cout << "[NORMAL] " << message << std::endl;
}
void LogError(const std::string& message) {
    std::cout << "[ERROR] " << message << std::endl;
}
void LogInfo(const std::string& message) {
    std::cout << "[INFO] " << message << std::endl;
}
void LogMessageAndReturn(ENUM_VALUE enum_value, const std::function<void(const std::string&)>& logFunc) {
    switch (enum_value) {
        case ENUM_VALUE::SUCC:
            logFunc("SUCC");
            break;
        case ENUM_VALUE::FAIL:
            logFunc("FAIL");
            break;
        case ENUM_VALUE::ABORT:
            logFunc("ABORT");
            break;
        case ENUM_VALUE::RETRY:
            logFunc("RETRY");
            break;
        default:
            break;
    }
}
ENUM_VALUE ProcessEnumValue(ENUM_VALUE enum_value) {
    static const std::unordered_map<ENUM_VALUE, std::function<void(const std::string&)>> logFunctions = {
        { ENUM_VALUE::SUCC, LogNormal },
        { ENUM_VALUE::FAIL, LogError },
        { ENUM_VALUE::ABORT, LogInfo },
        { ENUM_VALUE::RETRY, LogInfo }
    };
    auto it = logFunctions.find(enum_value);
    if (it != logFunctions.end()) {
        LogMessageAndReturn(enum_value, it->second);
    }
    return enum_value;
}
int main() {
    ENUM_VALUE enum_value = TestFunction();
    ProcessEnumValue(enum_value);
    return 0;
}

 

 

매크로의 장단점

장점:

간단함: 매크로는 단순한 문자열 치환이므로 매우 간단하게 반복 코드를 줄일 수 있습니다.

전처리 단계: 컴파일 전에 치환되므로 코드 크기를 줄이는 데 유용합니다.

 

단점:

디버깅 어려움: 매크로는 전처리 단계에서 치환되기 때문에 디버깅 시 원본 코드를 찾기 어렵습니다.

타입 안전성 부족: 매크로는 타입 체크를 하지 않기 때문에 타입 오류가 발생하기 쉽습니다.

예상치 못한 부작용: 매크로 치환은 코드 구조를 변경할 수 있어 예상치 못한 부작용을 일으킬 수 있습니다.

 

 

 

템플릿의 장단점

장점:

타입 안전성: 템플릿은 컴파일 타임에 타입 체크를 하므로 타입 안전성을 보장합니다.

유연성: 다양한 타입에 대해 재사용 가능하며, 코드를 보다 유연하게 작성할 수 있습니다.

가독성: 템플릿을 사용하면 코드의 가독성과 유지보수성을 높일 수 있습니다.

 

단점:

복잡성: 템플릿 문법은 복잡할 수 있으며, 초보자에게는 이해하기 어려울 수 있습니다.

컴파일 시간 증가: 템플릿은 컴파일 타임에 많은 작업을 수행하므로 컴파일 시간이 증가할 수 있습니다.

에러 메시지: 템플릿 에러 메시지는 매우 길고 이해하기 어려울 수 있습니다.

'C++ > C++ Common' 카테고리의 다른 글

Endian 변환  (0) 2024.06.26
가변 인수 매크로  (0) 2023.03.30
Time  (0) 2023.03.30
Bit Shift  (0) 2023.03.27
시간 출력  (0) 2022.11.05