genode/repos/base/include/util/avl_string.h

86 lines
1.6 KiB
C
Raw Normal View History

2011-12-22 15:19:25 +00:00
/*
* \brief Utility for handling strings as AVL-node keys
* \author Norman Feske
* \date 2006-07-12
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
2011-12-22 15:19:25 +00:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 15:19:25 +00:00
*/
#ifndef _INCLUDE__UTIL__AVL_STRING_H_
#define _INCLUDE__UTIL__AVL_STRING_H_
#include <util/avl_tree.h>
#include <util/string.h>
namespace Genode {
class Avl_string_base;
template <int> class Avl_string;
}
2011-12-22 15:19:25 +00:00
class Genode::Avl_string_base : public Avl_node<Avl_string_base>
{
private:
2011-12-22 15:19:25 +00:00
struct { const char *_str; };
2011-12-22 15:19:25 +00:00
protected:
2011-12-22 15:19:25 +00:00
/**
* Constructor
*
* \noapi
*/
Avl_string_base(const char *str) : _str(str) { }
2011-12-22 15:19:25 +00:00
public:
2011-12-22 15:19:25 +00:00
const char *name() const { return _str; }
2011-12-22 15:19:25 +00:00
/************************
** Avl node interface **
************************/
2011-12-22 15:19:25 +00:00
bool higher(Avl_string_base *c) { return (strcmp(c->_str, _str) > 0); }
2011-12-22 15:19:25 +00:00
/**
* Find by name
*/
Avl_string_base *find_by_name(const char *name)
{
if (strcmp(name, _str) == 0) return this;
2011-12-22 15:19:25 +00:00
Avl_string_base *c = Avl_node<Avl_string_base>::child(strcmp(name, _str) > 0);
return c ? c->find_by_name(name) : 0;
}
};
2011-12-22 15:19:25 +00:00
/*
* The template pumps up the Avl_string_base object and provides the buffer for
* the actual string.
*/
template <int STR_LEN>
class Genode::Avl_string : public Avl_string_base
{
private:
2011-12-22 15:19:25 +00:00
char _str_buf[STR_LEN];
2011-12-22 15:19:25 +00:00
public:
Avl_string(const char *str) : Avl_string_base(_str_buf)
{
copy_cstring(_str_buf, str, sizeof(_str_buf));
}
};
2011-12-22 15:19:25 +00:00
#endif /* _INCLUDE__UTIL__AVL_STRING_H_ */