Sfoglia il codice sorgente

clean up time to use constexpr. add helper for hashing identity<T>

Sam Jaffe 3 anni fa
parent
commit
bfe1b84255

+ 0 - 4
util/gameutils.xcodeproj/project.pbxproj

@@ -11,7 +11,6 @@
 		CD62FD082291988F00376440 /* osx_env.mm in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD072291988F00376440 /* osx_env.mm */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CD79A0F927246F4500CAFFC2 /* state_machine.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD79A0F727246F4500CAFFC2 /* state_machine.cxx */; };
 		CD79A0FA27246F4500CAFFC2 /* state_machine.h in Headers */ = {isa = PBXBuildFile; fileRef = CD79A0F827246F4500CAFFC2 /* state_machine.h */; };
-		CD7E87772295FA1F00D877FE /* time.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD7E876F2295FA1F00D877FE /* time.cxx */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CD7E884822960F4800D877FE /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD7E884722960F4800D877FE /* AppKit.framework */; };
 /* End PBXBuildFile section */
 
@@ -55,7 +54,6 @@
 		CD62FD072291988F00376440 /* osx_env.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = osx_env.mm; sourceTree = "<group>"; };
 		CD79A0F727246F4500CAFFC2 /* state_machine.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = state_machine.cxx; sourceTree = "<group>"; };
 		CD79A0F827246F4500CAFFC2 /* state_machine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = state_machine.h; sourceTree = "<group>"; };
-		CD7E876F2295FA1F00D877FE /* time.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = time.cxx; sourceTree = "<group>"; };
 		CD7E884722960F4800D877FE /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
 		CDA31AB32725C6190071CDC2 /* smart_map.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = smart_map.hpp; sourceTree = "<group>"; };
 /* End PBXFileReference section */
@@ -100,7 +98,6 @@
 				CD79A0F727246F4500CAFFC2 /* state_machine.cxx */,
 				CDA31AB32725C6190071CDC2 /* smart_map.hpp */,
 				CD79A0F827246F4500CAFFC2 /* state_machine.h */,
-				CD7E876F2295FA1F00D877FE /* time.cxx */,
 			);
 			path = src;
 			sourceTree = "<group>";
@@ -252,7 +249,6 @@
 			files = (
 				CD62FD082291988F00376440 /* osx_env.mm in Sources */,
 				CD62FD04229195FF00376440 /* files.cxx in Sources */,
-				CD7E87772295FA1F00D877FE /* time.cxx in Sources */,
 				CD79A0F927246F4500CAFFC2 /* state_machine.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

+ 39 - 35
util/include/game/util/hash.hpp

@@ -13,40 +13,44 @@
 
 template <typename, typename> class identity;
 
+#define IDENTITY_HASH(type)                                                    \
+  struct ::std::hash<T>                                                        \
+      : public ::std::hash<identity<T, decltype(std::declval<T>().id)>> {}
+
 namespace std {
-  template <typename T, typename I> struct hash<identity<T, I>> {
-    std::size_t operator()(identity<T, I> tp) const {
-      return std::hash<I>()(tp.id);
-    }
-  };
-
-  template <typename T, typename S> struct hash<std::pair<T, S>> {
-    std::size_t operator()(std::pair<T, S> const & pair) const {
-      return std::hash<T>()(pair.first) ^ std::hash<S>()(pair.second);
-    }
-  };
-
-  template <std::size_t I> struct tuple_hash;
-
-  template <> struct tuple_hash<0> {
-    template <typename... As>
-    std::size_t operator()(std::tuple<As...> const &) const {
-      return 0;
-    }
-  };
-
-  template <std::size_t I> struct tuple_hash {
-    template <typename... As>
-    std::size_t operator()(std::tuple<As...> const & tuple) const {
-      using elt = typename std::tuple_element<I - 1, std::tuple<As...>>::type;
-      return std::hash<elt>()(std::get<I - 1>(tuple)) ^
-             tuple_hash<I - 1>()(tuple);
-    }
-  };
-
-  template <typename... As> struct hash<std::tuple<As...>> {
-    std::size_t operator()(std::tuple<As...> const & tuple) const {
-      return tuple_hash<sizeof...(As)>()(tuple);
-    }
-  };
+template <typename T, typename I> struct hash<identity<T, I>> {
+  std::size_t operator()(identity<T, I> tp) const {
+    return std::hash<I>()(tp.id);
+  }
+};
+
+template <typename T, typename S> struct hash<std::pair<T, S>> {
+  std::size_t operator()(std::pair<T, S> const & pair) const {
+    return std::hash<T>()(pair.first) ^ std::hash<S>()(pair.second);
+  }
+};
+
+template <std::size_t I> struct tuple_hash;
+
+template <> struct tuple_hash<0> {
+  template <typename... As>
+  std::size_t operator()(std::tuple<As...> const &) const {
+    return 0;
+  }
+};
+
+template <std::size_t I> struct tuple_hash {
+  template <typename... As>
+  std::size_t operator()(std::tuple<As...> const & tuple) const {
+    using elt = typename std::tuple_element<I - 1, std::tuple<As...>>::type;
+    return std::hash<elt>()(std::get<I - 1>(tuple)) ^
+           tuple_hash<I - 1>()(tuple);
+  }
+};
+
+template <typename... As> struct hash<std::tuple<As...>> {
+  std::size_t operator()(std::tuple<As...> const & tuple) const {
+    return tuple_hash<sizeof...(As)>()(tuple);
+  }
+};
 }

+ 8 - 8
util/include/game/util/time.hpp

@@ -26,12 +26,12 @@ template <typename T> T current_time() {
 }
 }
 
-namespace env {
-struct fps {
-  static clock::duration const v24;
-  static clock::duration const v30;
-  static clock::duration const v60;
-  static clock::duration const v120;
-  static clock::duration const v144;
-};
+namespace env::fps {
+using std::literals::chrono_literals::operator""ns;
+
+constexpr clock::duration v24{41666666ns};
+constexpr clock::duration v30{33333333ns};
+constexpr clock::duration v60{16666666ns};
+constexpr clock::duration v120{8333333ns};
+constexpr clock::duration v144{6944444ns};
 }

+ 0 - 20
util/src/time.cxx

@@ -1,20 +0,0 @@
-//
-//  time.cpp
-//  engine
-//
-//  Created by Sam Jaffe on 9/3/16.
-//
-
-#include "game/util/time.hpp"
-
-#include <chrono>
-
-namespace env {
-using std::literals::chrono_literals::operator""ns;
-
-clock::duration const fps::v24{41666666ns};
-clock::duration const fps::v30{33333333ns};
-clock::duration const fps::v60{16666666ns};
-clock::duration const fps::v120{8333333ns};
-clock::duration const fps::v144{6944444ns};
-}