Skip to content

Commit

Permalink
Refactor|StringPool|PathTree|File: Return const String references whe…
Browse files Browse the repository at this point in the history
…re suitable
  • Loading branch information
danij-deng committed Nov 8, 2012
1 parent 7d4c1da commit dc7e74a
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 41 deletions.
9 changes: 6 additions & 3 deletions doomsday/engine/portable/include/file.h
Expand Up @@ -80,7 +80,7 @@ class File1
virtual ~File1();

/// @return Name of this file.
virtual String /*const&*/ name() const;
virtual String const& name() const;

/**
* Compose the absolute VFS path to this file.
Expand Down Expand Up @@ -263,8 +263,11 @@ class File1
/// Categorization flags.
Flags flags;

/// Absolute variable-length path in the vfs.
ddstring_t path_;
/// Absolute path (including name) in the vfs.
String path_;

/// Name of this file.
String name_;

/// Load order depth index.
uint order;
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/pathtree.h
Expand Up @@ -147,7 +147,7 @@ namespace de
}

/// @return Name for this node's path fragment.
String /*const&*/ name() const;
String const& name() const;

/// @return Hash for this node's path fragment.
ushort hash() const;
Expand Down Expand Up @@ -306,7 +306,7 @@ namespace de
*/

/// @return The path fragment associated with @a fragmentId.
String /*const&*/ fragmentName(FragmentId fragmentId) const;
String const& fragmentName(FragmentId fragmentId) const;

