26 #include "generic_type_internal.hpp"
27 #include "casadi_misc.hpp"
28 #include "exception.hpp"
29 #include "serializing_stream.hpp"
32 #include "function.hpp"
95 return std::vector<casadi_int>();
97 return std::vector< std::vector<casadi_int> >();
99 return std::vector<bool>();
101 return std::vector<double>();
103 return std::vector< std::vector<double> >();
105 return std::vector<std::string>();
107 return std::vector<std::vector<std::string> >();
109 return std::vector<GenericType::Dict>();
111 return std::vector<std::vector< GenericType> >();
113 return std::vector<GenericType>();
115 casadi_error(
"empty_from_type. Unsupported type " +
str(type));
130 return "OT_INTVECTOR";
132 return "OT_INTVECTORVECTOR";
134 return "OT_BOOLVECTOR";
136 return "OT_DOUBLEVECTOR";
138 return "OT_DOUBLEVECTORVECTOR";
140 return "OT_STRINGVECTOR";
142 return "OT_STRINGVECTORVECTOR";
146 return "OT_DICTVECTOR";
148 return "OT_VECTORVECTOR";
152 return "OT_FUNCTION";
154 return "OT_FUNCTIONVECTOR";
269 std::vector<casadi_int> temp(iv.size());
270 std::copy(iv.begin(), iv.end(), temp.begin());
279 std::vector<casadi_int> i_vec(b_vec.size());
280 std::copy(b_vec.begin(), b_vec.end(), i_vec.begin());
322 casadi_assert_dev(
is_int());
323 return static_cast<const IntType*
>(
get())->d_;
410 return static_cast<bool>(
to_int());
412 casadi_assert(
is_bool(),
"type mismatch");
419 return static_cast<casadi_int
>(
to_double());
421 return static_cast<casadi_int
>(
to_bool());
423 casadi_assert(
is_int(),
"type mismatch");
430 return static_cast<double>(
to_int());
432 casadi_assert(
is_double(),
"type mismatch");
438 casadi_assert(
is_string(),
"type mismatch");
452 GenericType::operator std::vector<int>()
const {
453 std::vector<int> ret;
454 std::vector<casadi_int> source = to_int_vector();
461 std::vector<bool> ret(v.size());
462 for (casadi_int i=0; i<v.size(); ++i) {
463 casadi_assert(v[i]==0 || v[i]==1,
"Entries must be zero or one");
477 return std::vector<double>(v.begin(), v.end());
487 std::vector< std::vector<double> > ret(v.size());
488 for (casadi_int i=0;i<v.size();++i)
489 ret[i].
assign(v[i].begin(), v[i].end());
500 return std::vector<std::string>(1, s);
503 casadi_assert(v.empty(),
"Cast only permitted for zero-length vectors");
507 casadi_assert(v.empty(),
"Cast only permitted for zero-length vectors");
521 casadi_assert(
is_dict(),
"type mismatch");
529 return std::vector<Dict>(1, e);
538 std::vector<GenericType> ret(v.size());
539 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
543 std::vector<GenericType> ret(v.size());
544 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
548 std::vector<GenericType> ret(v.size());
549 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
553 std::vector<GenericType> ret(v.size());
554 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
558 std::vector<GenericType> ret(v.size());
559 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
563 std::vector<GenericType> ret(v.size());
564 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
568 std::vector<GenericType> ret(v.size());
569 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
573 std::vector<GenericType> ret(v.size());
574 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
578 std::vector<GenericType> ret(v.size());
579 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
583 std::vector<GenericType> ret(v.size());
584 for (casadi_int i=0;i<v.size();++i) ret[i] = v[i];
587 casadi_assert(
is_vector(),
"type mismatch");
595 std::vector<std::vector<GenericType> > ret(v.size());
596 for (casadi_int i=0;i<v.size();++i) {
597 ret[i].resize(v[i].size());
598 for (casadi_int j=0;j<v[i].size();++j) {
605 std::vector<std::vector<GenericType> > ret(v.size());
606 for (casadi_int i=0;i<v.size();++i) {
607 ret[i].resize(v[i].size());
608 for (casadi_int j=0;j<v[i].size();++j) {
615 std::vector<std::vector<GenericType> > ret(v.size());
616 for (casadi_int i=0;i<v.size();++i) {
617 ret[i].resize(v[i].size());
618 for (casadi_int j=0;j<v[i].size();++j) {
644 casadi_assert(i==0,
"Only zero pointers accepted");
650 return !(*
this != op2);
669 if (v1.size() != v2.size())
return true;
670 for (casadi_int i=0; i<v1.size(); ++i)
671 if (v1[i] != v2[i])
return true;
678 if (v1.size() != v2.size())
return true;
679 for (casadi_int i=0; i<v1.size(); ++i)
680 if (v1[i] != v2[i])
return true;
687 if (v1.size() != v2.size())
return true;
688 for (casadi_int i=0; i<v1.size(); ++i) {
689 if (v1[i].size() != v2[i].size())
return true;
690 for (casadi_int j=0; j<v1[i].size(); ++j) {
691 if (v1[i][j] != v2[i][j])
return true;
700 if (v1.size() != v2.size())
return true;
701 for (casadi_int i=0; i<v1.size(); ++i) {
702 if (v1[i].size() != v2[i].size())
return true;
703 for (casadi_int j=0; j<v1[i].size(); ++j) {
704 if (v1[i][j] != v2[i][j])
return true;
743 s.
pack(
"GenericType::type",
static_cast<int>(
getType()));
749 s.
unpack(
"GenericType::type", itype);
797 if (first.empty())
return second;
798 if (second.empty())
return first;
805 for (
auto&& e : source) {
807 auto it = target.find(e.first);
808 if (it!=target.end() && it->second.is_dict()) {
809 Dict local = it->second;
810 casadi_assert(e.second.is_dict(),
811 "update_dict error: Key '" + it->first +
"' exists in target, "
812 "but source value is not a dict");
818 target[e.first] = e.second;
825 auto it = target.find(key);
826 if (it==target.end()) {
830 casadi_assert(it->second.is_dict() && value.
is_dict(),
831 "update_dict error: Key '" + key +
"' exists in target, but values are not dicts");
832 Dict orig = it->second;
840 typedef GenericTypeInternal<OT_STRING, std::string>
StringType;
841 typedef GenericTypeInternal<OT_DOUBLE, double>
DoubleType;
842 typedef GenericTypeInternal<OT_INT, casadi_int>
IntType;
843 typedef GenericTypeInternal<OT_BOOL, bool>
BoolType;
844 typedef GenericTypeInternal<OT_DOUBLEVECTOR, std::vector<double> >
DoubleVectorType;
847 typedef GenericTypeInternal<OT_INTVECTOR, std::vector<casadi_int> >
IntVectorType;
850 typedef GenericTypeInternal<OT_STRINGVECTOR, std::vector<std::string> >
StringVectorType;
851 typedef GenericTypeInternal<OT_FUNCTION, Function>
FunctionType;
852 typedef GenericTypeInternal<OT_FUNCTIONVECTOR, std::vector<Function> >
FunctionVectorType;
853 typedef GenericTypeInternal<OT_DICT, Dict>
DictType;
854 typedef GenericTypeInternal<OT_DICTVECTOR, std::vector<Dict> >
DictVectorType;
855 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.
const std::vector< casadi_int > & as_bool_vector() const
Cast to the internal 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.
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_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