mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Added intrusive link definitions. forward_link, bidirectional_link, tree_link
This commit is contained in:
parent
3342303ce6
commit
a0b93c46fa
122
intrusive_links.h
Normal file
122
intrusive_links.h
Normal file
@ -0,0 +1,122 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2016 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __ETL_INTRUSIVE_LINKS__
|
||||
#define __ETL_INTRUSIVE_LINKS__
|
||||
|
||||
#include "nullptr.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
namespace link_option
|
||||
{
|
||||
enum
|
||||
{
|
||||
NO_AUTO_UNLINK = false,
|
||||
AUTO_UNLINK = true
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A forward link.
|
||||
//***************************************************************************
|
||||
template <const size_t ID_ = 0>
|
||||
struct forward_link
|
||||
{
|
||||
enum
|
||||
{
|
||||
ID = ID_
|
||||
};
|
||||
|
||||
forward_link* etl_next;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A bidirectional link.
|
||||
//***************************************************************************
|
||||
template <const size_t ID_ = 0, const bool AUTO_UNLINK = etl::link_option::NO_AUTO_UNLINK>
|
||||
struct bidirectional_link
|
||||
{
|
||||
enum
|
||||
{
|
||||
ID = ID_,
|
||||
};
|
||||
|
||||
bidirectional_link* etl_previous;
|
||||
bidirectional_link* etl_next;
|
||||
};
|
||||
|
||||
// Specialisation for auto unlinked option.
|
||||
// When this link is destroyed it will automatically unlink itself.
|
||||
template <const size_t ID_>
|
||||
struct bidirectional_link<ID_, etl::link_option::AUTO_UNLINK>
|
||||
{
|
||||
enum
|
||||
{
|
||||
ID = ID_
|
||||
};
|
||||
|
||||
~bidirectional_link()
|
||||
{
|
||||
// Connect the previous link with the next.
|
||||
if (etl_previous != nullptr)
|
||||
{
|
||||
etl_previous->etl_next = etl_next;
|
||||
}
|
||||
|
||||
// Connect the next link with the previous.
|
||||
if (etl_next != nullptr)
|
||||
{
|
||||
etl_next->etl_previous = etl_previous;
|
||||
}
|
||||
}
|
||||
|
||||
bidirectional_link* etl_previous;
|
||||
bidirectional_link* etl_next;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A tree link.
|
||||
//***************************************************************************
|
||||
template <const size_t ID_ = 0>
|
||||
struct tree_link
|
||||
{
|
||||
enum
|
||||
{
|
||||
ID = ID_
|
||||
};
|
||||
|
||||
tree_link* etl_parent;
|
||||
tree_link* etl_left;
|
||||
tree_link* etl_right;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
133
test/test_intrusive_links.cpp
Normal file
133
test/test_intrusive_links.cpp
Normal file
@ -0,0 +1,133 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2016 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
#include "ExtraCheckMacros.h"
|
||||
|
||||
#include "data.h"
|
||||
|
||||
#include "../intrusive_links.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
//*******************************************************
|
||||
typedef etl::bidirectional_link<0, etl::link_option::AUTO_UNLINK> FirstLink;
|
||||
typedef etl::bidirectional_link<1> SecondLink;
|
||||
|
||||
struct Data : public FirstLink, public SecondLink
|
||||
{
|
||||
Data(int value)
|
||||
: value(value)
|
||||
{
|
||||
}
|
||||
|
||||
int value;
|
||||
};
|
||||
|
||||
//*******************************************************
|
||||
bool operator ==(const Data& lhs, const Data& rhs)
|
||||
{
|
||||
return lhs.value == rhs.value;
|
||||
}
|
||||
|
||||
bool operator !=(const Data& lhs, const Data& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*******************************************************
|
||||
template <typename TLink>
|
||||
void join(TLink* lhs, TLink* rhs)
|
||||
{
|
||||
if ((lhs == nullptr) && (rhs != nullptr))
|
||||
{
|
||||
rhs->etl_previous = nullptr;
|
||||
}
|
||||
else if ((lhs != nullptr) && (rhs == nullptr))
|
||||
{
|
||||
lhs->etl_next = nullptr;
|
||||
}
|
||||
else if ((lhs != nullptr) && (rhs != nullptr))
|
||||
{
|
||||
lhs->etl_next = rhs;
|
||||
rhs->etl_previous = lhs;
|
||||
}
|
||||
}
|
||||
|
||||
SUITE(test_forward_list)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_bidirectional_link)
|
||||
{
|
||||
// FirstLink is auto-unlink, SecondLink is not.
|
||||
|
||||
Data* data0 = new Data(0);
|
||||
Data* data1 = new Data(1);
|
||||
Data* data2 = new Data(2);
|
||||
Data* data3 = new Data(3);
|
||||
|
||||
join<FirstLink>(nullptr, data0);
|
||||
join<FirstLink>(data0, data1);
|
||||
join<FirstLink>(data1, data2);
|
||||
join<FirstLink>(data2, data3);
|
||||
join<FirstLink>(data3, nullptr);
|
||||
|
||||
join<SecondLink>(nullptr, data0);
|
||||
join<SecondLink>(data0, data1);
|
||||
join<SecondLink>(data1, data2);
|
||||
join<SecondLink>(data2, data3);
|
||||
join<SecondLink>(data3, nullptr);
|
||||
|
||||
delete data1;
|
||||
CHECK(data0->FirstLink::etl_next == data2);
|
||||
CHECK(data2->FirstLink::etl_previous == data0);
|
||||
|
||||
CHECK(data0->SecondLink::etl_next != data2);
|
||||
CHECK(data0->SecondLink::etl_next != nullptr);
|
||||
CHECK(data2->SecondLink::etl_previous != data0);
|
||||
CHECK(data2->SecondLink::etl_previous != nullptr);
|
||||
|
||||
delete data0;
|
||||
CHECK(data2->FirstLink::etl_next == data3);
|
||||
CHECK(data2->FirstLink::etl_previous == nullptr);
|
||||
CHECK(data3->FirstLink::etl_previous == data2);
|
||||
|
||||
CHECK(data2->SecondLink::etl_next == data3);
|
||||
CHECK(data3->SecondLink::etl_previous != nullptr);
|
||||
|
||||
delete data3;
|
||||
CHECK(data2->FirstLink::etl_next == nullptr);
|
||||
CHECK(data2->FirstLink::etl_previous == nullptr);
|
||||
|
||||
CHECK(data2->SecondLink::etl_next != nullptr);
|
||||
CHECK(data2->SecondLink::etl_previous != nullptr);
|
||||
|
||||
delete data2;
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user