libcruft-util/memory/buffer/circular.hpp

68 lines
2.0 KiB
C++
Raw Normal View History

2015-11-11 16:56:35 +11:00
/*
* 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.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
#define __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
#include "../../view.hpp"
2015-11-11 16:56:35 +11:00
#include <cstddef>
2017-01-05 15:06:49 +11:00
namespace util::memory::buffer {
2015-11-11 16:56:35 +11:00
// buffer size is advisory and will likely depend on page size. the user
// must check the size after creation if this field is important for
// their usage.
template <typename ValueT>
2015-11-11 16:56:35 +11:00
class circular {
public:
using value_type = ValueT;
using iterator = value_type*;
using const_iterator = const value_type*;
2015-11-11 16:56:35 +11:00
2016-03-17 18:09:40 +11:00
explicit circular (size_t bytes);
2015-11-11 16:56:35 +11:00
~circular ();
circular (const circular&) = delete;
circular (circular&&) = delete;
circular& operator= (const circular&) = delete;
circular& operator= (circular&&) = delete;
value_type& operator[] (size_t)&;
const value_type& operator[] (size_t) const&;
2015-11-11 16:56:35 +11:00
iterator begin (void)&;
iterator end (void)&;
2015-11-11 16:56:35 +11:00
const_iterator begin (void) const&;
const_iterator end (void) const&;
2015-11-11 16:56:35 +11:00
size_t size (void) const;
/// rebases the pointer so that it points to within the first
/// repetition of the data buffer.
iterator constrain (iterator);
/// rebases a pair of pointers so they fall within a contiguous span of
/// the data buffer.
util::view<iterator> constrain (util::view<iterator>);
2015-11-11 16:56:35 +11:00
private:
value_type *m_begin, *m_end;
2015-11-11 16:56:35 +11:00
};
2017-01-05 15:06:49 +11:00
}
2015-11-11 16:56:35 +11:00
#endif