Browse Source

Merge pull request #95 from winny-/throw_by_value_catch_by_reference

Throw exceptions by value, catch by reference
Dalton Nell 6 years ago
parent
commit
adf0635296
No account linked to committer's email
9 changed files with 30 additions and 30 deletions
  1. 1
    1
      src/keyboard.cpp
  2. 8
    8
      src/main.cpp
  3. 1
    1
      src/mouse.cpp
  4. 1
    1
      src/resource.cpp
  5. 7
    7
      src/shader.cpp
  6. 4
    4
      src/slop.cpp
  7. 6
    6
      src/window.cpp
  8. 1
    1
      src/x.cpp
  9. 1
    1
      src/xshaperectangle.cpp

+ 1
- 1
src/keyboard.cpp View File

@@ -64,7 +64,7 @@ slop::Keyboard::Keyboard( X11* x11 ) {
64 64
     }
65 65
     // Non-fatal.
66 66
     if ( err != GrabSuccess ) {
67
-        //throw new std::runtime_error( "Couldn't grab the keyboard after 10 tries." );
67
+        //throw std::runtime_error( "Couldn't grab the keyboard after 10 tries." );
68 68
     }
69 69
     XQueryKeymap( x11->display, deltaState );
70 70
     keyDown = false;

+ 8
- 8
src/main.cpp View File

@@ -43,13 +43,13 @@ glm::vec4 parseColor( std::string value ) {
43 43
             value = value.substr(sz+1);
44 44
             found[3] = std::stof(value,&sz);
45 45
             if ( value.size() != sz ) {
46
-                throw "dur";
46
+                throw std::runtime_error("dur");
47 47
             }
48 48
         } else {
49 49
             found[3] = 1;
50 50
         }
51 51
     } catch ( ... ) {
52
-        throw new std::invalid_argument("Unable to parse value `" + valuecopy + "` as a color. Should be in the format r,g,b or r,g,b,a. Like 1,1,1,1.");
52
+        throw std::invalid_argument("Unable to parse value `" + valuecopy + "` as a color. Should be in the format r,g,b or r,g,b,a. Like 1,1,1,1.");
53 53
     }
54 54
     return found;
55 55
 }
@@ -102,7 +102,7 @@ SlopOptions* getOptions( cxxopts::Options& options ) {
102 102
     if ( options.count( "nodecorations" ) > 0 ) {
103 103
         foo->nodecorations = options["nodecorations"].as<int>();
104 104
         if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) {
105
-            throw new std::invalid_argument( "--nodecorations must be between 0 and 2. Or be used as a flag." );
105
+            throw std::invalid_argument( "--nodecorations must be between 0 and 2. Or be used as a flag." );
106 106
         }
107 107
     }
108 108
     return foo;
