From: Michal Michalski Date: Tue, 7 May 2019 08:36:56 +0000 (+0200) Subject: [common] Define PlatformEnum class template. X-Git-Tag: submit/tizen/20190805.070446~4^2^2~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a7df7befcda84eea79ff799ed2e338ac974a18c8;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [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 --- diff --git a/src/common/common.gyp b/src/common/common.gyp index 89f99a81..c3b685cd 100644 --- a/src/common/common.gyp +++ b/src/common/common.gyp @@ -39,6 +39,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 00000000..6c626954 --- /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_