FIX: fixed const correctness

This commit is contained in:
Dominik Meyer 2019-07-07 21:59:10 +02:00
parent ef31885f61
commit 75e6d2007b
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
2 changed files with 16 additions and 22 deletions

View File

@ -23,16 +23,11 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <memory>
#include <string>
#include <list>
#include <algorithm>
#include <iostream>
/**
* @brief main namespace for the libtree++ library
@ -57,11 +52,11 @@ namespace Tree
* @brief get the root of the tree
*
*/
std::shared_ptr<Tree::BaseNode> getRoot()
std::shared_ptr<Tree::BaseNode> getRoot() const
{
if (parent==nullptr)
{
return shared_from_this();
return std::const_pointer_cast<Tree::BaseNode>(shared_from_this());
}
return parent->getRoot();
@ -131,14 +126,14 @@ namespace Tree
*
* @return iterator to the beginning of the list of children
*/
std::list<std::shared_ptr<Tree::BaseNode>>::iterator getChildrenBegin() {return children.begin();}
std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator getChildrenBegin() const {return children.begin();}
/**
* @brief returns an iterator to the end of the list of children
*
* @return iterator to the end of the list of children
*/
std::list<std::shared_ptr<Tree::BaseNode>>::iterator getChildrenEnd() {return children.end();}
std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator getChildrenEnd() const {return children.end();}
/**
* @brief returns the nr of direct children
@ -227,7 +222,7 @@ namespace Tree
* @return nullptr - a node of that type could not be found
* @return the found node as a shared_ptr
*/
std::shared_ptr<Tree::BaseNode> findNext(const std::string &t);
std::shared_ptr<const Tree::BaseNode> findNext(const std::string &t) const;
/**
* @brief finds all nodes of a given type
@ -236,13 +231,12 @@ namespace Tree
* @param c - a container for holding all found nodes, has to be holding objects of type std::shared_ptr<Tree::BaseNode>
*/
template <typename Container>
void findAll(const std::string &t, Container &c)
void findAll(const std::string &t, Container &c) const
{
std::list<std::shared_ptr<Tree::BaseNode>>::iterator it;
std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator it;
if (type() == t)
{
c.push_back(shared_from_this());
c.push_back(std::const_pointer_cast<Tree::BaseNode>(shared_from_this()));
}
for(it = children.begin(); it != children.end(); ++it)
@ -259,13 +253,13 @@ namespace Tree
* @param c - a container for holding all found nodes, has to be holding objects of type std::shared_ptr<Tree::BaseNode>
*/
template <class Container>
void findAllBase(const std::string &b, Container &c)
void findAllBase(const std::string &b, Container &c) const
{
std::list<std::shared_ptr<Tree::BaseNode>>::iterator it;
std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator it;
if (base() == b)
{
c.push_back(shared_from_this());
c.push_back(std::const_pointer_cast<Tree::BaseNode>(shared_from_this()));
}
for(it = children.begin(); it != children.end(); ++it)

View File

@ -20,10 +20,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <Tree/tree.hpp>
std::shared_ptr<Tree::BaseNode> Tree::BaseNode::findNext(const std::string &t)
std::shared_ptr<const Tree::BaseNode> Tree::BaseNode::findNext(const std::string &t) const
{
std::list<std::shared_ptr<Tree::BaseNode>>::iterator childIT;
std::shared_ptr<Tree::BaseNode> temp;
std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator childIT;
std::shared_ptr<const Tree::BaseNode> temp;
if (type() == t )
{
@ -72,7 +72,7 @@ void Tree::BaseNode::printTree(const uint32_t depth)
std::cout << toString() << std::endl;
std::list<std::shared_ptr<Tree::BaseNode>>::iterator childIT;
std::list<std::shared_ptr<Tree::BaseNode>>::const_iterator childIT;
for(childIT=getChildrenBegin(); childIT != getChildrenEnd(); ++childIT)
{