From: Mu-Woong Lee Date: Thu, 3 Aug 2017 07:03:27 +0000 (+0900) Subject: Add an API function to duplicate job context handle X-Git-Tag: submit/tizen/20170806.070303~1^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0e52c8954be08928387e1b15ebc93aaa2f8bcd8f;p=platform%2Fcore%2Fcontext%2Fjob-scheduler.git Add an API function to duplicate job context handle Change-Id: If538c2fb6746f5811e393ae7fa837e92186f5a28 Signed-off-by: Mu-Woong Lee --- diff --git a/include/job_scheduler_internal.h b/include/job_scheduler_internal.h index b6b0d78..9a46bb4 100644 --- a/include/job_scheduler_internal.h +++ b/include/job_scheduler_internal.h @@ -511,6 +511,8 @@ int ctx_sched_custom_register(ctx_sched_h scheduler, const char* uri); int ctx_sched_custom_unregister(ctx_sched_h scheduler, const char* uri); int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char* uri, const char* pkg_id, bool* registered); +// Job context handler duplicator +ctx_sched_job_context_h ctx_sched_job_context_duplicate(ctx_sched_job_context_h job_context); /** * @} diff --git a/src/client-dummy/job_scheduler.cpp b/src/client-dummy/job_scheduler.cpp index cf71d3f..f91f801 100644 --- a/src/client-dummy/job_scheduler.cpp +++ b/src/client-dummy/job_scheduler.cpp @@ -281,3 +281,8 @@ EXPORT_API int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char* { return E_SUPPORT; } + +EXPORT_API ctx_sched_job_context_h ctx_sched_job_context_duplicate(ctx_sched_job_context_h job_context) +{ + return NULL; +} diff --git a/src/client/job_scheduler.cpp b/src/client/job_scheduler.cpp index fc58284..a53e210 100644 --- a/src/client/job_scheduler.cpp +++ b/src/client/job_scheduler.cpp @@ -689,3 +689,27 @@ EXPORT_API int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char* return E_NONE; } + +EXPORT_API ctx_sched_job_context_h ctx_sched_job_context_duplicate(ctx_sched_job_context_h job_context) +{ + IF_FAIL_RETURN(job_context, NULL); + + ctx_sched_job_context_s* dup = new(std::nothrow) ctx_sched_job_context_s(); + IF_FAIL_RETURN_TAG(dup, NULL, _E, E_STR_ALLOC); + + if (!job_context->jobContext) + return dup; + + if (job_context->jobContext->getType() == JobContext::Type::TRIGGER) + dup->jobContext = new(std::nothrow) JobTrigger(*static_cast(job_context->jobContext)); + else + dup->jobContext = new(std::nothrow) JobRequirement(*static_cast(job_context->jobContext)); + + if (!dup->jobContext) { + _E_ALLOC; + delete dup; + return NULL; + } + + return dup; +} diff --git a/src/shared/Attribute.cpp b/src/shared/Attribute.cpp index aafc6b6..b7a36d9 100644 --- a/src/shared/Attribute.cpp +++ b/src/shared/Attribute.cpp @@ -55,6 +55,23 @@ Attribute* Attribute::build(const std::string& name, Json::Value& jsonNode) return NULL; } +Attribute* Attribute::build(const Attribute& other) +{ + Attribute::Type type = other.getType(); + + if (type == Attribute::Type::INTEGER) { + Attribute* dup = new IntegerAttribute(other.getName()); + *dup = other; + return dup; + + } else if (type == Attribute::Type::STRING) { + Attribute* dup = new StringAttribute(other.getName()); + *dup = other; + return dup; + } + + return NULL; +} IntegerAttribute::IntegerAttribute(const std::string& name) : Attribute(name), diff --git a/src/shared/Attribute.h b/src/shared/Attribute.h index e774b3a..9835132 100644 --- a/src/shared/Attribute.h +++ b/src/shared/Attribute.h @@ -45,6 +45,7 @@ namespace ctx { virtual bool operator<=(const std::string& antecedent) = 0; static Attribute* build(const std::string& name, Json::Value& jsonNode); + static Attribute* build(const Attribute& other); protected: Attribute(const std::string& name); diff --git a/src/shared/JobContext.cpp b/src/shared/JobContext.cpp index 39a26c8..6442b1c 100644 --- a/src/shared/JobContext.cpp +++ b/src/shared/JobContext.cpp @@ -24,6 +24,21 @@ using namespace ctx; +static void __dup_attributes(std::list& dest, std::list& source) +{ + for (auto& attr : dest) { + delete attr; + } + + dest.clear(); + + for (auto& attr : source) { + Attribute* dup = Attribute::build(*attr); + if (dup) + dest.push_back(dup); + } +} + JobContext::JobContext(const std::string& uri) : __uri(uri), __disjunction(false) @@ -37,12 +52,20 @@ JobContext::JobContext(const std::string& uri, Json::Value& jsonNode) : Json::Value::Members members = attrNode.getMemberNames(); for (auto& name : members) { - __attributes.push_back(Attribute::build(name, attrNode[name])); + Attribute* attr = Attribute::build(name, attrNode[name]); + if (attr) + __attributes.push_back(attr); } __disjunction = jsonNode[KEY_DISJUNCTION].asBool(); } +JobContext::JobContext(JobContext& other) : + __uri(other.__uri), + __disjunction(other.__disjunction) +{ +} + JobContext::~JobContext() { for (auto& attr : __attributes) { @@ -156,6 +179,12 @@ JobTrigger::JobTrigger(const std::string& uri, Json::Value& jsonNode) : { } +JobTrigger::JobTrigger(JobTrigger& other) : + JobContext(static_cast(other)) +{ + __dup_attributes(__attributes, other.__attributes); +} + JobTrigger::~JobTrigger() { } @@ -182,6 +211,13 @@ JobRequirement::JobRequirement(const std::string& uri, Json::Value& jsonNode) : __optional = jsonNode[KEY_OPTIONAL].asBool(); } +JobRequirement::JobRequirement(JobRequirement& other) : + JobContext(static_cast(other)), + __optional(other.__optional) +{ + __dup_attributes(__attributes, other.__attributes); +} + JobRequirement::~JobRequirement() { } diff --git a/src/shared/JobContext.h b/src/shared/JobContext.h index 94986b9..09dfa61 100644 --- a/src/shared/JobContext.h +++ b/src/shared/JobContext.h @@ -57,14 +57,15 @@ namespace ctx { protected: JobContext(const std::string& uri); JobContext(const std::string& uri, Json::Value& jsonNode); - - private: - virtual void __toJson(Json::Value& jsonNode) const = 0; + JobContext(JobContext& other); std::string __uri; std::list __attributes; bool __disjunction; + + private: + virtual void __toJson(Json::Value& jsonNode) const = 0; }; @@ -72,6 +73,7 @@ namespace ctx { public: JobTrigger(const std::string& uri); JobTrigger(const std::string& uri, Json::Value& jsonNode); + JobTrigger(JobTrigger& other); ~JobTrigger(); JobContext::Type getType() const; @@ -85,6 +87,7 @@ namespace ctx { public: JobRequirement(const std::string& uri, bool optional); JobRequirement(const std::string& uri, Json::Value& jsonNode); + JobRequirement(JobRequirement& other); ~JobRequirement(); JobContext::Type getType() const;