26 #include "resource_internal.hpp"
27 #include "casadi_misc.hpp"
28 #include "archiver_impl.hpp"
30 #include "filesystem_impl.hpp"
47 stream <<
"DirResource(\"" << path_ <<
"\")";
55 if (option_name==
"serialize_mode") {
58 "Invalid serialization mode: " +
serialize_mode_ +
". Pick 'link' or 'embed'.");
60 casadi_error(
"Option '" + option_name +
"' does not exist");
66 "Unzipping '" + path_ +
"' requires advanced filesystem access. "
67 "Compile CasADi with WITH_GHC_FILESYSTEM=ON.\n"
68 "Alternatively, manually unzip it into a direcory, "
69 "and pass this directory name instead of the zip file name.");
75 "Unzipping '" + path_ +
"' requires libzip. Compile CasADi with WITH_LIBZIP=ON.\n"
76 "Alternatively, manually unzip it into a direcory, "
77 "and pass this directory name instead of the zip file name.");
87 "Unzipping stream requires libzip. Compile CasADi with WITH_LIBZIP=ON.\n"
88 "Alternatively, save with serialize option set to link. ");
93 blob_.seekg(0, std::ios::beg);
104 blob_ << src.rdbuf();
109 stream <<
"ZipResource(\"" << path_ <<
"\") -> \"" <<
dir_ <<
"\"";
113 stream <<
"ZipMemResource(blob) -> \"" <<
dir_ <<
"\"";
120 if (!suffix.empty()) {
121 dir_ +=
"." + suffix;
133 stream <<
"TemporaryDirResource(\"" <<
dir_ <<
"\")";
140 casadi_warning(
"Error: Cannot remove temporary directory: " +
dir_);
145 casadi_warning(
"Error: Cannot remove lock file: " +
lock_file_);
150 s.
version(
"ResourceInternal", 1);
169 s.
version(
"ResourceInternal", 1);
179 casadi_error(
"Cannot deserialize type '" +
class_name +
"'");
200 s.
unpack(
"ZipResource::path", path_);
205 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
206 std::lock_guard<std::mutex> lock(mutex_blob_);
208 s.
version(
"ZipMemResource", 1);
209 s.
unpack(
"ZipMemResource::blob", blob_);
215 s.
unpack(
"DirResource::path", path_);
251 casadi_assert(binary_ptr,
252 "Could not open zip file '" + path_ +
"'.");
253 s.
pack(
"ZipMemResource::blob", *binary_ptr);
255 s.
pack(
"ZipResource::path", path_);
264 std::stringstream ss;
267 ss.seekg(0, std::ios::beg);
268 s.
pack(
"ZipMemResource::blob", ss);
270 s.
pack(
"DirResource::path", path_);
277 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
278 std::lock_guard<std::mutex> lock(mutex_blob_);
281 s.
version(
"ZipMemResource", 1);
282 s.
pack(
"ZipMemResource::blob", blob_);
286 blob_.seekg(0, std::ios::beg);
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void serialize_type(SerializingStream &s) const override
std::string class_name() const override
Get type name.
DirResource(const std::string &path)
Initialize with a path.
void serialize_body(SerializingStream &s) const override
const std::string & path() const override
Get path for a consumer.
void disp(std::ostream &stream, bool more) const override
Print description.
static ResourceInternal * deserialize(DeserializingStream &s)
static casadi_int remove_all(const std::string &path)
static std::string absolute(const std::string &path)
static std::string filename(const std::string &path)
static bool create_directories(const std::string &path)
static std::unique_ptr< std::istream > ifstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::in, bool fail=true)
static bool remove(const std::string &path)
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
std::string to_string() const
Convert to a type.
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.
RAII class base for reading from resources.
ResourceInternal()
Initialize with a path.
void serialize(SerializingStream &s) const
virtual void serialize_body(SerializingStream &s) const
void change_option(const std::string &option_name, const GenericType &option_value)
virtual void serialize_type(SerializingStream &s) const
static ResourceInternal * deserialize(DeserializingStream &s)
std::string serialize_mode_
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
virtual std::string class_name() const =0
Readable name of the internal class.
Base class for resources that use a temporary directory.
~TemporaryDirResource() override
void disp(std::ostream &stream, bool more) const override
Print description.
const std::string & path() const override
Get path for a consumer.
void setup_temp_dir(const std::string &base_name, const std::string &suffix="")
Set up lock file and directory paths from a base name.
RAII class for reading from a zip held in memory.
void serialize_body(SerializingStream &s) const override
ZipMemResource(const std::istream &src)
void disp(std::ostream &stream, bool more) const override
Print description.
static ResourceInternal * deserialize(DeserializingStream &s)
RAII class for reading from a zip file.
void disp(std::ostream &stream, bool more) const override
Print description.
static ResourceInternal * deserialize(DeserializingStream &s)
void serialize_body(SerializingStream &s) const override
void serialize_type(SerializingStream &s) const override
Potentially decay into ZipMemResource.
ZipResource(const std::string &path)
Initialize with a path.
std::string class_name() const override
Get type name.
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)
std::string temporary_file(const std::string &prefix, const std::string &suffix, const std::string &directory)