1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2007-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 #if ! defined (octave_oct_handle_h)
27 #define octave_oct_handle_h 1
28 
29 #include "octave-config.h"
30 
31 #include "dMatrix.h"
32 #include "lo-ieee.h"
33 
34 #include "error.h"
35 #include "ov.h"
36 
37 // ---------------------------------------------------------------------
38 
39 class octave_handle
40 {
41 public:
octave_handle(void)42   octave_handle (void) : val (octave::numeric_limits<double>::NaN ()) { }
43 
octave_handle(const octave_value & a)44   octave_handle (const octave_value& a)
45     : val (octave::numeric_limits<double>::NaN ())
46   {
47     if (a.isempty ())
48       ; // do nothing
49     else
50       {
51         try
52           {
53             val = a.double_value ();
54           }
55         catch (octave::execution_exception& e)
56           {
57             error (e, "invalid handle");
58           }
59       }
60   }
61 
octave_handle(int a)62   octave_handle (int a) : val (a) { }
63 
octave_handle(double a)64   octave_handle (double a) : val (a) { }
65 
octave_handle(const octave_handle & a)66   octave_handle (const octave_handle& a) : val (a.val) { }
67 
68   octave_handle& operator = (const octave_handle& a)
69   {
70     if (&a != this)
71       val = a.val;
72 
73     return *this;
74   }
75 
76   ~octave_handle (void) = default;
77 
value(void)78   double value (void) const { return val; }
79 
as_octave_value(void)80   octave_value as_octave_value (void) const
81   {
82     return ok () ? octave_value (val) : octave_value (Matrix ());
83   }
84 
85   // Prefix increment/decrement operators.
86   octave_handle& operator ++ (void)
87   {
88     ++val;
89     return *this;
90   }
91 
92   octave_handle& operator -- (void)
93   {
94     --val;
95     return *this;
96   }
97 
98   // Postfix increment/decrement operators.
99   const octave_handle operator ++ (int)
100   {
101     octave_handle old_value = *this;
102     ++(*this);
103     return old_value;
104   }
105 
106   const octave_handle operator -- (int)
107   {
108     octave_handle old_value = *this;
109     --(*this);
110     return old_value;
111   }
112 
ok(void)113   bool ok (void) const { return ! octave::math::isnan (val); }
114 
115 private:
116   double val;
117 };
118 
119 inline bool
120 operator == (const octave_handle& a, const octave_handle& b)
121 {
122   return a.value () == b.value ();
123 }
124 
125 inline bool
126 operator != (const octave_handle& a, const octave_handle& b)
127 {
128   return a.value () != b.value ();
129 }
130 
131 inline bool
132 operator < (const octave_handle& a, const octave_handle& b)
133 {
134   return a.value () < b.value ();
135 }
136 
137 inline bool
138 operator <= (const octave_handle& a, const octave_handle& b)
139 {
140   return a.value () <= b.value ();
141 }
142 
143 inline bool
144 operator >= (const octave_handle& a, const octave_handle& b)
145 {
146   return a.value () >= b.value ();
147 }
148 
149 inline bool
150 operator > (const octave_handle& a, const octave_handle& b)
151 {
152   return a.value () > b.value ();
153 }
154 
155 #endif
156