GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
url-handle-manager.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2017-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // Extracted from urlwrite.cc.
27 
28 #if ! defined (octave_url_handle_manager)
29 #define octave_url_handle_manager 1
30 
31 #include "octave-config.h"
32 
33 #include <cmath>
34 
35 #include <map>
36 #include <set>
37 
38 #include "lo-mappers.h"
39 
40 #include "oct-handle.h"
41 #include "url-transfer.h"
42 
44 
46 
47 class OCTINTERP_API url_handle_manager
48 {
49 public:
50 
52  : m_handle_map (), m_handle_free_list (),
53  m_next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) { }
54 
55  OCTAVE_DISABLE_COPY_MOVE (url_handle_manager)
56 
57  ~url_handle_manager () = default;
58 
59  url_handle get_handle ();
60 
61  void free (const url_handle& h);
62 
63  url_handle lookup (double val)
64  {
65  iterator p = (math::isnan (val) ? m_handle_map.end ()
66  : m_handle_map.find (val));
67 
68  return (p != m_handle_map.end ()) ? p->first : url_handle ();
69  }
70 
72  {
73  return val.is_real_scalar () ? lookup (val.double_value ())
74  : url_handle ();
75  }
76 
77  url_transfer get_object (double val)
78  {
79  return get_object (lookup (val));
80  }
81 
83  {
84  return get_object (lookup (val));
85  }
86 
88  {
89  iterator p = (h.ok () ? m_handle_map.find (h) : m_handle_map.end ());
90 
91  return (p != m_handle_map.end ()) ? p->second : url_transfer ();
92  }
93 
94  url_handle make_url_handle (const std::string& host,
95  const std::string& user,
96  const std::string& passwd,
97  std::ostream& os)
98  {
99  url_handle h = get_handle ();
100 
101  url_transfer obj (host, user, passwd, os);
102 
103  if (! obj.is_valid ())
104  error ("support for URL transfers was disabled when Octave was built");
105 
106  m_handle_map[h] = obj;
107 
108  return h;
109  }
110 
112  {
113  Matrix retval (1, m_handle_map.size ());
114 
115  octave_idx_type i = 0;
116  for (const auto& h_obj : m_handle_map)
117  {
118  url_handle h = h_obj.first;
119 
120  retval(i++) = h.value ();
121  }
122 
123  return retval;
124  }
125 
126 
127 private:
128 
129  typedef std::map<url_handle, url_transfer>::iterator iterator;
130  typedef std::map<url_handle, url_transfer>::const_iterator const_iterator;
131 
132  typedef std::set<url_handle>::iterator free_list_iterator;
133  typedef std::set<url_handle>::const_iterator const_free_list_iterator;
134 
135  // A map of handles to curl objects.
136  std::map<url_handle, url_transfer> m_handle_map;
137 
138  // The available curl handles.
139  std::set<url_handle> m_handle_free_list;
140 
141  // The next handle available if handle_free_list is empty.
142  double m_next_handle;
143 };
144 
145 OCTAVE_END_NAMESPACE(octave)
146 
147 #endif
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
Definition: dMatrix.h:42
double value() const
Definition: oct-handle.h:78
bool ok() const
Definition: oct-handle.h:113
bool is_real_scalar() const
Definition: ov.h:610
double double_value(bool frc_str_conv=false) const
Definition: ov.h:841
Definition: oct-rand.h:45
url_handle lookup(const octave_value &val)
url_handle make_url_handle(const std::string &host, const std::string &user, const std::string &passwd, std::ostream &os)
url_handle lookup(double val)
url_transfer get_object(double val)
url_transfer get_object(const url_handle &h)
~url_handle_manager()=default
url_transfer get_object(const octave_value &val)
bool is_valid() const
Definition: url-transfer.h:182
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
bool isnan(bool)
Definition: lo-mappers.h:178
void free(void *)
octave_handle url_handle