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
cmPropertyDefinition.cxx
cmPropertyDefinition.h
cmPropertyDefinitionMap.cxx
cmPropertyDefinitionMap.h
cmPropertyMap.cxx
cmPropertyMap.h
cmQtAutoGen.cxx

View File

@ -2,17 +2,38 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmPropertyDefinition.h"
#include <utility>
#include <tuple>
cmPropertyDefinition::cmPropertyDefinition(std::string name,
cmProperty::ScopeType scope,
std::string shortDescription,
cmPropertyDefinition::cmPropertyDefinition(std::string shortDescription,
std::string fullDescription,
bool chain)
: Name(std::move(name))
, ShortDescription(std::move(shortDescription))
bool chained)
: ShortDescription(std::move(shortDescription))
, FullDescription(std::move(fullDescription))
, Scope(scope)
, Chained(chain)
, Chained(chained)
{
}
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 <map>
#include <string>
#include <utility>
#include "cmProperty.h"
@ -13,25 +15,19 @@
* \brief Property meta-information
*
* This class contains the following meta-information about property:
* - Name;
* - Various documentation strings;
* - The scope of the property;
* - If the property is chained.
*/
class cmPropertyDefinition
{
public:
/// Constructor
cmPropertyDefinition(std::string name, cmProperty::ScopeType scope,
std::string ShortDescription,
std::string FullDescription, bool chained = false);
cmPropertyDefinition(std::string shortDescription,
std::string fullDescription, bool chained);
/// Is the property chained?
bool IsChained() const { return this->Chained; }
/// Get the scope
cmProperty::ScopeType GetScope() const { return this->Scope; }
/// Get the documentation (short version)
const std::string& GetShortDescription() const
{
@ -44,12 +40,30 @@ public:
return this->FullDescription;
}
protected:
std::string Name;
private:
std::string ShortDescription;
std::string FullDescription;
cmProperty::ScopeType Scope;
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

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

View File

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

View File

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