sg-s/cpplab

cpplab should disallow anything other than a double to be stored in the tree

Closed this issue · 2 comments

sg-s commented
cpplab should disallow anything other than a double to be stored in the tree
sg-s commented

for example, in xolotl, it should not be possible to do this:

x.AB.ACurrent.gbar = randn(1e3,1);

When the xolotl object is serialized and the number of parameter names and parameter values do not match up, an error like this occurs:

Error using mex
/home/marder/code/xolotl/mexBridge0d801a.cpp: In functionvoid mexFunction(int, mxArray**, int, const mxArray**)’:
/home/marder/code/xolotl/mexBridge0d801a.cpp:114:22: error:AB_ACurrent_gbarwas not declared in this scope
 ACurrent AB_ACurrent(AB_ACurrent_gbar,AB_ACurrent_E,AB_ACurrent_m,AB_ACurrent_h);
                      ^~~~~~~~~~~~~~~~
/home/marder/code/xolotl/mexBridge0d801a.cpp:114:22: note: suggested alternative:AB_HCurrent_gbarACurrent AB_ACurrent(AB_ACurrent_gbar,AB_ACurrent_E,AB_ACurrent_m,AB_ACurrent_h);
                      ^~~~~~~~~~~~~~~~
                      AB_HCurrent_gbar


Error in xolotl/compile (line 50)
	mex('-silent',ipath,mexBridge_name,'-outdir',self.xolotl_folder)

Parameter names and values do not match up with a parameter has an empty value (eg. []) or when a parameter has a non-scalar value (eg. [1 2]). Since MATLAB does not disambiguate clearly between scalars and matrices in a type-oriented way, each parameter value must be checked independently. This requires a call to

isscalar(parameter_value)

This is a small performance hit.

>> tic; isscalar(x.AB.ACurrent.gbar); toc;
Elapsed time is 0.000591 seconds.
>> tic; isscalar(x.AB.ACurrent.gbar); toc;
Elapsed time is 0.000848 seconds.
>> tic; for ii = 1:1000, isscalar(x.AB.ACurrent.gbar); end; toc
Elapsed time is 0.126321 seconds.

The best compromise solution is to throw an error when length(parameter_names) ~= length(parameter_values) during serializing, and to have a flag with turns on and off "safety" mode (similar to an inbounds macro in other languages), which turns on and off checking for scalar values.