aboutsummaryrefslogtreecommitdiff
path: root/libutil++/comma_list.h
blob: 1868ee094440966e44a44f5d331c11706c4b3c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
 * @file comma_list.h
 * Container holding items from a list of comma separated items
 *
 * @remark Copyright 2003 OProfile authors
 * @remark Read the file COPYING
 *
 * @author Philippe Elie
 */

#ifndef COMMA_LIST_H
#define COMMA_LIST_H

#include <string>
#include <vector>

#include "string_manip.h"

/**
 * hold a list of item of type T, tracking also if item has been set.
 */
template <class T>
class comma_list
{
public:
	comma_list();

	/**
	 * @param str  list of comma separated item
	 *
	 * setup items array according to str parameters. Implement PP:3.17
	 * w/o restriction on charset and with the special string all which
	 * match anything.
	 */
	void set(std::string const & str);

	/// return true if a specific value is held by this container
	bool is_set() const {
		return !is_all;
	}

	/**
	 * @param value  the value to test
	 *
	 * return true if value match one the stored value in items
	 */
	bool match(T const & value) const;

private:
	typedef T value_type;
	typedef std::vector<value_type> container_type;
	typedef typename container_type::const_iterator const_iterator;
	bool is_all;
	container_type items;
};


template <class T>
comma_list<T>::comma_list()
	: is_all(true)
{
}


template <class T>
void comma_list<T>::set(std::string const & str)
{
	items.clear();

	is_all = false;

	std::vector<std::string> result = separate_token(str, ',');
	for (size_t i = 0 ; i < result.size() ; ++i) {
		if (result[i] == "all") {
			is_all = true;
			items.clear();
			break;
		}
		items.push_back(op_lexical_cast<T>(result[i]));
	}
}


template <class T>
bool comma_list<T>::match(T const & value) const
{
	if (is_all)
		return true;

	const_iterator cit = items.begin();
	const_iterator const end = items.end();

	for (; cit != end; ++cit) {
		if (value == *cit)
			return true;
	}

	return false;
}


#endif /* !COMMA_LIST_H */