From 91a2562fffaa0f8d53145ec0ec68df9ef9dd68e0 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Mar 2020 12:17:15 -0400 Subject: [PATCH] CFactoryMgr: Make use of std::array Same behavior, with stronger typing. We can also leverage the iterator member functions directly. --- Runtime/CFactoryMgr.cpp | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/Runtime/CFactoryMgr.cpp b/Runtime/CFactoryMgr.cpp index 0f530c74f..62f427058 100644 --- a/Runtime/CFactoryMgr.cpp +++ b/Runtime/CFactoryMgr.cpp @@ -1,9 +1,22 @@ #include "Runtime/CFactoryMgr.hpp" +#include +#include +#include +#include + #include "Runtime/CStopwatch.hpp" #include "Runtime/IObj.hpp" namespace urde { +constexpr std::array TypeTable{ + FOURCC('CLSN'), FOURCC('CMDL'), FOURCC('CSKR'), FOURCC('ANIM'), FOURCC('CINF'), FOURCC('TXTR'), FOURCC('PLTT'), + FOURCC('FONT'), FOURCC('ANCS'), FOURCC('EVNT'), FOURCC('MADF'), FOURCC('MLVL'), FOURCC('MREA'), FOURCC('MAPW'), + FOURCC('MAPA'), FOURCC('SAVW'), FOURCC('SAVA'), FOURCC('PART'), FOURCC('WPSC'), FOURCC('SWHC'), FOURCC('DPSC'), + FOURCC('ELSC'), FOURCC('CRSC'), FOURCC('AFSM'), FOURCC('DCLN'), FOURCC('AGSC'), FOURCC('ATBL'), FOURCC('CSNG'), + FOURCC('STRG'), FOURCC('SCAN'), FOURCC('PATH'), FOURCC('DGRP'), FOURCC('HMAP'), FOURCC('CTWK'), FOURCC('FRME'), + FOURCC('HINT'), FOURCC('MAPU'), FOURCC('DUMB'), FOURCC('OIDS'), +}; CFactoryFnReturn CFactoryMgr::MakeObject(const SObjectTag& tag, urde::CInputStream& in, const CVParamTransfer& paramXfer, CObjectReference* selfRef) { @@ -52,24 +65,19 @@ CFactoryFnReturn CFactoryMgr::MakeObjectFromMemory(const SObjectTag& tag, std::u } } -static const FourCC TypeTable[] = { - FOURCC('CLSN'), FOURCC('CMDL'), FOURCC('CSKR'), FOURCC('ANIM'), FOURCC('CINF'), FOURCC('TXTR'), FOURCC('PLTT'), - FOURCC('FONT'), FOURCC('ANCS'), FOURCC('EVNT'), FOURCC('MADF'), FOURCC('MLVL'), FOURCC('MREA'), FOURCC('MAPW'), - FOURCC('MAPA'), FOURCC('SAVW'), FOURCC('SAVA'), FOURCC('PART'), FOURCC('WPSC'), FOURCC('SWHC'), FOURCC('DPSC'), - FOURCC('ELSC'), FOURCC('CRSC'), FOURCC('AFSM'), FOURCC('DCLN'), FOURCC('AGSC'), FOURCC('ATBL'), FOURCC('CSNG'), - FOURCC('STRG'), FOURCC('SCAN'), FOURCC('PATH'), FOURCC('DGRP'), FOURCC('HMAP'), FOURCC('CTWK'), FOURCC('FRME'), - FOURCC('HINT'), FOURCC('MAPU'), FOURCC('DUMB'), FOURCC('OIDS')}; - CFactoryMgr::ETypeTable CFactoryMgr::FourCCToTypeIdx(FourCC fcc) { - for (int i = 0; i < 4; ++i) - fcc.getChars()[i] = char(toupper(fcc.getChars()[i])); - auto search = - std::find_if(std::begin(TypeTable), std::end(TypeTable), [fcc](const FourCC& test) { return test == fcc; }); - if (search == std::end(TypeTable)) + for (size_t i = 0; i < 4; ++i) { + fcc.getChars()[i] = char(std::toupper(fcc.getChars()[i])); + } + + const auto search = + std::find_if(TypeTable.cbegin(), TypeTable.cend(), [fcc](const FourCC& test) { return test == fcc; }); + if (search == TypeTable.cend()) { return ETypeTable::Invalid; - return ETypeTable(search - std::begin(TypeTable)); + } + return ETypeTable(std::distance(TypeTable.cbegin(), search)); } -FourCC CFactoryMgr::TypeIdxToFourCC(ETypeTable fcc) { return TypeTable[int(fcc)]; } +FourCC CFactoryMgr::TypeIdxToFourCC(ETypeTable fcc) { return TypeTable[size_t(fcc)]; } } // namespace urde