From 1a74e719068c97518d6f9521c86862a17166f32e Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Fri, 21 Oct 2016 23:52:51 +0200 Subject: Introduce CM_UNORDERED_MAP Avoid duplicating switch among std::unordered_map, cmsys::hash_map, and std::map. --- Source/cmDefinitions.h | 21 ++------------------- Source/cmFileTimeComparison.cxx | 34 ++-------------------------------- Source/cmGlobalGenerator.h | 25 ++++--------------------- Source/cmMakefile.h | 31 ++++--------------------------- Source/cmTarget.h | 19 ++----------------- Source/cm_unordered_map.hxx | 25 +++++++++++++++++++++++++ Source/cmake.cxx | 7 ++----- 7 files changed, 41 insertions(+), 121 deletions(-) create mode 100644 Source/cm_unordered_map.hxx diff --git a/Source/cmDefinitions.h b/Source/cmDefinitions.h index 569b3a2..8dfb9ea 100644 --- a/Source/cmDefinitions.h +++ b/Source/cmDefinitions.h @@ -6,20 +6,11 @@ #include #include "cmLinkedTree.h" +#include "cm_unordered_map.hxx" #include #include -#if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP -#include -#else -#include "cmsys/hash_map.hxx" -#endif -#else -#include -#endif - /** \class cmDefinitions * \brief Store a scope of variable definitions for CMake language. * @@ -85,15 +76,7 @@ private: }; static Def NoDef; -#if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP - typedef std::unordered_map MapType; -#else - typedef cmsys::hash_map MapType; -#endif -#else - typedef std::map MapType; -#endif + typedef CM_UNORDERED_MAP MapType; MapType Map; static Def const& GetInternal(const std::string& key, StackIter begin, diff --git a/Source/cmFileTimeComparison.cxx b/Source/cmFileTimeComparison.cxx index a6c9cd0..55d8c2a 100644 --- a/Source/cmFileTimeComparison.cxx +++ b/Source/cmFileTimeComparison.cxx @@ -7,14 +7,7 @@ #include #include -// Use a hash table to avoid duplicate file time checks from disk. -#if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP -#include -#else -#include -#endif -#endif +#include "cm_unordered_map.hxx" // Use a platform-specific API to get file times efficiently. #if !defined(_WIN32) || defined(__CYGWIN__) @@ -35,27 +28,9 @@ public: bool FileTimesDiffer(const char* f1, const char* f2); private: -#if defined(CMAKE_BUILD_WITH_CMAKE) - // Use a hash table to efficiently map from file name to modification time. - class HashString - { - public: - size_t operator()(const std::string& s) const { return h(s.c_str()); } -#ifdef CMake_HAVE_CXX_UNORDERED_MAP - std::hash h; -#else - cmsys::hash h; -#endif - }; -#ifdef CMake_HAVE_CXX_UNORDERED_MAP - typedef std::unordered_map + typedef CM_UNORDERED_MAP FileStatsMap; FileStatsMap Files; -#endif // Internal methods to lookup and compare modification times. inline bool Stat(const char* fname, cmFileTimeComparison_Type* st); @@ -68,7 +43,6 @@ private: bool cmFileTimeComparisonInternal::Stat(const char* fname, cmFileTimeComparison_Type* st) { -#if defined(CMAKE_BUILD_WITH_CMAKE) // Use the stored time if available. cmFileTimeComparisonInternal::FileStatsMap::iterator fit = this->Files.find(fname); @@ -76,7 +50,6 @@ bool cmFileTimeComparisonInternal::Stat(const char* fname, *st = fit->second; return true; } -#endif #if !defined(_WIN32) || defined(__CYGWIN__) // POSIX version. Use the stat function. @@ -97,11 +70,8 @@ bool cmFileTimeComparisonInternal::Stat(const char* fname, *st = fdata.ftLastWriteTime; #endif -#if defined(CMAKE_BUILD_WITH_CMAKE) // Store the time for future use. this->Files[fname] = *st; -#endif - return true; } diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h index d8d47a1..4bf4bd1 100644 --- a/Source/cmGlobalGenerator.h +++ b/Source/cmGlobalGenerator.h @@ -12,6 +12,7 @@ #include "cmTarget.h" #include "cmTargetDepend.h" #include "cm_codecvt.hxx" +#include "cm_unordered_map.hxx" #include #include @@ -22,11 +23,6 @@ #if defined(CMAKE_BUILD_WITH_CMAKE) #include "cmFileLockPool.h" -#ifdef CMake_HAVE_CXX_UNORDERED_MAP -#include -#else -#include -#endif #endif class cmCustomCommandLines; @@ -468,22 +464,9 @@ protected: const char* GetPredefinedTargetsFolder(); private: -#if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP - typedef std::unordered_map TargetMap; - typedef std::unordered_map - GeneratorTargetMap; - typedef std::unordered_map MakefileMap; -#else - typedef cmsys::hash_map TargetMap; - typedef cmsys::hash_map GeneratorTargetMap; - typedef cmsys::hash_map MakefileMap; -#endif -#else - typedef std::map TargetMap; - typedef std::map GeneratorTargetMap; - typedef std::map MakefileMap; -#endif + typedef CM_UNORDERED_MAP TargetMap; + typedef CM_UNORDERED_MAP GeneratorTargetMap; + typedef CM_UNORDERED_MAP MakefileMap; // Map efficiently from target name to cmTarget instance. // Do not use this structure for looping over all targets. // It contains both normal and globally visible imported targets. diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index b61e81b..3067f2c 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -12,6 +12,7 @@ #include "cmStateSnapshot.h" #include "cmTarget.h" #include "cmTargetLinkLibraryType.h" +#include "cm_unordered_map.hxx" #include "cmake.h" #if defined(CMAKE_BUILD_WITH_CMAKE) @@ -27,14 +28,6 @@ #include #include -#if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP -#include -#else -#include -#endif -#endif - class cmCommand; class cmCompiledGeneratorExpression; class cmCustomCommandLines; @@ -783,15 +776,6 @@ protected: // libraries, classes, and executables mutable cmTargets Targets; -#if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP - typedef std::unordered_map TargetMap; -#else - typedef cmsys::hash_map TargetMap; -#endif -#else - typedef std::map TargetMap; -#endif std::map AliasTargets; std::vector SourceFiles; @@ -863,6 +847,7 @@ private: friend class cmParseFileScope; std::vector ImportedTargetsOwned; + typedef CM_UNORDERED_MAP TargetMap; TargetMap ImportedTargets; // Internal policy stack management. @@ -899,16 +884,8 @@ private: */ cmSourceFile* LinearGetSourceFileWithOutput(const std::string& cname) const; -// A map for fast output to input look up. -#if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP - typedef std::unordered_map OutputToSourceMap; -#else - typedef cmsys::hash_map OutputToSourceMap; -#endif -#else - typedef std::map OutputToSourceMap; -#endif + // A map for fast output to input look up. + typedef CM_UNORDERED_MAP OutputToSourceMap; OutputToSourceMap OutputToSource; void UpdateOutputToSourceMap(std::vector const& outputs, diff --git a/Source/cmTarget.h b/Source/cmTarget.h index 929e204..1f035a1 100644 --- a/Source/cmTarget.h +++ b/Source/cmTarget.h @@ -12,6 +12,7 @@ #include "cmPropertyMap.h" #include "cmStateTypes.h" #include "cmTargetLinkLibraryType.h" +#include "cm_unordered_map.hxx" #include #include @@ -20,14 +21,6 @@ #include #include -#if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP -#include -#else -#include -#endif -#endif - class cmMakefile; class cmSourceFile; class cmGlobalGenerator; @@ -324,15 +317,7 @@ private: cmListFileBacktrace Backtrace; }; -#ifdef CMAKE_BUILD_WITH_CMAKE -#ifdef CMake_HAVE_CXX_UNORDERED_MAP -typedef std::unordered_map cmTargets; -#else -typedef cmsys::hash_map cmTargets; -#endif -#else -typedef std::map cmTargets; -#endif +typedef CM_UNORDERED_MAP cmTargets; class cmTargetSet : public std::set { diff --git a/Source/cm_unordered_map.hxx b/Source/cm_unordered_map.hxx new file mode 100644 index 0000000..dc8ca35 --- /dev/null +++ b/Source/cm_unordered_map.hxx @@ -0,0 +1,25 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#ifndef CM_UNORDERED_MAP_HXX +#define CM_UNORDERED_MAP_HXX + +#include + +#if defined(CMake_HAVE_CXX_UNORDERED_MAP) + +#include +#define CM_UNORDERED_MAP std::unordered_map + +#elif defined(CMAKE_BUILD_WITH_CMAKE) + +#include +#define CM_UNORDERED_MAP cmsys::hash_map + +#else + +#include +#define CM_UNORDERED_MAP std::map + +#endif + +#endif diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 388984f..44b76ef 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -27,6 +27,7 @@ #if defined(CMAKE_BUILD_WITH_CMAKE) #include "cmGraphVizWriter.h" #include "cmVariableWatch.h" +#include "cm_unordered_map.hxx" #include #endif @@ -122,11 +123,7 @@ class cmCommand; namespace { #if defined(CMAKE_BUILD_WITH_CMAKE) -#ifdef CMake_HAVE_CXX_UNORDERED_MAP -typedef std::unordered_map JsonValueMapType; -#else -typedef cmsys::hash_map JsonValueMapType; -#endif +typedef CM_UNORDERED_MAP JsonValueMapType; #endif } // namespace -- cgit v0.12