mirror of
				https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
				synced 2025-11-04 04:27:48 +00:00 
			
		
		
		
	Allow constructing Buffers from std::vector
This commit is contained in:
		@@ -77,6 +77,21 @@ public:
 | 
				
			|||||||
        static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)");
 | 
					        static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Constructs a buffer from a vector
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * \param data The vector to be used as input
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    template <typename T>
 | 
				
			||||||
 | 
					    Buffer(const std::vector<T>& data)
 | 
				
			||||||
 | 
					    : data_(data.data()), size_(data.size()) {
 | 
				
			||||||
 | 
					        static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Don't allow construction from temporary vectors
 | 
				
			||||||
 | 
					    template <typename T>
 | 
				
			||||||
 | 
					    Buffer(std::vector<T>&& data) = delete;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * \brief Construct a buffer from a const string ref
 | 
					     * \brief Construct a buffer from a const string ref
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
@@ -85,7 +100,7 @@ public:
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    Buffer(const std::string& data); 
 | 
					    Buffer(const std::string& data); 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Don't allow construction from temporaries
 | 
					    // Don't allow construction from temporary strings
 | 
				
			||||||
    Buffer(std::string&&) = delete;
 | 
					    Buffer(std::string&&) = delete;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Buffer(const Buffer&) = delete;
 | 
					    Buffer(const Buffer&) = delete;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,6 +43,13 @@ TEST_F(BufferTest, VectorConversion) {
 | 
				
			|||||||
    EXPECT_EQ(data, string(buffer_as_vector.begin(), buffer_as_vector.end()));
 | 
					    EXPECT_EQ(data, string(buffer_as_vector.begin(), buffer_as_vector.end()));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_F(BufferTest, VectorConstruction) {
 | 
				
			||||||
 | 
					    const string str_data = "Hello world!";
 | 
				
			||||||
 | 
					    const vector<uint8_t> data(str_data.begin(), str_data.end());
 | 
				
			||||||
 | 
					    Buffer buffer(data);
 | 
				
			||||||
 | 
					    EXPECT_EQ(str_data, buffer);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST_F(BufferTest, Equality) {
 | 
					TEST_F(BufferTest, Equality) {
 | 
				
			||||||
    string data = "Hello world!";
 | 
					    string data = "Hello world!";
 | 
				
			||||||
    Buffer buffer1(data);
 | 
					    Buffer buffer1(data);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user