annotate OctConf2013/examples/myobject.cc @ 8:50abddcc3409 default tip

Presentation delivered by cdf at OctConf 2013.
author Carlo de Falco <cdf@users.sourceforge.net>
date Mon, 22 Jul 2013 21:54:27 +0200
parents FEMTEC2013/examples/myobject.cc@60233b0075a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
1 // Copyright (C) 2012 Carlo de Falco <carlo.defalco@gmail.com>
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
2 //
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
3 // This program is free software; you can redistribute it and/or modify it under
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
4 // the terms of the GNU General Public License as published by the Free Software
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
5 // Foundation; either version 3 of the License, or (at your option) any later
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
6 // version.
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
7 //
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
8 // This program is distributed in the hope that it will be useful, but WITHOUT
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
11 // details.
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
12 //
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
13 // You should have received a copy of the GNU General Public License along with
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
14 // this program; if not, see <http://www.gnu.org/licenses/>.
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
15
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
16 #include <octave/oct.h>
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
17 #include <myobject.h>
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
18
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
19 void load_myobject_type ()
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
20 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
21 myobject::register_type ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
22 myobject_type_loaded = true;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
23 mlock ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
24 std::cout << "myobject_type_loaded" << std::endl;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
25 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
26
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
27 const std::string & myobject::get_str () const
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
28 { return str; }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
29
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
30
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
31 void myobject::set_str (std::string & _str)
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
32 { str = _str; }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
33
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
34
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
35 // PKG_ADD: autoload ("myobject_get_str", "myobject.oct");
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
36 DEFUN_DLD (myobject_get_str, args, ,
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
37 "get the str field")
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
38 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
39 octave_value retval;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
40 octave_idx_type nargin = args.length ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
41 if (nargin < 1 || nargin > 1)
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
42 print_usage ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
43 else
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
44 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
45
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
46 if (! myobject_type_loaded)
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
47 load_myobject_type ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
48
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
49 if (args(0).type_id () == myobject::static_type_id ())
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
50 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
51 const myobject & mo = static_cast<const myobject&> (args(0).get_rep ());
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
52 retval = octave_value (mo.get_str ());
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
53 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
54 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
55 return retval;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
56 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
57
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
58 // PKG_ADD: autoload ("myobject_init", "myobject.oct");
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
59 DEFUN_DLD (myobject_init, args, , "initialize a myobject")
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
60 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
61 octave_value retval = 0;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
62
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
63 octave_idx_type nargin = args.length ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
64 if (nargin != 3)
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
65 print_usage ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
66 else
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
67 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
68 if (! myobject_type_loaded)
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
69 load_myobject_type ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
70
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
71 std::string str = args(0).string_value ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
72 int i = args(1).int_value ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
73 double dbl = args(2).double_value ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
74
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
75 if (!error_state)
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
76 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
77 retval = new myobject (str, i, dbl);
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
78 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
79 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
80 return retval;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
81 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
82
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
83
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
84 // PKG_ADD: autoload ("myobject_set_str", "myobject.oct");
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
85 DEFUN_DLD (myobject_set_str, args, nargout,
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
86 "set the str field")
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
87 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
88 octave_value retval;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
89 octave_idx_type nargin = args.length ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
90 if (nargin < 2 || nargin > 2)
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
91 print_usage ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
92 else
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
93 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
94
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
95 if (! myobject_type_loaded)
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
96 load_myobject_type ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
97
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
98 std::string str = args(1).string_value ();
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
99 if (args(0).type_id () == myobject::static_type_id ())
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
100 {
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
101 const myobject mo (static_cast<const myobject&> (args(0).get_rep ()));
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
102 *(mo.p_str) = str;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
103 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
104 }
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
105 return retval;
0a1567794b40 [mq]: folder_reorganization
Carlo de Falco <cdf@users.sourceforge.net>
parents:
diff changeset
106 }