CVB++ 15.0
decl_block_base.hpp
1#pragma once
2
3#include <stdexcept>
4#include <utility>
5
6#include "../../rect.hpp"
7
8namespace Cvb
9{
10 CVB_BEGIN_INLINE_NS
11
21 template <class AccessTrait>
22 class BlockBase
23 {
24 template <class OTHER_ACCESSTRAIT>
25 friend class BlockBase;
26
27 Size2D<int> size_;
28 AccessTrait access_;
29
30 protected:
31 /*
32 * \brief Creates a new block.
33 *
34 * \pre
35 * _access_.Valid() == \b true \n
36 * \a size not empty
37 *
38 * \param [in] access Access trait instance.
39 * \param [in] size Size in pixels of this block.
40 * \throws std::logic_error if this not Valid().
41 */
42 explicit BlockBase(const AccessTrait &access, Size2D<int> size)
43 : size_(std::move(size))
44 , access_(access)
45 {
46 if (!this->Valid())
47 throw std::logic_error{"Block is invalid"};
48 }
49
50 /*
51 * \brief Creates a new block.
52 *
53 * \pre
54 * _other_.Valid() == \b true \n
55 * \a aoi is inside _other_.Size() and not empty
56 *
57 * \param [in] other Block to create a new one from.
58 * \param [in] aoi Area of interest of this block.
59 * \throws std::logic_error if this not Valid().
60 * \throws std::length_error if \a aoi is outside _other_'s bounds.
61 */
62 template <class OTHER_ACCESSTRAIT>
63 explicit BlockBase(const BlockBase<OTHER_ACCESSTRAIT> &other, Rect<int> aoi)
64 : size_(aoi.Size())
65 , access_(other.access_.NewMoved(aoi))
66 {
67 if (aoi.Left() < 0 || aoi.Right() >= other.Size().Width() || aoi.Top() < 0
68 || aoi.Bottom() >= other.Size().Height())
69 throw std::length_error{"aoi out of bounds"};
70
71 if (!this->Valid())
72 throw std::logic_error{"Block is invalid"};
73 }
74
75 BlockBase(const BlockBase &other) noexcept = default;
76 BlockBase &operator=(const BlockBase &other) noexcept = default;
77 BlockBase(BlockBase &&other) noexcept = default;
78 BlockBase &operator=(BlockBase &&other) noexcept = default;
79 ~BlockBase() = default;
80
81 public:
87 CVB_FORCE_INLINE const AccessTrait &Access() const noexcept
88 {
89 return access_;
90 }
91
97 CVB_FORCE_INLINE AccessTrait &Access() noexcept
98 {
99 return access_;
100 }
101
109 CVB_FORCE_INLINE size_t Length() const noexcept
110 {
111 return static_cast<size_t>(size_.Width()) * static_cast<size_t>(size_.Height());
112 }
113
119 CVB_FORCE_INLINE Size2D<int> Size() const noexcept
120 {
121 return size_;
122 }
123
129 CVB_FORCE_INLINE int Width() const noexcept
130 {
131 return size_.Width();
132 }
133
139 CVB_FORCE_INLINE int Height() const noexcept
140 {
141 return size_.Height();
142 }
143
150 bool Valid() const noexcept
151 {
152 return size_.Width() > 0 && size_.Height() > 0 && access_.Valid();
153 }
154 };
155
156 CVB_END_INLINE_NS
157} // namespace Cvb
CVB_FORCE_INLINE AccessTrait & Access() noexcept
Gets the pixel access.
Definition decl_block_base.hpp:97
CVB_FORCE_INLINE Size2D< int > Size() const noexcept
Gets the size of this block.
Definition decl_block_base.hpp:119
bool Valid() const noexcept
Gets whether this block is valid.
Definition decl_block_base.hpp:150
CVB_FORCE_INLINE const AccessTrait & Access() const noexcept
Gets the pixel access.
Definition decl_block_base.hpp:87
CVB_FORCE_INLINE int Width() const noexcept
Gets the width of this block.
Definition decl_block_base.hpp:129
CVB_FORCE_INLINE size_t Length() const noexcept
Gets the total number of pixels in this block.
Definition decl_block_base.hpp:109
CVB_FORCE_INLINE int Height() const noexcept
Gets the height of this block.
Definition decl_block_base.hpp:139
Rectangle object.
Definition rect.hpp:24
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
T Height() const noexcept
Gets the vertical component of the size.
Definition size_2d.hpp:77
T Width() const noexcept
Gets the horizontal component of the size.
Definition size_2d.hpp:57
T move(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17