Skip to content

Commit 3d26e0a

Browse files
committed
fable: Add set_factory method to Factory schema
1 parent 2d03edb commit 3d26e0a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

fable/include/fable/schema/factory.hpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,27 @@ class FactoryBase : public Base<CRTP> {
192192
bool has_factory(const std::string& key) const { return available_.count(key); }
193193

194194
/**
195-
* Add a factory with the given key, schema, and function.
195+
* Add a factory with the given key, schema, and function, provided it
196+
* doesn't already exist.
197+
*
198+
* Return true if successful, false otherwise.
196199
*/
197-
void add_factory(const std::string& key, Box&& s, MakeFunc f) {
200+
bool add_factory(const std::string& key, Box&& s, MakeFunc f) {
201+
if (!available_.count(key)) {
202+
available_.insert(std::make_pair(key, TypeFactory{std::move(s), f}));
203+
reset_schema();
204+
return true;
205+
}
206+
return false;
207+
}
208+
209+
/**
210+
* Add or replace a factory with the given key, schema, and function.
211+
*/
212+
void set_factory(const std::string& key, Box&& s, MakeFunc f) {
213+
if (!available_.count(key)) {
214+
available_.erase(key);
215+
}
198216
available_.insert(std::make_pair(key, TypeFactory{std::move(s), f}));
199217
reset_schema();
200218
}

0 commit comments

Comments
 (0)