Matlab.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_Matlab_H
21 #define MIRTK_Matlab_H
22 
23 // Disable "Private field 'dependency' is not used" warning
24 #if defined __clang__
25  #pragma clang diagnostic push
26  #pragma clang diagnostic ignored "-Wunused-private-field"
27 #endif
28 // Include MATLAB Compiler Runtime header
29 #include <mclmcrrt.h>
30 // Enable warnings again
31 #if defined __clang__
32  #pragma clang diagnostic pop
33 #endif
34 
35 
36 namespace mirtk {
37 
38 
39 /**
40  * Interface to MATLAB Compiler Runtime (MCR)
41  *
42  * The static Matlab::Initialize or Matlab::InitializeApplication
43  * functions should be called before any code that either makes use of the
44  * MCR C++ functions or code generated by the MATLAB Compiler, respectively.
45  * Only the first invocation of these functions will execute the MCR
46  * initialization code. This is to ensure that the MCR was initialized.
47  * Usually this initialization should take place at the begin of the main
48  * function already, after the parsing of the command-line options.
49  *
50  * Code that makes use of the MATLAB C++ types and functions should include
51  * the MCR proxy header file instead of the C++ header files shipped with
52  * MATLAB itself. This guarantees that users only need an installation of the
53  * free MCR to execute the code. Note that mclmcrrt.h is included by
54  * mirtkMatlab.h and that it is recommended to include mirtkMatlab.h instead.
55  *
56  * @code
57  * #include "mirtk/Matlab.h"
58  *
59  * int main(int argc, char *argv[]) {
60  * // Either load MCR libraries and environment only
61  * mirtk::Matlab::Initialize();
62  * // or initialize MCR completely
63  * mirtk::Matlab::InitializeApplication();
64  *
65  * // [...] Use MATLAB types and functions
66  *
67  * return 0;
68  * }
69  * @endcode
70  */
71 class Matlab
72 {
73  // ---------------------------------------------------------------------------
74  // Attributes
75 private:
76 
77  /// Singleton object, auto-terminates MCR application when destructed
78  static Matlab _Instance;
79 
80  /// Whether the MCR libraries are loaded and the environment is initialized
81  static bool _McrInitialized;
82 
83  /// Whether the MCR application is initialized
84  static bool _AppInitialized;
85 
86  // ---------------------------------------------------------------------------
87  // Construction/destruction
88 private:
89 
90  /// Singleton object constructor
91  Matlab();
92 
93  /// Terminates MCR application if it was initialized during program execution
94  ~Matlab();
95 
96 public:
97 
98  /// Get static singleton object
99  static Matlab &Instance();
100 
101  // ---------------------------------------------------------------------------
102  // Initialization
103 
104  /// Load MATLAT Compiler Runtime libraries and set environment
105  ///
106  /// Loads three main libraries (libmex, libmat, libmclmcr) and sets runtime
107  /// environment for running MATLAB functions.
108  ///
109  /// Use InitializeApplication to initialize a standalone MCR application,
110  /// which will also call mclmcrInitialize. This function may be used if
111  /// mclInitializeApplication is not required, e.g., when only using some
112  /// of MATLAB's C++ types and functions such as to read/write a MAT file.
113  /// If code generated from .m files using the MATLAB Compiler is called,
114  /// use InitializeApplication instead. If the MCR application uses a mclRunMain
115  /// function, however, call Initialize first in the main function and then
116  /// InitializeApplication in the mclRunMain function.
117  ///
118  /// This static function can be called at any time by the main thread before
119  /// the execution of any MATLAB Compiler generated code. It will only
120  /// initialize the MCR upon the first invocation.
121  ///
122  /// @sa mclmrcInitialize
123  /// @sa http://uk.mathworks.com/matlabcentral/newsreader/view_thread/240279
124  static void Initialize();
125 
126  /// Initialize MATLAB Compiler Runtime
127  ///
128  /// This function must be called before any of the code generated from .m
129  /// files using the MATLAB Compiler is executed. It calls the MCR function
130  /// mclInitializeApplication which implicitly loads the MCR libraries,
131  /// and throws an error if this function failed.
132  ///
133  /// The MCR application is terminated automatically by the destructor of the
134  /// static singleton object, i.e., mclTerminateApplication must not be called
135  /// explicitly by the user.
136  ///
137  /// This static function can be called at any time by the main thread before
138  /// the execution of any MATLAB Compiler generated code. It will only
139  /// initialize the MCR upon the first invocation.
140  ///
141  /// @sa mclmrcInitialize
142  /// @sa mclInitializeApplication
143  /// @sa http://uk.mathworks.com/matlabcentral/newsreader/view_thread/240279
144  static void InitializeApplication(const char **options, int count);
145 
146 };
147 
148 
149 } // namespace mirtk
150 
151 #endif // MIRTK_Matlab_H
static void InitializeApplication(const char **options, int count)
Definition: IOConfig.h:41
static Matlab & Instance()
Get static singleton object.
static void Initialize()