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.
5 #ifndef COMMON_UTILS_GLIST_RANGE_H_
6 #define COMMON_UTILS_GLIST_RANGE_H_
13 // Range with mutable forward iterator based on GList
14 // supporting language foreach construct
23 typedef std::size_t difference_type;
24 typedef std::forward_iterator_tag iterator_category;
26 explicit Iterator(std::nullptr_t ptr = nullptr) : ptr_(ptr) { }
27 explicit Iterator(GList* ptr) : ptr_(ptr) { }
28 explicit operator bool() const {
31 const reference& operator*() const {
32 return reinterpret_cast<const T&>(ptr_->data);
34 reference& operator*() {
35 return reinterpret_cast<T&>(ptr_->data);
37 const pointer operator->() const {
38 return reinterpret_cast<pointer>(&ptr_->data);
40 pointer operator->() {
41 return reinterpret_cast<pointer>(&ptr_->data);
43 Iterator& operator++() {
44 ptr_ = g_list_next(ptr_);
47 Iterator operator++(int) {
49 ptr_ = g_list_next(ptr_);
52 bool operator==(const Iterator& other) const {
53 return ptr_ == other.ptr_;
55 bool operator!=(const Iterator& other) const {
56 return !this->operator==(other);
63 explicit GListRange(GList* list) : list_(list) { }
65 return Iterator(list_);
75 #endif // COMMON_UTILS_GLIST_RANGE_H_