Skip to content

Commit

Permalink
Refactor|Record: Record member navigation; renamed addRecord()
Browse files Browse the repository at this point in the history
The term "subrecord" is used to mean a record owned by another record.

However, the member (.) notation (for instance in Record::operator[])
did not allow navigating into non-owned records. This commit fixes
that and renames Record::addRecord() to addSubrecord() to match the
terms used in other methods.
  • Loading branch information
skyjake committed Oct 28, 2015
1 parent 13eac2d commit b5d4383
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 42 deletions.
2 changes: 1 addition & 1 deletion doomsday/apps/client/src/render/stateanimator.cpp
Expand Up @@ -278,7 +278,7 @@ DENG2_PIMPL(StateAnimator)

indexForPassName[passName] = passIndex++;

Record &passRec = names.addRecord(passName);
Record &passRec = names.addSubrecord(passName);
passRec.addBoolean(VAR_ENABLED,
ScriptedInfo::isTrue(passDef, DEF_ENABLED, true))
.audienceForChange() += this;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/apps/client/src/world/bindings_world.cpp
Expand Up @@ -50,7 +50,7 @@ void initBindings(Binder &binder, Record &worldModule)
{
// Thing
{
Record &thing = worldModule.addRecord("Thing");
Record &thing = worldModule.addSubrecord("Thing");
binder.init(thing)
<< DENG2_FUNC_NOARG(Thing_Health, "health");
}
Expand Down
22 changes: 11 additions & 11 deletions doomsday/apps/libdoomsday/src/defs/ded.cpp
Expand Up @@ -55,17 +55,17 @@ float ded_ptcstage_t::particleRadius(int ptcIDX) const
}

