2#ifndef INCLUDED_OBJECT_H_
3#define INCLUDED_OBJECT_H_
12typedef at::indexing::TensorIndex
Index;
160 template <
typename T>
161 class Iterator :
public std::iterator<std::input_iterator_tag, T> {
167 friend class Dict<
Context, std::weak_ptr<Context>>;
170 No =
static_cast<int8_t
> (ObjType::None),
171 Yes =
static_cast<int8_t
> (ObjType::Iterator),
172 End =
static_cast<int8_t
> (ObjType::IteratorEnd),
196 explicit Iterator(std::shared_ptr<T> obj,
const char * find_key =
"(.|\\n)*") :
229 const char * str_filter =
reinterpret_cast<const char *
> (filter);
231 if (iter && str_filter) {
240 }
catch (
const std::regex_error &err) {
241 LOG_RUNTIME(
"Regular expression for '%s' error '%s'!", str_filter, err.what());
249 }
else if (iter->
m_filter.empty()) {
347 class Obj :
public Dict<Obj>,
public std::enable_shared_from_this<Obj> {
378 typename std::enable_if<std::is_pointer<T>::value || std::is_same<std::string, T>::value,
PairType>::type
379 static Arg(T value,
const std::string
name =
"") {
384 typename std::enable_if<!std::is_same<PairType, T>::value && !std::is_pointer<T>::value && !std::is_same<std::string, T>::value,
PairType>::type
385 static Arg(T value,
const std::string
name =
"") {
391 return shared_from_this();
392 }
catch (std::bad_weak_ptr &err) {
393 LOG_RUNTIME(
"Exception thrown bad_weak_ptr! %s", err.what());
419#define TEST_CONST_() if (m_is_const) {LOG_RUNTIME("Can`t edit const value '%s'!", toString().c_str());}
420#define TEST_INIT_() if (!m_var_is_init) {LOG_RUNTIME("Object not initialized '%s'!", toString().c_str());}
627 virtual int64_t
size(int64_t ind)
const;
630 virtual bool empty()
const;
633 Obj *
const obj = (
Obj *
const)
this;
640 return obj->Dict<Obj>::at(
name);
657 if (!
find->is_string_type()) {
667 std::string temp(text);
668 std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
669 return temp.compare(
"true") == 0;
684 template <
typename... T>
687 auto list = {args...};
689 for (
auto &elem : list) {
690 arg->push_back(elem);
695 template <
typename... T>
698 auto list = {args...};
700 for (
auto &elem : list) {
731 template <
typename I>
732 typename std::enable_if < std::is_integral<I>::value && !std::is_pointer<I>::value,
const PairType &>::type
740 template <
typename N>
741 typename std::enable_if < std::is_same<N, std::string>::value || std::is_pointer<N>::value,
const PairType &>::type
758 return Dict::begin();
772 return Dict::begin();
777 Obj::const_iterator
end()
const {
814 return Dict::insert(pos, data);
819 const std::string &
name(
const int64_t
index)
const override {
823 int64_t
resize(int64_t new_size,
const Type fill,
const std::string &
name =
"")
override {
830 void erase(
const size_t from,
const size_t to)
override;
848 void clear_(
bool clear_iterator_name);
854 explicit inline operator bool()
const {
858#define PLACE_RANGE_CHECK_VALUE(itype, utype)\
859 explicit inline operator itype() const { \
860 int64_t result = GetValueAsInteger(); \
861 if (result > std::numeric_limits<itype>::max() || result < std::numeric_limits<itype>::lowest()) { \
862 LOG_RUNTIME("Value '%s' is out of range of the casting type %s!", GetValueAsString().c_str(), #itype); \
866 explicit inline operator utype() const { \
867 int64_t result = GetValueAsInteger(); \
868 if (result > std::numeric_limits<utype>::max() || result < 0) { \
869 LOG_RUNTIME("Value '%s' is out of range of the casting type %s!", GetValueAsString().c_str(), #utype); \
878 explicit inline operator int64_t()
const {
882 explicit inline operator uint64_t()
const {
891#undef PLACE_RANGE_CHECK_VALUE
893 explicit operator float()
const;
894 explicit operator double()
const;
896 explicit inline operator std::string()
const {
900 explicit inline operator std::wstring()
const {
904 explicit operator void *()
const {
905 return std::get<void *>(
m_var);
928 LOG_RUNTIME(
"Object '%s' not numeric!", obj->toString().c_str());
987 result->op_div_ceil_(value);
998 result->op_concat_(value, mode);
1155 result->op_bit_and_set(obj, strong);
1301 template <
typename T>
1311 return (*obj)[
index].second;
1316 for (
auto &elem : *
this) {
1322 if (elem.first.empty()) {
1324 str.append(elem.second->toString(
false));
1331 if (deep || !(elem.second->is_tensor_type() || elem.second->getType() ==
ObjType::Class)) {
1332 str.append(elem.first);
1334 str.append(elem.second->toString(
false));
1335 }
else if (elem.second->getType() ==
ObjType::Class && elem.second->m_is_reference) {
1337 str.append(elem.second->getName());
1340 str.append(elem.second->getName());
1342 str.append(elem.second->toString());
1351 std::string
toString(
bool deep =
true)
const;
1366 ASSERT(
"Not implemented!");
1368 LOG_RUNTIME(
"Value %s incompatible to Rational or convert not implemented!",
toString().c_str());
1377 return std::make_shared<Obj>(type,
nullptr,
nullptr, fixed,
is_init, sync);
1439 obj->m_rational = val;
1446 std::remove_copy(val.begin(), val.end(), back_inserter(str),
'_');
1455 size_t pos = str.find(
"\\");
1458 if (pos == std::string::npos) {
1459 obj->m_rational.set_(0);
1463 obj->m_rational.set_(str.substr(0, pos), str.substr(pos + 1, str.length()));
1465 if (obj->m_rational.m_denominator.isZero()) {
1466 LOG_RUNTIME(
"Denominator must be different from zero!");
1470 obj->m_var_is_init =
true;
1471 if (str.compare(obj->m_rational.GetAsString().c_str()) != 0) {
1473 LOG_RUNTIME(
"Rational value '%s' does not match source string '%s'!", obj->m_rational.GetAsString().c_str(), str.c_str());
1499 std::vector<int64_t>
result;
1500 for (
int i = 0; i <
size(); i++) {
1501 if (raise && !
at(i).second->is_integer()) {
1503 LOG_RUNTIME(
"Item does not contain an integer value! '%s'",
at(i).second->GetValueAsString().c_str());
1505 result.push_back(
at(i).second->GetValueAsInteger());
1530 result->SetValue_(value);
1535 template <
typename T1,
typename T2,
typename T3>
1545 template <
typename T1,
typename T2>
1560 template <
typename T>
1561 typename std::enable_if < std::is_same<T, std::string>::value || std::is_same<T, const char *>::value,
ObjPtr>::type
1566 template <
typename T>
1567 typename std::enable_if < std::is_same<T, std::wstring>::value || std::is_same<T, const wchar_t *>::value,
ObjPtr>::type
1572 template <
typename T>
1573 typename std::enable_if<std::is_integral<T>::value,
ObjPtr>::type
1580 result->m_var =
static_cast<int64_t
> (value);
1584 template <
typename T>
1585 typename std::enable_if<std::is_floating_point<T>::value,
ObjPtr>::type
1592 result->m_var =
static_cast<double> (value);
1598 result->m_value = std::string(str);
1604 result->m_string = std::wstring(str);
1611 result->m_var =
static_cast<int64_t
> (1);
1612 return result->MakeConst();
1617 result->m_var =
static_cast<int64_t
> (0);
1618 return result->MakeConst();
1629 template <
typename... T>
1630 typename std::enable_if<is_all<
PairType, T ...>::value,
ObjPtr>::type
1632 std::array <
PairType,
sizeof...(args) > list = {args...};
1634 for (
auto &elem : list) {
1637 result->m_var_is_init =
true;
1644 for (
size_t i = 0; i < arr.size(); i++) {
1645 result->push_back(arr[i]);
1660 result->m_var_is_init =
true;
1665 template <
typename... T>
1666 typename std::enable_if<is_all<
PairType, T ...>::value,
ObjPtr>::type
1668 std::array <
PairType,
sizeof...(args) > list = {args...};
1671 for (
auto &elem : list) {
1674 result->m_var_is_init =
true;
1695 clone->m_var_name = new_name;
1701 if (&
clone ==
this) {
1711 if (
clone.get() ==
this) {
1727 clone->toType_(type);
1764 template <
typename T>
1765 typename std::enable_if < std::is_same<T, bool>::value,
void>::type
1768 SetValue_(
static_cast<int64_t
> (value));
1771 template <
typename T>
1772 typename std::enable_if<std::is_integral<T>::value,
void>::type
1778 m_var =
static_cast<int64_t
> (value);
1783 template <
typename T>
1784 typename std::enable_if<std::is_floating_point<T>::value,
void>::type
1790 m_var =
static_cast<double> (value);
1795 template <
typename T>
1796 typename std::enable_if<std::is_same<char *, T>::value,
void>::type
1799 std::string str(text);
1803 template <
typename T>
1804 typename std::enable_if < std::is_same<wchar_t *, T>::value,
void>::type
1807 std::wstring str(text);
1861 Dict::remove(value);
1895 std::variant < std::monostate, int64_t, double,
void *,
1896 bool *, int8_t *, int16_t *, int32_t *, int64_t *,
float *,
double *,
NativeData,
std::pair< std::string, Type > PairType
ListType::iterator find(const std::string_view name)
virtual int64_t index(const std::string_view field_name)
virtual int64_t resize(int64_t new_size, const Type fill, const std::string &name="")
PairType & push_back(const PairType &p)
virtual const std::string & name(const int64_t index) const
ListType::const_iterator at_index_const(const int64_t index) const
ListType::iterator at_index(const int64_t index)
static PairType pair(const Type value, const std::string name="")
virtual PairType & at(const int64_t index)
std::list< PairType > ListType
virtual void erase(const int64_t index)
IntAny(const ObjPtr value, ObjType type)
IntMinus(const ObjPtr value)
IntPlus(const ObjPtr value)
const iterator operator++(int) const
bool operator==(const iterator &other) const
Iterator(std::shared_ptr< T > obj, CompareFuncType *func, T *arg, void *extra=nullptr)
const IterPairType & data() const
bool operator!=(const iterator &other) const
static IterCmp CompareFuncDefault(const IterPairType &pair, const T *filter, void *extra)
Iterator(const Iterator &iter)
Dict< T >::Type IterObjPtr
const IterPairType & data()
Dict< T >::ListType IterListType
const char * m_base_filter
IterCmp CompareFuncType(const IterPairType &pair, const T *args, void *extra)
Dict< T >::iterator m_found
Iterator(std::shared_ptr< T > obj, const char *find_key="(.|\\n)*")
Iterator< const T > const_iterator
const IterPairType & operator*()
std::shared_ptr< T > m_iter_obj
Dict< T >::PairType IterPairType
ObjPtr read_and_next(int64_t count)
const IterPairType & operator*() const
const iterator & operator++() const
static const IterPairType m_Iterator_end
void SetClassName(std::string &name)
ObjPtr operator/(ObjPtr obj)
std::enable_if<!is_all< Obj::PairType, T... >::value, ObjPtr >::type operator()(T ... args)
static ObjPtr No(Sync *sync=nullptr)
ObjPtr op_rrshift_set(ObjPtr obj)
ObjPtr op_concat(Obj &value, ConcatMode mode=ConcatMode::Error)
ObjPtr operator^=(ObjPtr obj)
static ObjPtr ConstructorSimpleType_(Context *ctx, Obj &args)
void SetValue_(std::wstring text)
static PairType ArgNull(const std::string name="")
ObjPtr op_concat(ObjPtr obj, ConcatMode mode=ConcatMode::Error)
ObjPtr op_pow(Obj &obj) const
bool is_string_char_type() const
Dict< Obj >::PairType & at(ObjPtr find)
ObjPtr CallNative(Context *ctx, Obj args)
ObjPtr op_set_index(ObjPtr index, ObjPtr value)
ObjPtr op_bit_and_set(Obj &obj, bool strong)
bool is_function_type() const
virtual ObjPtr IteratorReset()
const ObjPtr index_get(const std::vector< Index > &index) const
Obj::iterator find(const std::string name)
ObjPtr operator*(ObjPtr obj)
static ObjPtr CreateString(const std::wstring_view str, Sync *sync=nullptr)
ObjPtr operator-(ObjPtr obj)
bool is_simple_type() const
static ObjPtr ConstructorStub_(Context *ctx, Obj &args)
ObjPtr op_call(ObjPtr args)
static std::enable_if< std::is_floating_point< T >::value, ObjPtr >::type CreateValue(T value, ObjType fix_type=ObjType::None, Sync *sync=nullptr)
static ObjPtr CreateRange(T1 start, T2 stop, T3 step, Sync *sync=nullptr)
virtual ObjPtr IteratorMake(const char *filter=nullptr, bool check_create=true)
ObjPtr op_bit_and(Obj &obj, bool strong)
bool operator>=(ObjPtr obj)
ObjPtr operator%=(ObjPtr obj)
virtual int64_t size() const
int op_compare(Obj &value)
void ClonePropTo(Obj &clone) const
virtual bool empty() const
static ObjPtr CreateString(const std::string_view str, Sync *sync=nullptr)
static std::enable_if< std::is_pointer< T >::value||std::is_same< std::string, T >::value, PairType >::type Arg(T value, const std::string name="")
std::variant< std::monostate, int64_t, double, void *, bool *, int8_t *, int16_t *, int32_t *, int64_t *, float *, double *, NativeData, std::string, TermPtr, Iterator< Obj > > m_var
Dict< Obj >::PairType & at(const std::string name) override
static ObjPtr GetIndex(ObjPtr obj, TermPtr index_arg)
static ObjPtr ConstructorDictionary_(Context *ctx, Obj &args)
bool CallAll(const char *func_name, ObjPtr &arg_in, ObjPtr &result, ObjPtr object=nullptr, size_t limit=0)
ObjPtr toType(ObjType type) const
size_t ItemValueCount(ObjPtr &find, bool strong)
Obj(ObjType type=ObjType::None, const char *var_name=nullptr, TermPtr func_proto=nullptr, ObjType fixed=ObjType::None, bool init=false, Sync *sync=nullptr)
std::vector< ObjPtr > m_class_parents
Родительские классы (типы)
static ObjPtr CreateEmpty(Sync *sync=nullptr)
virtual ObjPtr IteratorData()
ObjPtr operator/(Obj value)
ObjPtr & operator+(ObjPtr &obj)
ObjType m_var_type_current
Текущий тип значения объекта
Rational m_rational
Содержит дробь из длинных чисел
const std::string & getName() const
ObjPtr op_assign(ObjPtr obj)
std::enable_if< std::is_same< N, std::string >::value||std::is_pointer< N >::value, constPairType & >::type operator[](const N name)
ObjPtr operator-(Obj value)
bool is_plain_type() const
static ObjPtr CreateRange(T1 start, T2 stop, Sync *sync=nullptr)
bool is_none_type() const
ObjPtr op_div_ceil_(ObjPtr obj)
std::enable_if< is_all< Obj::PairType, T... >::value, ObjPtr >::type operator()(T ... args)
std::shared_ptr< Rational > GetValueAsRational() const
static ObjPtr Yes(Sync *sync=nullptr)
bool m_var_is_init
Содержит ли объект корректное значение ???
static ObjPtr CreateDict(const std::vector< ObjPtr > &arr, Sync *sync=nullptr)
bool is_string_type() const
std::enable_if< std::is_integral< T >::value, void >::type SetValue_(T value)
double GetValueAsNumber() const
std::string GetValueAsString() const
static bool is_true(const char *text)
static ObjPtr CreatePointer(void *ptr=nullptr, Sync *sync=nullptr)
ObjPtr operator+(Obj value)
void erase(const size_t from, const size_t to) override
static PairType Arg(ObjPtr value, const std::string name="")
std::string m_func_mangle_name
ObjPtr operator+(ObjPtr obj)
bool operator>(ObjPtr obj)
std::enable_if< std::is_same< T, bool >::value, void >::type SetValue_(bool value)
static ObjPtr GetIndex(ObjPtr obj, size_t index)
ObjPtr IteratorNext(ObjPtr count)
ObjPtr index_set_(const std::vector< Index > &index, const ObjPtr value)
std::string m_var_name
Имя переменной, в которой хранится объект
ObjPtr operator%(Obj value)
ObjPtr m_dimensions
Размерности для ObjType::Type.
std::enable_if< std::is_same< char *, T >::value, void >::type SetValue_(T text)
static ObjPtr CreateBaseType(ObjType type)
bool is_type_name() const
std::string m_module_name
Obj::iterator at_index(const int64_t index)
bool GetValueAsBoolean() const
ObjPtr op_div_ceil(Obj value)
std::vector< int64_t > toIntVector(bool raise=true) const
std::shared_ptr< Iterator< Obj > > m_iterator
Итератор для данных
bool is_tensor_type() const
ObjPtr operator/=(ObjPtr obj)
ObjPtr operator*(Obj value)
bool is_arithmetic_type() const
ObjPtr op_concat_(Obj &obj, ConcatMode mode=ConcatMode::Error)
ObjPtr Clone(const char *new_name=nullptr) const
void testResultIntegralType(ObjType type, bool upscalint)
bool is_class_type() const
std::enable_if< std::is_same< wchar_t *, T >::value, void >::type SetValue_(T text)
ObjPtr op_div_ceil(ObjPtr obj)
std::string toString(bool deep=true) const
void remove(const Dict::PairType &value)
static ObjPtr CreateDict(Sync *sync=nullptr)
static ObjPtr ConstructorClass_(Context *ctx, Obj &args)
static std::enable_if< is_all< PairType, T... >::value, ObjPtr >::type CreateClass(std::string name, T ... args)
void testConvertType(ObjType type)
bool operator<=(ObjPtr obj)
ObjPtr operator+=(ObjPtr obj)
Obj::const_iterator begin() const
std::string m_class_name
Имя класса объекта (у базовых типов отсуствует)
static ObjPtr CreateBool(bool value, Sync *sync=nullptr)
static ObjPtr ConstructorStruct_(Context *ctx, Obj &args)
bool exist(ObjPtr &find, bool strong)
std::enable_if< std::is_integral< I >::value &&!std::is_pointer< I >::value, constPairType & >::type operator[](const I index)
static std::enable_if<!std::is_same< PairType, T >::value &&!std::is_pointer< T >::value &&!std::is_same< std::string, T >::value, PairType >::type Arg(T value, const std::string name="")
ObjPtr operator-=(ObjPtr obj)
void erase(const int64_t index) override
PairType & push_back(const Type value, const std::string &name="")
ObjPtr op_concat_(ObjPtr obj, ConcatMode mode=ConcatMode::Error)
std::enable_if< std::is_floating_point< T >::value, void >::type SetValue_(T value)
std::wstring GetValueAsStringWide() const
const TermPtr m_prototype
Описание прототипа функции (или данных)
void toType_(ObjType type)
static ObjPtr ConstructorEnum_(Context *ctx, Obj &args)
static ObjPtr CreateClass(std::string name)
bool op_equal(ObjPtr value)
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 ConstructorNative_(Context *ctx, Obj &args)
bool operator<(ObjPtr obj)
const std::string & name(const int64_t index) const override
int64_t resize_(int64_t size, ObjPtr fill, const std::string="")
static ObjPtr CreateRational(const Rational &val)
std::shared_ptr< torch::Tensor > m_tensor
Содержит только размерные тензоры (скляры хранятся в поле m_pointer и не создают m_tensor->defined())
static std::enable_if< is_all< PairType, T... >::value, ObjPtr >::type CreateDict(T ... args)
bool is_dictionary_type() const
static std::enable_if< std::is_same< T, std::wstring >::value||std::is_same< T, constwchar_t * >::value, ObjPtr >::type CreateValue(T value, Sync *sync=nullptr)
ObjPtr operator*=(ObjPtr obj)
std::wstring m_string
Содержит строку широких символов
Obj::const_iterator end() const
ObjPtr op_lshift_set(ObjPtr obj)
ObjPtr op_rrshift_set(Obj)
bool op_accurate(ObjPtr obj)
ObjPtr m_iter_range_value
ObjPtr index_set(const std::vector< Index > &index, const ObjPtr value) const
Obj::const_iterator insert(Obj::const_iterator pos, const PairType &data)
std::string m_value
Содержит байтовую строку или байтовый массив с данными для представления в нативном виде (Struct,...
ObjPtr operator|=(ObjPtr obj)
bool op_duck_test(ObjPtr obj, bool strong)
void SetValue_(std::string text)
bool CallOnce(ObjPtr &arg_in, ObjPtr &result, ObjPtr object=nullptr)
static bool op_duck_test_prop(Obj *base, Obj *value, bool strong)
ObjPtr op_rshift_set(ObjPtr obj)
int64_t GetValueAsInteger() const
TermPtr m_sequence
Последовательно распарсенных команд для выполнения
Dict< Obj >::PairType & at(const std::string name) const
void CloneTo(ObjPtr &clone)
ObjPtr op_pow(ObjPtr obj) const
ObjPtr op_rshift_set(Obj)
PairType & push_back(const PairType &p)
ObjType m_var_type_fixed
Максимальный размер для арифметических типов, который задается разработчиком
static ObjPtr BaseTypeConstructor(Context *ctx, Obj &in)
void SetTermProp(Term &term)
int64_t resize(int64_t new_size, const Type fill, const std::string &name="") override
Obj::const_iterator at_index_const(const int64_t index) const
static ObjPtr CreateNone(Sync *sync=nullptr)
ObjPtr op_lshift_set(Obj)
ObjPtr op_assign(Obj &obj)
ObjPtr operator%(ObjPtr obj)
static ObjPtr CreateType(ObjType type, ObjType fixed=ObjType::None, bool is_init=false, Sync *sync=nullptr)
void CloneDataTo(Obj &clone) const
static std::enable_if< std::is_integral< T >::value, ObjPtr >::type CreateValue(T value, ObjType fix_type=ObjType::None, Sync *sync=nullptr)
static ObjPtr CreateNil(Sync *sync=nullptr)
bool is_string_wide_type() const
void dump_dict_(std::string &str, bool deep=true) const
static ObjPtr CreateRational(const std::string val)
bool is_bool_type() const
ObjPtr op_bit_and(ObjPtr obj, bool strong)
bool op_class_test(ObjPtr obj, Context *ctx) const
ObjPtr op_pow_(ObjPtr obj)
bool is_other_type() const
virtual ObjPtr IteratorNext(int64_t count)
std::shared_ptr< Rational > clone() const
#define LOG_RUNTIME(format,...)
#define STATIC_ASSERT(expr)
#define ASSERT(condition)
bool isFloatingType(ObjType t)
bool isStringChar(ObjType t)
std::weak_ptr< Obj > ObjWeak
bool isTypeName(ObjType t)
std::wstring utf8_decode(const std::string str)
bool isStringWide(ObjType t)
bool isArithmeticType(ObjType t)
bool isEllipsis(ObjType t)
newlang::ObjPtr clone(newlang::Context *ctx, newlang::Obj &in)
bool isIntegralType(ObjType t, bool includeBool)
int64_t ConcatData(Obj *dest, Obj &src, ConcatMode mode=ConcatMode::Error)
bool isDictionary(ObjType t)
std::shared_ptr< Term > TermPtr
std::shared_ptr< Obj > ObjPtr
bool isPlainDataType(ObjType t)
bool isIndexingType(ObjType curr, ObjType fix)
bool isBooleanType(ObjType t)
bool isComplexType(ObjType t)
bool isSimpleType(ObjType t)
ObjPtr CheckSystemField(const Obj *obj, const std::string name)
ObjType typeFromLimit(int64_t value, ObjType type_default=ObjType::Int64)
const char * toString(TermID type)
bool isFunction(ObjType t)
std::vector< int64_t > TensorShapeFromDict(const Obj *obj)
bool canCast(const ObjType from, const ObjType to)
at::indexing::TensorIndex Index
at::IntArrayRef Dimension
#define PLACE_RANGE_CHECK_VALUE(itype, utype)
std::ostream & operator<<(std::ostream &out, newlang::Obj &var)
#define NL_CHECK(cond, format,...)