ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
scispan.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef DGDS_UTIL_H
23 #define DGDS_UTIL_H
24 
25 #include "common/span.h"
26 #include "common/endian.h"
27 #include "common/file.h"
28 #include "common/memstream.h"
29 #include "common/scummsys.h"
30 #include "common/str.h"
31 
32 namespace Dgds {
33 
35 enum kSciDebugLevels {
36  kDebugLevelSound = 1,
37 };
38 
39 
40 #pragma mark -
41 #pragma mark SciSpanImpl - SciSpanIterator
42 
43 namespace SciSpanInternal {
44  template <typename Span, bool IsConst>
45  class SciSpanIterator : public Common::SpanInternal::SpanIterator<Span, IsConst> {
47  typedef typename Span::value_type span_value_type;
49 
50  public:
51  typedef typename Span::difference_type difference_type;
52  typedef typename Common::remove_const<span_value_type>::type value_type;
55 
56  inline SciSpanIterator() : super_type() {}
57 
58  inline SciSpanIterator(span_type *span, const difference_type index) : super_type(span, index) {}
59 
60  inline SciSpanIterator(const SciSpanIterator &other) : super_type(other) {}
61 
62  inline SciSpanIterator &operator=(const SciSpanIterator &other) {
63  super_type::operator=(other);
64  return *this;
65  }
66 
67  inline SciSpanIterator operator+(const difference_type delta) const {
68  SciSpanIterator it(*this);
69  it += delta;
70  return it;
71  }
72 
73  };
74 } // End of namespace SciSpanInternal
75 
81 template <typename ValueType, template <typename> class Derived>
82 class SciSpanImpl : public Common::NamedSpanImpl<ValueType, Derived> {
84  typedef Derived<ValueType> derived_type;
85 
86  template <typename T, template <typename> class U> friend class SciSpanImpl;
87 #if defined(CXXTEST_RUNNING) && CXXTEST_RUNNING
88  friend class ::SpanTestSuite;
89 #endif
90 
91 public:
92  typedef typename super_type::value_type value_type;
93  typedef typename super_type::difference_type difference_type;
94  typedef typename super_type::index_type index_type;
95  typedef typename super_type::size_type size_type;
98  typedef typename super_type::pointer pointer;
99  typedef typename super_type::const_pointer const_pointer;
100  typedef typename super_type::reference reference;
101  typedef typename super_type::const_reference const_reference;
102 
103  inline SciSpanImpl() : super_type() {}
104 
105  inline SciSpanImpl(const pointer data_,
106  const size_type size_,
107  const Common::String &name_ = Common::String(),
108  const size_type sourceByteOffset_ = 0) :
109  super_type(data_, size_, name_, sourceByteOffset_) {}
110 
111  template <typename Other>
112  inline SciSpanImpl(const Other &other) : super_type(other) {}
113 
114  inline const_iterator cbegin() const { return const_iterator(&this->impl(), 0); }
115  inline const_iterator cend() const { return const_iterator(&this->impl(), this->size()); }
116  inline const_iterator begin() const { return const_iterator(&this->impl(), 0); }
117  inline const_iterator end() const { return const_iterator(&this->impl(), this->size()); }
118  inline iterator begin() { return iterator(&this->impl(), 0); }
119  inline iterator end() { return iterator(&this->impl(), this->size()); }
120 
121 #pragma mark -
122 #pragma mark SciSpanImpl - ForwardIterator
123 
124 // Spans that are used as ForwardIterators must not be allowed inside of
125 // SpanOwner, since this will result in the wrong pointer to memory to be
126 // deleted
127 private:
128  typedef typename Common::remove_const<Derived<ValueType> >::type mutable_derived_type;
129 
130 public:
131 
132  inline const_reference operator*() const {
133  this->validate(0, sizeof(value_type));
134  return *this->_data;
135  }
136 
137  inline reference operator*() {
138  this->validate(0, sizeof(value_type));
139  return *this->_data;
140  }
141 
142  inline mutable_derived_type &operator+=(const difference_type delta) {
143  this->validate(0, delta * sizeof(value_type), Common::kValidateSeek);
144  this->_data += delta;
145  this->_size -= delta;
146  return this->impl();
147  }
148 
149  inline mutable_derived_type &operator++() {
150  return operator+=(1);
151  }
152 
153  inline mutable_derived_type operator++(int) {
154  mutable_derived_type span(this->impl());
155  operator+=(1);
156  return span;
157  }
158 
159  inline mutable_derived_type operator+(const difference_type delta) const {
160  mutable_derived_type span(this->impl());
161  span += delta;
162  return span;
163  }
164 };
165 
166 template <typename ValueType>
167 class SciSpan : public SciSpanImpl<ValueType, SciSpan> {
169 
170 public:
171  COMMON_SPAN_TYPEDEFS;
172 
173  inline SciSpan() : super_type() {}
174 
175  inline SciSpan(const pointer data_,
176  const size_type size_,
177  const Common::String &name_ = Common::String(),
178  const size_type sourceByteOffset_ = 0) :
179  super_type(data_, size_, name_, sourceByteOffset_) {}
180 
181  template <typename Other>
182  inline SciSpan(const Other &other) : super_type(other) {}
183 };
184 
185 } // End of namespace Dgds
186 
187 #endif // DGDS_UTIL_H
Definition: str.h:59
Definition: scispan.h:82
Definition: span.h:713
Definition: ads.h:28
Definition: span.h:254
Definition: type_traits.h:44
Definition: scispan.h:167
Definition: span.h:690