ded_s::ded_s()
: flags (names.addRecord("flags"))
, episodes (names.addRecord("episodes"))
, things (names.addRecord("things"))
, states (names.addRecord("states"))
, materials (names.addRecord("materials"))
, models (names.addRecord("models"))
, skies (names.addRecord("skies"))
, musics (names.addRecord("musics"))
, mapInfos (names.addRecord("mapInfos"))
, finales (names.addRecord("finales"))
, decorations(names.addRecord("decorations"))
: flags (names.addSubrecord("flags"))
, episodes (names.addSubrecord("episodes"))
, things (names.addSubrecord("things"))
, states (names.addSubrecord("states"))
, materials (names.addSubrecord("materials"))
, models (names.addSubrecord("models"))
, skies (names.addSubrecord("skies"))
, musics (names.addSubrecord("musics"))
, mapInfos (names.addSubrecord("mapInfos"))
, finales (names.addSubrecord("finales"))
, decorations(names.addSubrecord("decorations"))
{
decorations.addLookupKey("texture");
episodes.addLookupKey("id");
Expand Down
14 changes: 13 additions & 1 deletion doomsday/sdk/libcore/include/de/data/record.h
Expand Up @@ -159,9 +159,21 @@ class DENG2_PUBLIC Record

/**
* Determines if the record contains a subrecord named @a subrecordName.
* Subrecords are owned by this record.
*/
bool hasSubrecord(String const &subrecordName) const;

/**
* Determines if the record contains a variable @a recordName that
* references or owns a record. Records can be descended into with the
* member (.) notation.
*
* @param recordName Variable name.
*
* @return @c true if the variable points to a record.
*/
bool hasRecord(String const &recordName) const;

/**
* Adds a new variable to the record.
*
Expand Down Expand Up @@ -303,7 +315,7 @@ class DENG2_PUBLIC Record
*
* @return The new subrecord.
*/
Record &addRecord(String const &name);
Record &addSubrecord(String const &name);

/**
* Removes a subrecord from the record.
Expand Down
25 changes: 17 additions & 8 deletions doomsday/sdk/libcore/src/data/record.cpp
Expand Up @@ -130,8 +130,15 @@ DENG2_PIMPL(Record)
}
}

bool isRecord(Variable const &var) const
{
RecordValue const *value = var.value().maybeAs<RecordValue>();
return value && value->record();
}

bool isSubrecord(Variable const &var) const
{
// Subrecords are owned by this record.
RecordValue const *value = var.value().maybeAs<RecordValue>();
return value && value->record() && value->hasOwnership();
}
Expand Down Expand Up @@ -179,7 +186,7 @@ DENG2_PIMPL(Record)
String subName = name.substr(0, pos);
String remaining = name.substr(pos + 1);
// If it is a subrecord we can descend into it.
if(!self.hasSubrecord(subName)) return 0;
if(!self.hasRecord(subName)) return 0;
return self[subName].value<RecordValue>().dereference().d->findMemberByPath(remaining);
}

Expand Down Expand Up @@ -212,7 +219,7 @@ DENG2_PIMPL(Record)
if(!self.hasSubrecord(subName))
{
// Create it now.
rec = &self.addRecord(subName);
rec = &self.addSubrecord(subName);
}
else
{
Expand Down Expand Up @@ -339,11 +346,13 @@ bool Record::hasMember(String const &variableName) const
bool Record::hasSubrecord(String const &subrecordName) const
{
Variable const *found = d->findMemberByPath(subrecordName);
if(found)
{
return d->isSubrecord(*found);
}
return false;
return found? d->isSubrecord(*found) : false;
}

bool Record::hasRecord(String const &recordName) const
{
Variable const *found = d->findMemberByPath(recordName);
return found? d->isRecord(*found) : false;
}

Variable &Record::add(Variable *variable)
Expand Down Expand Up @@ -456,7 +465,7 @@ Record &Record::add(String const &name, Record *subrecord)
return *subrecord;
}

Record &Record::addRecord(String const &name)
Record &Record::addSubrecord(String const &name)
{
return add(name, new Record);
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/filesys/package.cpp
Expand Up @@ -203,7 +203,7 @@ void Package::parseMetadata(File &packageFile) // static
// The package's information is stored in a subrecord.
if(!packageFile.info().has(PACKAGE))
{
packageFile.info().addRecord(PACKAGE);
packageFile.info().addSubrecord(PACKAGE);
}

Record &metadata = packageFile.info().subrecord(PACKAGE);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/game/savedsession.cpp
Expand Up @@ -62,7 +62,7 @@ void SavedSession::Metadata::parse(String const &source)
info.parse(source);

// Rebuild the game rules subrecord.
Record &rules = addRecord("gameRules");
Record &rules = addSubrecord("gameRules");
foreach(Info::Element const *elem, info.root().contentsInOrder())
{
if(Info::KeyElement const *key = elem->maybeAs<Info::KeyElement>())
Expand Down
10 changes: 5 additions & 5 deletions doomsday/sdk/libcore/src/scriptsys/bindings_core.cpp
Expand Up @@ -151,15 +151,15 @@ void initCoreModule(Binder &binder, Record &coreModule)
{
// Dictionary
{
Record &dict = coreModule.addRecord("Dictionary");
Record &dict = coreModule.addSubrecord("Dictionary");
binder.init(dict)
<< DENG2_FUNC_NOARG(Dictionary_Keys, "keys")
<< DENG2_FUNC_NOARG(Dictionary_Values, "values");
}

// String
{
Record &str = coreModule.addRecord("String");
Record &str = coreModule.addSubrecord("String");
binder.init(str)
<< DENG2_FUNC_NOARG(String_Upper, "upper")
<< DENG2_FUNC_NOARG(String_Lower, "lower")
Expand All @@ -171,14 +171,14 @@ void initCoreModule(Binder &binder, Record &coreModule)

// Path
{
Record &path = coreModule.addRecord("Path");
Record &path = coreModule.addSubrecord("Path");
binder.init(path)
<< DENG2_FUNC(Path_WithoutFileName, "withoutFileName", "path");
}

// File
{
Record &file = coreModule.addRecord("File");
Record &file = coreModule.addSubrecord("File");
binder.init(file)
<< DENG2_FUNC (File_Locate, "locate", "relativePath")
<< DENG2_FUNC_NOARG(File_Read, "read")
Expand All @@ -194,7 +194,7 @@ void initCoreModule(Binder &binder, Record &coreModule)
Function::Defaults setValueFromArgs;
setValueFromArgs["delay"] = new NumberValue(0.0);

Record &anim = coreModule.addRecord("Animation");
Record &anim = coreModule.addSubrecord("Animation");
binder.init(anim)
<< DENG2_FUNC_NOARG(Animation_Value, "value")
<< DENG2_FUNC_NOARG(Animation_Target, "target")
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/scriptsys/nameexpression.cpp
Expand Up @@ -190,7 +190,7 @@ Value *NameExpression::evaluate(Evaluator &evaluator) const
(flags().testFlag(NewSubrecordIfNotInScope) && !variable))
{
// Replaces existing member with this identifier.
Record &record = spaces.front()->addRecord(d->identifier);
Record &record = spaces.front()->addSubrecord(d->identifier);
return new RecordValue(record);
}

Expand Down
4 changes: 2 additions & 2 deletions doomsday/sdk/libcore/src/scriptsys/scriptedinfo.cpp
Expand Up @@ -115,7 +115,7 @@ DENG2_PIMPL(ScriptedInfo)
if(!ns.has(varName))
{
// If it doesn't exist yet, make sure it does.
ns.addRecord(varName);
ns.addSubrecord(varName);
}
ns.add("self") = new RecordValue(ns.subrecord(varName));
needRemoveSelf = true;
Expand Down Expand Up @@ -319,7 +319,7 @@ DENG2_PIMPL(ScriptedInfo)
// Create the block record if it doesn't exist.
if(!ns.has(varName))
{
ns.addRecord(varName);
ns.addSubrecord(varName);
}
Record &blockRecord = ns[varName];

Expand Down
20 changes: 10 additions & 10 deletions doomsday/tests/test_record/main.cpp
Expand Up @@ -36,35 +36,35 @@ int main(int argc, char **argv)
{
TextApp app(argc, argv);
app.initSubsystems(App::DisablePlugins);

Record rec;

LOG_MSG("Empty record:\n") << rec;

rec.add(new Variable("hello", new TextValue("World!")));
LOG_MSG("With one variable:\n") << rec;

rec.add(new Variable("size", new NumberValue(1024)));
LOG_MSG("With two variables:\n") << rec;

Record rec2;
Block b;
Writer(b) << rec;
LOG_MSG("Serialized record to ") << b.size() << " bytes.";

String str;
QTextStream os(&str);
for(duint i = 0; i < b.size(); ++i)
{
os << dint(b.data()[i]) << " ";
}
LOG_MSG(str);
Reader(b) >> rec2;

Reader(b) >> rec2;
LOG_MSG("After being deserialized:\n") << rec2;

Record before;
before.addRecord("subrecord");
before.addSubrecord("subrecord");
before.subrecord("subrecord").set("value", true);
DENG2_ASSERT(before.hasSubrecord("subrecord"));
LOG_MSG("Before copying:\n") << before;
Expand All @@ -79,5 +79,5 @@ int main(int argc, char **argv)
}

qDebug() << "Exiting main()...";
return 0;
return 0;
}

0 comments on commit b5d4383

Please sign in to comment.