/usr/include/polymake/internal/alias.h is in libpolymake-dev-common 3.2r2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | /* Copyright (c) 1997-2018
Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
http://www.polymake.org
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
--------------------------------------------------------------------------------
*/
#ifndef POLYMAKE_INTERNAL_ALIAS_H
#define POLYMAKE_INTERNAL_ALIAS_H
#include "polymake/internal/iterators.h"
#include <algorithm>
#include <memory>
#include <cstring>
#include <limits>
#include <cassert>
namespace pm {
template <typename T>
const type2type<T>& make_mutable_alias(T&, T&);
template <typename T> inline
void make_mutable_alias(const T&, const T&) {}
template <typename T, bool _apply=!std::is_same<typename deref<T>::type, typename attrib<T>::minus_const_ref>::value>
struct demasq {
typedef T type;
static const bool apply=_apply;
};
template <typename T>
struct demasq<T,true> : T {
typedef typename inherit_ref<typename object_traits<typename attrib<typename T::type>::minus_const_ref>::masquerade_for, T>::type
arg_type;
static const bool apply=true;
};
template <typename T>
struct deserves_special_alias {
static T& data();
struct helper {
static derivation::yes Test(T&);
static derivation::no Test(const type2type<T>&);
};
static const bool value= sizeof(helper::Test( make_mutable_alias(data(),data()) )) == sizeof(derivation::yes);
};
namespace object_classifier {
enum { alias_obj, alias_primitive, alias_ref, alias_special,
alias_temporary, alias_based, alias_masqueraded, alias_delayed_masquerade };
template <typename ObjectRef> struct alias_kind_of;
};
template <typename T, int kind=object_classifier::alias_kind_of<T>::value> class alias;
namespace object_classifier {
template <typename ObjectRef>
struct alias_kind_of {
typedef typename deref<ObjectRef>::type T;
static const int value=
is_masquerade<typename demasq<ObjectRef>::type>::value
? (demasq<ObjectRef>::apply ? alias_delayed_masquerade : alias_masqueraded) :
is_masquerade<ObjectRef>::value
? alias_masqueraded :
deserves_special_alias<T>::value
? alias_special :
(is_derived_from_instance2i<T,alias>::value && !check_container_feature<T,sparse>::value)
? alias_based :
object_traits<T>::is_temporary
? alias_temporary :
attrib<ObjectRef>::is_reference
? alias_ref :
std::is_enum<T>::value || (std::is_pod<T>::value && sizeof(T) <= 2 * sizeof(T*))
? alias_primitive
: alias_obj;
};
};
template <typename T, int kind> // kind==alias_obj
class alias {
// alias to a transient value: keep a copy under a shared pointer
public:
typedef alias alias_type;
typedef typename attrib<T>::minus_const_ref value_type;
typedef typename attrib<T>::plus_const_ref const_reference;
typedef typename attrib<T>::plus_ref reference;
typedef const value_type* const_pointer;
typedef value_type* pointer;
typedef const_reference arg_type;
alias() {}
alias(arg_type arg) : value(new(alloc.allocate(1)) value_type(arg)) {}
typedef typename std::conditional<attrib<T>::is_const, const alias<typename attrib<T>::minus_const>, type2type<T> >::type alt_alias_arg_type;
alias(alt_alias_arg_type& other) : value(*other) {}
reference operator* () { return *value; }
const_reference operator* () const { return *value; }
pointer operator-> () { return value.operator->(); }
const_pointer operator-> () const { return value.operator->(); }
value_type& get_object() { return *value; }
protected:
std::allocator<value_type> alloc;
shared_pointer<value_type> value;
template <typename,int> friend class alias;
};
template <typename T>
class alias<T, object_classifier::alias_primitive> {
// alias to a constant primitive data item: keep a verbatim copy
public:
typedef alias alias_type;
typedef typename attrib<T>::minus_const_ref value_type;
typedef typename attrib<T>::plus_const_ref reference;
typedef reference const_reference;
typedef const value_type* pointer;
typedef pointer const_pointer;
typedef typename function_argument<value_type>::type arg_type;
alias() {}
alias(arg_type arg) : val(arg) {}
typedef typename std::conditional<attrib<T>::is_const, const alias<typename attrib<T>::minus_const>, type2type<T> >::type alt_alias_arg_type;
alias(alt_alias_arg_type& other) : val(*other) {}
const_reference operator* () const { return val; }
const_pointer operator-> () const { return &val; }
protected:
value_type val;
template <typename,int> friend class alias;
};
template <typename T>
class alias<T, object_classifier::alias_ref> {
// alias to a persistent object: keep a reference
public:
typedef alias alias_type;
typedef typename attrib<T>::minus_const_ref value_type;
typedef typename attrib<T>::plus_ref reference;
typedef typename attrib<T>::plus_const_ref const_reference;
typedef typename attrib<T>::minus_ref* pointer;
typedef const value_type* const_pointer;
typedef reference arg_type;
alias() : ptr(0) {}
alias(arg_type arg) : ptr(&arg) {}
typedef typename std::conditional<attrib<T>::is_const, const alias<typename attrib<T>::minus_const>, type2type<T> >::type alt_alias_arg_type;
alias(alt_alias_arg_type& other) : ptr(other.ptr) {}
reference operator* () { return *ptr; }
const_reference operator* () const { return *ptr; }
pointer operator-> () { return ptr; }
const_pointer operator-> () const { return ptr; }
reference get_object() { return *ptr; }
protected:
pointer ptr;
template <typename,int> friend class alias;
};
template <typename T>
class alias<T, object_classifier::alias_special> {
// special alias requires extra treatment in the constructor
public:
typedef alias alias_type;
typedef typename attrib<T>::minus_const_ref value_type;
typedef typename attrib<T>::plus_ref reference;
typedef typename attrib<T>::plus_const_ref const_reference;
typedef typename attrib<T>::minus_ref* pointer;
typedef const value_type* const_pointer;
typedef typename function_argument<T>::type arg_type;
alias() {}
alias(arg_type arg) : val(arg) { make_mutable_alias(val,arg); }
typedef typename std::conditional<attrib<T>::is_const, const alias<typename attrib<T>::minus_const>, type2type<T> >::type alt_alias_arg_type;
alias(alt_alias_arg_type& other) : val(other.val) {}
reference operator* () { return val; }
const_reference operator* () const { return val; }
pointer operator-> () { return &val; }
const_pointer operator-> () const { return &val; }
value_type& get_object() { return val; }
protected:
value_type val;
template <typename,int> friend class alias;
};
template <typename T>
class alias<T, object_classifier::alias_temporary> {
// alias to a temporary object: keep an internal copy
public:
typedef alias alias_type;
typedef typename attrib<T>::minus_const_ref value_type;
typedef typename attrib<T>::plus_const_ref const_reference;
typedef typename std::conditional<object_traits<value_type>::is_always_const, const_reference, typename attrib<T>::plus_ref>::type reference;
typedef const value_type* const_pointer;
typedef typename std::conditional<attrib<reference>::is_const, const_pointer, typename attrib<T>::minus_ref*>::type pointer;
typedef const_reference arg_type;
alias() : init(false) {}
alias(arg_type arg) : init(true)
{
// if the constructor throws an exception, alias' destructor won't be called, hence it's safe to set init=true up front
new(allocate()) value_type(arg);
}
alias(const alias& other) : init(other.init)
{
if (init) new(allocate()) value_type(*other);
}
typedef typename std::conditional<attrib<T>::is_const, const alias<typename attrib<T>::minus_const>, type2type<T> >::type alt_alias_arg_type;
alias(alt_alias_arg_type& other) : init(other.init)
{
if (init) new(allocate()) value_type(*other);
}
alias& operator= (const alias& other)
{
if (this != &other) {
if (init) {
destroy_at(ptr());
init=false;
}
if (other.init) {
new(allocate()) value_type(*other);
init=true;
}
}
return *this;
}
~alias() { if (init) destroy_at(ptr()); }
reference operator* () { return *ptr(); }
const_reference operator* () const { return *ptr(); }
pointer operator-> () { return ptr(); }
const_pointer operator-> () const { return ptr(); }
value_type& get_object() { return *ptr(); }
protected:
POLYMAKE_ALIGN(char area[sizeof(value_type)], 8);
bool init;
void* allocate() { return area; }
value_type* ptr() { return reinterpret_cast<value_type*>(area); }
const value_type* ptr() const { return reinterpret_cast<const value_type*>(area); }
template <typename,int> friend class alias;
};
template <typename T>
class alias<T, object_classifier::alias_based>
: public deref<T>::type::alias_type {
typedef typename deref<T>::type::alias_type _super;
public:
typedef typename attrib<T>::minus_const_ref value_type;
typedef typename attrib<T>::plus_ref reference;
typedef typename attrib<T>::plus_const_ref const_reference;
typedef typename attrib<T>::minus_ref* pointer;
typedef const value_type* const_pointer;
typedef const_reference arg_type;
alias() {}
alias(typename _super::arg_type arg) : _super(arg) {}
alias(arg_type arg) : _super(arg) {}
typedef typename std::conditional<attrib<T>::is_const, const alias<typename attrib<T>::minus_const>, type2type<T> >::type alt_alias_arg_type;
alias(alt_alias_arg_type& other) : _super(other) {}
reference operator* () { return static_cast<reference>(static_cast<_super&>(*this)); }
const_reference operator* () const { return static_cast<const_reference>(static_cast<const _super&>(*this)); }
pointer operator-> () { return &operator*(); }
const_pointer operator-> () const { return &operator*(); }
template <typename,int> friend class alias;
};
template <typename T>
class alias<T, object_classifier::alias_masqueraded>
: public alias<typename is_masquerade<T>::hidden_stored_type> {
// masquerade: keep an alias to a hidden type
typedef alias<typename is_masquerade<T>::hidden_stored_type> _super;
public:
typedef alias alias_type;
typedef typename attrib<T>::plus_const_ref const_reference;
typedef typename std::conditional<object_traits<typename is_masquerade<T>::hidden_type>::is_always_const ||
object_traits<typename attrib<T>::minus_const_ref>::is_always_const,
const_reference, typename attrib<T>::plus_ref>::type
reference;
typedef const typename attrib<T>::minus_ref* const_pointer;
typedef typename std::conditional<attrib<reference>::is_const, const_pointer, typename attrib<T>::minus_ref*>::type pointer;
typedef reference arg_type;
alias() {}
alias(arg_type arg)
: _super(reinterpret_cast<typename is_masquerade<T>::hidden_stored_type>(arg)) {}
typedef typename std::conditional<attrib<T>::is_const, const alias<typename attrib<T>::minus_const>, type2type<T> >::type alt_alias_arg_type;
alias(alt_alias_arg_type& other) : _super(other) {}
reference operator* () { return reinterpret_cast<reference>(_super::operator*()); }
const_reference operator* () const { return reinterpret_cast<const_reference>(_super::operator*()); }
pointer operator-> () { return &operator*(); }
const_pointer operator-> () const { return &operator*(); }
template <typename,int> friend class alias;
};
template <typename T>
class alias<T, object_classifier::alias_delayed_masquerade>
: public alias<typename demasq<T>::arg_type> {
// delayed masquerade: keep an alias to a given source type
typedef typename demasq<T>::type M;
typedef alias<typename demasq<T>::arg_type> _super;
public:
typedef alias alias_type;
typedef typename attrib<M>::plus_const_ref const_reference;
typedef typename std::conditional<object_traits<typename is_masquerade<M>::hidden_type>::is_always_const ||
object_traits<typename attrib<M>::minus_const_ref>::is_always_const,
const_reference, typename attrib<M>::plus_ref>::type
reference;
typedef const typename attrib<M>::minus_ref* const_pointer;
typedef typename std::conditional<attrib<reference>::is_const, const_pointer, typename attrib<M>::minus_ref*>::type pointer;
alias() {}
alias(typename _super::arg_type arg) : _super(arg) {}
typedef typename std::conditional<attrib<T>::is_const, const alias<typename attrib<T>::minus_const>, type2type<T> >::type alt_alias_arg_type;
alias(alt_alias_arg_type& other) : _super(other) {}
reference operator* () { return reinterpret_cast<reference>(_super::operator*()); }
const_reference operator* () const { return reinterpret_cast<const_reference>(_super::operator*()); }
pointer operator-> () { return &operator*(); }
const_pointer operator-> () const { return &operator*(); }
template <typename,int> friend class alias;
};
// keeps nothing
template <>
class alias<nothing, object_classifier::alias_obj>
{
public:
typedef alias alias_type;
typedef const nothing& reference;
typedef reference const_reference;
typedef const nothing* pointer;
typedef pointer const_pointer;
typedef reference arg_type;
alias() {}
alias(arg_type) {}
const_reference operator* () const { return std::pair<nothing,nothing>::first; }
const_pointer operator-> () const { return &operator*(); }
};
template <typename T, int kind>
struct function_argument< alias<T,kind> > {
typedef typename alias<T,kind>::arg_type type;
};
} // end namespace pm
#endif // POLYMAKE_INTERNAL_SHARED_OBJECT_H
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|