فهرست منبع

Eliminate empty-throw.
Remove redundant exception handling in texture_or_uniform's code.

Sam Jaffe 6 سال پیش
والد
کامیت
701542f7fc
4فایلهای تغییر یافته به همراه24 افزوده شده و 6 حذف شده
  1. 18 0
      graphics/include/game/graphics/exception.h
  2. 3 4
      graphics/src/manager.cxx
  3. 2 1
      graphics/src/renderer.cxx
  4. 1 1
      graphics/src/texture.cpp

+ 18 - 0
graphics/include/game/graphics/exception.h

@@ -18,4 +18,22 @@ namespace graphics {
     file_read_error(std::string const & type, std::string const & file)
         : std::runtime_error("Unable to read " + type + " file " + file) {}
   };
+
+  struct unknown_texture_format : std::runtime_error {
+    unknown_texture_format(int components)
+        : std::runtime_error("Invalid number of components provided: " +
+                             std::to_string(components)) {}
+  };
+
+  template <typename E> struct unmapped_enum : std::logic_error {
+    unmapped_enum(E en)
+        : std::logic_error("Unknown " + type_name + std::to_string((int)en)) {}
+    unmapped_enum(std::string const & str)
+        : std::logic_error("Unknown " + type_name + str) {}
+
+    static std::string const type_name;
+  };
+
+  template <typename E>
+  std::string const unmapped_enum<E>::type_name{typeid(E).name()};
 }

+ 3 - 4
graphics/src/manager.cxx

@@ -8,6 +8,7 @@
 
 #include "game/graphics/manager.hpp"
 
+#include "game/graphics/exception.h"
 #include "game/graphics/material.hpp"
 #include "game/graphics/object.hpp"
 #include "game/graphics/shader.hpp"
@@ -63,7 +64,7 @@ materials::uniform uniform_id(std::string const & uniform) {
   } else if (uniform == "u_diffuseMap") {
     return materials::uniform::DIFFUSE;
   }
-  throw;
+  throw unmapped_enum<materials::uniform>(uniform);
 }
 
 identity<material> manager::get(identity<shader_program> program,
@@ -154,7 +155,5 @@ manager::texture_or_uniform(std::string const & path,
   prepare_uniforms();
   // The uniform is primed into the cache already.
   auto & cache = pcache_->textures;
-  auto found = cache.values.find(uniform);
-  if (found != cache.values.end()) { return found->second; }
-  throw;
+  return cache.values.find(uniform)->second;
 }

+ 2 - 1
graphics/src/renderer.cxx

@@ -9,6 +9,7 @@
 #include "game/graphics/renderer.hpp"
 #include <vector>
 
+#include "game/graphics/exception.h"
 #include "game/graphics/object.hpp"
 #include "game/graphics/vertex.h"
 #include "matrix/matrix.hpp"
@@ -22,7 +23,7 @@ renderer_impl * get_renderer_impl(driver d) {
   case driver::openGL:
     return get_renderer_impl<driver::openGL>();
   default:
-    throw;
+    throw unmapped_enum<driver>(d);
   }
 }
 

+ 1 - 1
graphics/src/texture.cpp

@@ -35,7 +35,7 @@ static format as_format(int comps) {
   case 4:
     return format::RGBA;
   default:
-    throw;
+    throw unknown_texture_format(comps);
   }
 }