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 126 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...
 
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
 
const std::string & path () const override
 Get path for a consumer. More...
 
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 setup_temp_dir (const std::string &base_name, const std::string &suffix="")
 Set up lock file and directory paths from a base name. More...
 
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 lock_file_
 
std::string dir_
 
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)

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 96 of file resource_internal.cpp.

98  path_ = path;
99  unpack();
100 }
const std::string & path() const override
Get path for a consumer.

References casadi::TemporaryDirResource::path(), and unpack().

Referenced by deserialize().

◆ ZipResource() [2/2]

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

Definition at line 198 of file resource_internal.cpp.

198  : TemporaryDirResource(s) {
199  s.version("ZipResource", 1);
200  s.unpack("ZipResource::path", path_);
201  unpack();
202 }

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

Reimplemented from casadi::TemporaryDirResource.

Definition at line 142 of file resource_internal.hpp.

142 {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.

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

◆ deserialize()

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

Definition at line 183 of file resource_internal.cpp.

183  {
184  ZipResource* ret = new ZipResource(s);
185  return ret;
186 }
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

Reimplemented from casadi::TemporaryDirResource.

Definition at line 108 of file resource_internal.cpp.

108  {
109  stream << "ZipResource(\"" << path_ << "\") -> \"" << dir_ << "\"";
110 }

References casadi::TemporaryDirResource::dir_.

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

205  {
206  return static_cast<const Internal*>(this)->count;
207  }

◆ 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::TemporaryDirResource::path ( ) const
inlineoverridevirtualinherited

Implements casadi::ResourceInternal.

Definition at line 105 of file resource_internal.hpp.

105 {return dir_;}

Referenced by casadi::open_resource(), and ZipResource().

◆ serialize()

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

Definition at line 149 of file resource_internal.cpp.

149  {
150  s.version("ResourceInternal", 1);
151  serialize_type(s);
152  serialize_body(s);
153 }
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 245 of file resource_internal.cpp.

245  {
247  s.version("ZipResource", 1);
248  if (serialize_mode_=="embed") {
249  // Decay into ZipMemResource
250  auto binary_ptr = Filesystem::ifstream_ptr(path_, std::ios_base::binary, false);
251  casadi_assert(binary_ptr,
252  "Could not open zip file '" + path_ + "'.");
253  s.pack("ZipMemResource::blob", *binary_ptr);
254  } else {
255  s.pack("ZipResource::path", path_);
256  }
257 }
static std::unique_ptr< std::istream > ifstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::in, bool fail=true)
Definition: filesystem.cpp:135

References casadi::Filesystem::ifstream_ptr(), 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 218 of file resource_internal.cpp.

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

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

◆ setup_temp_dir()

void casadi::TemporaryDirResource::setup_temp_dir ( const std::string &  base_name,
const std::string &  suffix = "" 
)
protectedinherited

Definition at line 116 of file resource_internal.cpp.

116  {
117  // Resolve absolute path since cwd may be changed by the user
118  lock_file_ = Filesystem::absolute(temporary_file(base_name + ".", ".lock"));
119  dir_ = lock_file_.substr(0, lock_file_.size() - 5);
120  if (!suffix.empty()) {
121  dir_ += "." + suffix;
122  }
123  // Create the directory
125 }
static std::string absolute(const std::string &path)
Definition: filesystem.cpp:78
static bool create_directories(const std::string &path)
Definition: filesystem.cpp:63
std::string temporary_file(const std::string &prefix, const std::string &suffix, const std::string &directory)

References casadi::Filesystem::absolute(), casadi::Filesystem::create_directories(), casadi::TemporaryDirResource::dir_, casadi::TemporaryDirResource::lock_file_, and casadi::temporary_file().

Referenced by casadi::TemporaryDirResource::TemporaryDirResource(), unpack(), and casadi::ZipMemResource::unpack().

◆ 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. "
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.");
70 
71  // Set up temporary directory
72  setup_temp_dir(Filesystem::filename(path_), "unzipped");
73 
74  casadi_assert(Archiver::has_plugin("libzip"),
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.");
78 
79  Archiver::getPlugin("libzip").exposed.unpack(path_, dir_);
80 }
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.
void setup_temp_dir(const std::string &base_name, const std::string &suffix="")
Set up lock file and directory paths from a base name.

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

Referenced by ZipResource().

◆ weak()

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

Definition at line 67 of file generic_shared_internal.hpp.

210  {
211 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
212  auto* w = weak_ref_.load(std::memory_order_acquire);
213  if (!w) {
214  auto* nw = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
215  GenericWeakRef<Shared, Internal>* expected = nullptr;
216  if (weak_ref_.compare_exchange_strong(
217  expected, nw, std::memory_order_release, std::memory_order_acquire)) {
218  w = nw;
219  } else {
220  delete nw; // lost the race; another thread published first
221  w = expected;
222  }
223  }
224  return w;
225 #else
226  if (weak_ref_==nullptr) {
227  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
228  }
229  return weak_ref_;
230 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
231  }

Member Data Documentation

◆ dir_

std::string casadi::TemporaryDirResource::dir_
protectedinherited

◆ lock_file_

std::string casadi::TemporaryDirResource::lock_file_
protectedinherited

◆ serialize_mode_

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

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