matlabstring.h
Go to the documentation of this file.
1 // Copyright (C) 2007 Peter Carbonetto. All Rights Reserved.
2 // This code is published under the Eclipse Public License.
3 //
4 // Author: Peter Carbonetto
5 // Dept. of Computer Science
6 // University of British Columbia
7 // May 19, 2007
8 
9 #ifndef INCLUDE_MATLABSTRING
10 #define INCLUDE_MATLABSTRING
11 
12 #include "mex.h"
13 #include <string>
14 #include <cstring>
15 
16 // Function declarations.
17 // -----------------------------------------------------------------
18 // Copy a C-style string (i.e. a null-terminated character array).
19 char* copystring (const char* source);
20 
21 // Class MatlabString.
22 // -----------------------------------------------------------------
23 // This class encapsulates read-only access to a MATLAB character
24 // array.
25 class MatlabString {
26 public:
27 
28  // The constructor accepts as input a pointer to a Matlab array,
29  // which must be a valid string (array of type CHAR).
30  explicit MatlabString (const mxArray* ptr);
31 
32  // This constructor accepts as input a null-terminated string.
33  explicit MatlabString (const char* s);
34 
35  // The copy constructor makes a full copy of the source string.
36  MatlabString (const MatlabString& source);
37 
38  // The destructor.
39  ~MatlabString();
40 
41  // Return true if the string is empty.
42  bool isempty () const { return strlen(s) == 0; };
43 
44  // Conversion operator for null-terminated string.
45  operator const char* () const { return s; };
46 
47  // Conversion operator for string object.
48  operator std::string () const { return std::string(s); };
49 
50 protected:
51  char* s; // The null-terminated string.
52 
53  // The copy assignment operator is not proper, thus remains
54  // protected.
55  MatlabString& operator= (const MatlabString& source) { return *this; };
56 };
57 
58 #endif