From fc4203318c6aac768d1a71bddd774092b6c60435 Mon Sep 17 00:00:00 2001 From: Vardan Akopian Date: Tue, 8 Sep 2015 18:43:20 -0700 Subject: [PATCH 1/3] add const and reference to methods and their arguments --- include/EFStream.hpp | 2 +- include/MatlabIO.hpp | 36 +++++++++++++++++------------------ include/MatlabIOContainer.hpp | 8 ++++---- src/MatlabIO.cpp | 24 +++++++++++------------ 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/include/EFStream.hpp b/include/EFStream.hpp index a17154b..bbeb2ee 100644 --- a/include/EFStream.hpp +++ b/include/EFStream.hpp @@ -56,7 +56,7 @@ class EFStream : public std::fstream { EFStream() : byte_swap_(false) {} // get and set byte swap methods - bool byteSwap(void) { return byte_swap_; } + bool byteSwap(void) const { return byte_swap_; } void setByteSwap(bool state) { byte_swap_ = state; } // method to swap the Endianness of a stream diff --git a/include/MatlabIO.hpp b/include/MatlabIO.hpp index de19995..2521b76 100644 --- a/include/MatlabIO.hpp +++ b/include/MatlabIO.hpp @@ -77,38 +77,38 @@ class MatlabIO { bool byte_swap_; int bytes_read_; std::string filename_; - EFStream fid_; + mutable EFStream fid_; // internal methods void getHeader(void); void setHeader(void); bool hasVariable(void) { return fid_.peek() != EOF; } - template MatlabIOContainer constructMatrix(std::vector& name, std::vector& dims, std::vector& real, std::vector& imag, uint32_t stor_type); - MatlabIOContainer constructString(std::vector& name, std::vector& dims, std::vector& real); - MatlabIOContainer constructSparse(std::vector& name, std::vector& dims, std::vector& real, std::vector& imag); - MatlabIOContainer constructCell(std::vector& name, std::vector& dims, std::vector& real); - MatlabIOContainer constructStruct(std::vector& name, std::vector& dims, std::vector& real); - const char * readVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const char *data); - MatlabIOContainer collateMatrixFields(uint32_t data_type, uint32_t nbytes, std::vector data); - std::vector uncompressVariable(uint32_t& data_type, uint32_t& dbytes, uint32_t& wbytes, const std::vector &data); - MatlabIOContainer readVariable(uint32_t data_type, uint32_t nbytes, const std::vector &data); - MatlabIOContainer readBlock(void); - MatlabIOContainer uncompressFromBin(std::vector data, uint32_t nbytes); + template MatlabIOContainer constructMatrix(std::vector& name, std::vector& dims, std::vector& real, std::vector& imag, uint32_t stor_type) const; + MatlabIOContainer constructString(std::vector& name, std::vector& dims, std::vector& real) const; + MatlabIOContainer constructSparse(std::vector& name, std::vector& dims, std::vector& real, std::vector& imag) const; + MatlabIOContainer constructCell(std::vector& name, std::vector& dims, std::vector& real) const; + MatlabIOContainer constructStruct(std::vector& name, std::vector& dims, std::vector& real) const; + const char * readVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const char *data) const; + MatlabIOContainer collateMatrixFields(uint32_t data_type, uint32_t nbytes, const std::vector& data) const; + std::vector uncompressVariable(uint32_t& data_type, uint32_t& dbytes, uint32_t& wbytes, const std::vector &data) const; + MatlabIOContainer readVariable(uint32_t data_type, uint32_t nbytes, const std::vector &data) const; + MatlabIOContainer readBlock(void) const; + MatlabIOContainer uncompressFromBin(std::vector data, uint32_t nbytes) const; public: // constructors MatlabIO() {} // destructor ~MatlabIO() { close(); } // get and set methods - std::string filename(void) { return std::string(filename_); } + const std::string& filename(void) const { return filename_; } // read and write routines - bool open(std::string filename, std::string mode); + bool open(const std::string& filename, const std::string& mode); bool close(void); std::vector read(void); - void whos(std::vector variables) const; + void whos(const std::vector& variables) const; // templated functions (must be declared and defined in the header file) template - T find(std::vector& variables, std::string name) const { + T find(const std::vector& variables, const std::string& name) const { for (unsigned int n = 0; n < variables.size(); ++n) { if (variables[n].name().compare(name) == 0) { if (isPrimitiveType()) { @@ -121,7 +121,7 @@ class MatlabIO { throw new std::exception(); } - MatlabIOContainer find(std::vector& variables, std::string name) const { + MatlabIOContainer find(const std::vector& variables, const std::string name) const { for (unsigned int n = 0; n < variables.size(); ++n) { if (variables[n].name().compare(name) == 0) return variables[n]; } @@ -129,7 +129,7 @@ class MatlabIO { } template - bool typeEquals(std::vector& variables, std::string name) const { + bool typeEquals(const std::vector& variables, const std::string name) const { for (unsigned int n = 0; n < variables.size(); ++n) { if (variables[n].name().compare(name) == 0) return variables[n].typeEquals(); } diff --git a/include/MatlabIOContainer.hpp b/include/MatlabIOContainer.hpp index bba4596..29ef5bb 100644 --- a/include/MatlabIOContainer.hpp +++ b/include/MatlabIOContainer.hpp @@ -64,12 +64,12 @@ class MatlabIOContainer { * @param name the string name of the variable * @param data the associated data, of any type */ - MatlabIOContainer(const std::string name, const boost::any data) : name_(name), data_(data) {} + MatlabIOContainer(const std::string& name, const boost::any data) : name_(name), data_(data) {} // destructor ~MatlabIOContainer() {} // set methods - void setName(const std::string name) { name_ = name; } - void setData(const boost::any data) { data_ = data; } + void setName(const std::string& name) { name_ = name; } + void setData(const boost::any& data) { data_ = data; } // get methods /*! @brief Check if the stored type is equal to the templated type * @@ -102,7 +102,7 @@ class MatlabIOContainer { if (tp == typeid(void)) return TypeName::toString(); return std::string(tp.name()); } - std::string name(void) const { return name_; } + const std::string& name(void) const { return name_; } /*! @brief The stored data * * Returns the stored data, cast to the templated type diff --git a/src/MatlabIO.cpp b/src/MatlabIO.cpp index a38c31d..8b64a09 100644 --- a/src/MatlabIO.cpp +++ b/src/MatlabIO.cpp @@ -51,7 +51,7 @@ using namespace cv; * @param mode either "r" for reading or "w" for writing * @return true if the file open succeeded, false otherwise */ -bool MatlabIO::open(string filename, string mode) { +bool MatlabIO::open(const string& filename, const string& mode) { // open the file filename_ = filename; @@ -181,7 +181,7 @@ void MatlabIO::getHeader(void) { * @param data the input binary blob * @return a pointer to the beginning of the data segment of the binary blob */ -const char * MatlabIO::readVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const char *data) { +const char * MatlabIO::readVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const char *data) const { bool small = false; const uint32_t *datai = reinterpret_cast(data); @@ -214,7 +214,7 @@ const char * MatlabIO::readVariableTag(uint32_t &data_type, uint32_t &dbytes, ui * @param real * @return */ -MatlabIOContainer MatlabIO::constructStruct(vector& name, vector& dims, vector& real) { +MatlabIOContainer MatlabIO::constructStruct(vector& name, vector& dims, vector& real) const { vector > array; const char* real_ptr = &(real[0]); @@ -275,7 +275,7 @@ MatlabIOContainer MatlabIO::constructStruct(vector& name, vector * @param real the real part * @return the wrapped cell array */ -MatlabIOContainer MatlabIO::constructCell(vector& name, vector& dims, vector& real) { +MatlabIOContainer MatlabIO::constructCell(vector& name, vector& dims, vector& real) const { vector cell; char* field_ptr = &(real[0]); @@ -303,7 +303,7 @@ MatlabIOContainer MatlabIO::constructCell(vector& name, vector& * @param imag * @return */ -MatlabIOContainer MatlabIO::constructSparse(vector&, vector&, vector&, vector&) { +MatlabIOContainer MatlabIO::constructSparse(vector&, vector&, vector&, vector&) const { MatlabIOContainer variable; return variable; @@ -319,7 +319,7 @@ MatlabIOContainer MatlabIO::constructSparse(vector&, vector&, ve * @param real the string data * @return the wrapped string */ -MatlabIOContainer MatlabIO::constructString(vector& name, vector&, vector& real) { +MatlabIOContainer MatlabIO::constructString(vector& name, vector&, vector& real) const { // make sure the data is null terminated real.push_back('\0'); return MatlabIOContainer(string(&(name[0])), string(&(real[0]))); @@ -345,7 +345,7 @@ MatlabIOContainer MatlabIO::constructString(vector& name, vector * @return the wrapped matrix */ template -MatlabIOContainer MatlabIO::constructMatrix(vector& name, vector& dims, vector& real, vector& imag, uint32_t stor_type) { +MatlabIOContainer MatlabIO::constructMatrix(vector& name, vector& dims, vector& real, vector& imag, uint32_t stor_type) const { vector vec_real; vector vec_imag; @@ -449,7 +449,7 @@ MatlabIOContainer MatlabIO::constructMatrix(vector& name, vector * * @return the variable (matrix, struct, cell, scalar) wrapped in a container */ -MatlabIOContainer MatlabIO::collateMatrixFields(uint32_t, uint32_t, vector data) { +MatlabIOContainer MatlabIO::collateMatrixFields(uint32_t, uint32_t, const vector& data) const { // get the flags bool complx = data[9] & (1 << 3); @@ -548,7 +548,7 @@ MatlabIOContainer MatlabIO::collateMatrixFields(uint32_t, uint32_t, vector * @param data the binary blob * @return the binary blob, uncompressed */ -vector MatlabIO::uncompressVariable(uint32_t& data_type, uint32_t& dbytes, uint32_t& wbytes, const vector &data) { +vector MatlabIO::uncompressVariable(uint32_t& data_type, uint32_t& dbytes, uint32_t& wbytes, const vector &data) const { // setup the inflation parameters char buf[8]; z_stream infstream; @@ -593,7 +593,7 @@ vector MatlabIO::uncompressVariable(uint32_t& data_type, uint32_t& dbytes, * @param data the binary blob * @return an interpreted variable */ -MatlabIOContainer MatlabIO::readVariable(uint32_t data_type, uint32_t nbytes, const vector &data) { +MatlabIOContainer MatlabIO::readVariable(uint32_t data_type, uint32_t nbytes, const vector &data) const { // interpret the data MatlabIOContainer variable; @@ -644,7 +644,7 @@ MatlabIOContainer MatlabIO::readVariable(uint32_t data_type, uint32_t nbytes, co * * @return the block of data interpreted as a variable and stored in a generic container */ -MatlabIOContainer MatlabIO::readBlock(void) { +MatlabIOContainer MatlabIO::readBlock(void) const { // allocate the output MatlabIOContainer variable; @@ -716,7 +716,7 @@ std::vector MatlabIO::read(void) { * a list of variables and their C++ datatypes stored in the associated .Mat file * @param variables the variables read from the .Mat file using the read() function */ -void MatlabIO::whos(vector variables) const { +void MatlabIO::whos(const vector& variables) const { // get the longest filename unsigned int flmax = 0; From 4bed05332e10b3b27774ec8fe71b7c17a94a7f52 Mon Sep 17 00:00:00 2001 From: Vardan Akopian Date: Tue, 8 Sep 2015 18:46:42 -0700 Subject: [PATCH 2/3] fix more reference arguments --- include/MatlabIO.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/MatlabIO.hpp b/include/MatlabIO.hpp index 2521b76..3e87dce 100644 --- a/include/MatlabIO.hpp +++ b/include/MatlabIO.hpp @@ -121,7 +121,7 @@ class MatlabIO { throw new std::exception(); } - MatlabIOContainer find(const std::vector& variables, const std::string name) const { + MatlabIOContainer find(const std::vector& variables, const std::string& name) const { for (unsigned int n = 0; n < variables.size(); ++n) { if (variables[n].name().compare(name) == 0) return variables[n]; } @@ -129,7 +129,7 @@ class MatlabIO { } template - bool typeEquals(const std::vector& variables, const std::string name) const { + bool typeEquals(const std::vector& variables, const std::string& name) const { for (unsigned int n = 0; n < variables.size(); ++n) { if (variables[n].name().compare(name) == 0) return variables[n].typeEquals(); } From 890eeb18b338ede7b7eda25ea0bfbd6e7cf36979 Mon Sep 17 00:00:00 2001 From: Vardan Akopian Date: Tue, 8 Sep 2015 19:00:43 -0700 Subject: [PATCH 3/3] even more reference arguments --- include/MatlabIO.hpp | 10 +++++----- src/MatlabIO.cpp | 16 +++++++--------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/include/MatlabIO.hpp b/include/MatlabIO.hpp index 3e87dce..594d4c2 100644 --- a/include/MatlabIO.hpp +++ b/include/MatlabIO.hpp @@ -82,11 +82,11 @@ class MatlabIO { void getHeader(void); void setHeader(void); bool hasVariable(void) { return fid_.peek() != EOF; } - template MatlabIOContainer constructMatrix(std::vector& name, std::vector& dims, std::vector& real, std::vector& imag, uint32_t stor_type) const; - MatlabIOContainer constructString(std::vector& name, std::vector& dims, std::vector& real) const; - MatlabIOContainer constructSparse(std::vector& name, std::vector& dims, std::vector& real, std::vector& imag) const; - MatlabIOContainer constructCell(std::vector& name, std::vector& dims, std::vector& real) const; - MatlabIOContainer constructStruct(std::vector& name, std::vector& dims, std::vector& real) const; + template MatlabIOContainer constructMatrix(const std::vector& name, const std::vector& dims, const std::vector& real, const std::vector& imag, uint32_t stor_type) const; + MatlabIOContainer constructString(const std::vector& name, const std::vector& dims,const std::vector& real) const; + MatlabIOContainer constructSparse(const std::vector& name, const std::vector& dims, const std::vector& real, const std::vector& imag) const; + MatlabIOContainer constructCell(const std::vector& name, const std::vector& dims, const std::vector& real) const; + MatlabIOContainer constructStruct(const std::vector& name, const std::vector& dims, const std::vector& real) const; const char * readVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const char *data) const; MatlabIOContainer collateMatrixFields(uint32_t data_type, uint32_t nbytes, const std::vector& data) const; std::vector uncompressVariable(uint32_t& data_type, uint32_t& dbytes, uint32_t& wbytes, const std::vector &data) const; diff --git a/src/MatlabIO.cpp b/src/MatlabIO.cpp index 8b64a09..9132b4e 100644 --- a/src/MatlabIO.cpp +++ b/src/MatlabIO.cpp @@ -214,7 +214,7 @@ const char * MatlabIO::readVariableTag(uint32_t &data_type, uint32_t &dbytes, ui * @param real * @return */ -MatlabIOContainer MatlabIO::constructStruct(vector& name, vector& dims, vector& real) const { +MatlabIOContainer MatlabIO::constructStruct(const vector& name, const vector& dims, const vector& real) const { vector > array; const char* real_ptr = &(real[0]); @@ -275,10 +275,10 @@ MatlabIOContainer MatlabIO::constructStruct(vector& name, vector * @param real the real part * @return the wrapped cell array */ -MatlabIOContainer MatlabIO::constructCell(vector& name, vector& dims, vector& real) const { +MatlabIOContainer MatlabIO::constructCell(const vector& name, const vector& dims, const vector& real) const { vector cell; - char* field_ptr = &(real[0]); + const char* field_ptr = &(real[0]); for (unsigned int n = 0; n < product(dims); ++n) { MatlabIOContainer field; uint32_t data_type; @@ -303,7 +303,7 @@ MatlabIOContainer MatlabIO::constructCell(vector& name, vector& * @param imag * @return */ -MatlabIOContainer MatlabIO::constructSparse(vector&, vector&, vector&, vector&) const { +MatlabIOContainer MatlabIO::constructSparse(const vector&, const vector&, const vector&, const vector&) const { MatlabIOContainer variable; return variable; @@ -319,10 +319,8 @@ MatlabIOContainer MatlabIO::constructSparse(vector&, vector&, ve * @param real the string data * @return the wrapped string */ -MatlabIOContainer MatlabIO::constructString(vector& name, vector&, vector& real) const { - // make sure the data is null terminated - real.push_back('\0'); - return MatlabIOContainer(string(&(name[0])), string(&(real[0]))); +MatlabIOContainer MatlabIO::constructString(const vector& name, const vector&, const vector& real) const { + return MatlabIOContainer(string(&(name[0])), string(&(real[0]), real.size())); } @@ -345,7 +343,7 @@ MatlabIOContainer MatlabIO::constructString(vector& name, vector * @return the wrapped matrix */ template -MatlabIOContainer MatlabIO::constructMatrix(vector& name, vector& dims, vector& real, vector& imag, uint32_t stor_type) const { +MatlabIOContainer MatlabIO::constructMatrix(const vector& name, const vector& dims, const vector& real, const vector& imag, uint32_t stor_type) const { vector vec_real; vector vec_imag;