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:
62  explicit ResourceInternal(DeserializingStream& s);
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 
99  class CASADI_EXPORT TemporaryDirResource : public ResourceInternal {
100  public:
102  TemporaryDirResource(const std::string& base_name, const std::string& suffix="");
103  ~TemporaryDirResource() override;
105  const std::string& path() const override {return dir_;}
106 
110  std::string class_name() const override {return "TemporaryDirResource";}
111 
113  void disp(std::ostream& stream, bool more) const override;
114  protected:
115  explicit TemporaryDirResource() : ResourceInternal() {}
116  explicit TemporaryDirResource(DeserializingStream& s) : ResourceInternal(s) {}
118  void setup_temp_dir(const std::string& base_name, const std::string& suffix="");
119  std::string lock_file_;
120  std::string dir_;
121  };
122 
126  class CASADI_EXPORT ZipResource : public TemporaryDirResource {
127  public:
135  ZipResource(const std::string& path);
136 
137  void unpack();
138 
142  std::string class_name() const override {return "ZipResource";}
143 
145  void disp(std::ostream& stream, bool more) const override;
146 
150  void serialize_type(SerializingStream& s) const override;
151  void serialize_body(SerializingStream& s) const override;
152 
153  static ResourceInternal* deserialize(DeserializingStream& s);
154  private:
155  std::string path_;
156  protected:
157  explicit ZipResource(DeserializingStream& s);
158  };
159 
160 
164  class CASADI_EXPORT ZipMemResource : public TemporaryDirResource {
165  public:
166  ZipMemResource(const std::istream& src);
167 
168  void unpack();
172  std::string class_name() const override {return "ZipMemResource";}
173 
175  void disp(std::ostream& stream, bool more) const override;
176 
177  void serialize_body(SerializingStream& s) const override;
178 
179  static ResourceInternal* deserialize(DeserializingStream& s);
180  private:
181  mutable std::stringstream blob_;
182  #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
183  mutable std::mutex mutex_blob_;
184  #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
185  protected:
186  explicit ZipMemResource(DeserializingStream& s);
187  };
188 
189 } // namespace casadi
191 #endif // CASADI_RESOURCE_INTERNAL_HPP
The casadi namespace.
Definition: archiver.hpp:32