cmPropertyDefinitionMap: simplify and shorten

This commit is contained in:
Tushar Maheshwari 2020-05-13 19:54:03 +05:30
parent 1cc4bc4191
commit 6728f0fa85
8 changed files with 64 additions and 117 deletions

View File

@ -383,8 +383,6 @@ set(SRCS
cmProperty.h cmProperty.h
cmPropertyDefinition.cxx cmPropertyDefinition.cxx
cmPropertyDefinition.h cmPropertyDefinition.h
cmPropertyDefinitionMap.cxx
cmPropertyDefinitionMap.h
cmPropertyMap.cxx cmPropertyMap.cxx
cmPropertyMap.h cmPropertyMap.h
cmQtAutoGen.cxx cmQtAutoGen.cxx

View File

@ -2,17 +2,38 @@
file Copyright.txt or https://cmake.org/licensing for details. */ file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmPropertyDefinition.h" #include "cmPropertyDefinition.h"
#include <utility> #include <tuple>
cmPropertyDefinition::cmPropertyDefinition(std::string name, cmPropertyDefinition::cmPropertyDefinition(std::string shortDescription,
cmProperty::ScopeType scope,
std::string shortDescription,
std::string fullDescription, std::string fullDescription,
bool chain) bool chained)
: Name(std::move(name)) : ShortDescription(std::move(shortDescription))
, ShortDescription(std::move(shortDescription))
, FullDescription(std::move(fullDescription)) , FullDescription(std::move(fullDescription))
, Scope(scope) , Chained(chained)
, Chained(chain)
{ {
} }
void cmPropertyDefinitionMap::DefineProperty(
const std::string& name, cmProperty::ScopeType scope,
const std::string& ShortDescription, const std::string& FullDescription,
bool chain)
{
auto it = this->Map_.find(key_type(name, scope));
if (it == this->Map_.end()) {
// try_emplace() since C++17
this->Map_.emplace(
std::piecewise_construct, std::forward_as_tuple(name, scope),
std::forward_as_tuple(ShortDescription, FullDescription, chain));
}
}
cmPropertyDefinition const* cmPropertyDefinitionMap::GetPropertyDefinition(
const std::string& name, cmProperty::ScopeType scope) const
{
auto it = this->Map_.find(key_type(name, scope));
if (it != this->Map_.end()) {
return &it->second;
}
return nullptr;
}

View File

@ -5,7 +5,9 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <map>
#include <string> #include <string>
#include <utility>
#include "cmProperty.h" #include "cmProperty.h"
@ -13,25 +15,19 @@
* \brief Property meta-information * \brief Property meta-information
* *
* This class contains the following meta-information about property: * This class contains the following meta-information about property:
* - Name;
* - Various documentation strings; * - Various documentation strings;
* - The scope of the property;
* - If the property is chained. * - If the property is chained.
*/ */
class cmPropertyDefinition class cmPropertyDefinition
{ {
public: public:
/// Constructor /// Constructor
cmPropertyDefinition(std::string name, cmProperty::ScopeType scope, cmPropertyDefinition(std::string shortDescription,
std::string ShortDescription, std::string fullDescription, bool chained);
std::string FullDescription, bool chained = false);
/// Is the property chained? /// Is the property chained?
bool IsChained() const { return this->Chained; } bool IsChained() const { return this->Chained; }
/// Get the scope
cmProperty::ScopeType GetScope() const { return this->Scope; }
/// Get the documentation (short version) /// Get the documentation (short version)
const std::string& GetShortDescription() const const std::string& GetShortDescription() const
{ {
@ -44,12 +40,30 @@ public:
return this->FullDescription; return this->FullDescription;
} }
protected: private:
std::string Name;
std::string ShortDescription; std::string ShortDescription;
std::string FullDescription; std::string FullDescription;
cmProperty::ScopeType Scope;
bool Chained; bool Chained;
}; };
/** \class cmPropertyDefinitionMap
* \brief Map property name and scope to their definition
*/
class cmPropertyDefinitionMap
{
public:
// define the property
void DefineProperty(const std::string& name, cmProperty::ScopeType scope,
const std::string& ShortDescription,
const std::string& FullDescription, bool chain);
// get the property definition if present, otherwise nullptr
cmPropertyDefinition const* GetPropertyDefinition(
const std::string& name, cmProperty::ScopeType scope) const;
private:
using key_type = std::pair<std::string, cmProperty::ScopeType>;
std::map<key_type, cmPropertyDefinition> Map_;
};
#endif #endif

View File

@ -1,35 +0,0 @@
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmPropertyDefinitionMap.h"
#include <tuple>
#include <utility>
void cmPropertyDefinitionMap::DefineProperty(
const std::string& name, cmProperty::ScopeType scope,
const std::string& ShortDescription, const std::string& FullDescription,
bool chain)
{
auto it = this->find(name);
if (it == this->end()) {
// try_emplace() since C++17
this->emplace(std::piecewise_construct, std::forward_as_tuple(name),
std::forward_as_tuple(name, scope, ShortDescription,
FullDescription, chain));
}
}
bool cmPropertyDefinitionMap::IsPropertyDefined(const std::string& name) const
{
return this->find(name) != this->end();
}
bool cmPropertyDefinitionMap::IsPropertyChained(const std::string& name) const
{
auto it = this->find(name);
if (it == this->end()) {
return false;
}
return it->second.IsChained();
}

View File

@ -1,30 +0,0 @@
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#ifndef cmPropertyDefinitionMap_h
#define cmPropertyDefinitionMap_h
#include "cmConfigure.h" // IWYU pragma: keep
#include <map>
#include <string>
#include "cmProperty.h"
#include "cmPropertyDefinition.h"
class cmPropertyDefinitionMap
: public std::map<std::string, cmPropertyDefinition>
{
public:
// define the property
void DefineProperty(const std::string& name, cmProperty::ScopeType scope,
const std::string& ShortDescription,
const std::string& FullDescription, bool chain);
// has a named property been defined
bool IsPropertyDefined(const std::string& name) const;
// is a named property set to chain
bool IsPropertyChained(const std::string& name) const;
};
#endif

View File

@ -265,7 +265,7 @@ void cmState::RemoveCacheEntryProperty(std::string const& key,
cmStateSnapshot cmState::Reset() cmStateSnapshot cmState::Reset()
{ {
this->GlobalProperties.Clear(); this->GlobalProperties.Clear();
this->PropertyDefinitions.clear(); this->PropertyDefinitions = {};
this->GlobVerificationManager->Reset(); this->GlobVerificationManager->Reset();
cmStateDetail::PositionType pos = this->SnapshotData.Truncate(); cmStateDetail::PositionType pos = this->SnapshotData.Truncate();
@ -331,39 +331,23 @@ void cmState::DefineProperty(const std::string& name,
const std::string& ShortDescription, const std::string& ShortDescription,
const std::string& FullDescription, bool chained) const std::string& FullDescription, bool chained)
{ {
this->PropertyDefinitions[scope].DefineProperty( this->PropertyDefinitions.DefineProperty(name, scope, ShortDescription,
name, scope, ShortDescription, FullDescription, chained); FullDescription, chained);
} }
cmPropertyDefinition const* cmState::GetPropertyDefinition( cmPropertyDefinition const* cmState::GetPropertyDefinition(
const std::string& name, cmProperty::ScopeType scope) const const std::string& name, cmProperty::ScopeType scope) const
{ {
if (this->IsPropertyDefined(name, scope)) { return this->PropertyDefinitions.GetPropertyDefinition(name, scope);
cmPropertyDefinitionMap const& defs =
this->PropertyDefinitions.find(scope)->second;
return &defs.find(name)->second;
}
return nullptr;
}
bool cmState::IsPropertyDefined(const std::string& name,
cmProperty::ScopeType scope) const
{
auto it = this->PropertyDefinitions.find(scope);
if (it == this->PropertyDefinitions.end()) {
return false;
}
return it->second.IsPropertyDefined(name);
} }
bool cmState::IsPropertyChained(const std::string& name, bool cmState::IsPropertyChained(const std::string& name,
cmProperty::ScopeType scope) const cmProperty::ScopeType scope) const
{ {
auto it = this->PropertyDefinitions.find(scope); if (auto def = this->GetPropertyDefinition(name, scope)) {
if (it == this->PropertyDefinitions.end()) { return def->IsChained();
return false;
} }
return it->second.IsPropertyChained(name); return false;
} }
void cmState::SetLanguageEnabled(std::string const& l) void cmState::SetLanguageEnabled(std::string const& l)

View File

@ -17,7 +17,7 @@
#include "cmListFileCache.h" #include "cmListFileCache.h"
#include "cmPolicies.h" #include "cmPolicies.h"
#include "cmProperty.h" #include "cmProperty.h"
#include "cmPropertyDefinitionMap.h" #include "cmPropertyDefinition.h"
#include "cmPropertyMap.h" #include "cmPropertyMap.h"
#include "cmStatePrivate.h" #include "cmStatePrivate.h"
#include "cmStateTypes.h" #include "cmStateTypes.h"
@ -25,7 +25,6 @@
class cmCacheManager; class cmCacheManager;
class cmCommand; class cmCommand;
class cmGlobVerificationManager; class cmGlobVerificationManager;
class cmPropertyDefinition;
class cmStateSnapshot; class cmStateSnapshot;
class cmMessenger; class cmMessenger;
class cmExecutionStatus; class cmExecutionStatus;
@ -131,9 +130,6 @@ public:
cmPropertyDefinition const* GetPropertyDefinition( cmPropertyDefinition const* GetPropertyDefinition(
const std::string& name, cmProperty::ScopeType scope) const; const std::string& name, cmProperty::ScopeType scope) const;
// Is a property defined?
bool IsPropertyDefined(const std::string& name,
cmProperty::ScopeType scope) const;
bool IsPropertyChained(const std::string& name, bool IsPropertyChained(const std::string& name,
cmProperty::ScopeType scope) const; cmProperty::ScopeType scope) const;
@ -225,7 +221,7 @@ private:
const std::string& variable, const std::string& variable,
cmListFileBacktrace const& bt); cmListFileBacktrace const& bt);
std::map<cmProperty::ScopeType, cmPropertyDefinitionMap> PropertyDefinitions; cmPropertyDefinitionMap PropertyDefinitions;
std::vector<std::string> EnabledLanguages; std::vector<std::string> EnabledLanguages;
std::map<std::string, Command> BuiltinCommands; std::map<std::string, Command> BuiltinCommands;
std::map<std::string, Command> ScriptedCommands; std::map<std::string, Command> ScriptedCommands;

View File

@ -416,7 +416,6 @@ CMAKE_CXX_SOURCES="\
cmProcessOutput \ cmProcessOutput \
cmProjectCommand \ cmProjectCommand \
cmPropertyDefinition \ cmPropertyDefinition \
cmPropertyDefinitionMap \
cmPropertyMap \ cmPropertyMap \
cmReturnCommand \ cmReturnCommand \
cmRulePlaceholderExpander \ cmRulePlaceholderExpander \