From 5ba9bc46546ff288c2694075106535f116c3f889 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Wed, 6 Apr 2016 13:21:03 +0900 Subject: [PATCH] Applying Tizen C++ coding style to custom context provider Change-Id: I7cdbed7358584669caef5eb7ffc5a59c08b70734 Signed-off-by: Somin Kim --- src/custom/CustomBase.cpp | 91 +++++++++++++++++++ src/custom/{custom_base.h => CustomBase.h} | 36 ++++---- ...provider.cpp => CustomContextProvider.cpp} | 84 ++++++++--------- src/custom/custom_base.cpp | 91 ------------------- 4 files changed, 151 insertions(+), 151 deletions(-) create mode 100644 src/custom/CustomBase.cpp rename src/custom/{custom_base.h => CustomBase.h} (64%) rename src/custom/{custom_context_provider.cpp => CustomContextProvider.cpp} (70%) delete mode 100644 src/custom/custom_base.cpp diff --git a/src/custom/CustomBase.cpp b/src/custom/CustomBase.cpp new file mode 100644 index 0000000..529835b --- /dev/null +++ b/src/custom/CustomBase.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2016 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 +#include "CustomBase.h" + +ctx::CustomBase::CustomBase(std::string subject, std::string name, ctx::Json tmpl, std::string owner) : + __subject(subject), + __name(name), + __tmpl(tmpl), + __owner(owner) +{ +} + +ctx::CustomBase::~CustomBase() +{ +} + +bool ctx::CustomBase::isSupported() +{ + return true; +} + +void ctx::CustomBase::submitTriggerItem() +{ + context_manager::registerTriggerItem(__subject.c_str(), OPS_SUBSCRIBE | OPS_READ, + __tmpl.str(), NULL, __owner.c_str()); +} + +void ctx::CustomBase::unsubmitTriggerItem() +{ + context_manager::unregisterTriggerItem(__subject.c_str()); +} + +int ctx::CustomBase::subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult) +{ + return ERR_NONE; + +} + +int ctx::CustomBase::unsubscribe(const char *subject, ctx::Json option) +{ + return ERR_NONE; +} + +int ctx::CustomBase::read(const char *subject, ctx::Json option, ctx::Json *requestResult) +{ + ctx::Json data = __latest.str(); + ctx::context_manager::replyToRead(__subject.c_str(), NULL, ERR_NONE, data); + return ERR_NONE; +} + +int ctx::CustomBase::write(const char *subject, ctx::Json data, ctx::Json *requestResult) +{ + return ERR_NONE; +} + +void ctx::CustomBase::handleUpdate(ctx::Json data) +{ + // Store latest state + __latest = data.str(); + ctx::context_manager::publish(__subject.c_str(), NULL, ERR_NONE, data); +} + +const char* ctx::CustomBase::getSubject() +{ + return __subject.c_str(); +} + +std::string ctx::CustomBase::getOwner() +{ + return __owner; +} + +ctx::Json ctx::CustomBase::getTemplate() +{ + return __tmpl; +} diff --git a/src/custom/custom_base.h b/src/custom/CustomBase.h similarity index 64% rename from src/custom/custom_base.h rename to src/custom/CustomBase.h index 68e45cc..c95ba5c 100644 --- a/src/custom/custom_base.h +++ b/src/custom/CustomBase.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef _CUSTOM_BASE_H_ -#define _CUSTOM_BASE_H_ +#ifndef _CONTEXT_CUSTOM_BASE_H_ +#define _CONTEXT_CUSTOM_BASE_H_ #include #include @@ -23,33 +23,33 @@ namespace ctx { - class custom_base : public ContextProviderBase { + class CustomBase : public ContextProviderBase { public: - custom_base(std::string subject, std::string name, ctx::Json tmpl, std::string owner); - ~custom_base(); + CustomBase(std::string subject, std::string name, ctx::Json tmpl, std::string owner); + ~CustomBase(); int subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult); int unsubscribe(const char *subject, ctx::Json option); int read(const char *subject, ctx::Json option, ctx::Json *requestResult); int write(const char *subject, ctx::Json data, ctx::Json *requestResult); - static bool is_supported(); - void submit_trigger_item(); - void unsubmit_trigger_item(); + static bool isSupported(); + void submitTriggerItem(); + void unsubmitTriggerItem(); - void handle_update(ctx::Json data); + void handleUpdate(ctx::Json data); - const char* get_subject(); - std::string get_owner(); - ctx::Json get_template(); + const char* getSubject(); + std::string getOwner(); + ctx::Json getTemplate(); private: - std::string _subject; - std::string _name; - ctx::Json _tmpl; - std::string _owner; - ctx::Json latest; + std::string __subject; + std::string __name; + ctx::Json __tmpl; + std::string __owner; + ctx::Json __latest; }; } -#endif // _CUSTOM_BASE_H_ +#endif /* End of _CONTEXT_CUSTOM_BASE_H_ */ diff --git a/src/custom/custom_context_provider.cpp b/src/custom/CustomContextProvider.cpp similarity index 70% rename from src/custom/custom_context_provider.cpp rename to src/custom/CustomContextProvider.cpp index 5bec97e..286f28e 100644 --- a/src/custom/custom_context_provider.cpp +++ b/src/custom/CustomContextProvider.cpp @@ -21,13 +21,13 @@ #include #include #include -#include "custom_base.h" +#include "CustomBase.h" -std::map custom_map; +static std::map __customMap; -static bool is_valid_fact(std::string subject, ctx::Json& fact); -static bool check_value_int(ctx::Json& tmpl, std::string key, int value); -static bool check_value_string(ctx::Json& tmpl, std::string key, std::string value); +static bool __isValidFact(std::string subject, ctx::Json& fact); +static bool __checkValueInt(ctx::Json& tmpl, std::string key, int value); +static bool __checkValueString(ctx::Json& tmpl, std::string key, std::string value); void registerProvider(const char *subject, const char *privilege) { @@ -35,27 +35,27 @@ void registerProvider(const char *subject, const char *privilege) ctx::custom_context_provider::destroy, const_cast(subject), privilege); ctx::context_manager::registerProvider(subject, providerInfo); - custom_map[subject]->submit_trigger_item(); + __customMap[subject]->submitTriggerItem(); } void unregisterProvider(const char* subject) { - custom_map[subject]->unsubmit_trigger_item(); + __customMap[subject]->unsubmitTriggerItem(); ctx::context_manager::unregisterProvider(subject); } EXTAPI ctx::ContextProviderBase* ctx::custom_context_provider::create(void *data) { // Already created in addItem() function. Return corresponding custom provider - return custom_map[static_cast(data)]; + return __customMap[static_cast(data)]; } EXTAPI void ctx::custom_context_provider::destroy(void *data) { - std::map::iterator it = custom_map.find(static_cast(data)); - if (it != custom_map.end()) { + std::map::iterator it = __customMap.find(static_cast(data)); + if (it != __customMap.end()) { delete it->second; - custom_map.erase(it); + __customMap.erase(it); } } @@ -71,15 +71,15 @@ EXTAPI bool ctx::initCustomContextProvider() IF_FAIL_RETURN_TAG(ret, false, _E, "Create template table failed"); // Register custom items - std::string q_select = "SELECT * FROM context_trigger_custom_template"; - ret = db_manager::execute_sync(q_select.c_str(), &record); + std::string qSelect = "SELECT * FROM context_trigger_custom_template"; + ret = db_manager::execute_sync(qSelect.c_str(), &record); IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); IF_FAIL_RETURN(record.size() > 0, true); int error; - std::vector::iterator vec_end = record.end(); - for (std::vector::iterator vec_pos = record.begin(); vec_pos != vec_end; ++vec_pos) { - ctx::Json elem = *vec_pos; + std::vector::iterator vedEnd = record.end(); + for (auto vecPos = record.begin(); vecPos != vedEnd; ++vecPos) { + ctx::Json elem = *vecPos; std::string subject; std::string name; std::string attributes; @@ -98,13 +98,13 @@ EXTAPI bool ctx::initCustomContextProvider() return true; } -EXTAPI int ctx::custom_context_provider::addItem(std::string subject, std::string name, ctx::Json tmpl, const char* owner, bool is_init) +EXTAPI int ctx::custom_context_provider::addItem(std::string subject, std::string name, ctx::Json tmpl, const char* owner, bool isInit) { - std::map::iterator it; - it = custom_map.find(subject); + std::map::iterator it; + it = __customMap.find(subject); - if (it != custom_map.end()) { - if ((it->second)->get_template() != tmpl) { // Same item name, but different template + if (it != __customMap.end()) { + if ((it->second)->getTemplate() != tmpl) { // Same item name, but different template return ERR_DATA_EXIST; } // Same item name with same template @@ -112,14 +112,14 @@ EXTAPI int ctx::custom_context_provider::addItem(std::string subject, std::strin } // Create custom base - ctx::custom_base* custom = new(std::nothrow) custom_base(subject, name, tmpl, owner); + ctx::CustomBase* custom = new(std::nothrow) CustomBase(subject, name, tmpl, owner); IF_FAIL_RETURN_TAG(custom, ERR_OUT_OF_MEMORY, _E, "Memory allocation failed"); - custom_map[subject] = custom; + __customMap[subject] = custom; - registerProvider(custom->get_subject(), NULL); + registerProvider(custom->getSubject(), NULL); // Add item to custom template db - if (!is_init) { + if (!isInit) { std::string q = "INSERT OR IGNORE INTO context_trigger_custom_template (subject, name, attributes, owner) VALUES ('" + subject + "', '" + name + "', '" + tmpl.str() + "', '" + owner + "'); "; std::vector record; @@ -132,9 +132,9 @@ EXTAPI int ctx::custom_context_provider::addItem(std::string subject, std::strin EXTAPI int ctx::custom_context_provider::removeItem(std::string subject) { - std::map::iterator it; - it = custom_map.find(subject); - IF_FAIL_RETURN_TAG(it != custom_map.end(), ERR_NOT_SUPPORTED, _E, "%s not supported", subject.c_str()); + std::map::iterator it; + it = __customMap.find(subject); + IF_FAIL_RETURN_TAG(it != __customMap.end(), ERR_NOT_SUPPORTED, _E, "%s not supported", subject.c_str()); unregisterProvider(subject.c_str()); @@ -149,20 +149,20 @@ EXTAPI int ctx::custom_context_provider::removeItem(std::string subject) EXTAPI int ctx::custom_context_provider::publishData(std::string subject, ctx::Json fact) { - std::map::iterator it; - it = custom_map.find(subject); - IF_FAIL_RETURN_TAG(it != custom_map.end(), ERR_NOT_SUPPORTED, _E, "%s not supported", subject.c_str()); + std::map::iterator it; + it = __customMap.find(subject); + IF_FAIL_RETURN_TAG(it != __customMap.end(), ERR_NOT_SUPPORTED, _E, "%s not supported", subject.c_str()); - bool ret = is_valid_fact(subject, fact); + bool ret = __isValidFact(subject, fact); IF_FAIL_RETURN_TAG(ret, ERR_INVALID_DATA, _E, "Invalid fact(%s)", subject.c_str()); - custom_map[subject]->handle_update(fact); + __customMap[subject]->handleUpdate(fact); return ERR_NONE; } -bool is_valid_fact(std::string subject, ctx::Json& fact) +bool __isValidFact(std::string subject, ctx::Json& fact) { - ctx::Json tmpl = custom_map[subject]->get_template(); + ctx::Json tmpl = __customMap[subject]->getTemplate(); IF_FAIL_RETURN_TAG(tmpl != EMPTY_JSON_OBJECT, false, _E, "Failed to get template"); bool ret; @@ -179,14 +179,14 @@ bool is_valid_fact(std::string subject, ctx::Json& fact) ret = fact.get(NULL, key.c_str(), &val); IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid data type"); - ret = check_value_int(tmpl, key, val); + ret = __checkValueInt(tmpl, key, val); IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid value"); } else if (type == "string") { std::string val_str; ret = fact.get(NULL, key.c_str(), &val_str); IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid data type"); - ret = check_value_string(tmpl, key, val_str); + ret = __checkValueString(tmpl, key, val_str); IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid value"); } else { _E("Custom fact: invalid data type"); @@ -197,7 +197,7 @@ bool is_valid_fact(std::string subject, ctx::Json& fact) return true; } -bool check_value_int(ctx::Json& tmpl, std::string key, int value) +bool __checkValueInt(ctx::Json& tmpl, std::string key, int value) { int min, max; @@ -212,16 +212,16 @@ bool check_value_int(ctx::Json& tmpl, std::string key, int value) return true; } -bool check_value_string(ctx::Json& tmpl, std::string key, std::string value) +bool __checkValueString(ctx::Json& tmpl, std::string key, std::string value) { // case1: any value is accepted if (tmpl.getSize(key.c_str(), "values") <= 0) return true; // case2: check acceptable value - std::string t_val; - for (int i = 0; tmpl.getAt(key.c_str(), "values", i, &t_val); i++) { - if (t_val == value) + std::string tmplValue; + for (int i = 0; tmpl.getAt(key.c_str(), "values", i, &tmplValue); i++) { + if (tmplValue == value) return true; } diff --git a/src/custom/custom_base.cpp b/src/custom/custom_base.cpp deleted file mode 100644 index 4371202..0000000 --- a/src/custom/custom_base.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2016 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 -#include "custom_base.h" - -ctx::custom_base::custom_base(std::string subject, std::string name, ctx::Json tmpl, std::string owner) : - _subject(subject), - _name(name), - _tmpl(tmpl), - _owner(owner) -{ -} - -ctx::custom_base::~custom_base() -{ -} - -bool ctx::custom_base::is_supported() -{ - return true; -} - -void ctx::custom_base::submit_trigger_item() -{ - context_manager::registerTriggerItem(_subject.c_str(), OPS_SUBSCRIBE | OPS_READ, - _tmpl.str(), NULL, _owner.c_str()); -} - -void ctx::custom_base::unsubmit_trigger_item() -{ - context_manager::unregisterTriggerItem(_subject.c_str()); -} - -int ctx::custom_base::subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult) -{ - return ERR_NONE; - -} - -int ctx::custom_base::unsubscribe(const char *subject, ctx::Json option) -{ - return ERR_NONE; -} - -int ctx::custom_base::read(const char *subject, ctx::Json option, ctx::Json *requestResult) -{ - ctx::Json data = latest.str(); - ctx::context_manager::replyToRead(_subject.c_str(), NULL, ERR_NONE, data); - return ERR_NONE; -} - -int ctx::custom_base::write(const char *subject, ctx::Json data, ctx::Json *requestResult) -{ - return ERR_NONE; -} - -void ctx::custom_base::handle_update(ctx::Json data) -{ - // Store latest state - latest = data.str(); - ctx::context_manager::publish(_subject.c_str(), NULL, ERR_NONE, data); -} - -const char* ctx::custom_base::get_subject() -{ - return _subject.c_str(); -} - -std::string ctx::custom_base::get_owner() -{ - return _owner; -} - -ctx::Json ctx::custom_base::get_template() -{ - return _tmpl; -} -- 2.34.1