void MessagePackAdaptorTests::testUndefined() { pack(Datum()); auto o = unpack(); CPPUNIT_ASSERT_EQUAL( msgpack::type::NIL, o.type ); Datum d = o.as<Datum>(); CPPUNIT_ASSERT( d.isUndefined() ); }
Datum ComponentRegistry::getValue(std::string expression, GenericDataType type) { std::pair<std::string, GenericDataType> cacheKey(expression, type); shared_ptr<Datum> test = data_cache[cacheKey]; if(test != NULL){ return *test; } double doubleValue; long longValue; bool boolValue; Datum value; try { switch(type){ case M_FLOAT: case M_INTEGER: case M_BOOLEAN: doubleValue = boost::lexical_cast<double>(expression); if (M_FLOAT == type) { value = Datum(doubleValue); } else if (M_INTEGER == type) { longValue = (long)doubleValue; if ((double)longValue != doubleValue) { throw NonFatalParserException("invalid integer literal", expression.c_str()); } value = Datum(longValue); } else { boolValue = (bool)doubleValue; if ((double)boolValue != doubleValue) { throw NonFatalParserException("invalid boolean literal", expression.c_str()); } value = Datum(boolValue); } break; case M_STRING: value = Datum(string(expression)); break; default: // No lexical_cast for other types break; } if (!value.isUndefined()) { data_cache[cacheKey] = shared_ptr<Datum>(new Datum(value)); return value; } } catch (NonFatalParserException& e){ // Until we work out how to effectively flag these issues, treat them as fatal errors throw FatalParserException(e.what()); } catch (boost::bad_lexical_cast& e){ // no biggie, we can do this the hard(er) way } return Datum(ParsedExpressionVariable::evaluateExpression(expression)); }
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 } }
Datum ExperimentPackager::packageExperiment(const boost::filesystem::path filename) { namespace bf = boost::filesystem; IncludedFilesParser parser(filename.string()); std::string working_path_string; Datum include_files; try{ parser.parse(false); working_path_string = parser.getWorkingPathString(); include_files = parser.getIncludedFilesManifest(); } catch(std::exception& e){ merror(M_PARSER_MESSAGE_DOMAIN, "Experiment packaging failed: %s", e.what()); return Datum(); } Datum eventPayload(M_DICTIONARY, M_EXPERIMENT_PACKAGE_NUMBER_ELEMENTS); { // Use getDocumentData to get the experiment file with any preprocessing and/or // XInclude substitutions already applied std::vector<xmlChar> fileData; parser.getDocumentData(fileData); Datum contents(reinterpret_cast<char *>(&(fileData.front())), fileData.size()); eventPayload.addElement(M_PACKAGER_EXPERIMENT_STRING, packageSingleFile(contents, XMLParser::squashFileName(filename.string()))); } if(include_files.getNElements() >= 1) { Datum mediaFilesPayload(M_LIST, include_files.getNElements()); for(int i=0; i< include_files.getNElements(); ++i) { // DDC there seem to be a lot of unnecessary steps here // simplified hackily for the moment std::string mediaName(include_files.getElement(i).getString()); bf::path mediaPath = expandPath(working_path_string, mediaName); //bf::path mediaPath(include_files.getElement(i).getElement(M_PACKAGER_FULL_NAME).getString()); //std::string mediaName(include_files.getElement(i).getElement(M_PACKAGER_RELATIVE_NAME).getString()); Datum mediaElement = packageSingleFile(mediaPath, mediaName); if(!mediaElement.isUndefined()) { mediaFilesPayload.addElement(mediaElement); } else { merror(M_FILE_MESSAGE_DOMAIN, "Can't find file: %s", mediaPath.string().c_str()); Datum undef; return undef; } } eventPayload.addElement(M_PACKAGER_MEDIA_BUFFERS_STRING, mediaFilesPayload); } return SystemEventFactory::systemEventPackage(M_SYSTEM_DATA_PACKAGE, M_EXPERIMENT_PACKAGE, eventPayload); }