Skip to content

Commit

Permalink
fable: Add set_factory method to Factory schema
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed May 22, 2021
1 parent 2d03edb commit 3d26e0a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions fable/include/fable/schema/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,27 @@ class FactoryBase : public Base<CRTP> {
bool has_factory(const std::string& key) const { return available_.count(key); }

/**
* Add a factory with the given key, schema, and function.
* Add a factory with the given key, schema, and function, provided it
* doesn't already exist.
*
* Return true if successful, false otherwise.
*/
void add_factory(const std::string& key, Box&& s, MakeFunc f) {
bool add_factory(const std::string& key, Box&& s, MakeFunc f) {
if (!available_.count(key)) {
available_.insert(std::make_pair(key, TypeFactory{std::move(s), f}));
reset_schema();
return true;
}
return false;
}

/**
* Add or replace a factory with the given key, schema, and function.
*/
void set_factory(const std::string& key, Box&& s, MakeFunc f) {
if (!available_.count(key)) {
available_.erase(key);
}
available_.insert(std::make_pair(key, TypeFactory{std::move(s), f}));
reset_schema();
}
Expand Down

0 comments on commit 3d26e0a

Please sign in to comment.