From 3e78decd5e6fe3590da3e91680aae51d43c62d5f Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Wed, 20 Feb 2019 16:21:46 -0600 Subject: Refactor VirtualKeyMap Currently, there's a lot of new and delete going on in VirtualKeyMap. Simplify this by using unique_ptr instead. Bug: 113575658 Test: atest libinput_tests inputflinger_tests Change-Id: Ib2a68ae23a4300b0e2cf72902371f4b9604cfee5 --- libs/input/VirtualKeyMap.cpp | 55 ++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 35 deletions(-) (limited to 'libs/input/VirtualKeyMap.cpp') diff --git a/libs/input/VirtualKeyMap.cpp b/libs/input/VirtualKeyMap.cpp index 3ec53bf5a0..624f152996 100644 --- a/libs/input/VirtualKeyMap.cpp +++ b/libs/input/VirtualKeyMap.cpp @@ -28,10 +28,6 @@ // Enables debug output for the parser. #define DEBUG_PARSER 0 -// Enables debug output for parser performance. -#define DEBUG_PARSER_PERFORMANCE 0 - - namespace android { static const char* WHITESPACE = " \t\r"; @@ -46,39 +42,28 @@ VirtualKeyMap::VirtualKeyMap() { VirtualKeyMap::~VirtualKeyMap() { } -status_t VirtualKeyMap::load(const std::string& filename, VirtualKeyMap** outMap) { - *outMap = nullptr; - - Tokenizer* tokenizer; - status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer); - if (status) { +std::unique_ptr VirtualKeyMap::load(const std::string& filename) { + Tokenizer* t; + status_t status = Tokenizer::open(String8(filename.c_str()), &t); + if (status != OK) { ALOGE("Error %d opening virtual key map file %s.", status, filename.c_str()); - } else { - VirtualKeyMap* map = new VirtualKeyMap(); - if (!map) { - ALOGE("Error allocating virtual key map."); - status = NO_MEMORY; - } else { -#if DEBUG_PARSER_PERFORMANCE - nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); -#endif - Parser parser(map, tokenizer); - status = parser.parse(); -#if DEBUG_PARSER_PERFORMANCE - nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; - ALOGD("Parsed key character map file '%s' %d lines in %0.3fms.", - tokenizer->getFilename().string(), tokenizer->getLineNumber(), - elapsedTime / 1000000.0); -#endif - if (status) { - delete map; - } else { - *outMap = map; - } - } - delete tokenizer; + return nullptr; + } + std::unique_ptr tokenizer(t); + // Using 'new' to access a non-public constructor + std::unique_ptr map(new VirtualKeyMap()); + if (!map) { + ALOGE("Error allocating virtual key map."); + return nullptr; } - return status; + + Parser parser(map.get(), tokenizer.get()); + status = parser.parse(); + if (status != OK) { + return nullptr; + } + + return map; } -- cgit v1.2.3-59-g8ed1b