| 1 |
|
| 2 |
extern size_t const *s_nErrors_; |
| 3 |
|
| 4 |
template <typename Tp_> |
| 5 |
struct TagOf; |
| 6 |
|
| 7 |
template <Tag_ tag> |
| 8 |
struct TypeOf; |
| 9 |
|
| 10 |
$insert polymorphicSpecializations |
| 11 |
// Individual semantic value classes are derived from Base, offering a |
| 12 |
// member returning the value's Tag_, a member cloning the object of its |
| 13 |
// derived Semantic<Tag_> and a member returning a pointerr to its |
| 14 |
// derived Semantic<Tag_> data. See also Bisonc++'s distribution file |
| 15 |
// README.polymorphic-techical |
| 16 |
class Base |
| 17 |
{ |
| 18 |
protected: |
| 19 |
Tag_ d_baseTag; // d_baseTag is assigned by Semantic. |
| 20 |
|
| 21 |
public: |
| 22 |
Base() = default; |
| 23 |
Base(Base const &other) = delete; |
| 24 |
|
| 25 |
virtual ~Base(); |
| 26 |
|
| 27 |
Tag_ tag() const; |
| 28 |
Base *clone() const; |
| 29 |
void *data() const; |
| 30 |
|
| 31 |
private: |
| 32 |
virtual Base *vClone() const = 0; |
| 33 |
virtual void *vData() const = 0; |
| 34 |
}; |
| 35 |
|
| 36 |
inline Base *Base::clone() const |
| 37 |
{ |
| 38 |
return vClone(); |
| 39 |
} |
| 40 |
|
| 41 |
inline void *Base::data() const |
| 42 |
{ |
| 43 |
return vData(); |
| 44 |
} |
| 45 |
|
| 46 |
inline Tag_ Base::tag() const |
| 47 |
{ |
| 48 |
return d_baseTag; |
| 49 |
} |
| 50 |
|
| 51 |
// The class Semantic stores a semantic value of the type matching tg_ |
| 52 |
template <Tag_ tg_> |
| 53 |
class Semantic: public Base |
| 54 |
{ |
| 55 |
typename TypeOf<tg_>::type d_data; |
| 56 |
|
| 57 |
public: |
| 58 |
Semantic(); |
| 59 |
Semantic(Semantic<tg_> const &other); // req'd for cloning |
| 60 |
|
| 61 |
// This constructor member template forwards its arguments to |
| 62 |
// d_data, allowing it to be initialized using whatever |
| 63 |
// constructor is available for DataType |
| 64 |
template <typename ...Params> |
| 65 |
Semantic(Params &&...params); |
| 66 |
|
| 67 |
private: |
| 68 |
Base *vClone() const override; |
| 69 |
void *vData() const override; |
| 70 |
}; |
| 71 |
|
| 72 |
template <Tag_ tg_> |
| 73 |
Semantic<tg_>::Semantic() |
| 74 |
{ |
| 75 |
d_baseTag = tg_; // Base's data member: |
| 76 |
} |
| 77 |
|
| 78 |
template <Tag_ tg_> |
| 79 |
Semantic<tg_>::Semantic(Semantic<tg_> const &other) |
| 80 |
: |
| 81 |
d_data(other.d_data) |
| 82 |
{ |
| 83 |
d_baseTag = other.d_baseTag; |
| 84 |
} |
| 85 |
|
| 86 |
template <Tag_ tg_> |
| 87 |
template <typename ...Params> |
| 88 |
Semantic<tg_>::Semantic(Params &&...params) |
| 89 |
: |
| 90 |
d_data(std::forward<Params>(params) ...) |
| 91 |
{ |
| 92 |
d_baseTag = tg_; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
template <Tag_ tg_> |
| 97 |
Base *Semantic<tg_>::vClone() const |
| 98 |
{ |
| 99 |
return new Semantic<tg_>{*this}; |
| 100 |
} |
| 101 |
|
| 102 |
template <Tag_ tg_> |
| 103 |
void *Semantic<tg_>::vData() const |
| 104 |
{ |
| 105 |
return const_cast<typename TypeOf<tg_>::type *>(&d_data); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
// The class SType wraps a pointer to Base. It becomes the polymorphic |
| 110 |
// STYPE_ type. It also defines get members, allowing constructions like |
| 111 |
// $$.get<INT> to be used. |
| 112 |
class SType: private std::unique_ptr<Base> |
| 113 |
{ |
| 114 |
using BasePtr = std::unique_ptr<Base>; |
| 115 |
|
| 116 |
public: |
| 117 |
SType(); |
| 118 |
SType(SType const &other); |
| 119 |
SType(SType &&tmp); |
| 120 |
|
| 121 |
~SType() = default; |
| 122 |
|
| 123 |
// Specific overloads are needed for SType = SType assignments |
| 124 |
SType &operator=(SType const &rhs); |
| 125 |
SType &operator=(SType &rhs); // required so it is used |
| 126 |
// instead of the template op= |
| 127 |
SType &operator=(SType &&tmp); |
| 128 |
|
| 129 |
$insert polymorphicOpAssignDecl |
| 130 |
template <Tag_ tagParam, typename ...Args> |
| 131 |
void assign(Args &&...args); |
| 132 |
|
| 133 |
// By default the get()-members check whether the specified <tag> |
| 134 |
// matches the tag returned by SType::tag (d_data's tag). If they |
| 135 |
// don't match a run-time fatal error results. |
| 136 |
template <Tag_ tag> |
| 137 |
typename TypeOf<tag>::type &get(); |
| 138 |
|
| 139 |
template <Tag_ tag> |
| 140 |
typename TypeOf<tag>::type const &get() const; |
| 141 |
|
| 142 |
Tag_ tag() const; |
| 143 |
}; |
| 144 |
|
| 145 |
inline SType::SType() |
| 146 |
: |
| 147 |
std::unique_ptr<Base>(new Semantic<Tag_::END_TAG_>{}) |
| 148 |
{} |
| 149 |
|
| 150 |
inline SType::SType(SType const &other) |
| 151 |
: |
| 152 |
BasePtr{ other->clone() } |
| 153 |
{} |
| 154 |
|
| 155 |
inline SType::SType(SType &&tmp) |
| 156 |
: |
| 157 |
BasePtr{ std::move(tmp) } |
| 158 |
{ |
| 159 |
tmp = SType{}; // tmp remains valid |
| 160 |
} |
| 161 |
|
| 162 |
inline SType &SType::operator=(SType const &rhs) |
| 163 |
{ |
| 164 |
reset(rhs->clone()); |
| 165 |
return *this; |
| 166 |
} |
| 167 |
|
| 168 |
inline SType &SType::operator=(SType &rhs) |
| 169 |
{ |
| 170 |
reset(rhs->clone()); |
| 171 |
return *this; |
| 172 |
} |
| 173 |
|
| 174 |
inline SType &SType::operator=(SType &&tmp) |
| 175 |
{ |
| 176 |
BasePtr::operator=(std::move(tmp)); |
| 177 |
return *this; |
| 178 |
} |
| 179 |
|
| 180 |
$insert polymorphicOpAssignImpl |
| 181 |
|
| 182 |
template <Tag_ tagParam, typename ...Args> |
| 183 |
void SType::assign(Args &&...args) |
| 184 |
{ |
| 185 |
reset(new Semantic<tagParam>(std::forward<Args>(args) ...)); |
| 186 |
} |
| 187 |
|
| 188 |
template <Tag_ tg> |
| 189 |
typename TypeOf<tg>::type &SType::get() |
| 190 |
{ |
| 191 |
$insert warnTagMismatches |
| 192 |
return *static_cast<typename TypeOf<tg>::type *>( (*this)->data() ); |
| 193 |
} |
| 194 |
|
| 195 |
template <Tag_ tg> |
| 196 |
typename TypeOf<tg>::type const &SType::get() const |
| 197 |
{ |
| 198 |
$insert warnTagMismatches |
| 199 |
return *static_cast<typename TypeOf<tg>::type *>( (*this)->data() ); |
| 200 |
} |
| 201 |
|
| 202 |
inline Tag_ SType::tag() const |
| 203 |
{ |
| 204 |
return (*this)->tag(); |
| 205 |
} |
| 206 |
|
| 207 |
} // namespace Meta_ |