Re-implement namespace ctx::shared as class ctx::SharedVars 88/59788/3
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 18 Feb 2016 12:03:16 +0000 (21:03 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 19 Feb 2016 02:14:52 +0000 (11:14 +0900)
Change-Id: I7fe4e6f35b83821ec453ed6fe4d22557ce7da558
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/SharedVars.h [moved from include/shared_vars.h with 52% similarity]
src/server/SharedVars.cpp [new file with mode: 0644]
src/server/shared_vars.cpp [deleted file]

similarity index 52%
rename from include/shared_vars.h
rename to include/SharedVars.h
index a5bb371..4171e1a 100644 (file)
  * limitations under the License.
  */
 
-#ifndef __CONTEXT_SHARED_VARS_H__
-#define __CONTEXT_SHARED_VARS_H__
+#ifndef _CONTEXT_SHARED_VARS_H_
+#define _CONTEXT_SHARED_VARS_H_
 
+#include <glib.h>
 #include <string>
+#include <map>
 
 namespace ctx {
-       namespace shared {
-               extern std::string wifi_bssid;
-       }
+
+       /*
+        * TODO: Maybe later, it would be possible to extend this to support a sort of
+        *       'update observation' feature, i.e., getting notifications when a variable is updated.
+        */
+       class SharedVars {
+       public:
+               enum VarId {
+                       WIFI_BSSID,
+               };
+
+               SharedVars();
+               ~SharedVars();
+
+               const std::string& set(VarId id, std::string value) const;
+               std::string get(VarId id);
+               void clear(VarId id);
+
+       private:
+               static GMutex __mutex;
+               static std::map<VarId, std::string> __varsMap;
+       };
+
 }
 
 #endif
diff --git a/src/server/SharedVars.cpp b/src/server/SharedVars.cpp
new file mode 100644 (file)
index 0000000..f23b8cc
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#include <types_internal.h>
+#include <ScopeMutex.h>
+#include <SharedVars.h>
+
+using namespace ctx;
+
+GMutex SharedVars::__mutex;
+std::map<SharedVars::VarId, std::string> SharedVars::__varsMap;
+
+SharedVars::SharedVars()
+{
+}
+
+SharedVars::~SharedVars()
+{
+}
+
+const std::string& SharedVars::set(SharedVars::VarId id, std::string value) const
+{
+       ScopeMutex sm(&__mutex);
+       _D("var[%d] = %s", id, value.c_str());
+       __varsMap[id] = value;
+       return __varsMap[id];
+}
+
+std::string SharedVars::get(SharedVars::VarId id)
+{
+       ScopeMutex sm(&__mutex);
+       auto it = __varsMap.find(id);
+       if (it != __varsMap.end())
+               return it->second;
+       else
+               return EMPTY_STRING;
+}
+
+void SharedVars::clear(SharedVars::VarId id)
+{
+       ScopeMutex sm(&__mutex);
+       _D("Remove var[%d]", id);
+       __varsMap.erase(id);
+}
diff --git a/src/server/shared_vars.cpp b/src/server/shared_vars.cpp
deleted file mode 100644 (file)
index 7679e41..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
- *
- * 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.
- */
-
-#include <shared_vars.h>
-
-std::string ctx::shared::wifi_bssid = "";