libtins
4.5
Toggle main menu visibility
Loading...
Searching...
No Matches
include
tins
small_uint.h
1
/*
2
* Copyright (c) 2017, Matias Fontanini
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are
7
* met:
8
*
9
* * Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above
12
* copyright notice, this list of conditions and the following disclaimer
13
* in the documentation and/or other materials provided with the
14
* distribution.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*
28
*/
29
30
#ifndef TINS_SMALL_UINT_H
31
#define TINS_SMALL_UINT_H
32
33
#include <stdint.h>
34
#include <stdexcept>
35
36
namespace
Tins
{
37
class
value_too_large
:
public
std::exception {
38
public
:
39
const
char
* what()
const
throw
() {
40
return
"Value is too large"
;
41
}
42
};
43
51
template
<
size_t
n>
52
class
small_uint
{
53
private
:
54
template
<
bool
cond,
typename
OnTrue,
typename
OnFalse>
55
struct
if_then_else {
56
typedef
OnTrue type;
57
};
58
59
template
<
typename
OnTrue,
typename
OnFalse>
60
struct
if_then_else<false, OnTrue, OnFalse> {
61
typedef
OnFalse type;
62
};
63
64
template
<
size_t
i>
65
struct
best_type {
66
typedef
typename
if_then_else<
67
(i <= 8),
68
uint8_t,
69
typename
if_then_else<
70
(i <= 16),
71
uint16_t,
72
typename
if_then_else<
73
(i <= 32),
74
uint32_t,
75
uint64_t
76
>::type
77
>::type
78
>::type type;
79
};
80
81
template
<u
int
64_t base, u
int
64_t pow>
82
struct
power {
83
static
const
uint64_t value = base * power<base, pow - 1>::value;
84
};
85
86
template
<u
int
64_t base>
87
struct
power<base, 0> {
88
static
const
uint64_t value = 1;
89
};
90
public
:
94
typedef
typename
best_type<n>::type
repr_type
;
95
99
static
const
repr_type
max_value
= power<2, n>::value - 1;
100
104
small_uint
() : value() {}
105
114
small_uint
(
repr_type
val) {
115
if
(val >
max_value
) {
116
throw
value_too_large
();
117
}
118
value = val;
119
}
120
124
operator
repr_type
()
const
{
125
return
value;
126
}
127
private
:
128
repr_type
value;
129
};
130
131
}
// Tins
132
133
#endif
// TINS_SMALL_UINT_H
Tins::small_uint::max_value
static const repr_type max_value
Definition
small_uint.h:99
Tins::small_uint::small_uint
small_uint()
Definition
small_uint.h:104
Tins::small_uint::repr_type
best_type< n >::type repr_type
Definition
small_uint.h:94
Tins::small_uint::small_uint
small_uint(repr_type val)
Copy constructs the stored value.
Definition
small_uint.h:114
Tins::value_too_large
Definition
small_uint.h:37
Tins
The Tins namespace.
Definition
address_range.h:38
Generated on
for libtins by
1.17.0