26 #include "generic_type_internal.hpp"
27 #include "casadi_misc.hpp"
28 #include "exception.hpp"
29 #include "serializing_stream.hpp"
32 #include "function.hpp"
98 return std::vector<casadi_int>();
100 return std::vector< std::vector<casadi_int> >();
102 return std::vector<bool>();
104 return std::vector<double>();
106 return std::vector< std::vector<double> >();
108 return std::vector<std::string>();
110 return std::vector<std::vector<std::string> >();
112 return std::vector<GenericType::Dict>();
114 return std::vector<std::vector< GenericType> >();
116 return std::vector<GenericType>();
118 casadi_error(
"empty_from_type. Unsupported type " +
str(type));
133 return "OT_INTVECTOR";
135 return "OT_INTVECTORVECTOR";
137 return "OT_BOOLVECTOR";
139 return "OT_DOUBLEVECTOR";
141 return "OT_DOUBLEVECTORVECTOR";
143 return "OT_STRINGVECTOR";
145 return "OT_STRINGVECTORVECTOR";
149 return "OT_DICTVECTOR";
151 return "OT_VECTORVECTOR";
155 return "OT_FUNCTION";
157 return "OT_FUNCTIONVECTOR";
272 std::vector<casadi_int> temp(iv.size());
273 std::copy(iv.begin(), iv.end(), temp.begin());
282 std::vector<bool> i_vec(b_vec.size());
283 std::copy(b_vec.begin(), b_vec.end(), i_vec.begin());
325 casadi_assert_dev(
is_int());
326 return static_cast<const IntType*
>(
get())->d_;
413 return static_cast<bool>(
to_int());
415 casadi_assert(
is_bool(),
"type mismatch");
422 return static_cast<casadi_int
>(
to_double());
424 return static_cast<casadi_int
>(
to_bool());
426 casadi_assert(
is_int(),
"type mismatch");
433 return static_cast<double>(
to_bool());
435 return static_cast<double>(
to_int());
437 casadi_assert(
is_double(),
"type mismatch");
443 casadi_assert(
is_string(),
"type mismatch");
454 return std::vector<casadi_int>(1,
as_bool() ? 1 : 0);
456 return std::vector<casadi_int>(1,
as_int());
458 return std::vector<casadi_int>(1,
static_cast<casadi_int
>(
as_double()));
464 GenericType::operator std::vector<int>()
const {
465 std::vector<int> ret;
466 std::vector<casadi_int> source = to_int_vector();
472 return std::vector<bool>(1,
as_bool());
475 casadi_assert(v==0 || v==1,
"Entry must be zero or one");
476 return std::vector<bool>(1, v==1);
479 casadi_assert(v==0.0 || v==1.0,
"Entry must be zero or one");
480 return std::vector<bool>(1, v==1.0);
486 std::vector<bool> ret(v.size());
487 for (casadi_int i=0; i<v.size(); ++i) {
488 casadi_assert(v[i]==0 || v[i]==1,
"Entries must be zero or one");
501 return std::vector<double>(1,
as_bool() ? 1.0 : 0.0);
503 return std::vector<double>(1,
static_cast<double>(
as_int()));
505 return std::vector<double>(1,
as_double());
508 return std::vector<double>(v.begin(), v.end());
518 std::vector< std::vector<double> > ret(v.size());
519 for (casadi_int i=0;i<v.size();++i)
520 ret[i].
assign(v[i].begin(), v[i].end());
531 return std::vector<std::string>(1, s);
534 casadi_assert(v.empty(),
"Cast only permitted for zero-length vectors");
538 casadi_assert(v.empty(),
"Cast only permitted for zero-length vectors");
552 casadi_assert(
is_dict(),
"type mismatch");
560 return std::vector<Dict>(1, e);
569 std::vector<GenericType> ret(v.size());
570 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
574 std::vector<GenericType> ret(v.size());
575 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
579 std::vector<GenericType> ret(v.size());
580 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
584 std::vector<GenericType> ret(v.size());
585 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
589 std::vector<GenericType> ret(v.size());
590 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
594 std::vector<GenericType> ret(v.size());
595 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
599 std::vector<GenericType> ret(v.size());
600 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
604 std::vector<GenericType> ret(v.size());
605 for (casadi_int i=0;i<v.size();++i) ret[i] =
static_cast<bool>(v[i]);
609 std::vector<GenericType> ret(v.size());
610 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
614 std::vector<GenericType> ret(v.size());
615 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
618 casadi_assert(
is_vector(),
"type mismatch");
626 std::vector<std::vector<GenericType> > ret(v.size());
627 for (casadi_int i=0;i<v.size();++i) {
628 ret[i].resize(v[i].size());
629 for (casadi_int j=0;j<v[i].size();++j) {
636 std::vector<std::vector<GenericType> > ret(v.size());
637 for (casadi_int i=0;i<v.size();++i) {
638 ret[i].resize(v[i].size());
639 for (casadi_int j=0;j<v[i].size();++j) {
646 std::vector<std::vector<GenericType> > ret(v.size());
647 for (casadi_int i=0;i<v.size();++i) {
648 ret[i].resize(v[i].size());
649 for (casadi_int j=0;j<v[i].size();++j) {
675 casadi_assert(i==0,
"Only zero pointers accepted");
681 return !(*
this != op2);
700 if (v1.size() != v2.size())
return true;
701 for (casadi_int i=0; i<v1.size(); ++i)
702 if (v1[i] != v2[i])
return true;
709 if (v1.size() != v2.size())
return true;
710 for (casadi_int i=0; i<v1.size(); ++i)
711 if (v1[i] != v2[i])
return true;
718 if (v1.size() != v2.size())
return true;
719 for (casadi_int i=0; i<v1.size(); ++i) {
720 if (v1[i].size() != v2[i].size())
return true;
721 for (casadi_int j=0; j<v1[i].size(); ++j) {
722 if (v1[i][j] != v2[i][j])
return true;
731 if (v1.size() != v2.size())
return true;
732 for (casadi_int i=0; i<v1.size(); ++i) {
733 if (v1[i].size() != v2[i].size())
return true;
734 for (casadi_int j=0; j<v1[i].size(); ++j) {
735 if (v1[i][j] != v2[i][j])
return true;
774 s.
pack(
"GenericType::type",
static_cast<int>(
getType()));
780 s.
unpack(
"GenericType::type", itype);
828 if (first.empty())
return second;
829 if (second.empty())
return first;
836 for (
auto&& e : source) {
838 auto it = target.find(e.first);
839 if (it!=target.end() && it->second.is_dict()) {
840 Dict local = it->second;
841 casadi_assert(e.second.is_dict(),
842 "update_dict error: Key '" + it->first +
"' exists in target, "
843 "but source value is not a dict");
849 target[e.first] = e.second;
856 auto it = target.find(key);
857 if (it==target.end()) {
861 casadi_assert(it->second.is_dict() && value.
is_dict(),
862 "update_dict error: Key '" + key +
"' exists in target, but values are not dicts");
863 Dict orig = it->second;
871 typedef GenericTypeInternal<OT_STRING, std::string>
StringType;
872 typedef GenericTypeInternal<OT_DOUBLE, double>
DoubleType;
873 typedef GenericTypeInternal<OT_INT, casadi_int>
IntType;
874 typedef GenericTypeInternal<OT_BOOL, bool>
BoolType;
875 typedef GenericTypeInternal<OT_DOUBLEVECTOR, std::vector<double> >
DoubleVectorType;
878 typedef GenericTypeInternal<OT_INTVECTOR, std::vector<casadi_int> >
IntVectorType;
881 typedef GenericTypeInternal<OT_STRINGVECTOR, std::vector<std::string> >
StringVectorType;
882 typedef GenericTypeInternal<OT_FUNCTION, Function>
FunctionType;
883 typedef GenericTypeInternal<OT_FUNCTIONVECTOR, std::vector<Function> >
FunctionVectorType;
884 typedef GenericTypeInternal<OT_DICT, Dict>
DictType;
885 typedef GenericTypeInternal<OT_DICTVECTOR, std::vector<Dict> >
DictVectorType;
886 typedef GenericTypeInternal<OT_VECTOR, std::vector<GenericType> >
VectorType;
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
SharedObjectInternal * get() const
Get a const pointer to the node.
void assign(SharedObjectInternal *node)
Assign the node to a node class pointer without reference counting.
bool is_null() const
Is a null pointer?
void own(SharedObjectInternal *node)
static GenericType deserialize(DeserializingStream &s)
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
const std::vector< double > & as_double_vector() const
Cast to the internal type.
std::vector< std::vector< casadi_int > > to_int_vector_vector() const
Convert to a type.
bool is_string() const
Check if a particular type.
bool operator==(const GenericType &op2) const
Equality.
std::map< std::string, GenericType > Dict
C++ equivalent of Python's dict or MATLAB's struct.
const std::vector< Dict > & as_dict_vector() const
Cast to the internal type.
const casadi_int & as_int() const
Cast to the internal type.
static GenericType deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
static std::string get_type_description(TypeID type)
Get a description of a type.
std::vector< std::vector< GenericType > > to_vector_vector() const
Convert to a type.
const double & as_double() const
Cast to the internal type.
void serialize(SerializingStream &s) const
Serialize an object.
std::string to_string() const
Convert to a type.
bool is_void_pointer() const
Check if a particular type.
std::vector< Function > to_function_vector() const
Convert to a type.
const std::vector< std::string > & as_string_vector() const
Cast to the internal type.
bool is_string_vector_vector() const
Check if a particular type.
const std::vector< std::vector< std::string > > & as_string_vector_vector() const
Cast to the internal type.
const std::vector< std::vector< casadi_int > > & as_int_vector_vector() const
Cast to the internal type.
bool is_bool() const
Check if a particular type.
std::vector< std::vector< double > > to_double_vector_vector() const
Convert to a type.
const bool & as_bool() const
Cast to the internal type.
bool is_empty_vector() const
Check if a particular type.
void * to_void_pointer() const
Convert to a type.
Dict to_dict() const
Convert to a type.
std::vector< double > to_double_vector() const
Convert to a type.
bool is_int_vector() const
Check if a particular type.
const std::vector< GenericType > & as_vector() const
Cast to the internal type.
Function to_function() const
Convert to a type.
std::vector< casadi_int > to_int_vector() const
Convert to a type.
std::vector< GenericType > to_vector() const
Convert to a type.
static GenericType create(SharedObjectInternal *node)
Create from node.
bool is_dict_vector() const
Check if a particular type.
const std::vector< std::vector< double > > & as_double_vector_vector() const
Cast to the internal type.
std::vector< int > to_int_type_vector() const
Convert to a type.
bool is_string_vector() const
Check if a particular type.
bool is_vector_vector() const
Check if a particular type.
bool is_function_vector() const
Check if a particular type.
bool operator!=(const GenericType &op2) const
double to_double() const
Convert to a type.
bool is_bool_vector() const
Check if a particular type.
bool is_double() const
Check if a particular type.
bool is_function() const
Check if a particular type.
const std::vector< std::vector< GenericType > > & as_vector_vector() const
Cast to the internal type.
bool is_int_vector_vector() const
Check if a particular type.
casadi_int to_int() const
Convert to a type.
bool is_dict() const
Check if a particular type.
const Function & as_function() const
Cast to the internal type.
std::vector< std::string > to_string_vector() const
Convert to a type.
bool is_double_vector_vector() const
Check if a particular type.
void *const & as_void_pointer() const
Cast to the internal type.
std::vector< Dict > to_dict_vector() const
Convert to a type.
GenericType()
Default constructor.
bool to_bool() const
Convert to a type.
bool is_vector() const
Check if a particular type.
bool is_double_vector() const
Check if a particular type.
bool can_cast_to(TypeID other) const
std::vector< std::vector< std::string > > to_string_vector_vector() const
Convert to a type.
std::vector< bool > to_bool_vector() const
Convert to a type.
const std::string & as_string() const
Cast to the internal type.
bool is_int() const
Check if a particular type.
static GenericType from_type(TypeID type)
Construct a GenericType given an TypeID.
const std::vector< Function > & as_function_vector() const
Cast to the internal type.
const Dict & as_dict() const
Cast to the internal type.
const std::vector< casadi_int > & as_int_vector() const
Cast to the internal type.
const std::vector< bool > & as_bool_vector() const
Cast to the internal type.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
GenericTypeInternal< OT_VOIDPTR, void * > VoidPointerType
GenericTypeInternal< OT_INT, casadi_int > IntType
GenericTypeInternal< OT_FUNCTION, Function > FunctionType
Dict combine(const Dict &first, const Dict &second, bool recurse)
Combine two dicts. First has priority.
int to_int(casadi_int rhs)
GenericTypeInternal< OT_INTVECTOR, std::vector< casadi_int > > IntVectorType
GenericTypeInternal< OT_DOUBLE, double > DoubleType
GenericTypeInternal< OT_STRING, std::string > StringType
GenericTypeInternal< OT_DICTVECTOR, std::vector< Dict > > DictVectorType
GenericTypeInternal< OT_DOUBLEVECTORVECTOR, std::vector< std::vector< double > > > DoubleVectorVectorType
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
GenericTypeInternal< OT_INTVECTORVECTOR, std::vector< std::vector< casadi_int > > > IntVectorVectorType
void update_dict(Dict &target, const Dict &source, bool recurse)
Update the target dictionary in place with source elements.
GenericTypeInternal< OT_DOUBLEVECTOR, std::vector< double > > DoubleVectorType
GenericTypeInternal< OT_STRINGVECTOR, std::vector< std::string > > StringVectorType
GenericTypeInternal< OT_BOOL, bool > BoolType
GenericTypeInternal< OT_VECTOR, std::vector< GenericType > > VectorType
GenericTypeInternal< OT_BOOLVECTOR, std::vector< bool > > BoolVectorType
GenericTypeInternal< OT_FUNCTIONVECTOR, std::vector< Function > > FunctionVectorType
GenericTypeInternal< OT_VECTORVECTOR, std::vector< std::vector< GenericType > > > VectorVectorType
GenericTypeInternal< OT_STRINGVECTORVECTOR, std::vector< std::vector< std::string > > > StringVectorVectorType
GenericTypeInternal< OT_DICT, Dict > DictType