Skip to content

Commit 3e5f891

Browse files
iscgarruevs
authored andcommitted
handle: extract handle-related code from dsc.h
This makes it easier to avoid including dsc.h when it's not needed.
1 parent d88b8c0 commit 3e5f891

5 files changed

Lines changed: 55 additions & 39 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ add_library(slvs-solver INTERFACE)
6565
target_sources(slvs-solver INTERFACE
6666
dsc.h
6767
expr.h
68+
handle.h
6869
param.h
6970
platform/platform.h
7071
solvespace.h

src/dsc.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,13 @@
1111
#include <cmath>
1212
#include <cstddef>
1313
#include <cstdint>
14-
#include <type_traits>
1514
#include <vector>
1615

1716
#include "defs.h"
1817
#include "util.h"
1918

2019
namespace SolveSpace {
2120

22-
/// Trait indicating which types are handle types and should get the associated operators.
23-
/// Specialize for each handle type and inherit from std::true_type.
24-
template<typename T>
25-
struct IsHandleOracle : std::false_type {};
26-
27-
// Equality-compare any two instances of a handle type.
28-
template<typename T>
29-
static inline typename std::enable_if<IsHandleOracle<T>::value, bool>::type
30-
operator==(T const &lhs, T const &rhs) {
31-
return lhs.v == rhs.v;
32-
}
33-
34-
// Inequality-compare any two instances of a handle type.
35-
template<typename T>
36-
static inline typename std::enable_if<IsHandleOracle<T>::value, bool>::type
37-
operator!=(T const &lhs, T const &rhs) {
38-
return !(lhs == rhs);
39-
}
40-
41-
// Less-than-compare any two instances of a handle type.
42-
template<typename T>
43-
static inline typename std::enable_if<IsHandleOracle<T>::value, bool>::type
44-
operator<(T const &lhs, T const &rhs) {
45-
return lhs.v < rhs.v;
46-
}
47-
48-
template<class T>
49-
struct HandleHasher {
50-
static_assert(IsHandleOracle<T>::value, "Not a valid handle type");
51-
52-
inline size_t operator()(const T &h) const {
53-
using Hasher = std::hash<decltype(T::v)>;
54-
return Hasher{}(h.v);
55-
}
56-
};
57-
5821
class Vector;
5922
class Vector4;
6023
class Point2d;

src/handle.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef SOVLESPACE_HANDLE_H
2+
#define SOVLESPACE_HANDLE_H
3+
4+
#include <functional>
5+
#include <type_traits>
6+
7+
namespace SolveSpace {
8+
9+
/// Trait indicating which types are handle types and should get the associated operators.
10+
/// Specialize for each handle type and inherit from std::true_type.
11+
template<class T>
12+
struct IsHandleOracle : std::false_type {};
13+
14+
// Equality-compare any two instances of a handle type.
15+
template<class T>
16+
static inline typename std::enable_if<IsHandleOracle<T>::value, bool>::type
17+
operator==(T const &lhs, T const &rhs) {
18+
return lhs.v == rhs.v;
19+
}
20+
21+
// Inequality-compare any two instances of a handle type.
22+
template<class T>
23+
static inline typename std::enable_if<IsHandleOracle<T>::value, bool>::type
24+
operator!=(T const &lhs, T const &rhs) {
25+
return !(lhs == rhs);
26+
}
27+
28+
// Less-than-compare any two instances of a handle type.
29+
template<class T>
30+
static inline typename std::enable_if<IsHandleOracle<T>::value, bool>::type
31+
operator<(T const &lhs, T const &rhs) {
32+
return lhs.v < rhs.v;
33+
}
34+
35+
template<class T>
36+
struct HandleHasher {
37+
static_assert(IsHandleOracle<T>::value, "Not a valid handle type");
38+
39+
inline size_t operator()(const T &h) const {
40+
using Hasher = std::hash<decltype(T::v)>;
41+
return Hasher{}(h.v);
42+
}
43+
};
44+
45+
} // namespace SolveSpace
46+
47+
#endif // !SOVLESPACE_HANDLE_H

src/param.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <cstdint>
55
#include <unordered_set>
66

7-
#include "dsc.h"
7+
#include "handle.h"
88

99
namespace SolveSpace {
1010

@@ -39,7 +39,11 @@ class Param {
3939
void Clear() {}
4040
};
4141

42-
using ParamList = IdList<Param,hParam>;
42+
// Use a forward declaration in order to avoid pulling dsc.h in for units that
43+
// don't need to use `ParamList`
44+
template<class T, class H> class IdList;
45+
46+
using ParamList = IdList<Param, hParam>;
4347

4448
using ParamSet = std::unordered_set<hParam, HandleHasher<hParam>>;
4549

src/srf/surface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cstdint>
1414

1515
#include "dsc.h"
16+
#include "handle.h"
1617
#include "polygon.h"
1718

1819
namespace SolveSpace {

0 commit comments

Comments
 (0)