@@ -113,7 +113,7 @@ std::string formatOutput( std::string input, SlopSelection selection ) {
113 113
     for( unsigned int i=0;i<input.length();i++) {
114 114
         if ( input[i] == '%' ) {
115 115
             if ( input.length() <= i+1 ) {
116
-                throw new std::invalid_argument( "Expected character after `%`, got END." );
116
+                throw std::invalid_argument( "Expected character after `%`, got END." );
117 117
             }
118 118
             switch( input[i+1] ) {
119 119
                 case 'x':
@@ -132,7 +132,7 @@ std::string formatOutput( std::string input, SlopSelection selection ) {
132 132
                 case 'i':
133 133
                 case 'I': output << selection.id; break;
134 134
                 case '%': output << "%"; break;
135
-                default: throw new std::invalid_argument( std::string()+"Expected x, y, w, h, g, i, c, or % after % in format. Got `" + input[i+1] + "`." );
135
+                default: throw std::invalid_argument( std::string()+"Expected x, y, w, h, g, i, c, or % after % in format. Got `" + input[i+1] + "`." );
136 136
              }
137 137
             i++;
138 138
             continue;
@@ -242,7 +242,7 @@ int app( int argc, char** argv ) {
242 242
     // Options just validates all of our input from argv
243 243
     auto& positional = options["positional"].as<std::vector<std::string>>();
244 244
     if ( positional.size() > 0 ) {
245
-        throw new std::invalid_argument("Unexpected positional argument: " + positional[0]);
245
+        throw std::invalid_argument("Unexpected positional argument: " + positional[0]);
246 246
     }
247 247
     bool help = false;
248 248
     if ( options.count( "help" ) > 0 ) {
@@ -297,8 +297,8 @@ int app( int argc, char** argv ) {
297 297
 int main( int argc, char** argv ) {
298 298
     try {
299 299
         return app( argc, argv );
300
-    } catch( std::exception* e ) {
301
-        std::cerr << "Slop encountered an error:\n" << e->what() << "\n";
300
+    } catch( std::exception& e ) {
301
+        std::cerr << "Slop encountered an error:\n" << e.what() << "\n";
302 302
         return 1;
303 303
     } // let the operating system handle any other kind of exception.
304 304
     return 1;

+ 1
- 1
src/mouse.cpp View File

@@ -58,7 +58,7 @@ slop::Mouse::Mouse(X11* x11, int nodecorations, Window ignoreWindow ) {
58 58
         tries++;
59 59
     }
60 60
     if ( err != GrabSuccess ) {
61
-        throw new std::runtime_error( "Couldn't grab the mouse after 10 tries." );
61
+        throw std::runtime_error( "Couldn't grab the mouse after 10 tries." );
62 62
     }
63 63
     this->nodecorations = nodecorations;
64 64
     this->ignoreWindow = ignoreWindow;

+ 1
- 1
src/resource.cpp View File

@@ -39,7 +39,7 @@ std::string slop::Resource::getRealPath( std::string localpath ) {
39 39
         return usrconfig + localpath;
40 40
     }
41 41
     std::string err = "The file or folder " + localpath + " was not found in " + usrconfig + "\n";
42
-    throw new std::runtime_error(err);
42
+    throw std::runtime_error(err);
43 43
     return localpath;
44 44
 }
45 45
 

+ 7
- 7
src/shader.cpp View File

@@ -8,13 +8,13 @@ slop::Shader::Shader( std::string vert, std::string frag, bool file ) {
8 8
         frag = resource->getRealPath(frag);
9 9
         std::ifstream v( vert.c_str() );
10 10
         if (!v.is_open()) {
11
-            throw new std::runtime_error( "Failed to open " + vert );
11
+            throw std::runtime_error( "Failed to open " + vert );
12 12
         }
13 13
         vert_contents = std::string((std::istreambuf_iterator<char>(v)),
14 14
                                    std::istreambuf_iterator<char>());
15 15
         std::ifstream f( frag.c_str() );
16 16
         if (!f.is_open()) {
17
-            throw new std::runtime_error( "Failed to open " + frag );
17
+            throw std::runtime_error( "Failed to open " + frag );
18 18
         }
19 19
         frag_contents = std::string((std::istreambuf_iterator<char>(f)),
20 20
                                   std::istreambuf_iterator<char>());
@@ -31,12 +31,12 @@ slop::Shader::Shader( std::string vert, std::string frag, bool file ) {
31 31
     
32 32
     if ( vert_contents.length() <= 0 ) {
33 33
         std::string errstring = "Failed to open file (or is empty) `" + vert + "`.\n";
34
-        throw new std::runtime_error(errstring);
34
+        throw std::runtime_error(errstring);
35 35
     }
36 36
 
37 37
     if ( frag_contents.length() <= 0 ) {
38 38
         std::string errstring = "Failed to open file (or is empty) `" + frag + "`.\n";
39
-        throw new std::runtime_error(errstring);
39
+        throw std::runtime_error(errstring);
40 40
     }
41 41
 
42 42
     // Compile both shaders.
@@ -47,7 +47,7 @@ slop::Shader::Shader( std::string vert, std::string frag, bool file ) {
47 47
 
48 48
     if ( err ) {
49 49
         std::string errstring = "Failed to compile shader `" + vert + "`:\n" + errortxt;
50
-        throw new std::runtime_error(errstring);
50
+        throw std::runtime_error(errstring);
51 51
         glDeleteShader( vertShader );
52 52
         return;
53 53
     }
@@ -57,7 +57,7 @@ slop::Shader::Shader( std::string vert, std::string frag, bool file ) {
57 57
     err = compile( fragShader, errortxt );
58 58
     if ( err ) {
59 59
         std::string errstring = "Failed to compile shader `" + frag + "`:\n" + errortxt;
60
-        throw new std::runtime_error(errstring);
60
+        throw std::runtime_error(errstring);
61 61
         glDeleteShader( vertShader );
62 62
         glDeleteShader( fragShader );
63 63
         return;
@@ -67,7 +67,7 @@ slop::Shader::Shader( std::string vert, std::string frag, bool file ) {
67 67
     err = link( vertShader, fragShader, errortxt );
68 68
     if ( err ) {
69 69
         std::string errstring = "Failed to link shader `" + vert + "` and  `" + frag + "`:\n" + errortxt;
70
-        throw new std::runtime_error(errstring);
70
+        throw std::runtime_error(errstring);
71 71
         glDeleteShader( vertShader );
72 72
         glDeleteShader( fragShader );
73 73
         return;

+ 4
- 4
src/slop.cpp View File

@@ -124,11 +124,11 @@ slop::SlopSelection slop::SlopSelect( slop::SlopOptions* options ) {
124 124
             window = new SlopWindow();
125 125
             if (!GLEW_VERSION_3_0) {
126 126
                 delete window;
127
-                throw new std::runtime_error( "OpenGL version is not high enough, slop requires OpenGL 3.0!\nOpenGL accelleration is disabled. Use -o or -q to suppress this message." );
127
+                throw std::runtime_error( "OpenGL version is not high enough, slop requires OpenGL 3.0!\nOpenGL accelleration is disabled. Use -o or -q to suppress this message." );
128 128
             }
129 129
             success = true;
130
-        } catch( std::exception* e ) {
131
-            errorstring += std::string(e->what()) + "\n";
130
+        } catch( std::exception& e ) {
131
+            errorstring += std::string(e.what()) + "\n";
132 132
             success = false;
133 133
         } catch (...) {
134 134
             success = false;
@@ -314,7 +314,7 @@ slop::SlopSelection slop::GLSlopSelect( slop::SlopOptions* options, SlopWindow*
314 314
                 case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
315 315
                 case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
316 316
             }
317
-            throw new std::runtime_error( "OpenGL threw an error: " + error );
317
+            throw std::runtime_error( "OpenGL threw an error: " + error );
318 318
         }
319 319
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
320 320
             memory->running = false;

+ 6
- 6
src/window.cpp View File

@@ -15,13 +15,13 @@ slop::SlopWindow::SlopWindow() {
15 15
     int nelements;
16 16
     int render_event_base, render_error_base;
17 17
     if(!XRenderQueryExtension(x11->display, &render_event_base, &render_error_base)) {
18
-        throw new std::runtime_error("No XRENDER extension found\n");
18
+        throw std::runtime_error("No XRENDER extension found\n");
19 19
     }
20 20
 
21 21
     GLXFBConfig* fbc = glXChooseFBConfig(x11->display, DefaultScreen(x11->display), attributeList, &nelements);
22 22
     GLXFBConfig fbconfig;
23 23
     if ( !fbc ) {
24
-        throw new std::runtime_error("No matching visuals available.\n");
24
+        throw std::runtime_error("No matching visuals available.\n");
25 25
     }
26 26
     XVisualInfo* vi ;
27 27
     XRenderPictFormat *pictFormat;
@@ -42,7 +42,7 @@ slop::SlopWindow::SlopWindow() {
42 42
         }
43 43
     }
44 44
     if (i == nelements ) {
45
-        throw new std::runtime_error( "No matching visuals available" );
45
+        throw std::runtime_error( "No matching visuals available" );
46 46
     }
47 47
     XFree( fbc );
48 48
 
@@ -64,7 +64,7 @@ slop::SlopWindow::SlopWindow() {
64 64
     XFree( vi );
65 65
 
66 66
     if ( !window ) {
67
-        throw new std::runtime_error( "Couldn't create a GL window!" );
67
+        throw std::runtime_error( "Couldn't create a GL window!" );
68 68
     }
69 69
 
70 70
     // Prep some hints for the window
@@ -97,14 +97,14 @@ slop::SlopWindow::SlopWindow() {
97 97
 
98 98
     context = glXCreateNewContext( x11->display, fbconfig, GLX_RGBA_TYPE, 0, True );
99 99
     if ( !context ) {
100
-        throw new std::runtime_error( "Failed to create an OpenGL context." );
100
+        throw std::runtime_error( "Failed to create an OpenGL context." );
101 101
     }
102 102
     setCurrent();
103 103
     // Finally we grab some OpenGL 3.3 stuffs.
104 104
     GLenum err = glewInit();
105 105
     if (GLEW_OK != err)
106 106
     {
107
-      throw new std::runtime_error((char*)glewGetErrorString(err));
107
+      throw std::runtime_error((char*)glewGetErrorString(err));
108 108
     }
109 109
     framebuffer = new Framebuffer( WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ) );
110 110
 

+ 1
- 1
src/x.cpp View File

@@ -92,7 +92,7 @@ X11::X11( std::string displayName ) {
92 92
     // Initialize display
93 93
     display = XOpenDisplay( displayName.c_str() );
94 94
     if ( !display ) {
95
-        throw new std::runtime_error(std::string("Error: Failed to open X display: ") + displayName );
95
+        throw std::runtime_error(std::string("Error: Failed to open X display: ") + displayName );
96 96
     }
97 97
     screen = ScreenOfDisplay( display, DefaultScreen( display ) );
98 98
     visual = DefaultVisual( display, XScreenNumberOfScreen( screen ) );

+ 1
- 1
src/xshaperectangle.cpp View File

@@ -137,7 +137,7 @@ XColor slop::XShapeRectangle::convertColor( glm::vec4 color ) {
137 137
     xc.blue = blue;
138 138
     int err = XAllocColor( x11->display, DefaultColormap( x11->display, XScreenNumberOfScreen( x11->screen ) ), &xc );
139 139
     if ( err == BadColor ) {
140
-        throw new std::runtime_error(std::string("Couldn't allocate a color"));
140
+        throw std::runtime_error(std::string("Couldn't allocate a color"));
141 141
     }
142 142
     return xc;
143 143
 }