Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Core Modules
dune-common
Commits
c2881917
Commit
c2881917
authored
2 years ago
by
Simon Praetorius
Browse files
Options
Downloads
Patches
Plain Diff
Add user-defined literal to represent index_constants
parent
384faac1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1077
Add user-defined literal to represent index_constants
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+2
-0
2 additions, 0 deletions
CHANGELOG.md
dune/common/indices.hh
+87
-0
87 additions, 0 deletions
dune/common/indices.hh
dune/common/test/indicestest.cc
+18
-0
18 additions, 0 deletions
dune/common/test/indicestest.cc
with
107 additions
and
0 deletions
CHANGELOG.md
+
2
−
0
View file @
c2881917
...
...
@@ -43,6 +43,8 @@ In order to build the DUNE core modules you need at least the following software
-
The storage type
`ReservedVector`
is extended to follow more closely the
`std::vector`
and
`std::array`
interfaces.
-
Add user-defined literals
`_uc`
and
`_sc`
to represent integer constants.
## Build System
-
Improve the the function
`dune_add_library`
by separating the target types normal, interface, and
...
...
This diff is collapsed.
Click to expand it.
dune/common/indices.hh
+
87
−
0
View file @
c2881917
...
...
@@ -7,6 +7,7 @@
#define DUNE_COMMON_INDICES_HH
#include
<cstddef>
#include
<stdexcept>
#include
<type_traits>
#include
<utility>
...
...
@@ -127,6 +128,92 @@ namespace Dune
return
f
(
std
::
integral_constant
<
I
,
i
>
()...);
}
namespace
Indices
::
Literals
{
namespace
Impl
{
// convert a single character into an unsigned integer
constexpr
unsigned
char2digit
(
const
char
c
)
{
if
(
c
>=
'0'
&&
c
<=
'9'
)
return
unsigned
(
c
)
-
unsigned
(
'0'
);
else
{
throw
std
::
invalid_argument
(
"Character is not a digit."
);
return
0u
;
}
}
// convert a sequence of character digits into an unsigned integer
template
<
class
T
,
char
...
digits
>
constexpr
T
chars2unsigned
()
{
const
char
arr
[]
=
{
digits
...};
if
(
arr
[
0
]
==
'-'
)
throw
std
::
invalid_argument
(
"Negative index constant not allowed."
);
T
result
=
0
;
T
power
=
1
;
const
T
base
=
10
;
const
int
N
=
sizeof
...(
digits
);
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
char
c
=
arr
[
N
-
1
-
i
];
result
+=
char2digit
(
c
)
*
power
;
power
*=
base
;
}
return
result
;
}
// convert a sequence of character digits into a signed integer
template
<
class
T
,
char
digit0
,
char
...
digits
>
constexpr
T
chars2signed
()
{
return
digit0
==
'-'
?
-
chars2unsigned
<
T
,
digits
...
>
()
:
chars2unsigned
<
T
,
digit0
,
digits
...
>
();
}
}
//namespace Impl
/**
* \brief Literal to create an index compile-time constant
*
* \b Example:
* `1_ic -> std::integral_constant<std::size_t,1>`
**/
template
<
char
...
digits
>
constexpr
auto
operator
""
_ic
()
{
return
std
::
integral_constant
<
std
::
size_t
,
Impl
::
chars2unsigned
<
std
::
size_t
,
digits
...
>
()
>
{};
}
/**
* \brief Literal to create an unsigned integer compile-time constant
*
* \b Example:
* `1_uc -> std::integral_constant<unsigned,1>`
**/
template
<
char
...
digits
>
constexpr
auto
operator
""
_uc
()
{
return
std
::
integral_constant
<
unsigned
,
Impl
::
chars2unsigned
<
unsigned
,
digits
...
>
()
>
{};
}
/**
* \brief Literal to create a signed integer compile-time constant
*
* \b Example:
* `-1_sc -> std::integral_constant<int,-1>`
**/
template
<
char
...
digits
>
constexpr
auto
operator
""
_sc
()
{
return
std
::
integral_constant
<
int
,
Impl
::
chars2signed
<
int
,
digits
...
>
()
>
{};
}
}
//namespace Indices::Literals
}
//namespace Dune
#endif // DUNE_COMMON_INDICES_HH
This diff is collapsed.
Click to expand it.
dune/common/test/indicestest.cc
+
18
−
0
View file @
c2881917
...
...
@@ -30,5 +30,23 @@ int main()
std
::
get
<
_1
>
(
v
)
=
4.14
;
std
::
get
<
_2
>
(
v
)
=
3.7
;
{
// test user-defined literal _xc
using
namespace
Dune
::
Indices
::
Literals
;
static_assert
(
_0
==
0
_ic
);
static_assert
(
_1
==
1
_ic
);
static_assert
(
_2
==
2
_ic
);
static_assert
(
_3
==
3
_ic
);
static_assert
(
_4
==
4
_ic
);
static_assert
(
_5
==
5
_ic
);
static_assert
(
_6
==
6
_ic
);
static_assert
(
_7
==
7
_ic
);
static_assert
(
_8
==
8
_ic
);
static_assert
(
_9
==
9
_ic
);
static_assert
(
123
_uc
==
123u
);
static_assert
(
-
123
_sc
==
-
123
);
}
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment