Navigation Menu

Skip to content

Commit

Permalink
#5907: EntityClass::_fileInfo is now an std::optional
Browse files Browse the repository at this point in the history
Use this C++17 template to avoid needing to create a dummy FileInfo when
constructing an entity which wasn't loaded from a file.
  • Loading branch information
Matthew Mott committed Mar 9, 2022
1 parent 9b4d329 commit 383f732
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
13 changes: 4 additions & 9 deletions radiantcore/eclass/EClassManager.cpp
Expand Up @@ -51,25 +51,20 @@ IEntityClassPtr EClassManager::findOrInsert(const std::string& name, bool has_br

// Return an error if no name is given
if (name.empty())
{
return IEntityClassPtr();
}

// Convert string to lowercase, for case-insensitive lookup
std::string lName = string::to_lower_copy(name);
// Convert string to lowercase, for case-insensitive lookup
std::string lName = string::to_lower_copy(name);

// Find and return if exists
EntityClass::Ptr eclass = findInternal(lName);
if (eclass)
{
return eclass;
}

// Otherwise insert the new EntityClass
//IEntityClassPtr eclass = eclass::EntityClass::create(lName, has_brushes);
// Otherwise insert the new EntityClass.
// greebo: Changed fallback behaviour when unknown entites are encountered to TRUE
// so that brushes of unknown entites don't get lost (issue #240)
eclass = EntityClass::create(lName, true);
eclass = EntityClass::createDefault(lName, true);

// Any overrides should also apply to entityDefs that are crated on the fly
GlobalEclassColourManager().applyColours(*eclass);
Expand Down
16 changes: 10 additions & 6 deletions radiantcore/eclass/EntityClass.cpp
Expand Up @@ -19,13 +19,18 @@ namespace

const EntityClassAttribute EntityClass::_emptyAttribute("", "", "");

EntityClass::EntityClass(const std::string& name, const vfs::FileInfo& fileInfo, bool fixedSize)
EntityClass::EntityClass(const std::string& name, bool fixedSize)
: _name(name),
_fileInfo(fileInfo),
_colour(UndefinedColour),
_fixedSize(fixedSize)
{}

EntityClass::EntityClass(const std::string& name, const vfs::FileInfo& fileInfo, bool fixedSize)
: EntityClass(name, fixedSize)
{
_fileInfo = fileInfo;
}

const std::string& EntityClass::getName() const
{
return _name;
Expand Down Expand Up @@ -156,10 +161,9 @@ void EntityClass::emplaceAttribute(EntityClassAttribute&& attribute)
}
}

EntityClass::Ptr EntityClass::create(const std::string& name, bool brushes)
EntityClass::Ptr EntityClass::createDefault(const std::string& name, bool brushes)
{
vfs::FileInfo emptyFileInfo("def/", "_autogenerated_by_darkradiant_.def", vfs::Visibility::HIDDEN);
auto eclass = std::make_shared<EntityClass>(name, emptyFileInfo, !brushes);
auto eclass = std::make_shared<EntityClass>(name, !brushes);

// Force the entity class colour to default
eclass->setColour(UndefinedColour);
Expand Down Expand Up @@ -293,7 +297,7 @@ bool EntityClass::isOfType(const std::string& className)

std::string EntityClass::getDefFileName()
{
return _fileInfo.fullPath();
return _fileInfo ? _fileInfo->fullPath() : "";
}

// Find a single attribute
Expand Down
31 changes: 10 additions & 21 deletions radiantcore/eclass/EntityClass.h
Expand Up @@ -36,8 +36,9 @@ class EntityClass
// The name of this entity class
std::string _name;

// Source file information
vfs::FileInfo _fileInfo;
// Source file information. May not exist if the entity class was created in code rather than
// loaded from a .def file.
std::optional<vfs::FileInfo> _fileInfo;

// Parent class pointer (or NULL)
EntityClass* _parent = nullptr;
Expand Down Expand Up @@ -91,28 +92,16 @@ class EntityClass
bool editorKeys) const;

public:
/**
* Static function to create a default entity class.
*
* @param name
* The name of the entity class to create.
*
* @param brushes
* Whether the entity contains brushes or not.
*/
static EntityClass::Ptr create(const std::string& name, bool brushes);

/**
* Constructor.
*
* @param name
* Entity class name.
*
* @param fixedSize
* Whether this entity has a fixed size.
*/
/// Construct an EntityClass with no FileInfo.
EntityClass(const std::string& name, bool isFixedSize = false);

/// Construct an EntityClass with a given FileInfo.
EntityClass(const std::string& name, const vfs::FileInfo& fileInfo, bool fixedSize = false);

/// Create a heap-allocated default/empty EntityClass
static EntityClass::Ptr createDefault(const std::string& name, bool brushes);

void emplaceAttribute(EntityClassAttribute&& attribute);

// IEntityClass implementation
Expand Down

0 comments on commit 383f732

Please sign in to comment.