]> git.cworth.org Git - apitrace/blobdiff - retrace/json.hpp
d3dretrace: Backout temporary debugging code.
[apitrace] / retrace / json.hpp
index 14ff501f5681777a19a56b8321643d2e68239388..0431cf775189d4a6a85fc68d8373a431cd88ac4c 100644 (file)
 #include <stddef.h>
 #include <wchar.h>
 
+#ifdef _MSC_VER
+#  include <float.h>
+#  define isfinite _finite
+#  define isnan _isnan
+#else
+#  include <math.h> // isfinite, isnan
+#endif
+
 #include <iomanip>
+#include <limits>
 #include <ostream>
 #include <string>
 
@@ -311,14 +320,14 @@ public:
      * Special case for char to prevent it to be written as a literal
      * character.
      */
-    inline void writeNumber(char n) {
+    inline void writeInt(signed char n) {
         separator();
         os << std::dec << static_cast<int>(n);
         value = true;
         space = ' ';
     }
 
-    inline void writeNumber(unsigned char n) {
+    inline void writeInt(unsigned char n) {
         separator();
         os << std::dec << static_cast<unsigned>(n);
         value = true;
@@ -326,16 +335,29 @@ public:
     }
 
     template<class T>
-    inline void writeNumber(T n) {
-        if (n != n) {
-            // NaN
-            writeNull();
+    inline void writeInt(T n) {
+        separator();
+        os << std::dec << n;
+        value = true;
+        space = ' ';
+    }
+    template<class T>
+    inline void writeFloat(T n) {
+        separator();
+        if (isnan(n)) {
+            // NaN is non-standard but widely supported
+            os << "NaN";
+        } else if (!isfinite(n)) {
+            // Infinite is non-standard but widely supported
+            if (n < 0) {
+                os << '-';
+            }
+            os << "Infinity";
         } else {
-            separator();
-            os << std::dec << std::setprecision(9) << n;
-            value = true;
-            space = ' ';
+            os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
         }
+        value = true;
+        space = ' ';
     }
     
     inline void writeStringMember(const char *name, const char *s) {
@@ -351,9 +373,9 @@ public:
     }
 
     template<class T>
-    inline void writeNumberMember(const char *name, T n) {
+    inline void writeIntMember(const char *name, T n) {
         beginMember(name);
-        writeNumber(n);
+        writeInt(n);
         endMember();
     }
 };