Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct mp_newmsg
- {
- char msg[ MP_MAX_MSG_SIZE ];
- int size, err;
- mp_newmsg() : size(0), err(MP_NO_ERROR) {}
- template< typename T >
- mp_newmsg& byte( T b )
- {
- if( !err )
- {
- if( size + 1 > MP_MAX_MSG_SIZE )
- err = MP_ERR_OVERFLOW;
- else
- msg[ size++ ] = (char) b;
- }
- return *this;
- }
- template< typename T >
- mp_newmsg& item( T val )
- {
- if( !err )
- {
- if( size + sizeof(T) > MP_MAX_MSG_SIZE )
- err = MP_ERR_OVERFLOW;
- else
- {
- memcpy( msg + size, &val, sizeof(T) );
- size += sizeof(T);
- }
- }
- return *this;
- }
- template< typename T >
- mp_newmsg& smallbuf( const char* str, T bufsize, T maxlen = 255 )
- {
- if( !err )
- {
- if( bufsize > maxlen )
- bufsize = maxlen;
- if( size + 1 + (int) bufsize > MP_MAX_MSG_SIZE )
- err = MP_ERR_OVERFLOW;
- else
- {
- msg[ size++ ] = (char) bufsize;
- memcpy( msg + size, str, bufsize );
- size += bufsize;
- }
- }
- return *this;
- }
- template< typename T >
- mp_newmsg& raw( const char* buf, T bufsize )
- {
- if( !err )
- {
- if( size + (int) bufsize > MP_MAX_MSG_SIZE )
- err = MP_ERR_OVERFLOW;
- else
- {
- memcpy( msg + size, buf, bufsize );
- size += bufsize;
- }
- }
- return *this;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement