%% Example: Generate C++ Code with Namespaces
% This example shows how to use these code generation settings to create namespaces.
%% Define MATLAB® Functions
% Define two MATLAB functions foo and bar in two separate files foo.m and bar.m. Place the file bar.m in a package myPackage. The function foo accepts a string input and then calls the function bar.

type foo.m
type +mypackage/bar.m

%
%% Define Code Configuration Object
% Create a code configuration object for a static library. Set the target language to C++. Specify the namespace that contains all generated code to be named allcode. Specify the namespace that contains MathWorks® code to be named notmycode.

cfg = coder.config('lib');
cfg.TargetLang = 'C++';
cfg.CppNamespace = 'allcode';
cfg.CppNamespaceForMathworksCode = 'notmycode';

%% Generate Code
% Generate a static C++ library and a code generation report. Specify the input type to be string scalar that can have any length.

s = "mystring";
t = coder.typeof(s);
t.Properties.Value = coder.typeof('a',[1 inf]);

codegen -config cfg foo -args {t} -report

%% Inspect Generated Code
% Open the code generation report and inspect the generated code.
% The file foo.h contains the declaration of the generated function foo. Because the MATLAB function foo that you created is not inside a package, the generated function is declared only inside the namespace allcode that contains all generated code.

type codegen/lib/foo/foo.h

% The file bar.h contains the declaration of the generated function bar. Because you created the MATLAB function bar inside the package mypackage, the generated function is declared inside the namespace hierarchy allcode::myPackage.

type codegen/lib/foo/bar.h

% The file string1.h contains the declaration of the generated class rtString that implements the MATLAB string data type. Because you instructed the code generator to place all code produced for MathWorks code inside the namespace notmycode, the generated class rtString is declared inside the namespace hierarchy allcode::notmycode.

type codegen/lib/foo/string1.h

% Copyright 2020 The MathWorks, Inc.