Use GList in manifest_x structures
[platform/core/appfw/app-installers.git] / src / common / utils / glist_range.h
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #ifndef COMMON_UTILS_GLIST_RANGE_H_
6 #define COMMON_UTILS_GLIST_RANGE_H_
7
8 #include <glib.h>
9
10 #include <cstddef>
11 #include <iterator>
12
13 // Range with mutable forward iterator based on GList
14 // supporting language foreach construct
15 template<typename T>
16 class GListRange {
17  public:
18   class Iterator {
19    public:
20     typedef T value_type;
21     typedef T& reference;
22     typedef T* pointer;
23     typedef std::size_t difference_type;
24     typedef std::forward_iterator_tag iterator_category;
25
26     explicit Iterator(std::nullptr_t ptr = nullptr) : ptr_(ptr) { }
27     explicit Iterator(GList* ptr) : ptr_(ptr) { }
28     explicit operator bool() const {
29       return ptr_;
30     }
31     const reference& operator*() const {
32       return reinterpret_cast<const T&>(ptr_->data);
33     }
34     reference& operator*() {
35       return reinterpret_cast<T&>(ptr_->data);
36     }
37     const pointer operator->() const {
38       return reinterpret_cast<pointer>(&ptr_->data);
39     }
40     pointer operator->() {
41       return reinterpret_cast<pointer>(&ptr_->data);
42     }
43     Iterator& operator++() {
44       ptr_ = g_list_next(ptr_);
45       return *this;
46     }
47     Iterator operator++(int) {
48       Iterator iter(ptr_);
49       ptr_ = g_list_next(ptr_);
50       return iter;
51     }
52     bool operator==(const Iterator& other) const {
53       return ptr_ == other.ptr_;
54     }
55     bool operator!=(const Iterator& other) const {
56       return !this->operator==(other);
57     }
58
59    private:
60     GList* ptr_;
61   };
62
63   explicit GListRange(GList* list) : list_(list) {  }
64   Iterator begin() {
65     return Iterator(list_);
66   }
67   Iterator end() {
68     return Iterator();
69   }
70
71  private:
72   GList* list_;
73 };
74
75 #endif  // COMMON_UTILS_GLIST_RANGE_H_