From 4ccefe2eeabd5b39ac6de9ef17667c0055ea952c Mon Sep 17 00:00:00 2001 From: Michal Michalski Date: Tue, 7 May 2019 10:36:56 +0200 Subject: [PATCH] [common] Define PlatformEnum class template. Will replace current way enums are defined in webapi-plugins. [Verification] Code compiles. Signed-off-by: Michal Michalski Change-Id: Id8c53df46ae9039e81d699884494b72c20d5cba8 --- src/common/common.gyp | 1 + src/common/platform_enum.h | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/common/platform_enum.h diff --git a/src/common/common.gyp b/src/common/common.gyp index fab56f4..9f7729a 100644 --- a/src/common/common.gyp +++ b/src/common/common.gyp @@ -41,6 +41,7 @@ 'optional.h', 'platform_result.cc', 'platform_result.h', + 'platform_enum.h', 'assert.h', 'GDBus/connection.cpp', 'GDBus/connection.h', diff --git a/src/common/platform_enum.h b/src/common/platform_enum.h new file mode 100644 index 0000000..6c62695 --- /dev/null +++ b/src/common/platform_enum.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd 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 COMMON_PLATFORM_ENUM_H_ +#define COMMON_PLATFORM_ENUM_H_ + +#include "platform_result.h" + +#include +#include +#include +#include + + +namespace common { + + +template +class PlatformEnum +{ +using MapType = std::map; +using RevMapType = std::map; + +using MapTypeConstIter = typename MapType::const_iterator; + +public: + PlatformEnum(std::initializer_list> m) : mapping_(m) { + for (auto it: mapping_) { + rev_mapping_[it.second] = it.first; + } + } + + PlatformResult getName(NativeEnumType value, std::string* name) const { + auto it = rev_mapping_.find(value); + if (it != rev_mapping_.end()) { + *name = it->second; + return PlatformResult(ErrorCode::NO_ERROR); + } + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "enum value not registered"); + } + + PlatformResult getValue(std::string name, NativeEnumType* value) const { + auto it = mapping_.find(name); + if (it != mapping_.end()) { + *value = it->second; + return PlatformResult(ErrorCode::NO_ERROR); + } + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "enum name not registered"); + } + + MapTypeConstIter begin() const { + return mapping_.cbegin(); + } + + MapTypeConstIter end() const { + return mapping_.cend(); + } + +private: + MapType mapping_; + RevMapType rev_mapping_; +}; + + +} // common + + +#endif // COMMON_PLATFORM_ENUM_H_ -- 2.7.4