List of all members | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
casadi::ZipResource Class Reference

RAII class for reading from a zip file. More...

#include <resource_internal.hpp>

Detailed Description

Extra doc: https://github.com/casadi/casadi/wiki/L_2c5

Definition at line 97 of file resource_internal.hpp.

Inheritance diagram for casadi::ZipResource:
Inheritance graph
[legend]
Collaboration diagram for casadi::ZipResource:
Collaboration graph
[legend]

Public Types

using weak_ref_type = WeakRefInternal
 

Public Member Functions

 ZipResource (const std::string &path)
 Initialize with a path. More...
 
 ~ZipResource () override
 
const std::string & path () const override
 Get path for a consumer. More...
 
void unpack ()
 
std::string class_name () const override
 Get type name. More...
 
void disp (std::ostream &stream, bool more) const override
 Print description. More...
 
void serialize_type (SerializingStream &s) const override
 Potentially decay into ZipMemResource. More...
 
void serialize_body (SerializingStream &s) const override
 
void serialize (SerializingStream &s) const
 
void change_option (const std::string &option_name, const GenericType &option_value)
 
casadi_int getCount () const
 Get the reference count. More...
 
std::string debug_repr (const SharedObjectInternal *) const
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 

Static Public Member Functions

static ResourceInternaldeserialize (DeserializingStream &s)
 

Protected Member Functions

 ZipResource (DeserializingStream &s)
 
void initSingleton ()
 
void destroySingleton ()
 
shared_from_this ()
 Get a shared object from the current internal object. More...
 
const B shared_from_this () const
 Get a shared object from the current internal object. More...
 

Protected Attributes

std::string serialize_mode_
 

Member Typedef Documentation

◆ weak_ref_type

Definition at line 152 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ ZipResource() [1/2]

casadi::ZipResource::ZipResource ( const std::string &  path)

If the path is a directory or empty, the path is passed through to the consumer. Otherwise, the zip file is extracted to a temporary directory.

Upon destruction, the temporary directory is removed.

Extra doc: https://github.com/casadi/casadi/wiki/L_2c6

Definition at line 99 of file resource_internal.cpp.

100  : ResourceInternal() {
101  path_ = path;
102  unpack();
103 }
ResourceInternal()
Initialize with a path.
const std::string & path() const override
Get path for a consumer.

References path(), and unpack().

Referenced by deserialize().

◆ ~ZipResource()

casadi::ZipResource::~ZipResource ( )
override

Definition at line 119 of file resource_internal.cpp.

119  {
120  try {
122  } catch (...) {
123  casadi_warning("Error: Cannot remove temporary directory: " + dir_);
124  }
125  try {
126  Filesystem::remove(lock_file_);
127  } catch (...) {
128  casadi_warning("Error: Cannot remove lock file: " + lock_file_);
129  }
130 }
static casadi_int remove_all(const std::string &path)
Definition: filesystem.cpp:48
static bool remove(const std::string &path)
Definition: filesystem.cpp:43

References casadi::Filesystem::remove(), and casadi::Filesystem::remove_all().

◆ ZipResource() [2/2]

casadi::ZipResource::ZipResource ( DeserializingStream s)
explicitprotected

Definition at line 194 of file resource_internal.cpp.

194  : ResourceInternal(s) {
195  s.version("ZipResource", 1);
196  s.unpack("ZipResource::path", path_);
197  unpack();
198 }

References unpack(), casadi::DeserializingStream::unpack(), and casadi::DeserializingStream::version().

Member Function Documentation

◆ change_option()

void casadi::ResourceInternal::change_option ( const std::string &  option_name,
const GenericType option_value 
)
inherited

Definition at line 53 of file resource_internal.cpp.

54  {
55  if (option_name=="serialize_mode") {
56  serialize_mode_ = option_value.to_string();
57  casadi_assert(serialize_mode_=="embed" || serialize_mode_=="link",
58  "Invalid serialization mode: " + serialize_mode_ + ". Pick 'link' or 'embed'.");
59  } else {
60  casadi_error("Option '" + option_name + "' does not exist");
61  }
62 }

References casadi::ResourceInternal::serialize_mode_, and casadi::GenericType::to_string().

◆ class_name()

std::string casadi::ZipResource::class_name ( ) const
inlineoverridevirtual

Extra doc: https://github.com/casadi/casadi/wiki/L_2cw

Implements casadi::SharedObjectInternal.

Definition at line 117 of file resource_internal.hpp.

117 {return "ZipResource";}

Referenced by serialize_type().

◆ debug_repr()

std::string casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::debug_repr ( const Internal *  i) const
inherited

Definition at line 62 of file generic_shared_internal.hpp.

162  {
163  // Note: i != this because of something something multiple inheritance
164  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
165  }
std::string str(const T &v)
String representation, any type.

◆ deserialize()

ResourceInternal * casadi::ZipResource::deserialize ( DeserializingStream s)
static

Definition at line 179 of file resource_internal.cpp.

179  {
180  ZipResource* ret = new ZipResource(s);
181  return ret;
182 }
ZipResource(const std::string &path)
Initialize with a path.

References ZipResource().

Referenced by casadi::ResourceInternal::deserialize().

◆ destroySingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::destroySingleton ( )
inlineprotectedinherited

Called in the destructor of singletons

Definition at line 77 of file generic_shared_internal.hpp.

77  {
78  static_cast<Internal*>(this)->count--;
79  }

◆ disp()

void casadi::ZipResource::disp ( std::ostream &  stream,
bool  more 
) const
overridevirtual

Implements casadi::SharedObjectInternal.

Definition at line 111 of file resource_internal.cpp.

