Пример #1
0
bool PhpParam::defValueNeedsVariable() const {
  DataType cppKindOf = kindOf();

  if (!hasDefault() || !isIndirectPass()) {
    return false;
  }

  fbstring defVal = getDefault();

  if (cppKindOf == KindOfString &&
      ((defVal == "empty_string") ||
       (defVal == "null_string") ||
       (g_knownStringConstants.count(defVal) > 0))) {
    return false;
  }
  if ((cppKindOf == KindOfArray) && (defVal == "null_array")) {
    return false;
  }
  if ((cppKindOf == KindOfObject) && (defVal == "null_object")) {
    return false;
  }
  if ((cppKindOf == KindOfResource) && (defVal == "null_resource")) {
    return false;
  }
  if ((cppKindOf == KindOfAny) && (defVal == "null_variant")) {
    return false;
  }

  return true;
}
Пример #2
0
 double TableColumn::min( ) const {
     if (hasDefault())
         throw std::invalid_argument("Can not lookup elements in a column with defaulted values.");
     if (m_values.size() > 0)
         return *std::min_element( m_values.begin() , m_values.end());
     else
         throw std::invalid_argument("Can not find max in empty column");
 }
Пример #3
0
    double ParserDoubleItem::getDefault() const {
        if (hasDefault())
            return m_default;

        if (sizeType() == Opm::ALL)
            return std::numeric_limits<double>::quiet_NaN();

        throw std::invalid_argument("No default value available for item "+name());
    }
Пример #4
0
    int ParserIntItem::getDefault() const {
        if (hasDefault())
            return m_default;

        if (sizeType() == Opm::ALL)
            return -1;

        throw std::invalid_argument("No default value available for item "+name());
    }
Пример #5
0
    TableIndex TableColumn::lookup( double argValue ) const {
        if (!m_schema.lookupValid( ))
            throw std::invalid_argument("Must have an ordered column to perform table argument lookup.");

        if (size() < 1)
            throw std::invalid_argument("Must have at least one elements in column for table argument lookup.");

        if (hasDefault())
            throw std::invalid_argument("Can not lookup elements in a column with defaulted values.");

        if (argValue >= max()) {
            const auto max_iter = std::max_element( m_values.begin() , m_values.end());
            const size_t max_index = max_iter - m_values.begin();
            return TableIndex( max_index , 1.0 );
        }

        if (argValue <= min()) {
            const auto min_iter = std::min_element( m_values.begin() , m_values.end());
            const size_t min_index = min_iter - m_values.begin();
            return TableIndex( min_index , 1.0 );
        }

        {
            bool isDescending = m_schema.isDecreasing( );
            size_t lowIntervalIdx = 0;
            size_t intervalIdx = (size() - 1)/2;
            size_t highIntervalIdx = size() - 1;
            double weight1;

            while (lowIntervalIdx + 1 < highIntervalIdx) {
                if (isDescending) {
                    if (m_values[intervalIdx] < argValue)
                        highIntervalIdx = intervalIdx;
                    else
                        lowIntervalIdx = intervalIdx;
                }
                else {
                    if (m_values[intervalIdx] < argValue)
                        lowIntervalIdx = intervalIdx;
                    else
                        highIntervalIdx = intervalIdx;
                }

                intervalIdx = (highIntervalIdx + lowIntervalIdx)/2;
            }

            weight1 = 1 - (argValue - m_values[intervalIdx])/(m_values[intervalIdx + 1] - m_values[intervalIdx]);

            return TableIndex( intervalIdx , weight1 );
        }
    }
Пример #6
0
fbstring PhpParam::getDefault() const {
  if (!hasDefault()) {
    return "";
  }
  auto value = m_param["value"].asString();
  if (value == "null") {
    switch (kindOf()) {
      case KindOfString: return "null_string";
      case KindOfArray: return "null_array";
      case KindOfObject: return "null_object";
      case KindOfResource: return "null_object";
      case KindOfAny: return "null_variant";
      default: break;
    }
  }
  return value;
}
Пример #7
0
bool KConfigGroup::hasDefault(const QString &key) const
{
    return hasDefault(key.toUtf8().constData());
}
Пример #8
0
int ChoiceField::Choice::initIds() {
	int id = first_id + (hasDefault() ? 1 : 0);
	FOR_EACH(c, choices) {
		c->first_id = id;
		id = c->initIds();
	}
Пример #9
0
	///----- SharedParallelAndTaskClause -----
	std::ostream& SharedParallelAndTaskClause::dump(std::ostream& out) const {
		if(hasIf()) { out << "if(" << *ifClause << "), "; }
		if(hasDefault()) { defaultClause->dump(out) << ", "; }
		if(hasShared()) { out << "shared(" << join(",", *sharedClause) << "), "; }
		return out;
	}
Пример #10
0
void Element::generate() {
    QString eh = QString();
    QString ehprivate = QString();
    QString ehpublic = QString();
    QString ehinclude = QString();
    QString ecppinclude = QString();
    QString ecpp = QString();

    Type *t = this->getType();

    if (t->hasSimpleContent()) {
        SimpleType *st = (SimpleType*) t;
        Attribute *a = new Attribute("value");
        if (hasDefault()) {
            a->setDefault(getDefault());
        }
        a->setSimpleType(st);
        AttributeUse *au = new AttributeUse(a);
        t->addAtribute(au);
    }

    // get all elements and attributes to generates
    QHash<QString, AttributeUse*> attributes = QHash<QString, AttributeUse*>();
    QHash<QString, ElementUse*> elements = QHash<QString, ElementUse*>();

    Type *tt = t;
    do {
        QList<AttributeUse*> *aus = tt->getAttributes();
        QList<ElementUse*> *eus;
        if (tt->hasComplexContent()) {
            eus = ((ComplexType*) tt)->getElements();
            for (int i = 0; i < eus->size(); i++) {
                ElementUse *eu = eus->at(i);
                QString name = eu->getElement()->getName();
                if (!elements.contains(name)) {
                    elements.insert(name, eu);
                } else {
                    qDebug() << "element overridden";
                }
            }
        }
        for (int i = 0; i < aus->size(); i++) {
            AttributeUse *au = aus->at(i);
            QString name = au->getAttribute()->getName();
            if (!attributes.contains(name)) {
                attributes.insert(name, au);
            } else {
                qDebug() << "attribute overridden";
            }
        }
        tt = tt->getBase();
    } while (tt != NULL);

    QString cname = className(getName());

    // generate attributes
    QList<AttributeUse*> attributesValues = attributes.values();
    for (int i = 0; i < attributesValues.size(); i++) {
        AttributeUse *au = attributesValues.at(i);
        au->generate(cname, ehprivate, ehpublic, ehinclude, ecpp, ecppinclude);
    }

    // generate elements
    QList<ElementUse*> elementsValues = elements.values();
    for (int i = 0; i < elementsValues.size(); i++) {
        ElementUse *eu = elementsValues.at(i);
        eu->generate(cname, ehprivate, ehpublic, ehinclude, ecpp, ecppinclude);
    }

    QList<QString> elementsNames = elements.keys();
    QList<QString> attributesNames = attributes.keys();

    // concatenate .h
    eh.append("#ifndef " + cname + "_H_\n");
    eh.append("#define " + cname + "_H_\n");
    eh.append("#include <QtCore/QtCore>\n");
    eh.append("#include <QDomDocument>\n");
    eh.append(ehinclude);
    eh.append("\n");

    eh.append("class " + cname + " {\n");

    eh.append("public:\n");
    eh.append(cname + "() {\n");
    for (int i = 0; i < elementsNames.size(); i++) {
        eh.append("  " + methodName(elementsNames.at(i), "init") + "();\n");
    }
    for (int i = 0; i < attributesNames.size(); i++) {
        eh.append("  " + methodName(attributesNames.at(i), "init") + "();\n");
    }
    eh.append("}\n");
    eh.append("static " + cname + "* fromFile( QFile &file);\n");
    eh.append("static " + cname + "* fromElement( QDomElement &element);\n");
    eh.append("static QDomDocument * loadXmlDocument(QFile & file);\n");
    eh.append(ehpublic);
    eh.append("\n");

    eh.append("private:\n");
    eh.append("void addSubElement( QDomElement &element);\n");
    eh.append("void setAttribute( QDomAttr &attr);\n");
    eh.append(ehprivate);
    eh.append("\n");

    eh.append("};\n");
    eh.append("#endif");

    // concatenate .cpp
    ecppinclude.prepend("#include \"" + cname + ".h\"\n");
    ecppinclude.append("\n");

    ecpp.prepend(ecppinclude);
    // static fromElement
    ecpp.append(
        cname + " * " + cname + "::fromElement(QDomElement &element) {\n");
    ecpp.append(
        "  " + cname + " *" + varName(cname, QString(), "_") + " = new " + cname + "();\n");
    ecpp.append("  QDomNodeList list_ = element.childNodes();\n");
    ecpp.append("  for (int i = 0; i < list_.size(); i++) {\n");
    ecpp.append("    if (list_.at(i).isElement()) {\n");
    ecpp.append("      QDomElement e = list_.at(i).toElement();\n");
    ecpp.append("      " + varName(cname, QString(), "_") + "->addSubElement(e);\n");
    ecpp.append("    }\n");
    ecpp.append("  }\n");
    ecpp.append("  QDomNamedNodeMap attrs_ = element.attributes();\n");
    ecpp.append("  for(int i = 0; i<attrs_.size(); i++) {\n");
    ecpp.append("    QDomAttr a = attrs_.item(i).toAttr();\n");
    ecpp.append("    " + varName(cname, QString(), "_") + "->setAttribute(a);\n");
    ecpp.append("  }\n");
    if (t->hasSimpleContent()) {
        ecpp.append("  " + varName(cname, QString(), "_") + "->setValue(element.text());\n");
    }
    ecpp.append("  return " + varName(cname, QString(), "_") + ";\n");
    ecpp.append("}\n");

    // addSubElement
    ecpp.append("void " + cname + "::addSubElement( QDomElement &element) {\n");
    if (Settings::settings()->isDebug()) {
        ecpp.append("  qDebug() << element.localName() <<\"subElement\";\n");
    }
    for (int i = 0; i < elementsNames.size(); i++) {
        QString cn = className(elementsNames.at(i));
        ecpp.append(
            "  if(element.localName().compare(\"" + cn
            + "\", Qt::CaseInsensitive)==0) {\n");
        ecpp.append("    " + cn + " *cn = " + cn + "::fromElement(element);\n");
        ecpp.append("    " + methodName(cn, "add") + "(cn);\n");
        ecpp.append("    return;\n");
        ecpp.append("  }\n");
    }
    ecpp.append("}\n");

    // setAttribute
    ecpp.append("void " + cname + "::setAttribute( QDomAttr &attr) {\n");

    for (int i = 0; i < attributesNames.size(); i++) {
        QString an = className(attributesNames.at(i));
        ecpp.append(
            "  if(attr.localName().compare(\"" + an
            + "\", Qt::CaseInsensitive)==0) {\n");
        ecpp.append("    " + methodName(an, "set") + "(attr.value());\n");
        ecpp.append("    return;\n");
        ecpp.append("  }\n");
    }
    ecpp.append("}\n");

    // static loadXmlDocument
    ecpp.append(
        "QDomDocument * " + cname + "::loadXmlDocument(QFile & file) {\n");
    ecpp.append("  if (!file.exists()) {\n");
    ecpp.append("    qDebug() << \"doesn't exist\";\n");
    ecpp.append("    return NULL;\n");
    ecpp.append("  }\n");
    ecpp.append("  QString errorStr;\n");
    ecpp.append("  int errorLine;\n");
    ecpp.append("  int errorColumn;\n");
    ecpp.append("  QDomDocument *doc = new QDomDocument();\n");
    ecpp.append(
        "  if (!doc->setContent(&file, true, &errorStr, &errorLine, &errorColumn)) {\n");
    ecpp.append(
        "    qDebug()<< QString(\"Error > %1 < in file %2 on line %3, column %4\").arg(\n");
    ecpp.append(
        "      errorStr).arg(file.fileName()).arg(errorLine).arg(errorColumn);\n");
    ecpp.append("    delete doc;\n");
    ecpp.append("    doc = NULL;\n");
    ecpp.append("  }\n");
    ecpp.append("  return doc;\n");
    ecpp.append("}\n");

    // static fromFile
    ecpp.append(cname + "  * " + cname + "::fromFile(QFile & file) {\n");
    ecpp.append(
        "  QDomDocument *doc = " + cname + "::loadXmlDocument(file);\n");
    ecpp.append("  if(doc==NULL) {\n");
    ecpp.append("    return NULL;\n");
    ecpp.append("  }\n");
    ecpp.append("  QDomElement root = doc->documentElement();\n");
    ecpp.append("  return " + cname + "::fromElement(root);\n");
    ecpp.append("}\n");

    QString d = Settings::settings()->getDir();
    saveToFile(d + "/" + cname + ".h", eh);
    saveToFile(d + "/" + cname + ".cpp", ecpp);
}
Пример #11
0
void AttributeUse::generate(QString &className, QString &ehprivate,
		QString &ehpublic, QString &ehinclude, QString &ecpp,
		QString &eccinclude) {
	Attribute *a = getAttribute();
	SimpleType *st = a->getSimpleType();
	QString dt = st->getDataType();
	QString name = unqualify(a->getName());

	// data model, ends with underscore
	ehprivate.append(dt + " " + varName(name, QString(), "Value") + ";\n");
	ehprivate.append("QString " + varName(name, QString(), "RawValue") + ";\n");
	ehprivate.append("bool " + varName(name, "has", "Value") + ";\n");

	// has ###, returns true also if default exists
	ehpublic.append("bool " + methodName(name, "has") + "() {\n");
	ehpublic.append("  return " + varName(name, "has", "Value") + ";\n");
	ehpublic.append("}\n");

	// is ### Valid, returns true if input can be set
	ehpublic.append(
			"bool " + methodName(name, "is", "Valid") + "(QString input);\n");
	ecpp.append(
			"bool " + className + "::" + methodName(name, "is", "Valid")
					+ "(QString input) {\n");
	ecpp.append("  bool ok;\n");
	// inserts control which expects string variable input and bool ok
	ecpp.append("  " + st->generateControl("input") + "\n");
	ecpp.append("  return ok;\n");
	ecpp.append("}\n");

	// get ###, returns default if exists or value if set
	ehpublic.append(dt + " " + methodName(name, "get") + "() {\n");
	ehpublic.append("  return " + varName(name, QString(), "Value") + ";\n");
	ehpublic.append("}\n");

	// getRaw ###, returns default if exists or value if set
	ehpublic.append("QString " + methodName(name, "getRaw") + "() {\n");
	ehpublic.append("  return " + varName(name, QString(), "RawValue") + ";\n");
	ehpublic.append("}\n");

	// set ###, doesn't necessarily do tests
	ehprivate.append("void " + methodName(name, "set") + "(QString input);\n");
	ecpp.append(
			"void " + className + "::" + methodName(name, "set")
					+ "(QString input) {\n");
	// inserts setting mechanism, expects string variable input and model variable
	ecpp.append(
			"  "
					+ st->generateSetter("input",
							varName(name, QString(), "Value")) + "\n");
	ecpp.append("  " + varName(name, "has", "Value") + " = true;\n");
	ecpp.append("  " + varName(name, QString(), "RawValue") + " = input;\n");
	if (Settings::settings()->isDebug()) {
		ecpp.append("qDebug() << \"set attribute\" << \"" + name + "\";\n");
	}
	ecpp.append("}\n");

	// default value handling, defaults in attribute are copied to attributeUse
	QString df;
	bool hasDf = false;
	if (hasDefault()) {
		hasDf = true;
		df = getDefault();
	}

	// init ###, sets default if exists
	ehprivate.append("void " + methodName(name, "init") + "();\n");
	ecpp.append(
			"void " + className + "::" + methodName(name, "init") + "() {\n");
	// inserts initializer of model
	ecpp.append(
			"  " + st->generateInit(varName(name, QString(), "Value")) + "\n");
	if (hasDf) {
		ecpp.append("  " + methodName(name, "set") + "(\"" + df + "\");\n");
	}
	ecpp.append("}\n");

}