-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
108 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#include "context_impl.hpp" | ||
|
||
#include "logging.hpp" | ||
#include "model/model_context_manager.hpp" | ||
#include "model/model_graph_impl.hpp" | ||
|
||
namespace ark { | ||
|
||
Context::Impl::Impl(Model& model) | ||
: context_manager_(std::make_shared<ModelContextManager>(model)) { | ||
static size_t next_id = 0; | ||
id_ = next_id++; | ||
} | ||
|
||
void Context::Impl::set(const std::string& key, const Json& value_json, | ||
ContextType type) { | ||
if (type == ContextType::Overwrite) { | ||
context_manager_->set(key, value_json); | ||
} else if (type == ContextType::Extend) { | ||
auto ctx = context_manager_->get(key); | ||
if (ctx.empty()) { | ||
context_manager_->set(key, value_json); | ||
} else if (!ctx.is_object() || !value_json.is_object()) { | ||
ERR(InvalidUsageError, | ||
"Context value must be a JSON object when type is " | ||
"ContextTypeExtend. Key: ", | ||
key, ", old value: ", ctx.dump(), | ||
", new value: ", value_json.dump()); | ||
} else { | ||
for (const auto& [k, v] : value_json.items()) { | ||
ctx[k] = v; | ||
} | ||
context_manager_->set(key, ctx); | ||
} | ||
} else if (type == ContextType::Immutable) { | ||
if (!context_manager_->has(key)) { | ||
context_manager_->set(key, value_json); | ||
} | ||
} else { | ||
ERR(InvalidUsageError, "Unknown context type"); | ||
} | ||
} | ||
|
||
} // namespace ark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#ifndef ARK_CONTEXT_IMPL_HPP_ | ||
#define ARK_CONTEXT_IMPL_HPP_ | ||
|
||
#include "ark/context.hpp" | ||
#include "model/model_json.hpp" | ||
|
||
namespace ark { | ||
|
||
class ModelContextManager; | ||
|
||
class Context::Impl { | ||
public: | ||
Impl(Model& model); | ||
|
||
void set(const std::string& key, const Json& value_json, ContextType type); | ||
|
||
protected: | ||
friend class Context; | ||
|
||
std::shared_ptr<ModelContextManager> context_manager_; | ||
size_t id_; | ||
}; | ||
|
||
} // namespace ark | ||
|
||
#endif // ARK_CONTEXT_IMPL_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters