Fix bug introduced with code smell fix
[ric-plt/xapp-frame-cpp.git] / src / messaging / message.cpp
index 41f5a78..bd11602 100644 (file)
@@ -52,24 +52,26 @@ namespace xapp {
 /*
        Create a new message wrapper for an existing RMR msg buffer.
 */
-xapp::Message::Message( rmr_mbuf_t* mbuf, void* mrc ) {
-       this->mrc = mrc;                        // the message router context for sends
-       this->mbuf = mbuf;
-}
+xapp::Message::Message( rmr_mbuf_t* mbuf, void* mrc ) :
+       mrc(  mrc ),                    // the message router context for sends
+       mbuf(  mbuf )
+{  /* empty body */ }
+
+xapp::Message::Message( void* rmr_ctx, int payload_len ) :
+       mrc(  rmr_ctx ),                                                                                // the message router context for sends
+       mbuf(  rmr_alloc_msg( rmr_ctx, payload_len ) )
+{ /* empty body */ }
 
-xapp::Message::Message( void* mrc, int payload_len ) {
-       this->mrc = mrc;
-       this->mbuf = rmr_alloc_msg( mrc, payload_len );
-}
 
 /*
        Copy builder.  Given a source object instance (soi), create a copy.
        Creating a copy should be avoided as it can be SLOW!
 */
-xapp::Message::Message( const Message& soi ) {
+xapp::Message::Message( const Message& soi ) :
+       mrc( soi.mrc )
+{
        int payload_size;
 
-       mrc = soi.mrc;
        payload_size = rmr_payload_size( soi.mbuf );            // rmr can handle a nil pointer
        mbuf = rmr_realloc_payload( soi.mbuf, payload_size, RMR_COPY, RMR_CLONE );
 }
@@ -99,10 +101,10 @@ Message& xapp::Message::operator=( const Message& soi ) {
        the soi ensuring that the destriction of the soi doesn't trash things from
        under us.
 */
-xapp::Message::Message( Message&& soi ) {
-       mrc = soi.mrc;
-       mbuf = soi.mbuf;
-
+xapp::Message::Message( Message&& soi ) :
+       mrc(  soi.mrc ),
+       mbuf(  soi.mbuf )
+{
        soi.mrc = NULL;         // prevent closing of RMR stuff on soi destroy
        soi.mbuf = NULL;
 }
@@ -152,20 +154,20 @@ xapp::Message::~Message() {
 */
 //char* Message::Copy_payload( ){
 std::unique_ptr<unsigned char> xapp::Message::Copy_payload( ){
-       unsigned char*  new_payload = NULL;
 
        if( mbuf != NULL ) {
-               new_payload = (unsigned char *) malloc( sizeof( unsigned char ) * mbuf->len );
+               unsigned char*  new_payload = new unsigned char[mbuf->len];
                memcpy( new_payload, mbuf->payload, mbuf->len );
+               return std::unique_ptr<unsigned char>( new_payload );
        }
 
-       return std::unique_ptr<unsigned char>( new_payload );
+       return NULL;
 }
 
 /*
        Makes a copy of the MEID and returns a smart pointer to it.
 */
-std::unique_ptr<unsigned char> xapp::Message::Get_meid(){
+std::unique_ptr<unsigned char> xapp::Message::Get_meid() const {
        unsigned char* m = NULL;
 
        m = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID );
@@ -180,11 +182,11 @@ std::unique_ptr<unsigned char> xapp::Message::Get_meid(){
        If mbuf isn't valid (nil, or message has a broken header) the return
        will be -1.
 */
-int xapp::Message::Get_available_size(){
+int xapp::Message::Get_available_size() const {
        return rmr_payload_size( mbuf );                // rmr can handle a nil pointer
 }
 
-int    xapp::Message::Get_mtype(){
+int    xapp::Message::Get_mtype() const {
        int rval = INVALID_MTYPE;
 
        if( mbuf != NULL ) {
@@ -197,11 +199,8 @@ int        xapp::Message::Get_mtype(){
 /*
        Makes a copy of the source field and returns a smart pointer to it.
 */
-std::unique_ptr<unsigned char> xapp::Message::Get_src(){
-       unsigned char* m = NULL;
-
-       m = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_SRC );
-       memset( m, 0, sizeof( unsigned char ) * RMR_MAX_SRC );
+std::unique_ptr<unsigned char> xapp::Message::Get_src() const {
+       unsigned char* m = new unsigned char[RMR_MAX_SRC];
 
        if( m != NULL ) {
                rmr_get_src( mbuf, m );
@@ -210,7 +209,7 @@ std::unique_ptr<unsigned char> xapp::Message::Get_src(){
        return std::unique_ptr<unsigned char>( m );
 }
 
-int    xapp::Message::Get_state( ){
+int    xapp::Message::Get_state( ) const {
        int state = INVALID_STATUS;
 
        if( mbuf != NULL ) {
@@ -220,7 +219,7 @@ int xapp::Message::Get_state( ){
        return state;
 }
 
-int    xapp::Message::Get_subid(){
+int    xapp::Message::Get_subid() const {
        int     rval = INVALID_SUBID;
 
        if( mbuf != NULL ) {
@@ -234,7 +233,7 @@ int xapp::Message::Get_subid(){
        Return the amount of the payload (bytes) which is used. See
        Get_available_size() to get the total usable space in the payload.
 */
-int    xapp::Message::Get_len(){
+int    xapp::Message::Get_len() const {
        int rval = 0;
 
        if( mbuf != NULL ) {
@@ -252,7 +251,7 @@ int xapp::Message::Get_len(){
        length by calling Message:Get_available_size(), and ensuring that
        writing beyond the indicated size does not happen.
 */
-Msg_component xapp::Message::Get_payload(){
+Msg_component xapp::Message::Get_payload() const {
        if( mbuf != NULL ) {
                return std::unique_ptr<unsigned char, unfreeable>( mbuf->payload );
        }
@@ -262,7 +261,7 @@ Msg_component xapp::Message::Get_payload(){
 
 void xapp::Message::Set_meid( std::shared_ptr<unsigned char> new_meid ) {
        if( mbuf != NULL ) {
-               rmr_str2meid( mbuf, (unsigned char *) new_meid.get() );
+               rmr_str2meid( mbuf, new_meid.get() );
        }
 }
 
@@ -365,6 +364,9 @@ bool xapp::Message::Send( int mtype, int subid, int payload_len, unsigned char*
                        case WORMHOLE_MSG:
                                mbuf = rmr_wh_send_msg( mrc, whid,  mbuf );
                                break;
+
+                       default:
+                               break;                                  // because sonar doesn't like defaultless switches even when there is no work :(
                }
 
                state = mbuf->state == RMR_OK;