Program Listing for File LoanableCollection.hpp
↰ Return to documentation for file (/tmp/ws/src/fastrtps/include/fastdds/dds/core/LoanableCollection.hpp
)
// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _FASTDDS_DDS_CORE_LOANABLECOLLECTION_HPP_
#define _FASTDDS_DDS_CORE_LOANABLECOLLECTION_HPP_
#include <cstdint>
namespace eprosima {
namespace fastdds {
namespace dds {
class LoanableCollection
{
public:
using size_type = int32_t;
using element_type = void*;
const element_type* buffer() const
{
return elements_;
}
bool has_ownership() const
{
return has_ownership_;
}
size_type maximum() const
{
return maximum_;
}
size_type length() const
{
return length_;
}
bool length(
size_type new_length)
{
if (new_length < 0)
{
return false;
}
if (new_length <= maximum_)
{
length_ = new_length;
return true;
}
if (!has_ownership_)
{
return false;
}
resize(new_length);
length_ = new_length;
return true;
}
bool loan(
element_type* buffer,
size_type new_maximum,
size_type new_length)
{
if (has_ownership_ && maximum_ > 0)
{
return false;
}
if ((nullptr == buffer) || (new_maximum < new_length) || (new_maximum < 1))
{
return false;
}
maximum_ = new_maximum;
length_ = new_length;
elements_ = buffer;
has_ownership_ = false;
return true;
}
element_type* unloan(
size_type& maximum,
size_type& length)
{
if (has_ownership_)
{
return nullptr;
}
element_type* ret_val = elements_;
maximum = maximum_;
length = length_;
maximum_ = 0;
length_ = 0;
elements_ = nullptr;
has_ownership_ = true;
return ret_val;
}
element_type* unloan()
{
size_type max, len;
return unloan(max, len);
}
protected:
LoanableCollection() = default;
virtual void resize(
size_type new_length) = 0;
size_type maximum_ = 0;
size_type length_ = 0;
element_type* elements_ = nullptr;
bool has_ownership_ = true;
};
} // namespace dds
} // namespace fastdds
} // namespace eprosima
#endif // _FASTDDS_DDS_CORE_LOANABLECOLLECTION_HPP_