forked from gsl-lite/gsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsl-lite-vc6.hpp
695 lines (552 loc) · 14.7 KB
/
gsl-lite-vc6.hpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//
// gsl-lite-vc6 is based on GSL: Guideline Support Library,
// For more information see https://github.com/martinmoene/gsl-lite
//
// Copyright (c) 2015 Martin Moene
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#ifndef GSL_GSL_LITE_H_INCLUDED
#define GSL_GSL_LITE_H_INCLUDED
#include <exception>
#include <iterator>
#include <limits>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#define gsl_lite_VERSION "0.0.0"
// Configuration:
#ifndef gsl_FEATURE_HAVE_IMPLICIT_MACRO
# define gsl_FEATURE_HAVE_IMPLICIT_MACRO 1
#endif
#ifndef gsl_FEATURE_HAVE_OWNER_MACRO
# define gsl_FEATURE_HAVE_OWNER_MACRO 1
#endif
#ifndef gsl_FEATURE_HAVE_SHARED_PTR
# define gsl_FEATURE_HAVE_SHARED_PTR 0
#endif
#ifndef gsl_FEATURE_HAVE_UNIQUE_PTR
# define gsl_FEATURE_HAVE_UNIQUE_PTR 0
#endif
#ifndef gsl_CONFIG_THROWS_FOR_TESTING
# define gsl_CONFIG_THROWS_FOR_TESTING 0
#endif
#ifndef gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS
# define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS 0
#endif
#ifndef gsl_CONFIG_SHARED_PTR_INCLUDE
# define gsl_CONFIG_SHARED_PTR_INCLUDE <boost/shared_ptr.hpp>
#endif
#ifndef gsl_CONFIG_UNIQUE_PTR_INCLUDE
# define gsl_CONFIG_UNIQUE_PTR_INCLUDE <boost/unique_ptr.hpp>
#endif
#ifndef gsl_CONFIG_SHARED_PTR_DECL
# define gsl_CONFIG_SHARED_PTR_DECL boost::shared_ptr
#endif
#ifndef gsl_CONFIG_UNIQUE_PTR_DECL
# define gsl_CONFIG_UNIQUE_PTR_DECL boost::unique_ptr
#endif
// Compiler detection:
#if defined(_MSC_VER)
# define gsl_COMPILER_MSVC_VERSION (_MSC_VER / 100 - 5 - (_MSC_VER < 1900))
#else
# define gsl_COMPILER_MSVC_VERSION 0
# define gsl_COMPILER_NON_MSVC 1
#endif
#if gsl_COMPILER_MSVC_VERSION != 6
# error GSL Lite: this header is for Visual C++ 6
#endif
// half-open range [lo..hi):
#define gsl_BETWEEN( v, lo, hi ) ( lo <= v && v < hi )
// Presence of C++ language features:
// C++ feature usage:
#if gsl_FEATURE_HAVE_IMPLICIT_MACRO
# define implicit
#endif
#define gsl_DIMENSION_OF( a ) ( sizeof(a) / sizeof(0[a]) )
#if gsl_FEATURE_HAVE_SHARED_PTR
# include gsl_CONFIG_SHARED_PTR_INCLUDE
#endif
#if gsl_FEATURE_HAVE_UNIQUE_PTR
# include gsl_CONFIG_UNIQUE_PTR_INCLUDE
#endif
namespace gsl {
//
// GSL.owner: ownership pointers
//
// ToDo:
#if gsl_FEATURE_HAVE_SHARED_PTR
using gsl_CONFIG_SHARED_PTR_DECL;
#endif
#if gsl_FEATURE_HAVE_UNIQUE_PTR
using gsl_CONFIG_UNIQUE_PTR_DECL;
#endif
template< class T > struct owner { typedef T type; };
#define gsl_HAVE_OWNER_TEMPLATE 0
#if gsl_FEATURE_HAVE_OWNER_MACRO
# define Owner(t) ::gsl::owner<t>::type
#endif
//
// GSL.assert: assertions
//
#define Expects(x) ::gsl::fail_fast_assert((x))
#define Ensures(x) ::gsl::fail_fast_assert((x))
#if gsl_CONFIG_THROWS_FOR_TESTING
struct fail_fast : public std::runtime_error
{
fail_fast()
: std::runtime_error( "GSL assertion" ) {}
explicit fail_fast( char const * const message )
: std::runtime_error( message ) {}
};
inline void fail_fast_assert( bool cond )
{
if ( !cond )
throw fail_fast();
}
inline void fail_fast_assert( bool cond, char const * const message )
{
if ( !cond )
throw fail_fast( message );
}
#else // gsl_CONFIG_THROWS_FOR_TESTING
inline void fail_fast_assert( bool cond )
{
if ( !cond )
terminate();
}
inline void fail_fast_assert( bool cond, char const * const )
{
if ( !cond )
terminate();
}
#endif // gsl_CONFIG_THROWS_FOR_TESTING
//
// GSL.util: utilities
//
class final_action
{
public:
typedef void (*Action)();
final_action( Action action )
: action_( action ) {}
~final_action()
{
action_();
}
private:
Action action_;
};
template< class Fn >
final_action finally( Fn const & f )
{
return final_action(( f ));
}
template< class T, class U >
T narrow_cast( U u )
{
return static_cast<T>( u );
}
struct narrowing_error : public std::exception {};
template< class T, class U >
T narrow( U u )
{
T t = narrow_cast<T>( u );
if ( static_cast<U>( t ) != u )
{
throw narrowing_error();
}
return t;
}
//
// GSL.views: views
//
//
// at() - Bounds-checked way of accessing static arrays, std::array, std::vector.
//
namespace detail {
struct precedence_0 {};
struct precedence_1 : precedence_0 {};
struct order_precedence : precedence_1 {};
template< class Array, class T >
T & at( Array & arr, size_t index, T*, precedence_0 const & )
{
Expects( index < gsl_DIMENSION_OF( arr ) );
return arr[index];
}
} // namespace detail
// Create an at( container ) function:
# define gsl_MK_AT( Cont ) \
namespace gsl { namespace detail { \
template< class T > \
inline T & at( Cont<T> & cont, size_t index, T*, precedence_1 const & ) \
{ \
Expects( index < cont.size() ); \
return cont[index]; \
} }}
template< class Cont >
int & at( Cont & cont, size_t index )
{
return detail::at( cont, index, &cont[0], detail::order_precedence() );
}
//
// not_null<> - Wrap any indirection and enforce non-null.
//
template<class T>
class not_null
{
public:
not_null( T t ) : ptr_ ( t ){ Expects( ptr_ != NULL ); }
not_null & operator=( T const & t ) { ptr_ = t ; Expects( ptr_ != NULL ); return *this; }
not_null( not_null const & other ) : ptr_ ( other.ptr_ ) {}
not_null & operator=( not_null const & other ) { ptr_ = other.ptr_; }
// VC6 accepts this anyway:
// template< typename U > not_null( not_null<U> const & other );
// template< typename U > not_null & operator=( not_null<U> const & other ) ;
private:
// Prevent compilation when initialized with a literal 0:
not_null( int );
not_null & operator=( int );
public:
T get() const
{
return ptr_;
}
operator T() const { return get(); }
T operator->() const { return get(); }
bool operator==(T const & rhs) const { return ptr_ == rhs; }
bool operator!=(T const & rhs) const { return !(*this == rhs); }
private:
T ptr_;
not_null & operator++();
not_null & operator--();
not_null operator++( int );
not_null operator--( int );
not_null & operator+ ( size_t );
not_null & operator+=( size_t );
not_null & operator- ( size_t );
not_null & operator-=( size_t );
};
//
// Byte-specific type.
//
typedef unsigned char byte;
//
// span<> - A 1D view of contiguous T's, replace (*,len).
//
template< class T >
class span
{
public:
typedef size_t size_type;
typedef T value_type;
typedef T & reference;
typedef T * pointer;
typedef T const * const_pointer;
typedef pointer iterator;
typedef const_pointer const_iterator;
typedef std::reverse_iterator< iterator, T > reverse_iterator;
typedef std::reverse_iterator< const_iterator, const T > const_reverse_iterator;
// Todo:
// typedef typename std::iterator_traits< iterator >::difference_type difference_type;
span()
: begin_( NULL )
, end_ ( NULL )
{
Expects( size() == 0 );
}
span( pointer begin, pointer end )
: begin_( begin )
, end_ ( end )
{
Expects( begin <= end );
}
span( pointer data, size_type size )
: begin_( data )
, end_ ( data + size )
{
Expects( size == 0 || ( size > 0 && data != NULL ) );
}
private:
struct precedence_0 {};
struct precedence_1 : precedence_0 {};
struct precedence_2 : precedence_1 {};
struct order_precedence : precedence_1 {};
template< class Array, class U >
span create( Array & arr, U*, precedence_0 const & ) const
{
return span( arr, gsl_DIMENSION_OF( arr ) );
}
span create( std::vector<T> & cont, T*, precedence_1 const & ) const
{
return span( &cont[0], cont.size() );
}
public:
template< class Cont >
span( Cont & cont )
{
*this = create( cont, &cont[0], order_precedence() );
}
#if 0
// =default constructor
span( span const & other )
: begin_( other.begin() )
, end_ ( other.end() )
{}
#endif
span & operator=( span const & other )
{
// VC6 balks at copy-swap implementation (here),
// so we do it the simple way:
begin_ = other.begin_;
end_ = other.end_;
return *this;
}
#if 0
// Converting from other span ?
template< typename U > operator=();
#endif
iterator begin() const
{
return iterator( begin_ );
}
iterator end() const
{
return iterator( end_ );
}
const_iterator cbegin() const
{
return const_iterator( begin() );
}
const_iterator cend() const
{
return const_iterator( end() );
}
reverse_iterator rbegin() const
{
return reverse_iterator( end() );
}
reverse_iterator rend() const
{
return reverse_iterator( begin() );
}
const_reverse_iterator crbegin() const
{
return const_reverse_iterator( cend() );
}
const_reverse_iterator crend() const
{
return const_reverse_iterator( cbegin() );
}
operator bool () const
{
return begin_ != NULL;
}
reference operator[]( size_type index )
{
return at( index );
}
bool operator==( span const & other ) const
{
return size() == other.size()
&& (begin_ == other.begin_ || std::equal( this->begin(), this->end(), other.begin() ) );
}
bool operator!=( span const & other ) const
{
return !( *this == other );
}
bool operator< ( span const & other ) const
{
return std::lexicographical_compare( this->begin(), this->end(), other.begin(), other.end() );
}
bool operator<=( span const & other ) const
{
return !( other < *this );
}
bool operator> ( span const & other ) const
{
return ( other < *this );
}
bool operator>=( span const & other ) const
{
return !( *this < other );
}
reference at( size_type index )
{
Expects( index >= 0 && index < size());
return begin_[ index ];
}
pointer data() const
{
return begin_;
}
bool empty() const
{
return size() == 0;
}
size_type size() const
{
return std::distance( begin_, end_ );
}
size_type length() const
{
return size();
}
size_type used_length() const
{
return length();
}
size_type bytes() const
{
return sizeof( value_type ) * size();
}
size_type used_bytes() const
{
return bytes();
}
void swap( span & other )
{
using std::swap;
swap( begin_, other.begin_ );
swap( end_ , other.end_ );
}
span< const byte > as_bytes() const
{
return span< const byte >( reinterpret_cast<const byte *>( data() ), bytes() );
}
span< byte > as_writeable_bytes() const
{
return span< byte >( reinterpret_cast<byte *>( data() ), bytes() );
}
template< class U >
struct mk
{
static span<U> view( U * data, size_type size )
{
return span<U>( data, size );
}
};
template< typename U >
span< U > as_span( U u = U() ) const
{
Expects( ( this->bytes() % sizeof(U) ) == 0 );
return mk<U>::view( reinterpret_cast<U *>( this->data() ), this->bytes() / sizeof( U ) );
}
private:
pointer begin_;
pointer end_;
};
// span creator functions (see ctors)
template< typename T>
span< const byte > as_bytes( span<T> spn )
{
return span< const byte >( reinterpret_cast<const byte *>( spn.data() ), spn.bytes() );
}
template< typename T>
span< byte > as_writeable_bytes( span<T> spn )
{
return span< byte >( reinterpret_cast<byte *>( spn.data() ), spn.bytes() );
}
template< typename T >
span<T> as_span( T * begin, T * end )
{
return span<T>( begin, end );
}
template< typename T >
span<T> as_span( T * begin, size_t size )
{
return span<T>( begin, size );
}
namespace detail {
template< class T >
struct mk
{
static span<T> view( std::vector<T> & cont )
{
return span<T>( cont );
}
};
}
template< class T >
span<T> as_span( std::vector<T> & cont )
{
return detail::mk<T>::view( cont );
}
//
// String types:
//
typedef char * zstring;
typedef wchar_t * zwstring;
typedef const char * czstring;
typedef const wchar_t * cwzstring;
typedef span< char > string_span;
typedef span< wchar_t > wstring_span;
typedef span< const char > cstring_span;
typedef span< const wchar_t > cwstring_span;
// to_string() allow (explicit) conversions from string_span to string
inline std::string to_string( string_span const & view )
{
return std::string( view.data(), view.length() );
}
inline std::string to_string( cstring_span const & view )
{
return std::string( view.data(), view.length() );
}
inline std::wstring to_string( wstring_span const & view )
{
return std::wstring( view.data(), view.length() );
}
inline std::wstring to_string( cwstring_span const & view )
{
return std::wstring( view.data(), view.length() );
}
//
// ensure_sentinel()
//
// Provides a way to obtain a span from a contiguous sequence
// that ends with a (non-inclusive) sentinel value.
//
// Will fail-fast if sentinel cannot be found before max elements are examined.
//
namespace detail {
template<class T, class SizeType, const T Sentinel>
struct ensure
{
static span<T> sentinel( T * seq, SizeType max = std::numeric_limits<SizeType>::max() )
{
typedef T * pointer;
typedef typename std::iterator_traits<pointer>::difference_type difference_type;
pointer cur = seq;
while ( std::distance( seq, cur ) < static_cast<difference_type>( max ) && *cur != Sentinel )
++cur;
Expects( *cur == Sentinel );
return span<T>( seq, cur - seq );
}
};
} // namespace detail
//
// ensure_z - creates a string_span for a czstring or cwzstring.
// Will fail fast if a null-terminator cannot be found before
// the limit of size_type.
//
template< typename T >
span<T> ensure_z( T * sz, size_t max = std::numeric_limits<size_t>::max() )
{
return detail::ensure<T, size_t, 0>::sentinel( sz, max );
}
} // namespace gsl
// at( std::vector ):
gsl_MK_AT( std::vector )
#endif // GSL_GSL_LITE_H_INCLUDED
// end of file