+ throw Error("cannot cast " + actual.TypeName()
+ + " to " + requested.TypeName());
+ }
+ } else {
+ throw Error("access to undefined object " + name);
+ }
+}
+
+const void *Interpreter::GetObject(
+ int typeId,
+ const std::string &name) const {
+ std::map<string, ParsedDefinition>::const_iterator
+ i(parsedDefinitions.find(name));
+ if (i != parsedDefinitions.end()) {
+ const TypeDescription &requested =
+ TypeDescription::Get(typeId);
+ const TypeDescription &actual =
+ TypeDescription::Get(i->second.type);
+ if (requested.TypeId() == actual.TypeId()) {
+ return values.at(actual.TypeId()).at(i->second.id);
+ } else if (actual.IsSubtypeOf(requested)) {
+ char *sub = reinterpret_cast<char *>(
+ values.at(actual.TypeId()).at(i->second.id));
+ std::ptrdiff_t offset =
+ actual.SupertypeOffset(requested);
+ return sub - offset;
+ } else {
+ throw Error("cannot cast " + actual.TypeName()
+ + " to " + requested.TypeName());