File: range.h

package info (click to toggle)
xstow 1.0.2-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,064 kB
  • ctags: 1,073
  • sloc: cpp: 7,264; sh: 1,216; makefile: 196; awk: 26
file content (83 lines) | stat: -rw-r--r-- 1,552 bytes parent folder | download | duplicates (3)
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
/*
 * $Log: range.h,v $
 * Revision 1.3  2005/07/04 21:59:42  martin
 * added logging to all files
 *
 */
#ifndef RANGE_H
#define RANGE_H

#include <exception>

// #define CORE_DUMP

template<class EClass> class EnumRange : public EClass
{    
public:
    struct Error : public std::exception {
	const char *err;
	Error( const char *err ) throw() : exception(), err(err) {}
	virtual const char * what() const throw() { return err; }
    };

    typedef typename EClass::ETYPE ETYPE;       

    static const ETYPE INVALID__ = static_cast<ETYPE>(-5000);
    ETYPE value;

    EnumRange( ETYPE value ) 
	: value( value )
	{
	    if( value <= EClass::FIRST__ || value >= EClass::LAST__ )
#ifdef CORE_DUMP
		int i = 1 / 0;
#else
		throw( Error( "EnumRange: Out of range!" ) );
#endif
	}    

    EnumRange() : value( INVALID__ ) {}

    ETYPE operator()() const {
	if( value == INVALID__ )
#ifdef CORE_DUMP
		int i = 1 / 0;
#else
	    throw( Error( "EnumRange: Uninitialized value!" ) );
#endif

	return value;
    }

    operator ETYPE () const {
	if( value == INVALID__ )
#ifdef CORE_DUMP
		int i = 1 / 0;
#else
	    throw( Error( "EnumRange: Uninitialized value!" ) );
#endif

	return value;
    }

    EnumRange<EClass>& operator=( ETYPE v ) {
	if( v <= EClass::FIRST__ || v >= EClass::LAST__ )
#ifdef CORE_DUMP
		int i = 1 / 0;
#else
	    throw( Error( "EnumRange: Out of range!" ) );
#endif
	    value = v;

	return *this;
    }

    EnumRange<EClass>& operator=( const EnumRange<EClass>& er ) {
	value = er.value;
	return *this;
    }
	
};


#endif