16 utf8line = std::wstring_convert < std::codecvt_utf8<wchar_t>>().to_bytes(wstr.c_str());
23 std::wstring wide_line;
28 wide_line = std::wstring_convert < std::codecvt_utf8<wchar_t>>().from_bytes(str.c_str());
38 if (term && term->m_ref) {
41 return std::shared_ptr<Sync>(
nullptr);
48 return std::make_shared<Sync>(ref_type);
51 return std::shared_ptr<Sync>(
nullptr);
55 const std::chrono::milliseconds & timeout_duration,
56 const std::string_view msg,
const std::source_location & loc)
57: std::runtime_error(msg.begin()), variable(const_cast<
Variable *> (&var)) {
59 if (std::holds_alternative<VariableShared> (var) && std::get<VariableShared>(var).sync) {
60 std::get<VariableShared>(var).sync->m_data = std::get<VariableShared>(var).data;
61 variable = std::get<VariableShared>(var).sync;
63 }
else if (std::holds_alternative<VariableWeak>(var) && std::get<VariableWeak>(var).sync) {
64 variable = std::get<VariableWeak>(var).sync;
66 }
else if (std::holds_alternative<VariableTaken> (var)) {
67 variable = std::get<VariableTaken>(var).variable;
71 if (std::holds_alternative<std::shared_ptr < Sync >> (
variable) && !std::get<std::shared_ptr < Sync >> (
variable)->SyncLock(edit_mode, timeout_duration)) {
73 static_cast<std::runtime_error&
> (*this) = std::runtime_error(
79 if (std::holds_alternative<VariableWeak> (var)) {
80 std::get<std::shared_ptr < Sync >> (
variable)->m_data = std::get<VariableWeak>(var).weak.lock();
81 if (!std::get<std::shared_ptr < Sync >> (
variable)->m_data) {
82 throw std::bad_weak_ptr();
90 if (std::holds_alternative<std::shared_ptr < Sync >> (
variable)) {
91 std::get<std::shared_ptr < Sync >> (
variable)->SyncUnLock();
96 const std::chrono::milliseconds & timeout_duration,
97 const std::string_view message,
const std::source_location & location) {
99 std::string
result(message.begin());
101 if (message.empty()) {
102 result +=
"VariableGuard error for ";
103 result += edit_mode ?
"write " :
"read ";
105 result += std::to_string(timeout_duration.count());
106 result +=
" milliseconds at: ";
108 result += location.file_name();
110 result += std::to_string(location.line());
112 result += std::to_string(location.column());
114 result += location.function_name();
121 result +=
"Stack trace:\n";
123 for (
const auto& entry : std::stacktrace()) {
124 result += std::to_string(counter);
127 if (entry.description().empty()) {
130 result += entry.description();
133 if (entry.source_file().empty()) {
136 result += entry.source_file();
139 result += std::to_string(entry.source_line());
167 return term && term->m_ref && !term->m_ref->m_text.empty();
174#pragma clang diagnostic push
175#pragma clang diagnostic error "-Wswitch"
182 if (!variable || std::holds_alternative<std::monostate>(*variable)) {
183 LOG_RUNTIME(
"Object not initialized (%s)!", variable ?
"monostate" :
"nullptr");
184 }
else if (std::holds_alternative<int64_t>(*variable)) {
186 }
else if (std::holds_alternative<double>(*variable)) {
188 }
else if (std::holds_alternative<std::string>(*variable)) {
190 }
else if (std::holds_alternative<std::wstring>(*variable)) {
192 }
else if (std::holds_alternative<Rational>(*variable)) {
194 }
else if (std::holds_alternative<ObjPtr>(*variable)) {
195 return std::get<ObjPtr>(*variable);
274 return !std::get<Rational>(*this).GetAsBoolean();
277 return std::get<VariableShared> (*this).data->GetValueAsBoolean();
280 return (*std::get<VariableTaken> (*
this)).GetValueAsBoolean();
283 return std::get<ObjPtr>(*this)->GetValueAsInteger();
288 return !std::get<std::string>(*this).empty();
290 return !std::get<std::wstring>(*this).empty();
304 return std::get<int64_t>(*
this);
306 return std::get<ObjPtr>(*this)->GetValueAsInteger();
308 return std::get<VariableShared> (*this).data->GetValueAsInteger();
310 return (*std::get<VariableTaken> (*
this)).GetValueAsInteger();
332 return std::get<double>(*
this);
335 return std::get<int64_t>(*
this);
338 return std::get<ObjPtr>(*this)->GetValueAsNumber();
341 return std::get<Rational>(*this).GetAsNumber();
344 return std::get<VariableShared> (*this).data->GetValueAsNumber();
347 return (*std::get<VariableTaken> (*
this)).GetValueAsNumber();
368 return std::get<std::string>(*
this);
398 return std::get<std::wstring>(*
this);
434 return *std::get<ObjPtr>(*this)->GetValueAsRational();
437 return std::get<Rational>(*
this);
440 return std::get<VariableShared> (*this).data->GetValueAsRational();
443 return (*std::get<VariableTaken> (*
this)).GetValueAsRational();
471 return std::get<ObjPtr>(*
this);
477 return std::get<VariableShared> (*this).data->GetValueAsObject();
480 return (*std::get<VariableTaken> (*
this)).GetValueAsObject();
517 return std::get<VariableShared> (*this).data->GetValueAsPointer();
520 return (*std::get<VariableTaken> (*
this)).GetValueAsPointer();
549 result.insert(0,
"Empty");
562 result.insert(0, std::get<VariableShared> (*this).data->toString());
563 result.insert(0,
"Shared: ");
567 result.insert(0, (*std::get<VariableTaken> (*
this)).toString());
568 result.insert(0,
"Guard: ");
572 result.insert(0, std::to_string(std::get<double>(*
this)));
576 result.insert(0, std::to_string(std::get<int64_t>(*
this)));
580 result.insert(0, std::get<ObjPtr> (*this)->toString());
581 result.insert(0,
"Object: ");
585 result.insert(0, std::get<Rational> (*this).GetAsString());
590 result.insert(0, std::get<std::string> (*
this));
596 result.insert(0, std::get<std::string> (*
this));
601 result.insert(0,
"link to:");
637 return Variable(std::get<ObjPtr>(var)->Copy());
669 return Variable(std::get<ObjPtr>(var)->Clone());
711 return std::get<ObjPtr>(*this)->is_scalar();
722 return std::get<ObjPtr>(*this)->is_floating();
735 return std::get<ObjPtr>(*this)->is_complex();
746 return std::get<ObjPtr>(*this)->is_rational();
758 return std::get<ObjPtr>(*this)->is_string_type();
769 if (&self == &other) {
812#define DEFINE_OPERATOR_IMPL(name) __attribute__ ((weak)) Variable & VariableOp::__ ## name ## __(Variable &self, const Variable &other)
816 if (!self.is_taked()) {
822 std::get<double>(self) += other.GetValueAsNumber();
826 std::get<int64_t>(self) += other.GetValueAsInteger();
830 *std::get<ObjPtr>(self) += other.GetValueAsObject();
834 std::get<Rational>(self) += other.GetValueAsRational();
858 std::get<std::string>(self) +=
const_cast<Variable&
> (other).GetValueAsString();
862 std::get<std::wstring>(self) +=
const_cast<Variable&
> (other).GetValueAsStringWide();
877 if (!self.is_taked()) {
883 std::get<double>(self) -= other.GetValueAsNumber();
887 std::get<int64_t>(self) -= other.GetValueAsInteger();
891 *std::get<ObjPtr>(self) -= other.GetValueAsObject();
895 std::get<Rational>(self) -= other.GetValueAsRational();
918 if (!self.is_taked()) {
924 std::get<double>(self) *= other.GetValueAsNumber();
928 std::get<int64_t>(self) *= other.GetValueAsInteger();
932 *std::get<ObjPtr>(self) *= other.GetValueAsObject();
936 std::get<Rational>(self) *= other.GetValueAsRational();
960 std::get<std::string>(self) =
repeat(std::get<std::string>(self), other.GetValueAsInteger());
964 std::get<std::wstring>(self) =
repeat(std::get<std::wstring>(self), other.GetValueAsInteger());
981 if (!self.is_taked()) {
987 std::get<double>(self) /= other.GetValueAsNumber();
991 std::get<int64_t>(self) /= other.GetValueAsInteger();
995 *std::get<ObjPtr>(self) /= other.GetValueAsObject();
999 std::get<Rational>(self) /= other.GetValueAsRational();
1030 if (!self.is_taked()) {
1036 std::get<double>(self) *= other.GetValueAsNumber();
1040 std::get<int64_t>(self) *= other.GetValueAsInteger();
1080 if (!self.is_taked()) {
1086 std::get<int64_t>(self) %= other.GetValueAsInteger();
1090 *std::get<ObjPtr>(self) %= other.GetValueAsObject();
1094 std::get<Rational>(self) %= other.GetValueAsRational();
1124#pragma clang diagnostic pop
static ObjPtr CreateString(const std::string_view str, Sync *sync=nullptr)
static std::enable_if< std::is_same< T, std::string >::value||std::is_same< T, constchar * >::value, ObjPtr >::type CreateValue(T value, Sync *sync=nullptr)
static ObjPtr CreateRational(const Rational &val)
static ObjPtr CreateNone(Sync *sync=nullptr)
int op_compare(const Rational &rational) const
static std::shared_ptr< Sync > CreateSync(const TermPtr &term)
static Variable & __imul_only_numbers__(Variable &self, const Variable &other)
static Variable & __itruediv__(Variable &self, const Variable &other)
static OperatorType ifloordiv
static VirtualFuncImpl< Variable, const Variable & > copy
static int __eq__(const Variable &self, const Variable &other)
static Variable __clone__(const Variable &clone)
static Variable & __imul__(Variable &self, const Variable &other)
static Variable & __isub__(Variable &self, const Variable &other)
static bool __strict_eq__(const Variable &self, const Variable &other)
static OperatorType itruediv
static Variable & __ifloordiv__(Variable &self, const Variable &other)
static VirtualFuncImpl< Variable, const Variable & > clone
static Variable & __iadd_only_numbers__(Variable &self, const Variable &other)
static Variable __copy__(const Variable ©)
static Variable & __imod__(Variable &self, const Variable &other)
static VirtualFuncImpl< bool, const Variable &, const Variable & > strict_eq
static VirtualFuncImpl< int, const Variable &, const Variable & > eq
static Variable & __iadd__(Variable &self, const Variable &other)
std::variant< Variable *, std::shared_ptr< Variable >, std::shared_ptr< Sync > > variable
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)
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())
bool is_floating_type() const
bool is_rational_type() const
int64_t GetValueAsInteger() const
double GetValueAsNumber() const
std::wstring & GetValueAsStringWide()
std::string & GetValueAsString()
bool is_object_type() const
bool GetValueAsBoolean() const
ObjPtr GetValueAsObject() const
void * GetValueAsPointer() const
static ObjPtr Object(Variable *variable)
bool is_complex_type() const
std::string toString() const
Rational GetValueAsRational() const
bool is_scalar_type() const
int compare(const Variable &other) const
static bool isShared(const TermPtr &term)
bool is_string_type() const
Variable Ref(const std::string_view ref) const
#define LOG_RUNTIME(format,...)
#define ASSERT(condition)
std::wstring utf8_decode(const std::string str)
std::shared_ptr< Term > TermPtr
std::shared_ptr< Obj > ObjPtr
const char * VariableCaseToString(size_t index)
constexpr RefType RefTypeFromString(const std::string_view text)
T repeat(T str, const std::size_t n)
std::string utf8_encode(const std::wstring wstr)
#define DEFINE_OPERATOR_IMPL(name)