GNU Octave  6.2.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-2021 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 
43 namespace octave
44 {
46 
47  class OCTINTERP_API url_handle_manager
48  {
49  public:
50 
52  : handle_map (), handle_free_list (),
53  next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) { }
54 
56 
57  url_handle_manager& operator = (const url_handle_manager&) = delete;
58 
59  ~url_handle_manager (void) = default;
60 
61  url_handle get_handle (void);
62 
63  void free (const url_handle& h);
64 
65  url_handle lookup (double val)
66  {
67  iterator p = (math::isnan (val) ? handle_map.end ()
68  : handle_map.find (val));
69 
70  return (p != handle_map.end ()) ? p->first : url_handle ();
71  }
72 
74  {
75  return val.is_real_scalar () ? lookup (val.double_value ())
76  : url_handle ();
77  }
78 
79  url_transfer get_object (double val)
80  {
81  return get_object (lookup (val));
82  }
83 
85  {
86  return get_object (lookup (val));
87  }
88 
90  {
91  iterator p = (h.ok () ? handle_map.find (h) : handle_map.end ());
92 
93  return (p != handle_map.end ()) ? p->second : url_transfer ();
94  }
95 
96  url_handle make_url_handle (const std::string& host,
97  const std::string& user,
98  const std::string& passwd,
99  std::ostream& os)
100  {
101  url_handle h = get_handle ();
102 
103  url_transfer obj (host, user, passwd, os);
104 
105  if (! obj.is_valid ())
106  error ("support for URL transfers was disabled when Octave was built");
107 
108  handle_map[h] = obj;
109 
110  return h;
111  }
112 
114  {
115  Matrix retval (1, handle_map.size ());
116 
117  octave_idx_type i = 0;
118  for (const auto& h_obj : handle_map)
119  {
120  url_handle h = h_obj.first;
121 
122  retval(i++) = h.value ();
123  }
124 
125  return retval;
126  }
127 
128 
129  private:
130 
131  typedef std::map<url_handle, url_transfer>::iterator iterator;
132  typedef std::map<url_handle, url_transfer>::const_iterator const_iterator;
133 
134  typedef std::set<url_handle>::iterator free_list_iterator;
135  typedef std::set<url_handle>::const_iterator const_free_list_iterator;
136 
137  // A map of handles to curl objects.
138  std::map<url_handle, url_transfer> handle_map;
139 
140  // The available curl handles.
141  std::set<url_handle> handle_free_list;
142 
143  // The next handle available if handle_free_list is empty.
144  double next_handle;
145  };
146 }
147 
148 #endif
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
Definition: dMatrix.h:42
std::map< url_handle, url_transfer > handle_map
url_handle lookup(double val)
std::set< url_handle >::iterator free_list_iterator
url_handle lookup(const octave_value &val)
url_transfer get_object(double val)
url_transfer get_object(const url_handle &h)
std::map< url_handle, url_transfer >::iterator iterator
std::set< url_handle > handle_free_list
std::map< url_handle, url_transfer >::const_iterator const_iterator
url_handle make_url_handle(const std::string &host, const std::string &user, const std::string &passwd, std::ostream &os)
url_handle_manager(const url_handle_manager &)=delete
url_transfer get_object(const octave_value &val)
std::set< url_handle >::const_iterator const_free_list_iterator
~url_handle_manager(void)=default
bool is_valid(void) const
Definition: url-transfer.h:196
double value(void) const
Definition: oct-handle.h:78
bool ok(void) const
Definition: oct-handle.h:113
bool is_real_scalar(void) const
Definition: ov.h:566
double double_value(bool frc_str_conv=false) const
Definition: ov.h:794
void error(const char *fmt,...)
Definition: error.cc:968
bool isnan(bool)
Definition: lo-mappers.h:178
octave_handle url_handle
void free(void *)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811