[common] Define PlatformEnum class template. 11/208811/1
authorMichal Michalski <m.michalski2@partner.samsung.com>
Tue, 7 May 2019 08:36:56 +0000 (10:36 +0200)
committerPiotr Kosko/Native/Web API (PLT) /SRPOL/Professional/삼성전자 <p.kosko@samsung.com>
Fri, 28 Jun 2019 13:26:27 +0000 (15:26 +0200)
Will replace current way enums are defined in webapi-plugins.

[Verification] Code compiles.

Signed-off-by: Michal Michalski <m.michalski2@partner.samsung.com>
Change-Id: Id8c53df46ae9039e81d699884494b72c20d5cba8

src/common/common.gyp
src/common/platform_enum.h [new file with mode: 0644]

index 89f99a8..c3b685c 100644 (file)
@@ -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 (file)
index 0000000..6c62695
--- /dev/null
@@ -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 <string>
+#include <map>
+#include <vector>
+#include <initializer_list>
+
+
+namespace common {
+
+
+template<typename NativeEnumType>
+class PlatformEnum
+{
+using MapType = std::map<std::string, NativeEnumType>;
+using RevMapType = std::map<NativeEnumType, std::string>;
+
+using MapTypeConstIter = typename MapType::const_iterator;
+
+public:
+  PlatformEnum(std::initializer_list<std::pair<const std::string, NativeEnumType>> 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_