debugging - Understanding some C++ coding practice -
i trying understand how following code (http://pastebin.com/zthurmyx) works, approach compiling software in debug , using gdb step through code.
however, i'm running problem 'step' not tell me going on. particularly unclear me execute {...}
cannot step into.
how go learning code doing?
1 /* 2 copyright 2008 brain research institute, melbourne, australia 3 4 written j-donald tournier, 27/06/08. 5 6 file part of mrtrix. 7 8 mrtrix free software: can redistribute and/or modify 9 under terms of gnu general public license published 10 free software foundation, either version 3 of license, or 11 (at option) later version. 12 13 mrtrix distributed in hope useful, 14 without warranty; without implied warranty of 15 merchantability or fitness particular purpose. see 16 gnu general public license more details. 17 18 should have received copy of gnu general public license 19 along mrtrix. if not, see <http://www.gnu.org/licenses/>. 20 21 22 15-10-2008 j-donald tournier <d.tournier@brain.org.au> 23 * fix -prs option handling 24 * remove mr::dicom_dw_gradients_prs flag 25 26 15-10-2008 j-donald tournier <d.tournier@brain.org.au> 27 * add -layout option manipulate data ordering within image file 28 29 14-02-2010 j-donald tournier <d.tournier@brain.org.au> 30 * fix -coord option "end" keyword can used 31 32 33 */ 34 35 #include "app.h" 36 #include "image/position.h" 37 #include "image/axis.h" 38 #include "math/linalg.h" 39 40 using namespace std; 41 using namespace mr; 42 43 set_version_default; 44 45 description = { 46 "perform conversion between different file types , optionally extract subset of input image.", 47 "if used correctly, program can useful workhorse. in addition converting images between different formats, can used extract specific studies data set, extract specific region of interest, flip images, or scale intensity of images.", 48 null 49 }; 50 51 arguments = { 52 argument ("input", "input image", "the input image.").type_image_in (), 53 argument ("ouput", "output image", "the output image.").type_image_out (), 54 argument::end 55 }; 56 57 58 const gchar* type_choices[] = { "real", "imag", "mag", "phase", "complex", null }; 59 const gchar* data_type_choices[] = { "float32", "float32le", "float32be", "float64", "float64le", "float64be", 60 "int32", "uint32", "int32le", "uint32le", "int32be", "uint32be", 61 "int16", "uint16", "int16le", "uint16le", "int16be", "uint16be", 62 "cfloat32", "cfloat32le", "cfloat32be", "cfloat64", "cfloat64le", "cfloat64be", 63 "int8", "uint8", "bit", null }; 64 65 options = { 66 option ("coord", "select coordinates", "extract data @ coordinates specified.", false, true) 67 .append (argument ("axis", "axis", "the axis of interest").type_integer (0, int_max, 0)) 68 .append (argument ("coord", "coordinates", "the coordinates of interest").type_sequence_int()), 69 70 option ("vox", "voxel size", "change voxel dimensions.") 71 .append (argument ("sizes", "new dimensions", "a comma-separated list of values. values specified changed. example: 1,,3.5 change voxel size along x & z axes, , leave y-axis voxel size unchanged.") 72 .type_sequence_float ()), 73 74 option ("datatype", "data type", "specify output image data type.") 75 .append (argument ("spec", "specifier", "the data type specifier.").type_choice (data_type_choices)), 76 77 option ("scale", "scaling factor", "apply scaling intensity values.") 78 .append (argument ("factor", "factor", "the factor multiply intensities.").type_float (nan, nan, 1.0)), 79 80 option ("offset", "offset", "apply offset intensity values.") 81 .append (argument ("bias", "bias", "the value of offset.").type_float (nan, nan, 0.0)), 82 83 option ("zero", "replace nan zero", "replace nan values zero."), 84 85 option ("output", "output type", "specify type of output") 86 .append (argument ("type", "type", "type of output.") 87 .type_choice (type_choices)), 88 89 option ("layout", "data layout", "specify layout of data in memory. actual layout produced depend on whether output image format can support it.") 90 .append (argument ("spec", "specifier", "the data layout specifier.").type_string ()), 91 92 option ("prs", "dw gradient specified prs", "assume dw gradients specified in prs frame (siemens dicom only)."), 93 94 option::end 95 }; 96 97 98 99 inline bool next (image::position& ref, image::position& other, const std::vector<int>* pos) 100 { 101 int axis = 0; 102 { 103 ref.inc (axis); 104 if (ref[axis] < ref.dim(axis)) { 105 other.set (axis, pos[axis][ref[axis]]); 106 return (true); 107 } 108 ref.set (axis, 0); 109 other.set (axis, pos[axis][0]); 110 axis++; 111 } while (axis < ref.ndim()); 112 return (false); 113 } 114 115 116 117 118 119 execute { 120 std::vector<optbase> opt = get_options (1); // vox 121 std::vector<float> vox; 122 if (opt.size()) 123 vox = parse_floats (opt[0][0].get_string()); 124 125 126 opt = get_options (3); // scale 127 float scale = 1.0; 128 if (opt.size()) scale = opt[0][0].get_float(); 129 130 opt = get_options (4); // offset 131 float offset = 0.0; 132 if (opt.size()) offset = opt[0][0].get_float(); 133 134 opt = get_options (5); // 0 135 bool replace_nan = opt.size(); 136 137 opt = get_options (6); // output 138 image::outputtype output_type = image::default; 139 if (opt.size()) { 140 switch (opt[0][0].get_int()) { 141 case 0: output_type = image::real; break; 142 case 1: output_type = image::imaginary; break; 143 case 2: output_type = image::magnitude; break; 144 case 3: output_type = image::phase; break; 145 case 4: output_type = image::realimag; break; 146 } 147 } 148 149 150 151 152 image::object &in_obj (*argument[0].get_image()); 153 154 image::header header (in_obj); 155 156 if (output_type == 0) { 157 if (in_obj.is_complex()) output_type = image::realimag; 158 else output_type = image::default; 159 } 160 161 if (output_type == image::realimag) header.data_type = datatype::cfloat32; 162 else if (output_type == image::phase) header.data_type = datatype::float32; 163 else header.data_type.unset_flag (datatype::complexnumber); 164 165 166 opt = get_options (2); // datatype 167 if (opt.size()) header.data_type.parse (data_type_choices[opt[0][0].get_int()]); 168 169 (guint n = 0; n < vox.size(); n++) 170 if (isfinite (vox[n])) header.axes.vox[n] = vox[n]; 171 172 opt = get_options (7); // layout 173 if (opt.size()) { 174 std::vector<image::axis> ax = parse_axes_specifier (header.axes, opt[0][0].get_string()); 175 if (ax.size() != (guint) header.axes.ndim()) 176 throw exception (string("specified layout \"") + opt[0][0].get_string() + "\" not match image dimensions"); 177 178 (guint = 0; < ax.size(); i++) { 179 header.axes.axis[i] = ax[i].axis; 180 header.axes.forward[i] = ax[i].forward; 181 } 182 } 183 184 185 opt = get_options (8); // prs 186 if (opt.size() && header.dw_scheme.rows() && header.dw_scheme.columns()) { 187 (guint row = 0; row < header.dw_scheme.rows(); row++) { 188 double tmp = header.dw_scheme(row, 0); 189 header.dw_scheme(row, 0) = header.dw_scheme(row, 1); 190 header.dw_scheme(row, 1) = tmp; 191 header.dw_scheme(row, 2) = -header.dw_scheme(row, 2); 192 } 193 } 194 195 std::vector<int> pos[in_obj.ndim()]; 196 197 opt = get_options (0); // coord 198 (guint n = 0; n < opt.size(); n++) { 199 int axis = opt[n][0].get_int(); 200 if (pos[axis].size()) throw exception ("\"coord\" option specified twice axis " + str (axis)); 201 pos[axis] = parse_ints (opt[n][1].get_string(), header.dim(axis)-1); 202 header.axes.dim[axis] = pos[axis].size(); 203 } 204 205 (int n = 0; n < in_obj.ndim(); n++) { 206 if (pos[n].empty()) { 207 pos[n].resize (in_obj.dim(n)); 208 (guint = 0; < pos[n].size(); i++) pos[n][i] = i; 209 } 210 } 211 212 213 in_obj.apply_scaling (scale, offset); 214 215 216 217 218 219 220 image::position in (in_obj); 221 image::position out (*argument[1].get_image (header)); 222 223 (int n = 0; n < in.ndim(); n++) in.set (n, pos[n][0]); 224 225 progressbar::init (out.voxel_count(), "copying data..."); 226 227 { 228 229 float re, im = 0.0; 230 in.get (output_type, re, im); 231 if (replace_nan) if (gsl_isnan (re)) re = 0.0; 232 out.re (re); 233 234 if (output_type == image::realimag) { 235 if (replace_nan) if (gsl_isnan (im)) im = 0.0; 236 out.im (im); 237 } 238 239 progressbar::inc(); 240 } while (next (out, in, pos)); 241 242 progressbar::done(); 243 }
as noted in comments, execute
seems macro, apparent context function header (and maybe bit more, e.g. global variables , functions), part in curly braces function body.
to definition of execute
, have examine headers.
however, if can reach part of code during debugging, insert string
or char[]
@ point, giving stringified version of execute
, whatever preprocessor emit execute
at position in code.
#define str(x) #x #define stringify(x) str(x) char c[] = stringify(execute);
the 2 macros known little macro trick content of macro string literal. try out , inspect char array in debugger content of execute.
my wild guess here: execute
main function or replacement it, options
, arguments
describe arguments program expects , command line options can pass it. macros , of used functions , variables (get_options
, argument
) part of little framework should facilitate usage, evaluation , user information command line options.
Comments
Post a Comment