1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
9 namespace InferenceEngine {
12 * @Brief iterator for accesing standard c-style null terminated strings withing c++ algorithms
15 template<typename Char>
16 struct null_terminated_range_iterator : public std::iterator<std::forward_iterator_tag, Char> {
18 null_terminated_range_iterator() = delete;
20 // make a non-end iterator (well, unless you pass nullptr ;)
21 explicit null_terminated_range_iterator(Char *ptr) : ptr(ptr) {}
23 bool operator != (null_terminated_range_iterator const &that) const {
24 // iterators are equal if they point to the same location
25 return !(operator==(that));
28 bool operator == (null_terminated_range_iterator const &that) const {
29 // iterators are equal if they point to the same location
30 return ptr == that.ptr
31 // or if they are both end iterators
32 || (is_end() && that.is_end());
35 null_terminated_range_iterator<Char> &operator++() {
40 null_terminated_range_iterator<Char> &operator++(int) {
41 return this->operator++();
45 return *get_accessor();
49 Char *& get_accessor() {
51 throw std::logic_error("null_terminated_range_iterator dereference: pointer is zero");
56 // end iterators can be created by the default ctor
58 // or by advancing until a null character
65 template<typename Char>
66 struct null_terminated_range_iterator_end : public null_terminated_range_iterator<Char> {
68 // make an end iterator
69 null_terminated_range_iterator_end() : null_terminated_range_iterator<Char>(nullptr) {
70 null_terminated_range_iterator<Char>::ptr = nullptr;
75 inline null_terminated_range_iterator<const char> null_terminated_string(const char *a) {
76 return null_terminated_range_iterator<const char>(a);
79 inline null_terminated_range_iterator<const char> null_terminated_string_end() {
80 return null_terminated_range_iterator_end<const char>();
83 } // namespace InferenceEngine