Browse Source

Throw exceptions by value, catch by reference

This fixes bug #92 where exceptions are not being catch thrown by
cxxopts.

This is also standard practice :)
https://stackoverflow.com/a/2522311/2720026
Winston Weinert 6 years ago
parent
commit
71442ad080
9 changed files with 29 additions and 29 deletions
  1. 1
    1
      src/keyboard.cpp
  2. 7
    7
      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
     }
64
     }
65
     // Non-fatal.
65
     // Non-fatal.
66
     if ( err != GrabSuccess ) {
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
     XQueryKeymap( x11->display, deltaState );
69
     XQueryKeymap( x11->display, deltaState );
70
     keyDown = false;
70
     keyDown = false;

+ 7
- 7
src/main.cpp View File

49
             found[3] = 1;
49
             found[3] = 1;
50
         }
50
         }
51
     } catch ( ... ) {
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
     return found;
54
     return found;
55
 }
55
 }
102
     if ( options.count( "nodecorations" ) > 0 ) {
102
     if ( options.count( "nodecorations" ) > 0 ) {
103
         foo->nodecorations = options["nodecorations"].as<int>();
103
         foo->nodecorations = options["nodecorations"].as<int>();
104
         if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) {
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
     return foo;
108
     return foo;
113
     for( unsigned int i=0;i<input.length();i++) {
113
     for( unsigned int i=0;i<input.length();i++) {
114
         if ( input[i] == '%' ) {
114
         if ( input[i] == '%' ) {
115
             if ( input.length() <= i+1 ) {
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
             switch( input[i+1] ) {
118
             switch( input[i+1] ) {
119
                 case 'x':
119
                 case 'x':
132
                 case 'i':
132
                 case 'i':
133
                 case 'I': output << selection.id; break;
133
                 case 'I': output << selection.id; break;
134
                 case '%': output << "%"; break;
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
             i++;
137
             i++;
138
             continue;
138
             continue;
242
     // Options just validates all of our input from argv
242
     // Options just validates all of our input from argv
243
     auto& positional = options["positional"].as<std::vector<std::string>>();
243
     auto& positional = options["positional"].as<std::vector<std::string>>();
244
     if ( positional.size() > 0 ) {
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
     bool help = false;
247
     bool help = false;
248
     if ( options.count( "help" ) > 0 ) {
248
     if ( options.count( "help" ) > 0 ) {
297
 int main( int argc, char** argv ) {
297
 int main( int argc, char** argv ) {
298
     try {
298
     try {
299
         return app( argc, argv );
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
         return 1;
302
         return 1;
303
     } // let the operating system handle any other kind of exception.
303
     } // let the operating system handle any other kind of exception.
304
     return 1;
304
     return 1;

+ 1
- 1
src/mouse.cpp View File

58
         tries++;
58
         tries++;
59
     }
59
     }
60
     if ( err != GrabSuccess ) {
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
     this->nodecorations = nodecorations;
63
     this->nodecorations = nodecorations;
64
     this->ignoreWindow = ignoreWindow;
64
     this->ignoreWindow = ignoreWindow;

+ 1
- 1
src/resource.cpp View File

39
         return usrconfig + localpath;
39
         return usrconfig + localpath;
40
     }
40
     }
41
     std::string err = "The file or folder " + localpath + " was not found in " + usrconfig + "\n";
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
     return localpath;
43
     return localpath;
44
 }
44
 }
45
 
45
 

+ 7
- 7
src/shader.cpp View File

8
         frag = resource->getRealPath(frag);
8
         frag = resource->getRealPath(frag);
9
         std::ifstream v( vert.c_str() );
9
         std::ifstream v( vert.c_str() );
10
         if (!v.is_open()) {
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
         vert_contents = std::string((std::istreambuf_iterator<char>(v)),
13
         vert_contents = std::string((std::istreambuf_iterator<char>(v)),
14
                                    std::istreambuf_iterator<char>());
14
                                    std::istreambuf_iterator<char>());
15
         std::ifstream f( frag.c_str() );
15
         std::ifstream f( frag.c_str() );
16
         if (!f.is_open()) {
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
         frag_contents = std::string((std::istreambuf_iterator<char>(f)),
19
         frag_contents = std::string((std::istreambuf_iterator<char>(f)),
20
                                   std::istreambuf_iterator<char>());
20
                                   std::istreambuf_iterator<char>());
31
     
31
     
32
     if ( vert_contents.length() <= 0 ) {
32
     if ( vert_contents.length() <= 0 ) {
33
         std::string errstring = "Failed to open file (or is empty) `" + vert + "`.\n";
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
     if ( frag_contents.length() <= 0 ) {
37
     if ( frag_contents.length() <= 0 ) {
38
         std::string errstring = "Failed to open file (or is empty) `" + frag + "`.\n";
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
     // Compile both shaders.
42
     // Compile both shaders.
47
 
47
 
48
     if ( err ) {
48
     if ( err ) {
49
         std::string errstring = "Failed to compile shader `" + vert + "`:\n" + errortxt;
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
         glDeleteShader( vertShader );
51
         glDeleteShader( vertShader );
52
         return;
52
         return;
53
     }
53
     }
57
     err = compile( fragShader, errortxt );
57
     err = compile( fragShader, errortxt );
58
     if ( err ) {
58
     if ( err ) {
59
         std::string errstring = "Failed to compile shader `" + frag + "`:\n" + errortxt;
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
         glDeleteShader( vertShader );
61
         glDeleteShader( vertShader );
62
         glDeleteShader( fragShader );
62
         glDeleteShader( fragShader );
63
         return;
63
         return;
67
     err = link( vertShader, fragShader, errortxt );
67
     err = link( vertShader, fragShader, errortxt );
68
     if ( err ) {
68
     if ( err ) {
69
         std::string errstring = "Failed to link shader `" + vert + "` and  `" + frag + "`:\n" + errortxt;
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
         glDeleteShader( vertShader );
71
         glDeleteShader( vertShader );
72
         glDeleteShader( fragShader );
72
         glDeleteShader( fragShader );
73
         return;
73
         return;

+ 4
- 4
src/slop.cpp View File

124
             window = new SlopWindow();
124
             window = new SlopWindow();
125
             if (!GLEW_VERSION_3_0) {
125
             if (!GLEW_VERSION_3_0) {
126
                 delete window;
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
             success = true;
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
             success = false;
132
             success = false;
133
         } catch (...) {
133
         } catch (...) {
134
             success = false;
134
             success = false;
314
                 case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
314
                 case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
315
                 case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
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
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
319
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
320
             memory->running = false;
320
             memory->running = false;

+ 6
- 6
src/window.cpp View File

15
     int nelements;
15
     int nelements;
16
     int render_event_base, render_error_base;
16
     int render_event_base, render_error_base;
17
     if(!XRenderQueryExtension(x11->display, &render_event_base, &render_error_base)) {
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
     GLXFBConfig* fbc = glXChooseFBConfig(x11->display, DefaultScreen(x11->display), attributeList, &nelements);
21
     GLXFBConfig* fbc = glXChooseFBConfig(x11->display, DefaultScreen(x11->display), attributeList, &nelements);
22
     GLXFBConfig fbconfig;
22
     GLXFBConfig fbconfig;
23
     if ( !fbc ) {
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
     XVisualInfo* vi ;
26
     XVisualInfo* vi ;
27
     XRenderPictFormat *pictFormat;
27
     XRenderPictFormat *pictFormat;
42
         }
42
         }
43
     }
43
     }
44
     if (i == nelements ) {
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
     XFree( fbc );
47
     XFree( fbc );
48
 
48
 
64
     XFree( vi );
64
     XFree( vi );
65
 
65
 
66
     if ( !window ) {
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
     // Prep some hints for the window
70
     // Prep some hints for the window
97
 
97
 
98
     context = glXCreateNewContext( x11->display, fbconfig, GLX_RGBA_TYPE, 0, True );
98
     context = glXCreateNewContext( x11->display, fbconfig, GLX_RGBA_TYPE, 0, True );
99
     if ( !context ) {
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
     setCurrent();
102
     setCurrent();
103
     // Finally we grab some OpenGL 3.3 stuffs.
103
     // Finally we grab some OpenGL 3.3 stuffs.
104
     GLenum err = glewInit();
104
     GLenum err = glewInit();
105
     if (GLEW_OK != err)
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
     framebuffer = new Framebuffer( WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ) );
109
     framebuffer = new Framebuffer( WidthOfScreen( x11->screen ), HeightOfScreen( x11->screen ) );
110
 
110
 

+ 1
- 1
src/x.cpp View File

92
     // Initialize display
92
     // Initialize display
93
     display = XOpenDisplay( displayName.c_str() );
93
     display = XOpenDisplay( displayName.c_str() );
94
     if ( !display ) {
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
     screen = ScreenOfDisplay( display, DefaultScreen( display ) );
97
     screen = ScreenOfDisplay( display, DefaultScreen( display ) );
98
     visual = DefaultVisual( display, XScreenNumberOfScreen( screen ) );
98
     visual = DefaultVisual( display, XScreenNumberOfScreen( screen ) );

+ 1
- 1
src/xshaperectangle.cpp View File

137
     xc.blue = blue;
137
     xc.blue = blue;
138
     int err = XAllocColor( x11->display, DefaultColormap( x11->display, XScreenNumberOfScreen( x11->screen ) ), &xc );
138
     int err = XAllocColor( x11->display, DefaultColormap( x11->display, XScreenNumberOfScreen( x11->screen ) ), &xc );
139
     if ( err == BadColor ) {
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
     return xc;
142
     return xc;
143
 }
143
 }