File: Wary.h

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (139 lines) | stat: -rw-r--r-- 3,394 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 1997-2024
   Ewgenij Gawrilow, Michael Joswig, and the polymake team
   Technische Universität Berlin, Germany
   https://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.
--------------------------------------------------------------------------------
*/

#pragma once

#include "polymake/internal/iterators.h"

namespace pm {

template <typename T, typename Model=typename object_traits<T>::model>
class Wary_traits {};

template <typename T>
class Wary_traits<T, is_container> : public container_traits<T> {};

template <typename T>
class Wary
   : public Wary_traits<T>
   , public inherit_generic<Wary<T>, T>::type {
   Wary() = delete;
   ~Wary() = delete;
public:
   using inherit_generic<Wary,T>::type::operator=;
};

template <typename T>
class Generic< Wary<T> > {
   Generic() = delete;
   ~Generic() = delete;
public:
   using top_type = T;
   using persistent_type = T;
   using concrete_type = Wary<T>;

   const top_type& top() const &
   {
      return reinterpret_cast<const T&>(static_cast<const Wary<T>&>(*this));
   }
   top_type& top() &
   {
      return reinterpret_cast<T&>(static_cast<Wary<T>&>(*this));
   }
   top_type&& top() &&
   {
      return reinterpret_cast<T&&>(static_cast<Wary<T>&&>(*this));
   }
};

template <typename T, typename Feature>
struct check_container_feature<Wary<T>, Feature> : check_container_feature<T, Feature> {};

template <typename T>
struct redirect_object_traits< Wary<T> > : object_traits<T> {};

template <typename T>
decltype(auto) wary(T&& x)
{
   return reinterpret_cast<inherit_reference_t<Wary<pure_type_t<T>>, T&&>>(x);
}

template <typename T>
using MaybeWary = can_assign_to<T, Wary<T>>;

template <typename T>
typename std::enable_if<MaybeWary<T>::value, Wary<T>&>::type
maybe_wary(T& x)
{
   return wary(x);
}

template <typename T>
typename std::enable_if<!MaybeWary<T>::value, T&>::type
maybe_wary(T& x)
{
   return x;
}

template <typename T>
struct Unwary {
   using type = T;
};

template <typename T>
struct Unwary< Wary<T> > {
   using type = T;
};

template <typename T>
using pure_unwary_t = typename Unwary<typename Concrete<pure_type_t<T>>::type>::type;

template <typename T>
using unwary_t = inherit_reference_t<pure_unwary_t<T>, T>;

template <typename T>
constexpr bool is_wary()
{
   using concrete_type = typename Concrete<pure_type_t<T>>::type;
   return !std::is_same<concrete_type, typename Unwary<concrete_type>::type>::value;
}

template <typename T>
decltype(auto) unwary(T&& x, std::enable_if_t<!is_wary<T>(), void**> =nullptr)
{
   return concrete(std::forward<T>(x));
}

template <typename T>
decltype(auto) unwary(T&& x, std::enable_if_t<is_wary<T>(), void**> =nullptr)
{
   return static_cast<unwary_t<T&&>>(x.top());
}

}
namespace polymake {
   using pm::Wary;
   using pm::wary;
   using pm::is_wary;
}


// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: