Skip to content

Commit

Permalink
Cleanup string classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Anderson committed Jun 24, 2021
1 parent 7b85edc commit de54549
Show file tree
Hide file tree
Showing 29 changed files with 105 additions and 521 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ namespace ${namespace} {
#if $type == "string":

${name}::${name}String::${name}String(const char* src): StringBase() {
this->copyBuff(src,this->getCapacity());
this->copyBuff(src, sizeof(this->m_buf));
}

${name}::${name}String::${name}String(const Fw::StringBase& src): StringBase() {
this->copyBuff(src.toChar(),this->getCapacity());
this->copyBuff(src.toChar(), src.getCapacity());
}

${name}::${name}String::${name}String(const ${name}String& src): StringBase() {
this->copyBuff(src.toChar(),this->getCapacity());
this->copyBuff(src.toChar(), src.getCapacity());
}

${name}::${name}String::${name}String(void): StringBase() {
Expand All @@ -52,48 +52,14 @@ namespace ${namespace} {
${name}::${name}String::~${name}String(void) {
}

bool ${name}::${name}String::operator==(const ${name}String& src) const {
return (0 == strncmp(this->m_buf,src.m_buf,sizeof(this->m_buf)));
}

NATIVE_UINT_TYPE ${name}::${name}String::length(void) const {
return (NATIVE_UINT_TYPE)strnlen(this->m_buf,sizeof(this->m_buf));
}

const char* ${name}::${name}String::toChar(void) const {
return this->m_buf;
}

Fw::SerializeStatus ${name}::${name}String::serialize(Fw::SerializeBufferBase& buffer) const {
NATIVE_UINT_TYPE strSize = strnlen(this->m_buf,sizeof(this->m_buf));
// serialize string
return buffer.serialize((U8*)this->m_buf,strSize);
}

Fw::SerializeStatus ${name}::${name}String::deserialize(Fw::SerializeBufferBase& buffer) {
NATIVE_UINT_TYPE maxSize = sizeof(this->m_buf);
// deserialize string
Fw::SerializeStatus stat = buffer.deserialize((U8*)this->m_buf,maxSize);
// make sure it is null-terminated
this->terminate(maxSize);

return stat;
}

NATIVE_UINT_TYPE ${name}::${name}String::getCapacity(void) const {
return sizeof(this->m_buf);
}

void ${name}::${name}String::terminate(NATIVE_UINT_TYPE size) {
// null terminate the string
this->m_buf[size < sizeof(this->m_buf)?size:sizeof(this->m_buf)-1] = 0;
}

const ${name}::${name}String& ${name}::${name}String::operator=(const ${name}::${name}String& other) {
this->copyBuff(other.m_buf,this->getCapacity());
return *this;
}

#end if

// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,13 @@ namespace ${namespace} {
${name}String(const ${name}String& src); //!< string base constructor
${name}String(void); //!< default constructor
virtual ~${name}String(void); //!< destructor
const char* toChar(void) const; //!< retrieves char buffer of string
NATIVE_UINT_TYPE length(void) const; //!< returns length of string
bool operator==(const ${name}String& src) const; //!< equality operator

const ${name}String& operator=(const ${name}String& other); //!< equal operator for other strings

Fw::SerializeStatus serialize(Fw::SerializeBufferBase& buffer) const; //!< serialization function
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase& buffer); //!< deserialization function
const char* toChar(void) const; //!< retrieves char buffer of string
NATIVE_UINT_TYPE getCapacity(void) const ;

private:
NATIVE_UINT_TYPE getCapacity(void) const ;
void terminate(NATIVE_UINT_TYPE size); //!< terminate the string

char m_buf[${string_size}]; //!< buffer for string storage

};

#end if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace ${n} {
#if $type == "string":

${argname}String::${argname}String(const char* src): StringBase() {
this->copyBuff(src,this->getCapacity());
this->copyBuff(src, sizeof(this->m_buf));
}

${argname}String::${argname}String(const Fw::StringBase& src): StringBase() {
this->copyBuff(src.toChar(),this->getCapacity());
this->copyBuff(src.toChar(), src.getCapacity());
}

${argname}String::${argname}String(const ${argname}String& src): StringBase() {
this->copyBuff(src.toChar(),this->getCapacity());
this->copyBuff(src.toChar(), src.getCapacity());
}

${argname}String::${argname}String(void): StringBase() {
Expand All @@ -26,44 +26,14 @@ namespace ${n} {
${argname}String::~${argname}String(void) {
}

NATIVE_UINT_TYPE ${argname}String::length(void) const {
return (NATIVE_UINT_TYPE)strnlen(this->m_buf,sizeof(this->m_buf));
}

const char* ${argname}String::toChar(void) const {
return this->m_buf;
}

Fw::SerializeStatus ${argname}String::serialize(Fw::SerializeBufferBase& buffer) const {
NATIVE_UINT_TYPE strSize = strnlen(this->m_buf,sizeof(this->m_buf));
// serialize string as buffer
return buffer.serialize((U8*)this->m_buf,strSize);
}

Fw::SerializeStatus ${argname}String::deserialize(Fw::SerializeBufferBase& buffer) {
NATIVE_UINT_TYPE maxSize = sizeof(this->m_buf);
// deserialize string
Fw::SerializeStatus stat = buffer.deserialize((U8*)this->m_buf,maxSize);
// make sure it is null-terminated
this->terminate(maxSize);

return stat;
}

NATIVE_UINT_TYPE ${argname}String::getCapacity(void) const {
return sizeof(this->m_buf);
}

const ${argname}String& ${argname}String::operator=(const ${argname}String& other) {
this->copyBuff(other.m_buf,this->getCapacity());
return *this;
}

void ${argname}String::terminate(NATIVE_UINT_TYPE size) {
// null terminate the string
this->m_buf[size < sizeof(this->m_buf)?size:sizeof(this->m_buf)-1] = 0;
}

#end if
#if $type == "buffer":

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@ namespace ${namespace} {
${argname}String(const ${argname}String& src); //!< same type string constructor
${argname}String(void); //!< default constructor
virtual ~${argname}String(void); //!< destructor
const char* toChar(void) const; //!< return internal buffer
NATIVE_UINT_TYPE length(void) const; //!< length of string

const ${argname}String& operator=(const ${argname}String& other); //!< equal operator for other strings

Fw::SerializeStatus serialize(Fw::SerializeBufferBase& buffer) const; //!< deserialization method
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase& buffer); //!< serialization method
const char* toChar(void) const; //!< return internal buffer
NATIVE_UINT_TYPE getCapacity(void) const ;

private:
NATIVE_UINT_TYPE getCapacity(void) const ;
void terminate(NATIVE_UINT_TYPE size); //!< terminate the string

char m_buf[${size}]; //!< buffer for string
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace ${n} {
#if $type == "string":

${name}::${memname}String::${memname}String(const char* src): StringBase() {
this->copyBuff(src,this->getCapacity());
this->copyBuff(src,sizeof(this->m_buf));
}

${name}::${memname}String::${memname}String(const Fw::StringBase& src): StringBase() {
this->copyBuff(src.toChar(),this->getCapacity());
this->copyBuff(src.toChar(), src.getCapacity());
}

${name}::${memname}String::${memname}String(const ${memname}String& src): StringBase() {
this->copyBuff(src.toChar(),this->getCapacity());
this->copyBuff(src.toChar(), src.getCapacity());
}

${name}::${memname}String::${memname}String(void): StringBase() {
Expand All @@ -25,47 +25,13 @@ namespace ${n} {
${name}::${memname}String::~${memname}String(void) {
}

bool ${name}::${memname}String::operator==(const ${memname}String& src) const {
return (0 == strncmp(this->m_buf,src.m_buf,sizeof(this->m_buf)));
}

NATIVE_UINT_TYPE ${name}::${memname}String::length(void) const {
return (NATIVE_UINT_TYPE)strnlen(this->m_buf,sizeof(this->m_buf));
}

const char* ${name}::${memname}String::toChar(void) const {
return this->m_buf;
}

Fw::SerializeStatus ${name}::${memname}String::serialize(Fw::SerializeBufferBase& buffer) const {
NATIVE_UINT_TYPE strSize = strnlen(this->m_buf,sizeof(this->m_buf));
// serialize string
return buffer.serialize((U8*)this->m_buf,strSize);
}

Fw::SerializeStatus ${name}::${memname}String::deserialize(Fw::SerializeBufferBase& buffer) {
NATIVE_UINT_TYPE maxSize = sizeof(this->m_buf);
// deserialize string
Fw::SerializeStatus stat = buffer.deserialize((U8*)this->m_buf,maxSize);
// make sure it is null-terminated
this->terminate(maxSize);

return stat;
}

NATIVE_UINT_TYPE ${name}::${memname}String::getCapacity(void) const {
return sizeof(this->m_buf);
}

void ${name}::${memname}String::terminate(NATIVE_UINT_TYPE size) {
// null terminate the string
this->m_buf[size < sizeof(this->m_buf)?size:sizeof(this->m_buf)-1] = 0;
}

const ${name}::${memname}String& ${name}::${memname}String::operator=(const ${name}::${memname}String& other) {
this->copyBuff(other.m_buf,this->getCapacity());
return *this;
}

#end if
#end for
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,13 @@ class ${name} : public Fw::Serializable {
${memname}String(const ${memname}String& src); //!< string base constructor
${memname}String(void); //!< default constructor
virtual ~${memname}String(void); //!< destructor
const char* toChar(void) const; //!< retrieves char buffer of string
NATIVE_UINT_TYPE length(void) const; //!< returns length of string
bool operator==(const ${memname}String& src) const; //!< equality operator

const ${memname}String& operator=(const ${memname}String& other); //!< equal operator for other strings

Fw::SerializeStatus serialize(Fw::SerializeBufferBase& buffer) const; //!< serialization function
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase& buffer); //!< deserialization function
const char* toChar(void) const; //!< retrieves char buffer of string
NATIVE_UINT_TYPE getCapacity(void) const ;

private:
NATIVE_UINT_TYPE getCapacity(void) const ;
void terminate(NATIVE_UINT_TYPE size); //!< terminate the string

char m_buf[${size}]; //!< buffer for string storage

};

#end if
Expand Down
37 changes: 3 additions & 34 deletions Fw/Cmd/CmdString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
namespace Fw {

CmdStringArg::CmdStringArg(const char* src) : StringBase() {
this->copyBuff(src,this->getCapacity());
this->copyBuff(src,sizeof(this->m_buf));
}

CmdStringArg::CmdStringArg(const StringBase& src) : StringBase() {
this->copyBuff(src.toChar(),this->getCapacity());
this->copyBuff(src.toChar(), src.getCapacity());
}

CmdStringArg::CmdStringArg(const CmdStringArg& src) : StringBase() {
this->copyBuff(src.m_buf,this->getCapacity());
this->copyBuff(src.toChar(), src.getCapacity());
}

CmdStringArg::CmdStringArg(void) : StringBase() {
Expand All @@ -27,42 +27,11 @@ namespace Fw {
CmdStringArg::~CmdStringArg(void) {
}

NATIVE_UINT_TYPE CmdStringArg::length(void) const {
return strnlen(this->m_buf,sizeof(this->m_buf));
}

const char* CmdStringArg::toChar(void) const {
return this->m_buf;
}

const CmdStringArg& CmdStringArg::operator=(const CmdStringArg& other) {
this->copyBuff(other.m_buf,this->getCapacity());
return *this;
}

SerializeStatus CmdStringArg::serialize(SerializeBufferBase& buffer) const {
NATIVE_UINT_TYPE strSize = strnlen(this->m_buf,sizeof(this->m_buf));
// serialize string
return buffer.serialize((U8*)this->m_buf,strSize);
}

SerializeStatus CmdStringArg::deserialize(SerializeBufferBase& buffer) {
NATIVE_UINT_TYPE maxSize = sizeof(this->m_buf);
// deserialize string
SerializeStatus stat = buffer.deserialize((U8*)this->m_buf,maxSize);
// make sure it is null-terminated
this->terminate(maxSize);

return stat;
}

NATIVE_UINT_TYPE CmdStringArg::getCapacity(void) const {
return FW_CMD_STRING_MAX_SIZE;
}

void CmdStringArg::terminate(NATIVE_UINT_TYPE size) {
// null terminate the string
this->m_buf[size < sizeof(this->m_buf)?size:sizeof(this->m_buf)-1] = 0;
}

}
10 changes: 2 additions & 8 deletions Fw/Cmd/CmdString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@ namespace Fw {
CmdStringArg(const CmdStringArg& src);
CmdStringArg(void);
~CmdStringArg(void);
const char* toChar(void) const;
NATIVE_UINT_TYPE length(void) const;

const CmdStringArg& operator=(const CmdStringArg& other); //!< equal operator for other strings

SerializeStatus serialize(SerializeBufferBase& buffer) const;
SerializeStatus deserialize(SerializeBufferBase& buffer);
const char* toChar(void) const;
NATIVE_UINT_TYPE getCapacity(void) const ; //!< return buffer size

private:
NATIVE_UINT_TYPE getCapacity(void) const ; //!< return buffer size
void terminate(NATIVE_UINT_TYPE size); //!< terminate the string

char m_buf[FW_CMD_STRING_MAX_SIZE];
};
Expand Down
Loading

0 comments on commit de54549

Please sign in to comment.