GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
cdef-property.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2012-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 #if ! defined (octave_cdef_property_h)
27 #define octave_cdef_property_h 1
28 
29 #include "octave-config.h"
30 
31 #include <map>
32 #include <set>
33 #include <string>
34 
35 #include "oct-refcount.h"
36 
37 #include "cdef-object.h"
38 #include "error.h"
39 #include "ov.h"
40 
42 
43 class
44 OCTINTERP_API
46 {
47  friend class cdef_class;
48 
49 private:
50 
51  class
52  cdef_property_rep : public cdef_meta_object_rep
53  {
54  public:
55 
56  cdef_property_rep () : cdef_meta_object_rep () { }
57 
58  cdef_property_rep& operator = (const cdef_property_rep& p) = delete;
59 
60  ~cdef_property_rep () = default;
61 
62  cdef_object_rep * copy () const
63  {
64  return new cdef_property_rep (*this);
65  }
66 
67  bool is_property () const { return true; }
68 
69  std::string get_name () const { return get("Name").string_value (); }
70 
71  void set_name (const std::string& nm) { put ("Name", nm); }
72 
73  bool is_constant () const { return get("Constant").bool_value (); }
74 
75  octave_value get_value (bool do_check_access = true,
76  const std::string& who = "") const;
77 
78  octave_value get_value (const cdef_object& obj,
79  bool do_check_access = true,
80  const std::string& who = "") const;
81 
82  void set_value (cdef_object& obj, const octave_value& val,
83  bool do_check_access = true,
84  const std::string& who = "");
85 
86  OCTINTERP_API bool check_get_access () const;
87 
88  OCTINTERP_API bool check_set_access () const;
89 
90  private:
91  cdef_property_rep (const cdef_property_rep& p)
93  { }
94 
95  OCTINTERP_API bool is_recursive_set (const cdef_object& obj) const;
96 
97  cdef_property wrap ()
98  {
99  m_count++;
100  return cdef_property (this);
101  }
102 
103  OCTINTERP_API OCTAVE_NORETURN
104  void err_property_access (const std::string& from,
105  bool is_set = false) const;
106  };
107 
108 public:
109 
111 
112  cdef_property (const std::string& nm)
113  : cdef_meta_object (new cdef_property_rep ())
114  {
115  get_rep ()->set_name (nm);
116  }
117 
118  cdef_property (const cdef_property& prop) : cdef_meta_object (prop) { }
119 
121  : cdef_meta_object (obj)
122  {
123  // This should never happen...
124  if (! is_property ())
125  error ("internal error: invalid assignment from %s to meta.property object",
126  class_name ().c_str ());
127  }
128 
130  {
132 
133  return *this;
134  }
135 
136  ~cdef_property () = default;
137 
138  octave_value get_value (const cdef_object& obj, bool do_check_access = true,
139  const std::string& who = "") const
140  {
141  return get_rep ()->get_value (obj, do_check_access, who);
142  }
143 
144  octave_value get_value (bool do_check_access = true,
145  const std::string& who = "") const
146  {
147  return get_rep ()->get_value (do_check_access, who);
148  }
149 
150  void set_value (cdef_object& obj, const octave_value& val,
151  bool do_check_access = true,
152  const std::string& who = "")
153  {
154  get_rep ()->set_value (obj, val, do_check_access, who);
155  }
156 
157  bool check_get_access () const
158  {
159  return get_rep ()->check_get_access ();
160  }
161 
162  bool check_set_access () const
163  {
164  return get_rep ()->check_set_access ();
165  }
166 
167  std::string get_name () const { return get_rep ()->get_name (); }
168 
169  bool is_constant () const { return get_rep ()->is_constant (); }
170 
171 private:
172 
173  cdef_property_rep * get_rep ()
174  {
175  return dynamic_cast<cdef_property_rep *> (cdef_object::get_rep ());
176  }
177 
178  const cdef_property_rep * get_rep () const
179  {
180  return dynamic_cast<const cdef_property_rep *> (cdef_object::get_rep ());
181  }
182 };
183 
184 OCTAVE_END_NAMESPACE(octave)
185 
186 #endif
cdef_class & operator=(const cdef_class &cls)
Definition: cdef-class.h:256
std::string get_name() const
Definition: cdef-class.h:318
bool is_property() const
Definition: cdef-object.h:688
const cdef_object_rep * get_rep() const
Definition: cdef-object.h:310
cdef_object & operator=(const cdef_object &obj)
Definition: cdef-object.h:217
void put(const std::string &pname, const octave_value &val)
Definition: cdef-object.h:263
std::string class_name() const
Definition: cdef-object.h:234
cdef_object copy() const
Definition: cdef-object.h:250
octave_value get(const std::string &pname) const
Definition: cdef-object.h:268
octave_value get_value(bool do_check_access=true, const std::string &who="") const
void set_value(cdef_object &obj, const octave_value &val, bool do_check_access=true, const std::string &who="")
std::string get_name() const
bool is_constant() const
cdef_property(const cdef_object &obj)
cdef_property(const cdef_property &prop)
bool check_set_access() const
~cdef_property()=default
octave_value get_value(const cdef_object &obj, bool do_check_access=true, const std::string &who="") const
cdef_property(const std::string &nm)
bool check_get_access() const
bool bool_value(bool warn=false) const
Definition: ov.h:885
std::string string_value(bool force=false) const
Definition: ov.h:974
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988