From 73d52a862bfe73f591458b47a016b7400057ecab Mon Sep 17 00:00:00 2001
From: Vitaly Stakhovsky <vvs31415@gitlab.org>
Date: Thu, 12 Mar 2020 08:36:43 -0400
Subject: cmPropertyDefinition: Construct directly in defined state

Move `cmPropertyDefinitionMap::DefineProperty` functionality
directly into the constructor to avoid an intermediate state.
---
 Source/cmCPluginAPI.cxx            |  5 +++--
 Source/cmDefinePropertyCommand.cxx |  2 +-
 Source/cmPropertyDefinition.cxx    | 26 ++++++++++++--------------
 Source/cmPropertyDefinition.h      | 11 ++++-------
 Source/cmPropertyDefinitionMap.cxx | 18 +++++++++---------
 Source/cmPropertyDefinitionMap.h   |  4 ++--
 Source/cmState.cxx                 |  4 ++--
 Source/cmState.h                   |  4 ++--
 8 files changed, 35 insertions(+), 39 deletions(-)

diff --git a/Source/cmCPluginAPI.cxx b/Source/cmCPluginAPI.cxx
index f6c1e47..80441be 100644
--- a/Source/cmCPluginAPI.cxx
+++ b/Source/cmCPluginAPI.cxx
@@ -775,8 +775,9 @@ void CCONV DefineSourceFileProperty(void* arg, const char* name,
                                     const char* longDocs, int chained)
 {
   cmMakefile* mf = static_cast<cmMakefile*>(arg);
-  mf->GetState()->DefineProperty(name, cmProperty::SOURCE_FILE, briefDocs,
-                                 longDocs, chained != 0);
+  mf->GetState()->DefineProperty(name, cmProperty::SOURCE_FILE,
+                                 briefDocs ? briefDocs : "",
+                                 longDocs ? longDocs : "", chained != 0);
 }
 
 } // close the extern "C" scope
diff --git a/Source/cmDefinePropertyCommand.cxx b/Source/cmDefinePropertyCommand.cxx
index f4e4fda..4e2d9b0 100644
--- a/Source/cmDefinePropertyCommand.cxx
+++ b/Source/cmDefinePropertyCommand.cxx
@@ -95,7 +95,7 @@ bool cmDefinePropertyCommand(std::vector<std::string> const& args,
 
   // Actually define the property.
   status.GetMakefile().GetState()->DefineProperty(
-    PropertyName, scope, BriefDocs.c_str(), FullDocs.c_str(), inherited);
+    PropertyName, scope, BriefDocs, FullDocs, inherited);
 
   return true;
 }
diff --git a/Source/cmPropertyDefinition.cxx b/Source/cmPropertyDefinition.cxx
index 6a3174c..c8efaf6 100644
--- a/Source/cmPropertyDefinition.cxx
+++ b/Source/cmPropertyDefinition.cxx
@@ -2,19 +2,17 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmPropertyDefinition.h"
 
-void cmPropertyDefinition::DefineProperty(const std::string& name,
-                                          cmProperty::ScopeType scope,
-                                          const char* shortDescription,
-                                          const char* fullDescription,
-                                          bool chain)
+#include <utility>
+
+cmPropertyDefinition::cmPropertyDefinition(std::string name,
+                                           cmProperty::ScopeType scope,
+                                           std::string shortDescription,
+                                           std::string fullDescription,
+                                           bool chain)
+  : Name(std::move(name))
+  , ShortDescription(std::move(shortDescription))
+  , FullDescription(std::move(fullDescription))
+  , Scope(scope)
+  , Chained(chain)
 {
-  this->Name = name;
-  this->Scope = scope;
-  this->Chained = chain;
-  if (shortDescription) {
-    this->ShortDescription = shortDescription;
-  }
-  if (fullDescription) {
-    this->FullDescription = fullDescription;
-  }
 }
diff --git a/Source/cmPropertyDefinition.h b/Source/cmPropertyDefinition.h
index 0d68c32..d2e4467 100644
--- a/Source/cmPropertyDefinition.h
+++ b/Source/cmPropertyDefinition.h
@@ -21,13 +21,10 @@
 class cmPropertyDefinition
 {
 public:
-  /// Define this property
-  void DefineProperty(const std::string& name, cmProperty::ScopeType scope,
-                      const char* ShortDescription,
-                      const char* FullDescription, bool chained);
-
-  /// Default constructor
-  cmPropertyDefinition() { this->Chained = false; }
+  /// Constructor
+  cmPropertyDefinition(std::string name, cmProperty::ScopeType scope,
+                       std::string ShortDescription,
+                       std::string FullDescription, bool chained = false);
 
   /// Is the property chained?
   bool IsChained() const { return this->Chained; }
diff --git a/Source/cmPropertyDefinitionMap.cxx b/Source/cmPropertyDefinitionMap.cxx
index f752ed7..614d5a4 100644
--- a/Source/cmPropertyDefinitionMap.cxx
+++ b/Source/cmPropertyDefinitionMap.cxx
@@ -2,20 +2,20 @@
    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 char* ShortDescription,
-                                             const char* FullDescription,
-                                             bool chain)
+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);
-  cmPropertyDefinition* prop;
   if (it == this->end()) {
-    prop = &(*this)[name];
-    prop->DefineProperty(name, scope, ShortDescription, FullDescription,
-                         chain);
+    // try_emplace() since C++17
+    this->emplace(std::piecewise_construct, std::forward_as_tuple(name),
+                  std::forward_as_tuple(name, scope, ShortDescription,
+                                        FullDescription, chain));
   }
 }
 
diff --git a/Source/cmPropertyDefinitionMap.h b/Source/cmPropertyDefinitionMap.h
index 8ec7910..2ae6efb 100644
--- a/Source/cmPropertyDefinitionMap.h
+++ b/Source/cmPropertyDefinitionMap.h
@@ -17,8 +17,8 @@ class cmPropertyDefinitionMap
 public:
   // define the property
   void DefineProperty(const std::string& name, cmProperty::ScopeType scope,
-                      const char* ShortDescription,
-                      const char* FullDescription, bool chain);
+                      const std::string& ShortDescription,
+                      const std::string& FullDescription, bool chain);
 
   // has a named property been defined
   bool IsPropertyDefined(const std::string& name) const;
diff --git a/Source/cmState.cxx b/Source/cmState.cxx
index 9fc7615..51e4976 100644
--- a/Source/cmState.cxx
+++ b/Source/cmState.cxx
@@ -335,8 +335,8 @@ cmStateSnapshot cmState::Reset()
 
 void cmState::DefineProperty(const std::string& name,
                              cmProperty::ScopeType scope,
-                             const char* ShortDescription,
-                             const char* FullDescription, bool chained)
+                             const std::string& ShortDescription,
+                             const std::string& FullDescription, bool chained)
 {
   this->PropertyDefinitions[scope].DefineProperty(
     name, scope, ShortDescription, FullDescription, chained);
diff --git a/Source/cmState.h b/Source/cmState.h
index 6ee2b0c..460c6bb 100644
--- a/Source/cmState.h
+++ b/Source/cmState.h
@@ -121,8 +121,8 @@ public:
   cmStateSnapshot Reset();
   // Define a property
   void DefineProperty(const std::string& name, cmProperty::ScopeType scope,
-                      const char* ShortDescription,
-                      const char* FullDescription, bool chain = false);
+                      const std::string& ShortDescription,
+                      const std::string& FullDescription, bool chain = false);
 
   // get property definition
   cmPropertyDefinition const* GetPropertyDefinition(
-- 
cgit v0.12