/// @return Hash associated with @a fragmentId.
ushort fragmentHash(FragmentId fragmentId) const;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/dd_wad.cpp
Expand Up @@ -58,7 +58,7 @@ AutoStr* W_LumpName(lumpnum_t absoluteLumpNum)
try
{
lumpnum_t lumpNum = absoluteLumpNum;
String /*const&*/ name = App_FileSystem()->nameIndexForLump(lumpNum).lump(lumpNum).name();
String const& name = App_FileSystem()->nameIndexForLump(lumpNum).lump(lumpNum).name();
QByteArray nameUtf8 = name.toUtf8();
return AutoStr_FromTextStd(nameUtf8.constData());
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/edit_map.cpp
Expand Up @@ -1766,7 +1766,7 @@ static int printMissingMaterialWorker(de::StringPool::Id internId, void* paramet
{
// Print mode.
int const refCount = materialDict->userValue(internId);
de::String /*const&*/ materialUri = materialDict->string(internId);
de::String const& materialUri = materialDict->string(internId);
QByteArray materialUriUtf8 = materialUri.toUtf8();
Con_Message(" %4u x \"%s\"\n", refCount, materialUriUtf8.constData());
}
Expand Down
17 changes: 6 additions & 11 deletions doomsday/engine/portable/src/file.cpp
Expand Up @@ -29,21 +29,19 @@
namespace de {

File1::File1(FileHandle& hndl, char const* _path, FileInfo const& _info, File1* _container)
: handle_(&hndl), info_(_info), container_(_container), flags(DefaultFlags)
: handle_(&hndl), info_(_info), container_(_container),
flags(DefaultFlags), path_(String(_path)), name_(String(_path).fileName())
{
// Used to favor newer files when duplicates are pruned.
/// @todo Does not belong at this level. Load order should be determined
/// at file system level. -ds
static uint fileCounter = 0;
order = fileCounter++;

Str_Init(&path_); Str_Set(&path_, _path);
}

File1::~File1()
{
App_FileSystem()->releaseFile(*this);
Str_Free(&path_);
if(handle_) delete handle_;
}

Expand All @@ -70,9 +68,8 @@ de::FileHandle& File1::handle()

String File1::composePath(char delimiter) const
{
String result = String(Str_Text(&path_));
if(delimiter != '/')
throw de::Error("File1::composePath", "Non '/' delimiter not yet implemented");
String result = path_;
if(delimiter != '/') throw Error("File1::composePath", "Non '/' delimiter not yet implemented");
return result;
}

Expand Down Expand Up @@ -105,11 +102,9 @@ File1& File1::setCustom(bool yes)
return *this;
}

String /*const&*/ File1::name() const
String const& File1::name() const
{
/// @todo Contained files will be able to provide the name without needing to
/// extract it from the virtual path.
return String(Str_Text(&path_)).fileName();
return name_;
}

size_t File1::read(uint8_t* /*buffer*/, bool /*tryCache*/)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -1511,7 +1511,7 @@ AutoStr* F_LumpName(lumpnum_t absoluteLumpNum)
try
{
lumpnum_t lumpNum = absoluteLumpNum;
String /*const&*/ name = App_FileSystem()->nameIndexForLump(lumpNum).lump(lumpNum).name();
String const& name = App_FileSystem()->nameIndexForLump(lumpNum).lump(lumpNum).name();
QByteArray nameUtf8 = name.toUtf8();
return AutoStr_FromTextStd(nameUtf8.constData());
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/p_data.cpp
Expand Up @@ -289,7 +289,7 @@ AutoStr* P_NameForMapEntityDef(MapEntityDef* def)
if(def)
{
de::StringPool::Id id = entityDefs->iterate(P_NameForMapEntityDefWorker, def);
de::String /*const&*/ name = entityDefs->string(id);
de::String const& name = entityDefs->string(id);
QByteArray nameUtf8 = name.toUtf8();
return AutoStr_FromText(nameUtf8.constData());
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/pathtree.cpp
Expand Up @@ -288,7 +288,7 @@ PathTree::Node& PathTree::find(int flags, char const* searchPath, char delimiter
return *foundNode;
}

String /*const&*/ PathTree::fragmentName(FragmentId fragmentId) const
String const& PathTree::fragmentName(FragmentId fragmentId) const
{
return d->fragments.string(fragmentId);
}
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/pathtreenode.cpp
Expand Up @@ -86,7 +86,7 @@ PathTree::FragmentId PathTree::Node::fragmentId() const
return d->fragmentId;
}

String /*const&*/ PathTree::Node::name() const
String const& PathTree::Node::name() const
{
return tree().fragmentName(d->fragmentId);
}
Expand Down Expand Up @@ -184,7 +184,7 @@ int PathTree::Node::comparePath(PathMap& searchPattern, int flags) const
int sfragmentLength = sfragment->length();
qstrncpy(buf, sfragment->from, sfragmentLength + 1);

String /*const&*/ fragment = node->name();
String const& fragment = node->name();
QByteArray fragmentUtf8 = fragment.toUtf8();
if(!matchPathFragment(fragmentUtf8.constData(), buf))
{
Expand Down Expand Up @@ -230,7 +230,7 @@ typedef struct pathconstructorparams_s {
*/
static void pathConstructor(pathconstructorparams_t& parm, PathTree::Node const& trav)
{
String /*const&*/ fragment = trav.name();
String const& fragment = trav.name();

#ifdef LIBDENG_STACK_MONITOR
maxStackDepth = MAX_OF(maxStackDepth, stackStart - (void*)&fragment);
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/resource/models.cpp
Expand Up @@ -411,7 +411,7 @@ model_t* R_ModelForId(uint modelRepositoryId)
AutoStr* R_ComposePathForModelId(uint modelRepositoryId)
{
DENG_ASSERT(modelRepository);
de::String /*const&*/ path = modelRepository->string(modelRepositoryId);
de::String const& path = modelRepository->string(modelRepositoryId);
QByteArray pathUtf8 = path.toUtf8();
return AutoStr_FromTextStd(pathUtf8.constData());
}
Expand Down Expand Up @@ -510,7 +510,7 @@ static int R_LoadModel(de::Uri const& uri)
if(!numFoundSkins)
{
// Lastly try a skin named similarly to the model in the same directory.
de::String /*const&*/ mdlFileName = modelRepository->string(index);
de::String const& mdlFileName = modelRepository->string(index);
de::String skinSearchPath = mdlFileName.fileNamePath() / "/" / mdlFileName.fileNameWithoutExtension();

QByteArray skinSearchPathUtf8 = skinSearchPath.toUtf8();
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/resourcenamespace.cpp
Expand Up @@ -416,7 +416,7 @@ bool ResourceNamespace::add(PathTree::Node& resourceNode)
// We are only interested in leafs (i.e., files and not folders).
if(!resourceNode.isLeaf()) return false;

String /*const&*/ name_ = resourceNode.name();
String const& name_ = resourceNode.name();
QByteArray name_Utf8 = name_.toUtf8();
AutoStr* name = composeResourceName(name_Utf8.constData());
NameHash::key_type key = hashResourceName(name);
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/wad.cpp
Expand Up @@ -63,7 +63,7 @@ class WadFile : public File1
{}

/// @return Name of this file.
String /*const&*/ name() const
String const& name() const
{
return directoryNode().name();
}
Expand Down Expand Up @@ -157,7 +157,7 @@ class WadFile : public File1
crc_ = uint(info_.size);

PathTree::Node const& node = directoryNode();
String /*const&*/ name = node.name();
String const& name = node.name();
int const nameLen = name.length();
for(int k = 0; k < nameLen; ++k)
{
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/zip.cpp
Expand Up @@ -146,7 +146,7 @@ class ZipFile : public File1
{}

/// @return Name of this file.
String /*const&*/ name() const
String const& name() const
{
return directoryNode().name();
}
Expand Down
4 changes: 2 additions & 2 deletions doomsday/libdeng/include/de/stringpool.h
Expand Up @@ -121,7 +121,7 @@ namespace de
*
* @return The interned copy of the string owned by the pool.
*/
String /*const&*/ internAndRetrieve(String str);
String const& internAndRetrieve(String str);

/**
* Sets the user-specified custom value associated with the string @a id.
Expand Down Expand Up @@ -179,7 +179,7 @@ namespace de
*
* @return Interned string associated with @a internId. Owned by the pool.
*/
String /*const&*/ string(Id id) const;
String const& string(Id id) const;

/**
* Removes a string from the pool.
Expand Down
14 changes: 7 additions & 7 deletions doomsday/libdeng/src/stringpool.cpp
Expand Up @@ -319,7 +319,7 @@ StringPool::Id StringPool::intern(String str)
return EXPORT_ID(d->copyAndAssignUniqueId(str)); // O(log n)
}

String /*const&*/ StringPool::internAndRetrieve(String str)
String const& StringPool::internAndRetrieve(String str)
{
InternalId id = IMPORT_ID(intern(str));
return *d->idMap[id];
Expand Down Expand Up @@ -386,9 +386,9 @@ StringPool::Id StringPool::isInterned(String str) const
return 0;
}

String /*const&*/ StringPool::string(Id id) const
String const& StringPool::string(Id id) const
{
if(id == 0) return "";
if(id == 0) return nullString; /// @todo Should error?

InternalId const internalId = IMPORT_ID(id);
DENG_ASSERT(internalId < d->idMap.size());
Expand Down Expand Up @@ -535,14 +535,14 @@ LIBDENG_DEFINE_UNITTEST(StringPool)

// Another string.
s = String("abc");
String /*const&*/ is = p.internAndRetrieve(s);
String const& is = p.internAndRetrieve(s);
DENG_ASSERT(!is.compare(s));

String s2 = String("ABC");
is = p.internAndRetrieve(s2);
DENG_ASSERT(!is.compare(s));
String const& is2 = p.internAndRetrieve(s2);
DENG_ASSERT(!is2.compare(s));

DENG_ASSERT(p.intern(is) == 2);
DENG_ASSERT(p.intern(is2) == 2);

DENG_ASSERT(p.size() == 2);
//p.print();
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/wadmapconverter/include/id1map.h
Expand Up @@ -83,7 +83,7 @@ class Id1Map
MaterialDictId addMaterialToDictionary(const char* name, MaterialDictGroup group);

private:
inline de::String /*const&*/ findMaterialInDictionary(MaterialDictId id)
inline de::String const& findMaterialInDictionary(MaterialDictId id)
{
return materials.string(id);
}
Expand All @@ -92,7 +92,7 @@ class Id1Map
AutoStr* composeMaterialRef(MaterialDictId id)
{
AutoStr* ref = AutoStr_NewStd();
de::String /*const&*/ material = findMaterialInDictionary(id);
de::String const& material = findMaterialInDictionary(id);
QByteArray materialUtf8 = material.toUtf8();
Str_Set(ref, materialUtf8.constData());
return ref;
Expand Down

0 comments on commit dc7e74a

Please sign in to comment.