NewLang Project
Yet another programm language
Loading...
Searching...
No Matches
variable.h
Go to the documentation of this file.
1#pragma once
2#ifndef INCLUDED_VARIABLE_H_
3#define INCLUDED_VARIABLE_H_
4
5//#include "warning_push.h"
6//#include <torch/torch.h>
7//#include <ATen/ATen.h>
8//#include "warning_pop.h"
9
10#include "types.h"
11#include "dict.h"
12#include "rational.h"
13//#include "context.h"
14
15namespace newlang {
16
17 // Convert a wide Unicode string to an UTF8 string
18
19 std::string utf8_encode(const std::wstring wstr);
20
21 // Convert an UTF8 string to a wide Unicode String
22
23 std::wstring utf8_decode(const std::string str);
24
25 class Term;
26 class Obj;
27
28 typedef std::shared_ptr<Obj> ObjPtr;
29 typedef std::shared_ptr<const Obj> ObjPtrConst;
30 typedef std::weak_ptr<Obj> ObjWeak;
31 typedef std::weak_ptr<const Obj> ObjWeakConst;
32
33 typedef std::shared_ptr<Term> TermPtr;
34 typedef std::shared_ptr<const Term> TermPtrConst;
35 // typedef std::weak_ptr<Term> TermWeak;
36
40 enum class RefType : uint8_t {
41 Value = 0, // По значению
42 Shared = 1, // Без ссылок
43 LiteSingle = 2, // Легкая ссылка для одного потока (без синхронизации) &
44 LiteThread = 3, // Легкая многопоточная ссылка (внешняя синхронизация with) &?
45 SyncMono = 4, // Объект синхронизации с монопольным мьютексом &&
46 SyncMulti = 5, // Объект синхронизации с рекурсивным мьютексом &*
47
48 SharedConst = 6,
51 SyncMonoConst = 9,
52 SyncMultiConst = 10,
53 };
54
55 inline constexpr RefType RefTypeFromString(const std::string_view text) {
56 if (text.empty()) {
57 return RefType::Value;
58 } else if (text.compare("*") == 0) {
59 return RefType::Shared;
60 } else if (text.compare("*^") == 0) {
62 } else if (text.compare("&") == 0) {
64 } else if (text.compare("&^") == 0) {
66 } else if (text.compare("&?") == 0) {
68 } else if (text.compare("&?^") == 0) {
70 } else if (text.compare("&*") == 0) {
71 return RefType::SyncMulti;
72 } else if (text.compare("&*^") == 0) {
74 } else if (text.compare("&&") == 0) {
75 return RefType::SyncMono;
76 } else if (text.compare("&&^") == 0) {
78 }
79 LOG_RUNTIME("Unknown reference type '%s'!", text.begin());
80 }
81
82 inline constexpr bool isLiteRef(RefType type) {
84 }
85
86 inline constexpr bool isLiteSyncRef(RefType type) {
87 return type == RefType::LiteThread || type == RefType::LiteThreadConst;
88 }
89
90 inline bool isHeavyRef(RefType type) {
91 return type == RefType::SyncMono || type == RefType::SyncMonoConst || type == RefType::SyncMulti || type == RefType::SyncMultiConst;
92 }
93
94 inline constexpr bool isConstRef(RefType type) {
96 }
97
98 inline constexpr bool isEditableRef(RefType type) {
99 return type == RefType::LiteSingle || type == RefType::LiteThread || type == RefType::SyncMono || type == RefType::SyncMulti;
100 }
101
102 inline constexpr bool isValidReference(RefType type, RefType test) {
103 switch (type) {
104 case RefType::Value:
105 return test == RefType::Value;
106 case RefType::Shared:
107 return test == RefType::Shared || test == RefType::SharedConst;
109 return test == RefType::LiteSingle || test == RefType::LiteSingleConst;
111 return test == RefType::LiteSingleConst;
113 return test == RefType::LiteThread || test == RefType::LiteThreadConst;
115 return test == RefType::LiteThreadConst;
117 return test == RefType::SyncMono || test == RefType::SyncMonoConst;
119 return test == RefType::SyncMonoConst;
121 return test == RefType::SyncMulti || test == RefType::SyncMultiConst;
123 return test == RefType::SyncMultiConst;
124 }
125 LOG_RUNTIME("Unknown reference type code! (%d)", static_cast<int> (type));
126 }
127
128
129 class Variable;
130 struct VariablePair;
131 typedef std::shared_ptr<Variable> VariablePtr;
132
133
138 class Sync {
139 protected:
140 friend class VariableTaken;
141
143 const std::thread::id m_thread_id;
144 const std::chrono::milliseconds & m_timeout;
145
146 mutable std::variant<std::monostate, std::shared_timed_mutex, std::recursive_timed_mutex> m_sync;
147
148 std::shared_ptr<Variable> m_data;
149
150 public:
151
152 static constexpr std::chrono::milliseconds SyncTimeoutDeedlock = std::chrono::milliseconds(5000);
153 static constexpr std::chrono::seconds SyncWithoutWait = std::chrono::seconds(0);
154
155 Sync(RefType type, const std::chrono::milliseconds & timeout_duration = SyncTimeoutDeedlock) :
156 m_type(type), m_thread_id(std::this_thread::get_id()), m_timeout(timeout_duration), m_sync(std::monostate()), m_data(nullptr) {
159 ASSERT(std::holds_alternative<std::monostate>(m_sync));
160 } else if (type == RefType::SyncMono || type == RefType::SyncMonoConst) {
161 m_sync.emplace<std::shared_timed_mutex>();
162 } else if (type == RefType::SyncMulti || type == RefType::SyncMultiConst) {
163 m_sync.emplace<std::recursive_timed_mutex>();
164 } else {
165 LOG_RUNTIME("Unknown synchronization type! (%d)", static_cast<int> (type));
166 }
167 }
168
169 static std::shared_ptr<Sync> CreateSync(const TermPtr &term);
170 static std::shared_ptr<Sync> CreateSync(const std::string_view ref);
171
172 inline RefType GetRefType() const {
173 return m_type;
174 }
175
176 inline std::thread::id GetThreadId() const {
177 return m_thread_id;
178 }
179
180 inline bool SyncLock(bool edit_mode = true, const std::chrono::milliseconds & timeout_duration = Sync::SyncTimeoutDeedlock) {
182 if (m_thread_id != std::this_thread::get_id()) {
183 LOG_RUNTIME("Calling a function on another thread!");
184 }
186 ASSERT(std::holds_alternative<std::shared_timed_mutex>(m_sync));
187 if (edit_mode) {
188 return std::get<std::shared_timed_mutex>(m_sync).try_lock_for(&timeout_duration == &Sync::SyncTimeoutDeedlock ? m_timeout : timeout_duration);
189 } else {
190 return std::get<std::shared_timed_mutex>(m_sync).try_lock_shared_for(&timeout_duration == &Sync::SyncTimeoutDeedlock ? m_timeout : timeout_duration);
191 }
193 ASSERT(std::holds_alternative<std::recursive_timed_mutex>(m_sync));
194 return std::get<std::recursive_timed_mutex>(m_sync).try_lock_for(&timeout_duration == &Sync::SyncTimeoutDeedlock ? m_timeout : timeout_duration);
195 }
196 return true;
197 }
198
199 inline bool SyncUnLock() {
202 ASSERT(m_thread_id == std::this_thread::get_id());
204 ASSERT(std::holds_alternative<std::shared_timed_mutex>(m_sync));
205 std::get<std::shared_timed_mutex> (m_sync).unlock();
207 ASSERT(std::holds_alternative<std::recursive_timed_mutex>(m_sync));
208 std::get<std::recursive_timed_mutex>(m_sync).unlock();
209 }
210 return true;
211 }
212
213 virtual ~Sync() {
214 }
215 };
216
220 class VariableTaken : public std::runtime_error {
221 friend class Variable;
222
223 SCOPE(protected) :
224 std::variant<Variable *, std::shared_ptr<Variable>, std::shared_ptr<Sync>> variable;
225
226
233 explicit VariableTaken(const Variable & var, bool edit_mode,
234 const std::chrono::milliseconds & timeout_duration = Sync::SyncTimeoutDeedlock,
235 const std::string_view message = "",
236 const std::source_location & location = std::source_location::current());
237
238 static std::string MakeTimeoutMessage(const Variable & var, bool edit_mode,
239 const std::chrono::milliseconds & timeout_duration,
240 const std::string_view message, const std::source_location & location);
241
242 public:
243
244 inline operator bool() const {
245 if(std::holds_alternative<Variable *> (variable)){
246 return std::get<Variable *> (variable);
247 } else if(std::holds_alternative<std::shared_ptr<Variable>> (variable)){
248 return std::get<std::shared_ptr<Variable>>(variable).get();
249 }
250 return std::get<std::shared_ptr<Sync>>(variable).get() && std::get<std::shared_ptr<Sync>>(variable)->m_data;
251 }
252
253 inline const Variable & operator*() const {
254 if(std::holds_alternative<Variable *> (variable)){
255 ASSERT(std::get<Variable *> (variable));
256 return *std::get<Variable *> (variable);
257 } else if(std::holds_alternative<std::shared_ptr<Variable>> (variable)){
258 ASSERT(std::get<std::shared_ptr<Variable>> (variable));
259 return *std::get<std::shared_ptr<Variable>> (variable);
260 }
261 ASSERT(std::get<std::shared_ptr<Sync>>(variable));
262 ASSERT(std::get<std::shared_ptr<Sync>>(variable)->m_data);
263 return *(std::get<std::shared_ptr<Sync>>(variable))->m_data;
264 }
265
266 inline Variable & operator*() {
267 if(std::holds_alternative<Variable *> (variable)){
268 ASSERT(std::get<Variable *> (variable));
269 return *std::get<Variable *> (variable);
270 } else if(std::holds_alternative<std::shared_ptr<Variable>> (variable)){
271 ASSERT(std::get<std::shared_ptr<Variable>> (variable));
272 return *std::get<std::shared_ptr<Variable>> (variable);
273 }
274 ASSERT(std::get<std::shared_ptr<Sync>>(variable));
275 ASSERT(std::get<std::shared_ptr<Sync>>(variable)->m_data);
276 return *std::get<std::shared_ptr<Sync>>(variable)->m_data;
277 }
278
279 VariableTaken & operator=(VariableTaken const& var) noexcept {
280 this->variable = var.variable;
281 return *this;
282 }
283
285 };
286
290 template< typename R, typename... Args>
291 class VirtualFuncImpl : public std::vector<void *> {
292 public:
293
294 typedef R VirtualFuncType(Args... args);
295
297 push_back(reinterpret_cast<void *> (func));
298 }
299
300 VirtualFuncImpl(std::initializer_list<VirtualFuncType *> list) {
301 for (auto func : list) {
302 push_back(reinterpret_cast<void *> (func));
303 }
304 }
305
306 inline R operator()(Args... args) {
307 ASSERT(!empty());
308 return (*reinterpret_cast<VirtualFuncType *> (back()))(args...);
309 }
310
312 VirtualFuncType * result = reinterpret_cast<VirtualFuncType *> (back());
313 push_back(reinterpret_cast<void *> (func));
314 return result;
315 }
316
318 for (auto it = rbegin(); it != rend(); it++) {
319 if (reinterpret_cast<void *> (current) == *it) {
320 ++it;
321 return reinterpret_cast<VirtualFuncType *> (*it);
322 }
323 }
324 return nullptr;
325 }
326
327 };
328
333 public:
335
337 static Variable __copy__(const Variable &copy);
338
340 static Variable __clone__(const Variable &clone);
341
343 static int __eq__(const Variable &self, const Variable &other);
344
346 static bool __strict_eq__(const Variable &self, const Variable &other);
347
348
349#define DEFINE_OPERATOR(name) static OperatorType name; \
350 static Variable& __ ## name ## __(Variable &self, const Variable &other)
351
352 /*
353 * x + y __add__(self, other) x += y __iadd__(self, other)
354 * x - y __sub__(self, other) x -= y __isub__(self, other)
355 * x * y __mul__(self, other) x *= y __imul__(self, other)
356 * x / y __truediv__(self, other) x /= y __itruediv__(self, other)
357 * x // y __floordiv__(self, other) x //= y __ifloordiv__(self, other)
358 * x % y __mod__(self, other) x %= y __imod__(self, other)
359 */
360
367
368#undef DEFINE_OPERATOR
369 static Variable& __iadd_only_numbers__(Variable &self, const Variable &other);
370 static Variable& __imul_only_numbers__(Variable &self, const Variable &other);
371
372 };
373
374
379 std::shared_ptr<Variable> data; // Сильный указатель на данные (владеющая ссылка)
380 std::shared_ptr<Sync> sync;
381 };
386 std::weak_ptr<Variable> weak;
387 std::shared_ptr<Sync> sync;
388 };
389
393 typedef std::variant< std::monostate, // Не инициализированная переменная
394 VariableShared, // Ссылочная или не захваченная защищенная переменная (владеющая ссылка)
395 VariableWeak, // Ссылочная переменная (слабая ссылка)
396 VariableTaken, // Захваченная защищенная или ссылочная переменная
397 // Далее идут варинаты данных
398 ObjPtr, // Универсальный объект
399 int64_t, double, std::string, std::wstring, Rational //, void * // Оптимизация для типизированных переменных
401
402
403#define VARIABLE_TYPES(_) \
404 _(EMPTY, 0) \
405 _(SHARED, 1) \
406 _(WEAK, 2) \
407 _(TAKEN, 3) \
408 \
409 _(OBJECT, 4) \
410 _(INTEGER, 5) \
411 _(DOUBLE, 6) \
412 _(STRING, 7) \
413 _(WSTRING, 8) \
414 _(RATIONAL, 9)
415 // _(POINTER, 10)
416
417 enum class VariableCase : uint8_t {
418#define DEFINE_ENUM(name, value) name = static_cast<uint8_t>(value),
420#undef DEFINE_ENUM
421 };
422
423#define MAKE_TYPE_NAME(type_name) type_name
424
425 inline const char * VariableCaseToString(size_t index) {
426#define DEFINE_CASE(name, _) \
427 case VariableCase:: name : \
428 return MAKE_TYPE_NAME(":" #name);
429
430 switch (static_cast<VariableCase> (index)) {
432
433 default:
434 LOG_RUNTIME("UNKNOWN VariableVariant index %d", static_cast<int> (index));
435 }
436#undef DEFINE_CASE
437#undef MAKE_TYPE_NAME
438 }
439
440 static_assert(static_cast<size_t> (VariableCase::EMPTY) == 0);
441 static_assert(static_cast<size_t> (VariableCase::SHARED) == 1);
442 static_assert(static_cast<size_t> (VariableCase::WEAK) == 2);
443 static_assert(static_cast<size_t> (VariableCase::TAKEN) == 3);
444 static_assert(std::is_same_v<std::monostate, std::variant_alternative_t < static_cast<size_t> (VariableCase::EMPTY), VariableVariant>>);
445 static_assert(std::is_same_v<VariableShared, std::variant_alternative_t < static_cast<size_t> (VariableCase::SHARED), VariableVariant>>);
446 static_assert(std::is_same_v<VariableWeak, std::variant_alternative_t < static_cast<size_t> (VariableCase::WEAK), VariableVariant>>);
447 static_assert(std::is_same_v<VariableTaken, std::variant_alternative_t < static_cast<size_t> (VariableCase::TAKEN), VariableVariant>>);
448
449 static_assert(std::is_same_v<ObjPtr, std::variant_alternative_t < static_cast<size_t> (VariableCase::OBJECT), VariableVariant>>);
450 static_assert(std::is_same_v<int64_t, std::variant_alternative_t < static_cast<size_t> (VariableCase::INTEGER), VariableVariant>>);
451 static_assert(std::is_same_v<double, std::variant_alternative_t < static_cast<size_t> (VariableCase::DOUBLE), VariableVariant>>);
452 static_assert(std::is_same_v<std::string, std::variant_alternative_t < static_cast<size_t> (VariableCase::STRING), VariableVariant>>);
453 static_assert(std::is_same_v<std::wstring, std::variant_alternative_t < static_cast<size_t> (VariableCase::WSTRING), VariableVariant>>);
454 static_assert(std::is_same_v<Rational, std::variant_alternative_t < static_cast<size_t> (VariableCase::RATIONAL), VariableVariant>>);
455 // static_assert(std::is_same_v<void *, std::variant_alternative_t < static_cast<size_t> (VariableCase::POINTER), VariableVariant>>);
456
457 template<typename T> struct is_shared_ptr : std::false_type {
458 };
459 template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type
460 {
461 };
462
478 class Variable : public VariableVariant {
479 friend class GlobalObjects;
480
481 SCOPE(protected) :
482 // mutable std::shared_ptr<Sync> sync; ///< Объект синхронизации доступа к защищённой переменной
483
484 // Variable(const Variable& var) : VariableVariant(static_cast<VariableVariant> (var)), sync(var.sync) {
485 // ASSERT(!sync); // Block copy guard variable
486 // }
487
488 // explicit Variable(VariableVariant val, std::shared_ptr<Sync> s) : VariableVariant(val), sync(s) {
489 // }
490
491 // Variable& operator=(const Variable& var) noexcept {
492 // static_cast<VariableVariant> (*this) = static_cast<VariableVariant> (var);
493 // sync = var.sync;
494 // ASSERT(!sync); // Block copy guard variable
495 // return *this;
496 // }
497 //
498 // Variable& operator=(Variable& var) noexcept {
499 // static_cast<VariableVariant> (*this) = static_cast<VariableVariant> (var);
500 // sync = std::move(var.sync);
501 // ASSERT(!sync); // Block copy guard variable
502 // return *this;
503 // }
504
505 static bool isShared(const TermPtr &term);
506 // static std::unique_ptr<Sync> MakeSync(const TermPtr &term);
507 // static std::unique_ptr<Sync> MakeSync(const std::string_view ref);
508
509 inline Variable copy() {
510 return VariableOp::copy(*this);
511 }
512
514 return std::make_shared<Variable>(std::monostate());
515 }
516 // explicit Variable(const VarWeak &ref) : sync(nullptr) {
517 // }
518 //
519 // explicit Variable(const VarPtr &ptr) : sync(nullptr) {
520 // }
521 //
522 // explicit Variable(std::unique_ptr<VarGuard> taken) : sync(nullptr) {
523 // }
524
525 public:
526
527 // template <class T>
528 // typename std::enable_if<is_shared_ptr<decltype(std::declval<T>().value)>::value, void>::type
529 // func(T t) {
530 // std::cout << "shared ptr" << std::endl;
531 // }
532 //
533 // template <class T>
534 // typename std::enable_if<!is_shared_ptr<decltype(std::declval<T>().value)>::value, void>::type
535 // func(T t) {
536 // std::cout << "non shared" << std::endl;
537 // }
538
539 explicit Variable(ObjPtr obj) : VariableVariant(obj) {
540 }
541
546 template <class T, typename = typename std::enable_if<!is_shared_ptr<decltype(std::declval<T>())>::value>::type>
547 Variable(T value) : VariableVariant(value) {
548 }
549
550 template <typename T, typename = typename std::enable_if<!is_shared_ptr<decltype(std::declval<T>())>::value>::type>
551 Variable(T value, const TermPtr && term) :
552 VariableVariant(isShared(term) ? static_cast<VariableVariant> (VariableShared(std::make_shared<Variable>(value), Sync::CreateSync(term))) : value) {
553 }
554
555 template <typename T, typename = typename std::enable_if<!is_shared_ptr<decltype(std::declval<T>())>::value>::type>
556 Variable(T value, const std::string_view ref) :
557 VariableVariant(ref.empty() ? value : static_cast<VariableVariant> (VariableShared(std::make_shared<Variable>(value), Sync::CreateSync(ref)))) {
558 }
559
560
561 // Variable(const Variable &&value);
562 //
563 // template <typename T, typename = typename std::enable_if<!is_shared_ptr<decltype(std::declval<T>())>::value>::type>
564 // Variable(T shared, std::unique_ptr<Sync> s) : VariableVariant(std::make_shared<T>(value)), sync(std::move(s)) {
565 // }
566
567
568 // template <typename D, typename T> static VarPtr Create(D d, T ref = nullptr) {
569 // return std::make_shared<VarData>(d, ref);
570 // }
571 //
572
573 // template <typename D> static VariableType CreatePair(TermPtr term, D d) {
574 // return std::make_shared<VariablePair>(term, Variable(d));
575 // }
576
577 static ObjPtr Object(Variable * variable);
578 static ObjPtr Object(VariablePair * pair);
579 //
580 // static Variable Undefined() {
581 // return Variable(std::monostate());
582 // }
583 //
584 // static VarPtr UndefinedPtr() {
585 // return std::make_shared<Variable>(std::monostate());
586 // }
587 // // static VarWeak GetWeak(const VarData & data){
588 // // return std::weak_ptr<VarData>(data.owner);
589 // // }
590
591 inline bool is_undefined() const {
592 return std::holds_alternative<std::monostate>(*this);
593 }
594
595 inline bool is_taked() const {
596 STATIC_ASSERT(static_cast<size_t> (VariableCase::TAKEN) == 3);
597 return this->index() >= static_cast<size_t> (VariableCase::TAKEN) ||
598 (std::holds_alternative<VariableShared> (*this) && !std::get<VariableShared>(*this).sync);
599 }
600
601 inline bool is_shared() const {
602 return std::holds_alternative<VariableShared> (*this) ||
603 std::holds_alternative<VariableTaken> (*this) ||
604 std::holds_alternative<VariableWeak> (*this);
605 }
606
607
608 Variable Ref(const std::string_view ref) const;
609
610 inline Variable Take(bool edit_mode = false, const std::chrono::milliseconds & timeout_duration = Sync::SyncTimeoutDeedlock,
611 const std::string_view message = "", const std::source_location location = std::source_location::current()) const {
612 return VariableTaken(*this, edit_mode, timeout_duration, message, location);
613 }
614
615 inline const Variable & operator*() const {
616 if (is_taked()) {
617 return *this;
618 }
619 return *Take();
620 }
621
622
623 bool GetValueAsBoolean() const;
624 int64_t GetValueAsInteger() const;
625 double GetValueAsNumber() const;
626 std::string & GetValueAsString();
627 std::wstring & GetValueAsStringWide();
628
630
631 ObjPtr GetValueAsObject() const;
632 void * GetValueAsPointer() const;
633 std::string toString() const;
634
635 /*
636 * Cast operators
637 *
638 */
639 explicit inline operator bool() const {
640 return GetValueAsBoolean();
641 }
642
643#define PLACE_RANGE_CHECK_VALUE(itype, utype)\
644 explicit inline operator itype() const { \
645 int64_t result = GetValueAsInteger(); \
646 if (result > std::numeric_limits<itype>::max() || result < std::numeric_limits<itype>::lowest()) { \
647 LOG_RUNTIME("Value '%s' is out of range of the casting type %s!", toString().c_str(), #itype); \
648 } \
649 return result; \
650 } \
651 explicit inline operator utype() const { \
652 int64_t result = GetValueAsInteger(); \
653 if (result > std::numeric_limits<utype>::max() || result < 0) { \
654 LOG_RUNTIME("Value '%s' is out of range of the casting type %s!", toString().c_str(), #utype); \
655 } \
656 return result; \
657 }
658
659 PLACE_RANGE_CHECK_VALUE(int8_t, uint8_t);
660 PLACE_RANGE_CHECK_VALUE(int16_t, uint16_t);
661 PLACE_RANGE_CHECK_VALUE(int32_t, uint32_t);
662
663 explicit inline operator int64_t() const {
664 return GetValueAsInteger();
665 }
666
667 explicit inline operator uint64_t() const {
668 int64_t result = GetValueAsInteger();
669 if (result < 0) {
670 LOG_RUNTIME("Value '%s' is out of range of the casting type %s!", toString().c_str(), "uint64_t");
671 }
672 return result;
673 }
674
675
676#undef PLACE_RANGE_CHECK_VALUE
677
678 // explicit inline operator float() const {
679 // double result = GetValueAsNumber();
680 // if (result > (double) std::numeric_limits<float>::max()) {
681 // LOG_RUNTIME("Value1 '%s' %.20f %.20f %.20f is out of range of the casting type float!", toString().c_str(), result, std::numeric_limits<float>::max(), std::numeric_limits<float>::lowest());
682 // }
683 //
684 // // __asm__ volatile ( "; //SOURCE: __FLT_MAX__ ");
685 // if (result < -__FLT_MAX__) {//(double) std::numeric_limits<float>::lowest()) {
686 // LOG_RUNTIME("Value2 '%s' %.20f %.20f %.20f is out of range of the casting type float!", toString().c_str(), result, std::numeric_limits<float>::max(), std::numeric_limits<float>::lowest());
687 // }
688 // LOG_DEBUG("operator float() '%s' %.20f", toString().c_str(), result);
689 // return result;
690 // }
691
692 // explicit inline operator double() const {
693 // return GetValueAsNumber();
694 // }
695
696 explicit inline operator std::string() const {
697 return toString();
698 }
699
700 explicit inline operator std::wstring() const {
702 }
703
704 explicit operator void *() const {
705 return GetValueAsPointer();
706 }
707
708 explicit inline operator Rational() const {
709 return GetValueAsRational();
710 }
711
712
713 // void set(const ObjPtr & value, bool edit_mode = false, const std::chrono::milliseconds & timeout_duration = Sync::SyncTimeoutDeedlock);
714 //
715 // template <typename T>
716 // typename std::enable_if<!std::is_same<T, ObjPtr>::value, void>::type
717 // set(const T & new_value, bool edit_mode = false, const std::chrono::milliseconds & timeout_duration = Sync::SyncTimeoutDeedlock);
718
719 inline Variable & operator+=(const Variable &other) {
720 return VariableOp::iadd(*this, other);
721 }
722
723 inline const Variable operator+(const Variable &other) {
725 result += other;
726 return result;
727 }
728
729 inline Variable & operator-=(const Variable &other) {
730 return VariableOp::isub(*this, other);
731 }
732
733 inline const Variable operator-(const Variable &other) {
735 result += other;
736 return result;
737 }
738
739 inline Variable & operator*=(const Variable &other) {
740 return VariableOp::imul(*this, other);
741 }
742
743 inline const Variable operator*(const Variable &other) {
745 result += other;
746 return result;
747 }
748
749 inline Variable & operator/=(const Variable &other) {
750 return VariableOp::itruediv(*this, other);
751 }
752
753 inline const Variable operator/(const Variable &other) {
755 result += other;
756 return result;
757 }
758
759 inline Variable & operator%=(const Variable &other) {
760 return VariableOp::imod(*this, other);
761 }
762
763 inline const Variable operator%(const Variable &other) {
765 result += other;
766 return result;
767 }
768
769 /*
770 *
771 *
772 */
773
774 bool is_object_type() const;
775 bool is_scalar_type() const;
776 bool is_floating_type() const;
777 bool is_complex_type() const;
778 bool is_rational_type() const;
779 bool is_string_type() const;
780
781 /*
782 *
783 */
784 inline bool operator==(const Variable & other) const {
785 return VariableOp::eq(*this, other) == 0;
786 }
787
788 inline bool operator<=(const Variable & other) const {
789
790 return VariableOp::eq(*this, other) <= 0;
791 }
792
793 inline bool operator<(const Variable &other) const {
794
795 return VariableOp::eq(*this, other) < 0;
796 }
797
798 inline bool operator>=(const Variable &other) const {
799
800 return VariableOp::eq(*this, other) >= 0;
801 }
802
803 inline bool operator>(const Variable &other) const {
804
805 return VariableOp::eq(*this, other) > 0;
806 }
807
808 inline bool operator!=(const Variable &other) const {
809
810 return VariableOp::eq(*this, other) != 0;
811 }
812
813 inline int compare(const Variable &other) const {
814
815 return VariableOp::eq(*this, other);
816 }
817
818 inline bool strict_eq(const Variable &other) const {
819 return VariableOp::strict_eq(*this, other);
820 }
821 };
822
823} // namespace newlang
824
825
826#endif // INCLUDED_VARIABLE_H_
static constexpr std::chrono::seconds SyncWithoutWait
Definition variable.h:153
const std::thread::id m_thread_id
Definition variable.h:143
const RefType m_type
Definition variable.h:142
const std::chrono::milliseconds & m_timeout
Definition variable.h:144
static std::shared_ptr< Sync > CreateSync(const TermPtr &term)
Definition variable.cpp:37
virtual ~Sync()
Definition variable.h:213
std::thread::id GetThreadId() const
Definition variable.h:176
Sync(RefType type, const std::chrono::milliseconds &timeout_duration=SyncTimeoutDeedlock)
Definition variable.h:155
RefType GetRefType() const
Definition variable.h:172
static constexpr std::chrono::milliseconds SyncTimeoutDeedlock
Definition variable.h:152
bool SyncLock(bool edit_mode=true, const std::chrono::milliseconds &timeout_duration=Sync::SyncTimeoutDeedlock)
Definition variable.h:180
std::variant< std::monostate, std::shared_timed_mutex, std::recursive_timed_mutex > m_sync
Definition variable.h:146
std::shared_ptr< Variable > m_data
Поле данных для использования в классе VariableTaken, чтобы уменьшить размер класса Variable.
Definition variable.h:148
bool SyncUnLock()
Definition variable.h:199
static Variable & __imul_only_numbers__(Variable &self, const Variable &other)
static OperatorType ifloordiv
Definition variable.h:365
static VirtualFuncImpl< Variable, const Variable & > copy
Definition variable.h:336
static int __eq__(const Variable &self, const Variable &other)
Definition variable.cpp:768
static Variable __clone__(const Variable &clone)
Definition variable.cpp:660
static bool __strict_eq__(const Variable &self, const Variable &other)
Definition variable.cpp:798
static OperatorType itruediv
Definition variable.h:364
static VirtualFuncImpl< Variable, const Variable & > clone
Definition variable.h:339
static OperatorType iadd
Definition variable.h:361
static Variable & __iadd_only_numbers__(Variable &self, const Variable &other)
static Variable __copy__(const Variable &copy)
Definition variable.cpp:628
static OperatorType isub
Definition variable.h:362
static VirtualFuncImpl< bool, const Variable &, const Variable & > strict_eq
Definition variable.h:345
VirtualFuncImpl< Variable &, Variable &, const Variable & > OperatorType
Definition variable.h:334
static OperatorType imul
Definition variable.h:363
static VirtualFuncImpl< int, const Variable &, const Variable & > eq
Definition variable.h:342
static OperatorType imod
Definition variable.h:366
std::variant< Variable *, std::shared_ptr< Variable >, std::shared_ptr< Sync > > variable
Definition variable.h:224
static std::string MakeTimeoutMessage(const Variable &var, bool edit_mode, const std::chrono::milliseconds &timeout_duration, const std::string_view message, const std::source_location &location)
Definition variable.cpp:95
VariableTaken(const Variable &var, bool edit_mode, const std::chrono::milliseconds &timeout_duration=Sync::SyncTimeoutDeedlock, const std::string_view message="", const std::source_location &location=std::source_location::current())
Definition variable.cpp:54
Variable & operator*()
Definition variable.h:266
const Variable & operator*() const
Definition variable.h:253
VariableTaken & operator=(VariableTaken const &var) noexcept
Definition variable.h:279
bool operator<(const Variable &other) const
Definition variable.h:793
Variable copy()
Definition variable.h:509
bool is_floating_type() const
Definition variable.cpp:717
Variable & operator*=(const Variable &other)
Definition variable.h:739
bool is_rational_type() const
Definition variable.cpp:741
bool operator>=(const Variable &other) const
Definition variable.h:798
bool is_shared() const
Definition variable.h:601
const Variable operator-(const Variable &other)
Definition variable.h:733
Variable(T value, const TermPtr &&term)
Definition variable.h:551
const Variable operator/(const Variable &other)
Definition variable.h:753
int64_t GetValueAsInteger() const
Definition variable.cpp:298
Variable & operator%=(const Variable &other)
Definition variable.h:759
Variable & operator/=(const Variable &other)
Definition variable.h:749
Variable & operator+=(const Variable &other)
Definition variable.h:719
double GetValueAsNumber() const
Definition variable.cpp:325
std::wstring & GetValueAsStringWide()
Definition variable.cpp:390
bool operator>(const Variable &other) const
Definition variable.h:803
std::string & GetValueAsString()
Definition variable.cpp:360
const Variable operator%(const Variable &other)
Definition variable.h:763
static VariablePtr UndefinedPtr()
Definition variable.h:513
Variable(T value, const std::string_view ref)
Definition variable.h:556
bool is_object_type() const
Definition variable.cpp:695
bool operator==(const Variable &other) const
Definition variable.h:784
bool GetValueAsBoolean() const
Definition variable.cpp:265
Variable(ObjPtr obj)
Definition variable.h:539
Variable Take(bool edit_mode=false, const std::chrono::milliseconds &timeout_duration=Sync::SyncTimeoutDeedlock, const std::string_view message="", const std::source_location location=std::source_location::current()) const
Definition variable.h:610
ObjPtr GetValueAsObject() const
Definition variable.cpp:458
void * GetValueAsPointer() const
Definition variable.cpp:507
const Variable operator+(const Variable &other)
Definition variable.h:723
bool is_undefined() const
Definition variable.h:591
bool operator<=(const Variable &other) const
Definition variable.h:788
const Variable & operator*() const
Definition variable.h:615
const Variable operator*(const Variable &other)
Definition variable.h:743
bool is_complex_type() const
Definition variable.cpp:728
std::string toString() const
Definition variable.cpp:543
Rational GetValueAsRational() const
Definition variable.cpp:421
bool is_taked() const
Definition variable.h:595
bool is_scalar_type() const
Definition variable.cpp:704
int compare(const Variable &other) const
Definition variable.h:813
bool strict_eq(const Variable &other) const
Definition variable.h:818
Variable & operator-=(const Variable &other)
Definition variable.h:729
Variable(T value)
Definition variable.h:547
static bool isShared(const TermPtr &term)
Definition variable.cpp:166
bool is_string_type() const
Definition variable.cpp:752
Variable Ref(const std::string_view ref) const
Definition variable.cpp:200
bool operator!=(const Variable &other) const
Definition variable.h:808
R operator()(Args... args)
Definition variable.h:306
VirtualFuncType * parent(VirtualFuncType *current)
Definition variable.h:317
R VirtualFuncType(Args... args)
Definition variable.h:294
VirtualFuncImpl(std::initializer_list< VirtualFuncType * > list)
Definition variable.h:300
VirtualFuncImpl(VirtualFuncType *func)
Definition variable.h:296
#define DEFINE_CASE(name)
int result
Definition lexer.l:367
#define LOG_RUNTIME(format,...)
Definition logger.h:26
#define SCOPE(scope)
Definition logger.h:38
#define STATIC_ASSERT(expr)
Definition logger.h:55
#define ASSERT(condition)
Definition logger.h:60
Definition nlc.h:59
std::weak_ptr< Obj > ObjWeak
Definition variable.h:30
std::wstring utf8_decode(const std::string str)
Definition variable.cpp:22
constexpr bool isValidReference(RefType type, RefType test)
Definition variable.h:102
constexpr bool isLiteRef(RefType type)
Definition variable.h:82
constexpr bool isEditableRef(RefType type)
Definition variable.h:98
std::weak_ptr< const Obj > ObjWeakConst
Definition variable.h:31
std::variant< std::monostate, VariableShared, VariableWeak, VariableTaken, ObjPtr, int64_t, double, std::string, std::wstring, Rational > VariableVariant
Definition variable.h:400
std::shared_ptr< Variable > VariablePtr
Definition variable.h:131
std::shared_ptr< Term > TermPtr
Definition variable.h:33
std::shared_ptr< Obj > ObjPtr
Definition variable.h:28
const char * VariableCaseToString(size_t index)
Definition variable.h:425
constexpr RefType RefTypeFromString(const std::string_view text)
Definition variable.h:55
std::shared_ptr< const Term > TermPtrConst
Definition variable.h:34
std::shared_ptr< const Obj > ObjPtrConst
Definition variable.h:29
constexpr bool isLiteSyncRef(RefType type)
Definition variable.h:86
std::string utf8_encode(const std::wstring wstr)
Definition variable.cpp:10
constexpr bool isConstRef(RefType type)
Definition variable.h:94
bool isHeavyRef(RefType type)
Definition variable.h:90
#define PLACE_RANGE_CHECK_VALUE(itype, utype)
Definition object.h:858
std::shared_ptr< Variable > data
Definition variable.h:379
std::shared_ptr< Sync > sync
Объект синхронизации доступа к защищённой переменной
Definition variable.h:380
std::shared_ptr< Sync > sync
Объект синхронизации доступа к ссылке
Definition variable.h:387
std::weak_ptr< Variable > weak
Слабый указатель на данные (не владеющая ссылка)
Definition variable.h:386
#define DEFINE_OPERATOR(name)
Definition variable.h:349
#define VARIABLE_TYPES(_)
Definition variable.h:403