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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/* 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
namespace pm { namespace perl {
class PropertyValue;
PropertyValue load_data(const std::string& filename);
template <typename T>
void save_data(const std::string& filename, const T& data, const std::string& description=std::string());
PropertyValue get_custom(const AnyString& name, const AnyString& key=AnyString());
class FunCall;
class PropertyValue : public Value {
friend class BigObject; friend class BigObjectType;
friend class FunCall;
friend PropertyValue load_data(const std::string& filename);
template <typename T> friend
void save_data(const std::string& filename, const T& data, const std::string& description);
friend
PropertyValue get_custom(const AnyString& name, const AnyString& key);
PropertyValue() {}
template <typename... Options>
explicit PropertyValue(SV* sv_arg, Options... option_args)
: Value(sv_arg)
{
set_options(option_args...);
}
void set_options() {}
template <typename... MoreOptions>
void set_options(ValueFlags flag, MoreOptions... more_options)
{
options |= flag;
set_options(more_options...);
}
template <typename... MoreOptions>
void set_options(allow_conversion, MoreOptions... more_options)
{
options |= ValueFlags::allow_conversion;
set_options(more_options...);
}
void operator= (const PropertyValue&) = delete;
static SV* load_data_impl(const std::string& filename);
void save_data_impl(const std::string&, const std::string&);
public:
PropertyValue(const PropertyValue& x);
~PropertyValue();
};
inline
Value::NoAnchors Value::put_val(const PropertyValue& x, int)
{
set_copy(x);
return NoAnchors();
}
inline
PropertyValue load_data(const std::string& filename)
{
return PropertyValue(PropertyValue::load_data_impl(filename));
}
template <typename T>
void save_data(const std::string& filename, const T& data, const std::string& description)
{
PropertyValue v;
v << data;
v.save_data_impl(filename, description);
}
template <typename TContainerRef>
struct Unrolled {
explicit Unrolled(TContainerRef c)
: container(c) {}
TContainerRef container;
};
template <typename TContainer>
inline
Unrolled<TContainer&&> unroll(TContainer&& c)
{
return Unrolled<TContainer&&>(std::forward<TContainer>(c));
}
class ListResult
: protected Array {
public:
ListResult(ListResult&&) = default;
ListResult& operator= (ListResult&&) = default;
template <typename Target>
ListResult& operator>> (Target& x)
{
if (!empty()) {
Value v(shift(), ValueFlags::allow_undef | ValueFlags::not_trusted);
v >> x;
v.forget();
}
return *this;
}
template <typename TContainerRef>
void operator>> (Unrolled<TContainerRef>&& c)
{
Value v(sv, ValueFlags::not_trusted);
v.retrieve_nomagic(c.container);
forget();
sv = nullptr;
}
void operator>> (Unrolled<Array&>&& c)
{
c.container = static_cast<Array&&>(*this);
}
protected:
ListResult(int items, FunCall& fc);
friend class BigObject;
friend class FunCall;
private:
ListResult(const ListResult&) = delete;
void operator=(const ListResult&) = delete;
};
class FunCall
: protected Stack {
void operator= (const FunCall&) = delete;
FunCall(const FunCall&) = delete;
public:
FunCall(FunCall&& other)
: Stack(std::move(other))
, func(other.func)
, method_name(other.method_name)
, val_flags(other.val_flags)
{
other.val_flags = ValueFlags::is_mutable;
}
~FunCall() noexcept(false);
template <typename... Args>
static
FunCall call_function(const AnyString& name, Args&&... args)
{
FunCall fc(false, name, count_args(std::forward<Args>(args)...));
(void)std::initializer_list<bool>{ (fc.push_arg(std::forward<Args>(args)), true)... };
return fc;
}
template <typename... Args>
static
FunCall call_method(const AnyString& name, SV* obj_arg, Args&&... args)
{
FunCall fc(true, name, 1 + count_args(std::forward<Args>(args)...));
fc.push(obj_arg);
(void)std::initializer_list<bool>{ (fc.push_arg(std::forward<Args>(args)), true)... };
return fc;
}
// single consumer: call in a scalar context
template <typename Target,
typename=std::enable_if_t<Concrete<Target>::value>>
operator Target ()
{
return PropertyValue(call_scalar_context(), ValueFlags::not_trusted);
}
operator ListResult ()
{
return ListResult(call_list_context(), *this);
}
template <typename Target>
ListResult operator>> (Target&& x)
{
return std::move(ListResult(call_list_context(), *this) >> std::forward<Target>(x));
}
protected:
FunCall(std::nullptr_t, ValueFlags val_flags_, Int reserve);
FunCall(std::nullptr_t, Int reserve)
: FunCall(nullptr, ValueFlags::allow_non_persistent | ValueFlags::allow_store_any_ref, reserve) {}
FunCall(bool is_method, ValueFlags val_flags_, const AnyString& name, Int reserve);
FunCall(bool is_method, const AnyString& name, Int reserve)
: FunCall(is_method, ValueFlags::allow_non_persistent | ValueFlags::allow_store_any_ref, name, reserve) {}
template <typename Arg>
using count_separately = typename mlist_or< is_instance_of<pure_type_t<Arg>, Unrolled>,
is_instance_of<pure_type_t<Arg>, mlist> >::type;
template <typename... Args>
static constexpr
std::enable_if_t<!mlist_or<count_separately<Args>...>::value, Int>
count_args(Args&&... args)
{
return sizeof...(Args);
}
template <typename Container, typename... MoreArgs>
static
Int count_args(Unrolled<Container>&& c, MoreArgs&&... more_args)
{
return c.container.size() + count_args(std::forward<MoreArgs>(more_args)...);
}
template <typename... Types, typename... MoreArgs>
static
Int count_args(const mlist<Types...>&, MoreArgs&&... more_args)
{
return sizeof...(Types) + count_args(std::forward<MoreArgs>(more_args)...);
}
template <typename FirstArg, typename... MoreArgs>
static
std::enable_if_t<!count_separately<FirstArg>::value && mlist_or<count_separately<MoreArgs>...>::value, Int>
count_args(FirstArg&& first_arg, MoreArgs&&... more_args)
{
return 1 + count_args(std::forward<MoreArgs>(more_args)...);
}
template <typename Arg>
std::enable_if_t<!count_separately<Arg>::value &&
!is_among<pure_type_t<Arg>, OptionSet, AnyString, FunCall, SV*>::value>
push_arg(Arg&& arg)
{
Value v(val_flags);
v.put(std::forward<Arg>(arg));
push(v.get_temp());
}
template <typename ContainerRef>
void push_arg(Unrolled<ContainerRef>&& c)
{
for (auto it=entire(c.container); !it.at_end(); ++it) {
Value v(val_flags);
v.put(*it);
push(v.get_temp());
}
}
template <typename... Types>
void push_types(const mlist<Types...>&)
{
(void)std::initializer_list<bool>{ (push_type(type_cache<pure_type_t<Types>>::get_proto()), true)... };
}
void push_type(SV* proto)
{
if (proto)
push(proto);
else
throw Undefined();
}
template <typename... Types>
void push_arg(const mlist<Types...>& t)
{
push_types(t);
if (sizeof...(Types) != 0) create_explicit_typelist(sizeof...(Types));
}
void push_current_application();
void push_current_application_pkg();
void create_explicit_typelist(size_t size);
void push_arg(FunCall&& x);
void push_arg(const OptionSet& options)
{
push(options.get());
}
void push_arg(const AnyString& s)
{
push(s);
}
void push_arg(SV* sv)
{
push(sv);
}
void check_call();
SV* call_scalar_context();
int call_list_context();
SV* func;
const char* method_name;
ValueFlags val_flags;
friend class ListResult;
};
class VarFunCall
: protected FunCall {
void operator= (const VarFunCall&) = delete;
VarFunCall(const VarFunCall&) = delete;
public:
VarFunCall(VarFunCall&&) = default;
static
VarFunCall prepare_call_function(const AnyString& name)
{
return VarFunCall(false, ValueFlags::allow_non_persistent | ValueFlags::allow_store_ref, name, 0);
}
template <typename TypeParams>
static
VarFunCall prepare_call_function(const AnyString& name, const TypeParams& explicit_type_params)
{
VarFunCall fc{prepare_call_function(name)};
if (explicit_type_params.size() != 0) {
fc.begin_type_params(explicit_type_params.size());
for (const auto& param : explicit_type_params)
fc.push_type_param(param);
fc.end_type_params();
}
return fc;
}
static
VarFunCall prepare_call_method(const AnyString& name, SV* obj_arg)
{
VarFunCall fc(true, ValueFlags::allow_non_persistent | ValueFlags::allow_store_ref, name, 1);
fc.push(obj_arg);
return fc;
}
template <typename Arg>
VarFunCall& operator<< (Arg&& arg)
{
check_push();
extend(count_args(std::forward<Arg>(arg)));
push_arg(std::forward<Arg>(arg));
return *this;
}
// seal the argument list, allowing calls
FunCall operator()()
{
// the actual call is performed in the conversion operator which depends on the context
return std::move(*this);
}
template <typename Target>
operator Target () = delete;
template <typename Target>
ListResult operator>> (Target&& x) = delete;
protected:
using FunCall::FunCall;
void begin_type_params(size_t n);
void push_type_param(const AnyString& param);
void end_type_params();
void check_push() const;
};
template <typename T, typename... Params>
class CachedObjectPointer {
CachedObjectPointer(const CachedObjectPointer&) = delete;
public:
CachedObjectPointer(CachedObjectPointer&& other)
: ptr(std::move(other.ptr))
, reset_on_destruction(other.reset_on_destruction)
{
other.reset_on_destruction = false;
}
CachedObjectPointer& operator= (const CachedObjectPointer& other)
{
ptr = other.ptr;
return *this;
}
explicit CachedObjectPointer(T* ptr_, bool reset_on_destruction_=false)
: ptr(std::make_shared<std::unique_ptr<T>>(ptr_))
, reset_on_destruction(reset_on_destruction_) {}
explicit CachedObjectPointer(const AnyString& function_name_)
: function_name(function_name_)
, ptr(std::make_shared<std::unique_ptr<T>>(nullptr))
, reset_on_destruction(false) {}
~CachedObjectPointer()
{
if (reset_on_destruction) ptr->reset(nullptr);
}
template <typename... Args>
T& get(Args&&... args)
{
if (!(*ptr)) FunCall::call_function(function_name, mlist<Params...>(), std::forward<Args>(args)...) >> *this;
return **ptr;
}
void set(T* obj) { ptr.reset(obj); }
private:
const AnyString function_name;
std::shared_ptr<std::unique_ptr<T>> ptr;
bool reset_on_destruction = false;
};
Int get_debug_level();
class PropertyTypeBuilder
: protected FunCall {
public:
using FunCall::FunCall;
template <typename... Params, bool exact_match>
static SV* build(const AnyString& pkg_name, const mlist<Params...>& params, bool_constant<exact_match>)
{
try {
PropertyTypeBuilder b(true, "typeof", 1 + sizeof...(Params));
b.push_arg(pkg_name);
b.push_types(params);
if (!exact_match) b.nonexact_match();
return b.call_scalar_context();
}
catch (const Undefined&) {
return nullptr;
}
}
private:
void nonexact_match();
};
} }
namespace polymake {
template <typename... Args>
pm::perl::FunCall call_function(const AnyString& name, Args&&... args)
{
return pm::perl::FunCall::call_function(name, std::forward<Args>(args)...);
}
inline
pm::perl::VarFunCall prepare_call_function(const AnyString& name)
{
return pm::perl::VarFunCall::prepare_call_function(name);
}
template <typename TypeParams, typename = std::enable_if_t<std::is_constructible<AnyString, typename pm::container_traits<TypeParams>::value_type>::value>>
pm::perl::VarFunCall prepare_call_function(const AnyString& name, const TypeParams& explicit_type_params)
{
return pm::perl::VarFunCall::prepare_call_function(name, explicit_type_params);
}
using pm::perl::CachedObjectPointer;
namespace perl {
using pm::perl::unroll;
}
}
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|