Datum ComponentRegistry::getNumber(std::string expression, GenericDataType type){

  Datum value;
    switch(type){
      case M_FLOAT:
      case M_INTEGER:
      case M_BOOLEAN:
      case M_STRING:
          value = getValue(expression, type);
          if (value.getDataType() == type) {
              return value;
          }
          break;
      default:
          throw FatalParserException("Attempt to cast a number of invalid type");
    }
	
	switch (type){
	
		case M_FLOAT:
			return Datum(value.getFloat());
		case M_INTEGER:
			return Datum(value.getInteger());
		case M_STRING:
			return Datum(value.getString());
		case M_BOOLEAN:
			return Datum(value.getBool());
		default:
			return value;			
	}
}
Exemple #2
0
boost::python::object convert_datum_to_python(const Datum &datum) {
    ScopedRecursionGuard srg(" while converting MWorks datum to Python object");
    
    if (datum.isUndefined()) {
        return boost::python::object();
    }
    
    switch (datum.getDataType()) {
        case M_INTEGER:
            return convert_longlong_to_python(datum.getInteger());
            
        case M_FLOAT:
            return manageNewRef( PyFloat_FromDouble(datum.getFloat()) );
            
        case M_BOOLEAN:
            return manageNewRef( PyBool_FromLong(datum.getBool()) );
            
        case M_STRING: {
            auto &str = datum.getString();
            return manageNewRef( PyString_FromStringAndSize(str.c_str(), str.size()) );
        }
            
        case M_LIST: {
            auto &listValue = datum.getList();
            boost::python::object list = manageNewRef( PyList_New(listValue.size()) );
            
            for (Py_ssize_t i = 0; i < listValue.size(); i++) {
                boost::python::object item = convert_datum_to_python(listValue.at(i));
                // PyList_SetItem "steals" the item reference, so we need to INCREF it
                Py_INCREF(item.ptr());
                if (PyList_SetItem(list.ptr(), i, item.ptr()))
                    throw_error_already_set();
            }
            
            return list;
        }
            
        case M_DICTIONARY: {
            boost::python::object dict = manageNewRef( PyDict_New() );
            
            for (auto &item : datum.getDict()) {
                if (PyDict_SetItem(dict.ptr(),
                                   convert_datum_to_python(item.first).ptr(),
                                   convert_datum_to_python(item.second).ptr()))
                {
                    throw_error_already_set();
                }
            }
            
            return dict;
        }
            
        default:
            PyErr_Format(PyExc_TypeError, "Cannot convert Datum of unknown type (%d)", datum.getDataType());
            throw_error_already_set();
            return boost::python::object();  // Never reached
    }
}