resource_internal.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_RESOURCE_INTERNAL_HPP
27 #define CASADI_RESOURCE_INTERNAL_HPP
28 
29 #include "resource.hpp"
30 #include "shared_object.hpp"
31 #include "serializing_stream.hpp"
32 
34 namespace casadi {
35 
36 
40  class CASADI_EXPORT ResourceInternal : public SharedObjectInternal {
41  public:
50  explicit ResourceInternal();
52  virtual const std::string& path() const = 0;
53  void serialize(SerializingStream& s) const;
54 
55  virtual void serialize_type(SerializingStream& s) const;
56  virtual void serialize_body(SerializingStream& s) const;
57 
58  static ResourceInternal* deserialize(DeserializingStream& s);
59  void change_option(const std::string& option_name,
60  const GenericType& option_value);
61  protected:
63  std::string serialize_mode_;
64  };
65 
66  class CASADI_EXPORT DirResource : public ResourceInternal {
67  public:
71  DirResource(const std::string& path);
72  ~DirResource() override;
74  const std::string& path() const override {return path_;}
75 
79  std::string class_name() const override {return "DirResource";}
80 
82  void disp(std::ostream& stream, bool more) const override;
83 
84  void serialize_type(SerializingStream& s) const override;
85  void serialize_body(SerializingStream& s) const override;
86 
87  static ResourceInternal* deserialize(DeserializingStream& s);
88  private:
89  std::string path_;
90  protected:
91  explicit DirResource(DeserializingStream& s);
92  };
93 
97  class CASADI_EXPORT ZipResource : public ResourceInternal {
98  public:
107  ZipResource(const std::string& path);
108  ~ZipResource() override;
110  const std::string& path() const override {return dir_;}
111 
112  void unpack();
113 
117  std::string class_name() const override {return "ZipResource";}
118 
120  void disp(std::ostream& stream, bool more) const override;
121 
125  void serialize_type(SerializingStream& s) const override;
126  void serialize_body(SerializingStream& s) const override;
127 
128  static ResourceInternal* deserialize(DeserializingStream& s);
129  private:
130  std::string lock_file_;
131  std::string dir_;
132  std::string path_;
133  protected:
134  explicit ZipResource(DeserializingStream& s);
135  };
136 
137 
141  class CASADI_EXPORT ZipMemResource : public ResourceInternal {
142  public:
143  ZipMemResource(const std::istream& src);
144  ~ZipMemResource() override;
146  const std::string& path() const override {return dir_;}
147 
148  void unpack();
152  std::string class_name() const override {return "ZipMemResource";}
153 
155  void disp(std::ostream& stream, bool more) const override;
156 
157  void serialize_body(SerializingStream& s) const override;
158 
159  static ResourceInternal* deserialize(DeserializingStream& s);
160  private:
161  std::string lock_file_;
162  std::string dir_;
163  mutable std::stringstream blob_;
164  #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
165  mutable std::mutex mutex_blob_;
166  #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
167  protected:
168  explicit ZipMemResource(DeserializingStream& s);
169  };
170 
171 } // namespace casadi
173 #endif // CASADI_RESOURCE_INTERNAL_HPP
Helper class for Serialization.
std::string class_name() const override
Get type name.
const std::string & path() const override
Get path for a consumer.
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
RAII class base for reading from resources.
virtual const std::string & path() const =0
Get path for a consumer.
Helper class for Serialization.
RAII class for reading from a zip held in memory.
std::string class_name() const override
Get type name.
const std::string & path() const override
Get path for a consumer.
RAII class for reading from a zip file.
const std::string & path() const override
Get path for a consumer.
std::string class_name() const override
Get type name.
The casadi namespace.
Definition: archiver.cpp:28
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)