1 |
#ifndef ASCXX_SET_H |
2 |
#define ASCXX_SET_H |
3 |
|
4 |
#include <iostream> |
5 |
#include <stdexcept> |
6 |
|
7 |
#include "symchar.h" |
8 |
|
9 |
#include "config.h" |
10 |
extern "C"{ |
11 |
#include <ascend/general/platform.h> |
12 |
#include <ascend/compiler/compiler.h> |
13 |
#include <ascend/compiler/setinstval.h> |
14 |
} |
15 |
|
16 |
/** |
17 |
This C++ template defines ASCXX_Set<long> and ASCXX_Set<SymChar> |
18 |
which can hold instance-variable sets (struct set_t) (as opposed |
19 |
to 'struct Set' which is different!). |
20 |
|
21 |
It has to be a template because the element type is different, |
22 |
so the element accessor functions are different. |
23 |
|
24 |
In python, this class can be wrapped in a more elegant way, see |
25 |
ascend.i for details. |
26 |
*/ |
27 |
|
28 |
class Empty{ |
29 |
// empty class for use in ASCXX_Set<Empty> |
30 |
}; |
31 |
|
32 |
template<class T> |
33 |
class ASCXX_Set{ |
34 |
private: |
35 |
const struct set_t *s; |
36 |
public: |
37 |
|
38 |
ASCXX_Set(){ |
39 |
throw std::runtime_error("Can't create new ASCXX_Set objects"); |
40 |
} |
41 |
|
42 |
ASCXX_Set(const struct set_t *s) : s(s){ |
43 |
if(!isCorrectType()){ |
44 |
throw std::runtime_error("Invalid set creation"); |
45 |
} |
46 |
} |
47 |
|
48 |
const bool isCorrectType() const; |
49 |
|
50 |
unsigned long length() const{ |
51 |
return Cardinality(s); |
52 |
} |
53 |
|
54 |
const T operator[](const unsigned long &index) const; |
55 |
inline const T at(const unsigned long &index) const{ |
56 |
return (*this)[index]; |
57 |
} |
58 |
}; |
59 |
|
60 |
template<> |
61 |
const bool ASCXX_Set<long>::isCorrectType() const; |
62 |
|
63 |
template<> |
64 |
const bool ASCXX_Set<SymChar>::isCorrectType() const; |
65 |
|
66 |
template<> |
67 |
const bool ASCXX_Set<void>::isCorrectType() const; |
68 |
|
69 |
template<class T> |
70 |
std::ostream& operator<< (std::ostream& os, const ASCXX_Set<T>& s){ |
71 |
os << "[ "; |
72 |
bool first=true; |
73 |
for(int i=1; i<= s.length(); ++i){ |
74 |
if(!first)os << ", "; |
75 |
else first=false; |
76 |
os << T(s[i]); |
77 |
} |
78 |
os << "]"; |
79 |
return os; |
80 |
} |
81 |
|
82 |
template<> |
83 |
const long ASCXX_Set<long>::operator[](const unsigned long &i) const; |
84 |
|
85 |
template<> |
86 |
const SymChar ASCXX_Set<SymChar>::operator[](const unsigned long &i) const; |
87 |
|
88 |
template<> |
89 |
const Empty ASCXX_Set<Empty>::operator[](const unsigned long &i) const; |
90 |
|
91 |
#endif |