bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& fullName, const Array::Ptr& templates, const Dictionary::Ptr& attrs, const Array::Ptr& errors) { NameComposer *nc = dynamic_cast<NameComposer *>(type.get()); Dictionary::Ptr nameParts; String name; if (nc) { nameParts = nc->ParseName(fullName); name = nameParts->Get("name"); } else name = fullName; ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(); builder->SetType(type->GetName()); builder->SetName(name); builder->SetScope(ScriptGlobal::GetGlobals()); builder->SetPackage("_api"); if (templates) { ObjectLock olock(templates); BOOST_FOREACH(const String& tmpl, templates) { ImportExpression *expr = new ImportExpression(MakeLiteral(tmpl)); builder->AddExpression(expr); } }
static inline Value NewObject(ScriptFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter, const String& zone, const String& package, bool ignoreOnError, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo()) { ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo); String checkName = name; if (!abstract) { Type::Ptr ptype = Type::GetByName(type); NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get()); if (nc) checkName = nc->MakeName(name, Dictionary::Ptr()); } if (!checkName.IsEmpty()) { ConfigItem::Ptr oldItem = ConfigItem::GetByTypeAndName(type, checkName); if (oldItem) { std::ostringstream msgbuf; msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo(); BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo)); } } item->SetType(type); item->SetName(name); item->AddExpression(new OwnedExpression(expression)); item->SetAbstract(abstract); item->SetScope(EvaluateClosedVars(frame, closedVars)); item->SetZone(zone); item->SetPackage(package); item->SetFilter(filter); item->SetIgnoreOnError(ignoreOnError); item->Compile()->Register(); return Empty; }