From 682d2eddb43e50db962d03c1e2d77d11a1c24157 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 27 Sep 2018 18:55:06 +0900 Subject: [PATCH] [coco] Introduce BlockIndex class (#1655) This commit introduces BlockIndex class which denotes the index of a block inside a block list. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/BlockIndex.h | 47 ++++++++++++++++++++++++++ contrib/coco/core/src/IR/BlockIndex.cpp | 14 ++++++++ contrib/coco/core/src/IR/BlockIndex.test.cpp | 34 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 contrib/coco/core/include/coco/IR/BlockIndex.h create mode 100644 contrib/coco/core/src/IR/BlockIndex.cpp create mode 100644 contrib/coco/core/src/IR/BlockIndex.test.cpp diff --git a/contrib/coco/core/include/coco/IR/BlockIndex.h b/contrib/coco/core/include/coco/IR/BlockIndex.h new file mode 100644 index 0000000..2418748 --- /dev/null +++ b/contrib/coco/core/include/coco/IR/BlockIndex.h @@ -0,0 +1,47 @@ +#ifndef __COCO_IR_BLOCK_INDEX_H__ +#define __COCO_IR_BLOCK_INDEX_H__ + +#include + +namespace coco +{ + +/** + * @brief A BlockIndex denotes the index of a block in a block list + */ +class BlockIndex final +{ +private: + static const uint32_t undefined = 0xffffffff; + +public: + BlockIndex() : _value{undefined} + { + // DO NOTHING + } + +public: + BlockIndex(uint32_t value) { set(value); } + +public: + bool valid(void) const { return _value != undefined; } + +public: + uint32_t value(void) const { return _value; } + +public: + void set(uint32_t value); + void reset(void) { _value = undefined; } + +private: + uint32_t _value; +}; + +static inline bool operator<(const BlockIndex &lhs, const BlockIndex &rhs) +{ + return lhs.value() < rhs.value(); +} + +} // namespace coco + +#endif // __COCO_IR_BLOCK_INDEX_H__ diff --git a/contrib/coco/core/src/IR/BlockIndex.cpp b/contrib/coco/core/src/IR/BlockIndex.cpp new file mode 100644 index 0000000..eb86c6a --- /dev/null +++ b/contrib/coco/core/src/IR/BlockIndex.cpp @@ -0,0 +1,14 @@ +#include "coco/IR/BlockIndex.h" + +#include + +namespace coco +{ + +void BlockIndex::set(uint32_t value) +{ + assert(value != undefined); + _value = value; +} + +} // namespace coco diff --git a/contrib/coco/core/src/IR/BlockIndex.test.cpp b/contrib/coco/core/src/IR/BlockIndex.test.cpp new file mode 100644 index 0000000..a95cf98 --- /dev/null +++ b/contrib/coco/core/src/IR/BlockIndex.test.cpp @@ -0,0 +1,34 @@ +#include "coco/IR/BlockIndex.h" + +#include + +namespace +{ + +class BlockIndexTest : public ::testing::Test +{ +}; + +} // namespace + +TEST_F(BlockIndexTest, default_constructor) +{ + coco::BlockIndex blk_ind; + + ASSERT_FALSE(blk_ind.valid()); +} + +TEST_F(BlockIndexTest, explicit_constructor) +{ + coco::BlockIndex blk_ind{3}; + + ASSERT_TRUE(blk_ind.valid()); + ASSERT_EQ(blk_ind.value(), 3); +} + +TEST_F(BlockIndexTest, operator_lt) +{ + // Valid index is always less than undefined one. + ASSERT_TRUE(coco::BlockIndex(3) < coco::BlockIndex()); + ASSERT_TRUE(coco::BlockIndex(3) < coco::BlockIndex(4)); +} -- 2.7.4