Suppose I have a class for working with XML, and I want to add a method to it, but that the class does not know about it. Simple inheritance does not fit. SimpleXmlNode CreateChild(const std::string& name); use simple inheritance, then let's say the function SimpleXmlNode CreateChild(const std::string& name); will return an object that does not have a new method each and it will not be available in the script. The word mixin comes to mind, but I don’t think anything. If done on templates, you have to put everything into header files, and I really would like to avoid it.
class SimpleXmlNode { public: explicit SimpleXmlNode(); SimpleXmlNode(const SimpleXmlNode& node); virtual ~SimpleXmlNode(); const std::string Attribute(const std::string& name) const; SimpleXmlNode operator[](const std::string& name); int AttributeInt(const std::string& name) const; // Write SimpleXmlNode CreateChild(const std::string& name); } I want to add the function each :
SimpleXmlNode& each(Sqrat::Function callback) { int i = 0; for (auto& node: childs) { Sqrat::SharedPtr<bool> res = callback.Evaluate<bool>(i, node); if (!!res && *res) { break; } i++; } return *this; } For you to understand, Sqrat::Function callback is a callback function written in a scripting language.
I need to call each function from a script:
node.each(function(index,elem){ /*do something*/}); And the class itself is registered in the scripting virtual machine:
vm.GetRootTable().Bind("SimpleXmlNode", Class<SimpleXmlNode>(vm.GetVM(), "SimpleXmlNode"). Func("Attribute", &SimpleXmlNode::Attribute). Func("AttributeInt", &SimpleXmlNode::AttributeInt). ... Func("GetAttributeCount", &SimpleXmlNode::GetAttributeCount) Func("each", &SimpleXmlNode::each) ); But I don’t want the SimpleXmlNode class SimpleXmlNode know anything about these scripts and depend on extra modules.
eachfunction free. - Abyx