# HG changeset patch # User Eugenio Gianniti # Date 1407402834 -7200 # Node ID f22588ae37af2ca6e475e42a9d88c84d29fa2adf # Parent 1e2a9be8083a76a8335641c6e518d6c8a351e61e Improve template meshfunction implementation * inst/import_meshfunction_type.m: provide to the auxiliary functions a type name and a valid identifier * inst/private/generate_mf_*.m: add space before closing angle bracket to avoid parse errors diff -r 1e2a9be8083a -r f22588ae37af inst/import_meshfunction_type.m --- a/inst/import_meshfunction_type.m Wed Aug 06 19:52:03 2014 +0200 +++ b/inst/import_meshfunction_type.m Thu Aug 07 11:13:54 2014 +0200 @@ -16,6 +16,7 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} import_meshfunction_type (@var{typename}) ## Add a wrapper for dolfin::MeshFunction <@var{typename}> +## @seealso {MeshFunction} ## @end deftypefn function import_meshfunction_type (typename) @@ -27,28 +28,29 @@ error ("import_meshfunction_type: first argument is not a valid string"); endif - # Ensure that the typename does not contain whitespace typename = strtrim (typename); - typename = strjoin (strsplit (typename), ""); + + # Ensure that the label does not contain whitespace or symbols + label = genvarname (strjoin (strsplit (typename), "")); if (! check_mf_files (typename)) n = length (mfilename ("fullpath")) - length (mfilename()); path = strtrunc(mfilename ("fullpath"), n); private = fullfile (path, "include/"); - output = generate_mf_header (typename); - output += generate_mf_constructor (typename); - output += generate_mf_save (typename); - output += generate_mf_makefile (typename, private); + output = generate_mf_header (typename, label); + output += generate_mf_constructor (label); + output += generate_mf_save (typename, label); + output += generate_mf_makefile (label, private); if (output != 0) error ("compilation failed"); else - [output, textfile] = system (["make -f Makefile_", typename, " all"]); + [output, textfile] = system (["make -f Makefile_", label, " all"]); if (output != 0) display (text); error ("compilation failed"); endif - [output, textfile] = system (["make -f Makefile_", typename, " clean"]); + [output, textfile] = system (["make -f Makefile_", label, " clean"]); if (output != 0) display (text); error ("compilation failed"); diff -r 1e2a9be8083a -r f22588ae37af inst/private/check_mf_files.m --- a/inst/private/check_mf_files.m Wed Aug 06 19:52:03 2014 +0200 +++ b/inst/private/check_mf_files.m Thu Aug 07 11:13:54 2014 +0200 @@ -18,14 +18,14 @@ ## function for internal usage only ## @end deftypefn -function found = check_mf_files (typename) - if (! ischar (typename)) +function found = check_mf_files (label) + if (! ischar (label)) error ("check_mf_files: invalid argument"); endif - [~, err, ~] = stat (["MeshFunction_", typename, ".oct"]); + [~, err, ~] = stat (["MeshFunction_", label, ".oct"]); read = (err == 0); - [~, err, ~] = stat (["@meshfunction_", typename, "/save.oct"]); + [~, err, ~] = stat (["@meshfunction_", label, "/save.oct"]); write = (err == 0); found = read && write; diff -r 1e2a9be8083a -r f22588ae37af inst/private/generate_mf_constructor.m --- a/inst/private/generate_mf_constructor.m Wed Aug 06 19:52:03 2014 +0200 +++ b/inst/private/generate_mf_constructor.m Thu Aug 07 11:13:54 2014 +0200 @@ -18,15 +18,15 @@ ## function for internal usage only ## @end deftypefn -function output = generate_mf_constructor (typename) +function output = generate_mf_constructor (label) STRING ="\n\ #include \n\ #include \n\ -#include ""meshfunction_@@TYPENAME@@.h""\n\ +#include ""meshfunction_@@LABEL@@.h""\n\ #include \n\ \n\ -DEFUN_DLD (MeshFunction_@@TYPENAME@@, args, nargout, ""MF = MeshFunction_@@TYPENAME@@ (MESH, FILENAME)"")\n\ +DEFUN_DLD (MeshFunction_@@LABEL@@, args, nargout, ""MF = MeshFunction_@@LABEL@@ (MESH, FILENAME)"")\n\ {\n\ \n\ int nargin = args.length ();\n\ @@ -43,10 +43,10 @@ mlock ();\n\ }\n\ \n\ - if (! meshfunction_@@TYPENAME@@_type_loaded)\n\ + if (! meshfunction_@@LABEL@@_type_loaded)\n\ {\n\ - meshfunction_@@TYPENAME@@::register_type ();\n\ - meshfunction_@@TYPENAME@@_type_loaded = true;\n\ + meshfunction_@@LABEL@@::register_type ();\n\ + meshfunction_@@LABEL@@_type_loaded = true;\n\ mlock ();\n\ }\n\ \n\ @@ -64,7 +64,7 @@ filename += "".xdmf"";\n\ \n\ try\n\ - { retval = new meshfunction_@@TYPENAME@@ (pmsh, filename); }\n\ + { retval = new meshfunction_@@LABEL@@ (pmsh, filename); }\n\ catch (std::runtime_error &)\n\ { error (""error reading file""); }\n\ }\n\ @@ -76,9 +76,9 @@ return retval;\n\ }"; -STRING = strrep (STRING, "@@TYPENAME@@", typename); +STRING = strrep (STRING, "@@LABEL@@", label); -fid = fopen (["MeshFunction_", typename, ".cc"], "w"); +fid = fopen (["MeshFunction_", label, ".cc"], "w"); if (is_valid_file_id (fid)) fputs (fid, STRING); output = fclose (fid); diff -r 1e2a9be8083a -r f22588ae37af inst/private/generate_mf_header.m --- a/inst/private/generate_mf_header.m Wed Aug 06 19:52:03 2014 +0200 +++ b/inst/private/generate_mf_header.m Thu Aug 07 11:13:54 2014 +0200 @@ -18,30 +18,30 @@ ## function for internal usage only ## @end deftypefn -function output = generate_mf_header (typename) +function output = generate_mf_header (typename, label) STRING ="\n\ -#ifndef _MESHFUNCTION_@@TYPENAME@@_OCTAVE_\n\ -#define _MESHFUNCTION_@@TYPENAME@@_OCTAVE_\n\ +#ifndef _MESHFUNCTION_@@LABEL@@_OCTAVE_\n\ +#define _MESHFUNCTION_@@LABEL@@_OCTAVE_\n\ \n\ #include \n\ #include \n\ #include \n\ \n\ -class meshfunction_@@TYPENAME@@ : public octave_base_value\n\ +class meshfunction_@@LABEL@@ : public octave_base_value\n\ {\n\ public:\n\ \n\ - meshfunction_@@TYPENAME@@ (void)\n\ + meshfunction_@@LABEL@@ (void)\n\ : octave_base_value () {}\n\ \n\ - meshfunction_@@TYPENAME@@ (dolfin::MeshFunction <@@TYPENAME@@> const & _mf)\n\ - : octave_base_value (), pmf (new dolfin::MeshFunction <@@TYPENAME@@> (_mf)) {}\n\ + meshfunction_@@LABEL@@ (dolfin::MeshFunction <@@TYPENAME@@ > const & _mf)\n\ + : octave_base_value (), pmf (new dolfin::MeshFunction <@@TYPENAME@@ > (_mf)) {}\n\ \n\ - meshfunction_@@TYPENAME@@ (SHARED_PTR mesh,\n\ + meshfunction_@@LABEL@@ (SHARED_PTR mesh,\n\ std::string const & filename)\n\ : octave_base_value (),\n\ - pmf (new dolfin::MeshFunction <@@TYPENAME@@> (mesh, filename)) {}\n\ + pmf (new dolfin::MeshFunction <@@TYPENAME@@ > (mesh, filename)) {}\n\ \n\ bool\n\ is_defined (void) const\n\ @@ -49,36 +49,42 @@ \n\ void\n\ print (std::ostream& os, bool pr_as_read_syntax = false) const\n\ - { os << ""MeshFunction <@@TYPENAME@@>: ""\n\ - << get_pmf ()->str (false) << std::endl; }\n\ + { os << ""MeshFunction <@@TYPENAME@@ >: ""\n\ + << get_pmf ()->str (@@VERBOSE@@) << std::endl; }\n\ \n\ - dolfin::MeshFunction <@@TYPENAME@@> const &\n\ + dolfin::MeshFunction <@@TYPENAME@@ > const &\n\ get_mf (void) const\n\ { return *pmf; }\n\ \n\ - SHARED_PTR const> const &\n\ + SHARED_PTR const> const &\n\ get_pmf (void) const\n\ { return pmf; }\n\ \n\ private:\n\ \n\ - SHARED_PTR const> pmf;\n\ + SHARED_PTR const> pmf;\n\ \n\ DECLARE_OCTAVE_ALLOCATOR;\n\ DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA;\n\ };\n\ \n\ -static bool meshfunction_@@TYPENAME@@_type_loaded = false;\n\ +static bool meshfunction_@@LABEL@@_type_loaded = false;\n\ \n\ -DEFINE_OCTAVE_ALLOCATOR (meshfunction_@@TYPENAME@@);\n\ -DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (meshfunction_@@TYPENAME@@,\n\ -""meshfunction_@@TYPENAME@@"", ""meshfunction_@@TYPENAME@@"");\n\ +DEFINE_OCTAVE_ALLOCATOR (meshfunction_@@LABEL@@);\n\ +DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (meshfunction_@@LABEL@@,\n\ +""meshfunction_@@LABEL@@"", ""meshfunction_@@LABEL@@"");\n\ \n\ #endif"; -STRING = strrep (STRING, "@@TYPENAME@@", typename); +STRING = strrep (STRING, "@@TYPENAME@@", typename); +STRING = strrep (STRING, "@@LABEL@@", label); +verbose = "false"; +if (strcmp (typename, "double")) + verbose = "true"; +endif +STRING = strrep (STRING, "@@VERBOSE@@", verbose); -fid = fopen (["meshfunction_", typename, ".h"], "w"); +fid = fopen (["meshfunction_", label, ".h"], "w"); if (is_valid_file_id (fid)) fputs (fid, STRING); output = fclose (fid); diff -r 1e2a9be8083a -r f22588ae37af inst/private/generate_mf_makefile.m --- a/inst/private/generate_mf_makefile.m Wed Aug 06 19:52:03 2014 +0200 +++ b/inst/private/generate_mf_makefile.m Thu Aug 07 11:13:54 2014 +0200 @@ -18,7 +18,7 @@ ## function for internal usage only ## @end deftypefn -function output = generate_mf_makefile (typename, path) +function output = generate_mf_makefile (label, path) STRING ="\n\ DIR = @@PATH@@\n\ @@ -27,36 +27,36 @@ MKOCTFILE = mkoctfile\n\ FFC = ffc\n\ \n\ -OCTFILES = MeshFunction_@@TYPENAME@@.oct save_@@TYPENAME@@.oct\n\ +OCTFILES = MeshFunction_@@LABEL@@.oct save_@@LABEL@@.oct\n\ \n\ all : $(OCTFILES)\n\ \n\ -MeshFunction_@@TYPENAME@@.oct: MeshFunction_@@TYPENAME@@.cc \ - meshfunction_@@TYPENAME@@.h\n\ +MeshFunction_@@LABEL@@.oct: MeshFunction_@@LABEL@@.cc \ + meshfunction_@@LABEL@@.h\n\ CPPFLAGS='$(CPPFLAGS)' $(MKOCTFILE) $< -o $@ -I$(DIR) -I. $(LDFLAGS) $(LIBS)\n\ \n\ -save_@@TYPENAME@@.oct: save_@@TYPENAME@@.cc meshfunction_@@TYPENAME@@.h mkmfdir\n\ - CPPFLAGS='$(CPPFLAGS)' $(MKOCTFILE) $< -o @meshfunction_@@TYPENAME@@/save.oct\ +save_@@LABEL@@.oct: save_@@LABEL@@.cc meshfunction_@@LABEL@@.h mkmfdir\n\ + CPPFLAGS='$(CPPFLAGS)' $(MKOCTFILE) $< -o @meshfunction_@@LABEL@@/save.oct\ -I$(DIR) -I. $(LDFLAGS) $(LIBS)\n\ \n\ mkmfdir:\n\ - mkdir -p @meshfunction_@@TYPENAME@@\n\ + mkdir -p @meshfunction_@@LABEL@@\n\ \n\ .PHONY: clean\n\ \n\ clean:\n\ - $(RM) meshfunction_@@TYPENAME@@.h save_@@TYPENAME@@.o\n\ - $(RM) MeshFunction_@@TYPENAME@@.o MeshFunction_@@TYPENAME@@.cc\n\ - $(RM) save_@@TYPENAME@@.cc\n\ - $(RM) Makefile_@@TYPENAME@@\n\ + $(RM) meshfunction_@@LABEL@@.h save_@@LABEL@@.o\n\ + $(RM) MeshFunction_@@LABEL@@.o MeshFunction_@@LABEL@@.cc\n\ + $(RM) save_@@LABEL@@.cc\n\ + $(RM) Makefile_@@LABEL@@\n\ "; -STRING = strrep (STRING, "@@TYPENAME@@", typename); +STRING = strrep (STRING, "@@LABEL@@", label); STRING = strrep (STRING, "@@PATH@@", path); STRING = strrep (STRING, "@@FF_CPPFLAGS@@", get_vars ("CPPFLAGS")); STRING = strrep (STRING, "@@FF_LIBS@@", get_vars ("LIBS")); -fid = fopen (["Makefile_", typename], "w"); +fid = fopen (["Makefile_", label], "w"); if (is_valid_file_id (fid)) fputs (fid, STRING); output = fclose (fid); diff -r 1e2a9be8083a -r f22588ae37af inst/private/generate_mf_save.m --- a/inst/private/generate_mf_save.m Wed Aug 06 19:52:03 2014 +0200 +++ b/inst/private/generate_mf_save.m Thu Aug 07 11:13:54 2014 +0200 @@ -18,10 +18,10 @@ ## function for internal usage only ## @end deftypefn -function output = generate_mf_save (typename) +function output = generate_mf_save (typename, label) STRING ="\n\ -#include ""meshfunction_@@TYPENAME@@.h""\n\ +#include ""meshfunction_@@LABEL@@.h""\n\ \n\ DEFUN_DLD (save, args, nargout, ""save (MESHFUNCTION, FILENAME)"")\n\ {\n\ @@ -32,24 +32,24 @@ print_usage ();\n\ else\n\ {\n\ - if (! meshfunction_@@TYPENAME@@_type_loaded)\n\ + if (! meshfunction_@@LABEL@@_type_loaded)\n\ {\n\ - meshfunction_@@TYPENAME@@::register_type ();\n\ - meshfunction_@@TYPENAME@@_type_loaded = true;\n\ + meshfunction_@@LABEL@@::register_type ();\n\ + meshfunction_@@LABEL@@_type_loaded = true;\n\ mlock ();\n\ }\n\ \n\ - if (args(0).type_id () == meshfunction_@@TYPENAME@@::static_type_id ()\n\ + if (args(0).type_id () == meshfunction_@@LABEL@@::static_type_id ()\n\ && args(1).is_string ())\n\ {\n\ - meshfunction_@@TYPENAME@@ const & mf_arg =\n\ - static_cast\n\ + meshfunction_@@LABEL@@ const & mf_arg =\n\ + static_cast\n\ (args(0).get_rep ());\n\ std::string str = args(1).string_value ();\n\ \n\ if (!error_state)\n\ {\n\ - dolfin::MeshFunction <@@TYPENAME@@> const &\n\ + dolfin::MeshFunction <@@TYPENAME@@ > const &\n\ mf = mf_arg.get_mf ();\n\ str += "".xdmf"";\n\ try\n\ @@ -58,7 +58,7 @@ file << mf;\n\ }\n\ catch (std::runtime_error &)\n\ - { error (""error saving meshfunction@@TYPENAME@@""); }\n\ + { error (""error saving meshfunction@@LABEL@@""); }\n\ retval = 0;\n\ }\n\ }\n\ @@ -69,9 +69,10 @@ return retval;\n\ }"; -STRING = strrep (STRING, "@@TYPENAME@@", typename); +STRING = strrep (STRING, "@@TYPENAME@@", typename); +STRING = strrep (STRING, "@@LABEL@@", label); -fid = fopen (["save_", typename, ".cc"], "w"); +fid = fopen (["save_", label, ".cc"], "w"); if (is_valid_file_id (fid)) fputs (fid, STRING); output = fclose (fid);