111  {
112  stream << "ZipResource(\"" << path_ << "\") -> \"" << dir_ << "\"";
113 }

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

186  {
187  return static_cast<const Internal*>(this)->count;
188  }

◆ initSingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::initSingleton ( )
inlineprotectedinherited

Called in the constructor of singletons to avoid that the counter reaches zero

Definition at line 71 of file generic_shared_internal.hpp.

71  {
72  casadi_assert_dev(static_cast<Internal*>(this)->count==0);
73  static_cast<Internal*>(this)->count++;
74  }

◆ path()

const std::string& casadi::ZipResource::path ( ) const
inlineoverridevirtual

Implements casadi::ResourceInternal.

Definition at line 110 of file resource_internal.hpp.

110 {return dir_;}

Referenced by ZipResource().

◆ serialize()

void casadi::ResourceInternal::serialize ( SerializingStream s) const
inherited

Definition at line 145 of file resource_internal.cpp.

145  {
146  s.version("ResourceInternal", 1);
147  serialize_type(s);
148  serialize_body(s);
149 }
virtual void serialize_body(SerializingStream &s) const
virtual void serialize_type(SerializingStream &s) const

References casadi::ResourceInternal::serialize_body(), casadi::ResourceInternal::serialize_type(), and casadi::SerializingStream::version().

◆ serialize_body()

void casadi::ZipResource::serialize_body ( SerializingStream s) const
overridevirtual

Reimplemented from casadi::ResourceInternal.

Definition at line 241 of file resource_internal.cpp.

241  {
243  s.version("ZipResource", 1);
244  if (serialize_mode_=="embed") {
245  // Decay into ZipMemResource
246  std::ifstream binary(path_, std::ios_base::binary);
247  casadi_assert(binary.good(),
248  "Could not open zip file '" + path_ + "'.");
249  s.pack("ZipMemResource::blob", binary);
250  } else {
251  s.pack("ZipResource::path", path_);
252  }
253 }

References casadi::SerializingStream::pack(), casadi::ResourceInternal::serialize_body(), casadi::ResourceInternal::serialize_mode_, and casadi::SerializingStream::version().

◆ serialize_type()

void casadi::ZipResource::serialize_type ( SerializingStream s) const
overridevirtual

Extra doc: https://github.com/casadi/casadi/wiki/L_2cx

Reimplemented from casadi::ResourceInternal.

Definition at line 214 of file resource_internal.cpp.

214  {
215  if (serialize_mode_=="embed") {
216  // Decay into ZipMemResource
217  std::string class_name = "ZipMemResource";
218  s.pack("ResourceInternal::type", class_name);
219  } else if (serialize_mode_=="link") {
220  std::string class_name = "ZipResource";
221  s.pack("ResourceInternal::type", class_name);
222  } else {
223  casadi_error("Unknown serialization mode: '" + serialize_mode_+ "'.");
224  }
225 }
std::string class_name() const override
Get type name.

References class_name(), casadi::SerializingStream::pack(), and casadi::ResourceInternal::serialize_mode_.

◆ shared_from_this() [1/2]

B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( )
inlineprotectedinherited

Definition at line 83 of file generic_shared_internal.hpp.

83  {
84  casadi_assert_dev(B::test_cast(static_cast<Internal*>(this)));
85  B ret;
86  ret.own(static_cast<Internal*>(this));
87  return ret;
88  }

◆ shared_from_this() [2/2]

const B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( ) const
inlineprotectedinherited

Definition at line 92 of file generic_shared_internal.hpp.

92  {
93  casadi_assert_dev(B::test_cast(static_cast<const Internal*>(this)));
94  B ret;
95  ret.own(const_cast<Internal*>(static_cast<const Internal*>(this)));
96  return ret;
97  }

◆ unpack()

void casadi::ZipResource::unpack ( )

Definition at line 64 of file resource_internal.cpp.

64  {
65  casadi_assert(Filesystem::is_enabled(),
66  "Unzipping '" + path_ + "' requires advanced filesystem access. Compile CasADi with WITH_GC=ON.\n"
67  "Alternatively, manually unzip it into a direcory, "
68  "and pass this directory name instead of the zip file name.");
69 
70  // Extract filename part of path
71  std::string zip_file = Filesystem::filename(path_);
72 
73  lock_file_ = temporary_file(zip_file + ".", ".lock");
74  dir_ = lock_file_.substr(0, lock_file_.size()-5) + ".unzipped";
75 
76  casadi_assert(Archiver::has_plugin("libzip"),
77  "Unzipping '" + path_ + "' requires libzip. Compile CasADi with WITH_LIBZIP=ON.\n"
78  "Alternatively, manually unzip it into a direcory, "
79  "and pass this directory name instead of the zip file name.");
80 
81 
82  Archiver::getPlugin("libzip").exposed.unpack(path_, dir_);
83 }
static std::string filename(const std::string &path)
Definition: filesystem.cpp:73
static bool is_enabled()
Definition: filesystem.cpp:83
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.
std::string temporary_file(const std::string &prefix, const std::string &suffix)

References casadi::Filesystem::filename(), casadi::PluginInterface< Archiver >::getPlugin(), casadi::PluginInterface< Archiver >::has_plugin(), casadi::Filesystem::is_enabled(), and casadi::temporary_file().

Referenced by ZipResource().

◆ weak()

Extra doc: https://github.com/casadi/casadi/wiki/L_1ai

Definition at line 67 of file generic_shared_internal.hpp.

191  {
192  if (weak_ref_==nullptr) {
193  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
194  }
195  return weak_ref_;
196  }

Member Data Documentation

◆ serialize_mode_

std::string casadi::ResourceInternal::serialize_mode_
protectedinherited

The documentation for this class was generated from the following files: