message: optimize getting subpart of a vector
[apps/agl-service-can-low-level.git] / low-can-binding / can / message / message.cpp
index 497304d..22887c9 100644 (file)
 message_t::message_t()
        : maxdlen_{0},
         length_{0},
-        format_{message_format_t::INVALID},
+        flags_{INVALID_FLAG},
         timestamp_{0},
-        sub_id_{-1},
-        flags_{0}
+        sub_id_{-1}
 {}
 
 /**
@@ -45,17 +44,15 @@ message_t::message_t()
  */
 message_t::message_t(uint32_t maxdlen,
        uint32_t length,
-       message_format_t format,
+       uint32_t flags,
        std::vector<uint8_t>& data,
-       uint64_t timestamp,
-       uint32_t flags)
+       uint64_t timestamp)
        : maxdlen_{maxdlen},
        length_{length},
-       format_{format},
+       flags_{flags},
        data_{data},
        timestamp_{timestamp},
-       sub_id_{-1},
-       flags_{flags}
+       sub_id_{-1}
 {}
 
 /**
@@ -86,30 +83,18 @@ const uint8_t* message_t::get_data() const
 /// @return pointer to the first element
 ///  of class member data_
 ///
-const std::vector<uint8_t> message_t::get_data_vector(int start,int end) const
+const std::vector<uint8_t> message_t::get_data_vector(int start, int end) const
 {
-       std::vector<uint8_t> ret;
-       if(start >= 0)
+       if ( start > length_ || end > length_ )
        {
-               if(end<length_)
-               {
-                       for(int i=start;i<=end;i++)
-                       {
-                               ret.push_back(data_[i]);
-                       }
-               }
-               else
-               {
-                       for(int i=start;i<length_;i++)
-                       {
-                               ret.push_back(data_[i]);
-                       }
-               }
-       }
-       else
-       {
-               AFB_ERROR("Error index to get data vector, [%d-%d] - for length %d",start,end,length_);
+               AFB_ERROR("Error index to get data vector, [%d-%d] - for length %d", start, end, length_);
+               return data_;
        }
+
+       std::vector<uint8_t>::const_iterator first = data_.begin() + start;
+       std::vector<uint8_t>::const_iterator last = data_.begin() + end;
+       std::vector<uint8_t> ret(first, last);
+
        return ret;
 }
 
@@ -138,7 +123,7 @@ uint32_t message_t::get_length() const
  *
  * @param data A vector of data
  */
-void message_t::set_data(std::vector<uint8_t> &data)
+void message_t::set_data(std::vector<uint8_t> data)
 {
        data_ = data;
 }
@@ -163,17 +148,6 @@ uint64_t message_t::get_timestamp() const
        return timestamp_;
 }
 
-/**
- * @brief Return the format of the message
- *
- * @return message_format_t The message format
- */
-message_format_t message_t::get_msg_format()
-{
-       return format_;
-}
-
-
 uint32_t message_t::get_flags()
 {
        return flags_;
@@ -184,13 +158,22 @@ void message_t::set_flags(uint32_t flags)
        flags_ = flags_ | flags;
 }
 
+void message_t::erase_flags()
+{
+       flags_ = 0;
+}
+
 uint32_t message_t::get_maxdlen()
 {
        return maxdlen_;
 }
 
-
 void message_t::set_maxdlen(uint32_t maxdlen)
 {
        maxdlen_ = maxdlen;
-}
\ No newline at end of file
+}
+
+void message_t::set_length(uint32_t length)
+{
+       length_ = length;
+}