1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
.. _oop:
Object Oriented Programming in HOC
----------------------------------
See `Object Oriented Programming <http://www.neuron.yale.edu/neuron/static/docs/refman/obj.html>`_
in the reference manual.
.. note::
Classes defined in HOC may be accessed in Python via ``h.ClassName``.
.. index:: begintemplate (keyword)
.. _begintemplate:
begintemplate
~~~~~~~~~~~~~
Syntax:
``begintemplate``
Description:
Declare a new class or data structure. Any HOC code may appear between the
``begintemplate`` and ``endtemplate`` declarations. Classes are instantiated with
the new statement.
Example:
.. code-block::
none
begintemplate String
public s
strdef s
proc init() {
if (numarg()) {
s = $s1
}
}
endtemplate String
objref s
s = new String("Hello")
print s.s
will print "Hello" to the screen.
.. index:: endtemplate (keyword)
.. _endtemplate:
endtemplate
~~~~~~~~~~~
Syntax:
``endtemplate``
Description:
Closes the class declaration
.. seealso::
:ref:`begintemplate`
.. index:: objectvar (keyword)
.. _objectvar:
objectvar
~~~~~~~~~
Syntax:
``objectvar``
Description:
Synonym for :ref:`objref`.
.. index:: objref (keyword)
.. _objref:
objref
~~~~~~
Syntax:
``objref``
Description:
A comma separated list declarations of object variables. Object
variables are labels (pointers, references) to the actual objects. Thus ``o1 = o2``
merely states that o1 and o2 are labels for the same object. Objects are
created with the ``new`` statement. When there are no labels for an object
the object is deleted. The keywords ``objectvar`` and ``objref`` are synonyms.
An object has a unique name that can be determined with the ``print obj`` statement
and consists of the template name followed by an index number in brackets.
This name can be used in place of an objref.
Example:
.. code-block::
none
objref vec, g
vec = new Vector(20)
g = new Graph()
creates a vector object and a graph object with pointers named vec and g, respectively.
.. seealso::
:ref:`new`, :ref:`begintemplate`, :class:`List`, :ref:`mech`, :class:`SectionList`
.. index:: public (keyword)
.. _keyword_public:
public
~~~~~~
Syntax:
``public``
Description:
A comma separated list of all the names in a class that are available
outside the class.
.. seealso::
:ref:`begintemplate`
.. index:: external (keyword)
.. _external:
external
~~~~~~~~
Syntax:
``external``
Description:
A comma separated list of functions, procedures, iterators, objects,
strings, or variables defined at the top
level that can be executed within this class. This statement is
optional but if it exists must follow the begintemplate or public line.
This allows an object to get information from the outside and can
be used as information shared by all instances. External iterators
can only use local variables and arguments.
Example:
.. code-block::
none
global_ra = 100
func ra_value() {return global_ra}
begintemplate Cell
external ra_value
create axon
proc init() {
forall Ra = ra_value() /* just the axon */
}
endtemplate Cell
:func:`execute1` can be used to obtain external information as well.
.. index:: new (keyword)
.. _new:
new
~~~
Syntax:
``objectvariable = new Object(args)``
Description:
Creates a new object/instance of type/class Object and makes
objectvariable label/point to it.
When the object no longer is pointed to, it no longer exists.
Example:
.. code-block::
none
objref vec
vec = new Vector(30)
creates a vector of size 30 with its pointer named ``vec``.
----
.. function:: init
Syntax:
``proc init() { ... }``
Description:
If an init procedure is defined in a template, then it is called whenever
an instance of the template is created.
.. seealso::
:ref:`new`
----
.. function:: unref
Syntax:
``proc unref() { print this, " refcount=", $1 }``
Description:
If an unref procedure is defined in a template, then it is called whenever
the reference count of an object of that type is decremented. The reference
count is passed as the argument. When the count is 0, the object will be
destroyed on return from unref. This is useful in properly managing
objects which mutually reference each other. Note that unref may be
called recursively.
----
.. index:: NULLobject
.. _nil:
NULLobject
~~~~~~~~~~
Syntax:
``objref nil``
Description:
When an object variable is first declared, it refers to NULLobject
until it has been associated with an instance of some object class
by a :ref:`new` statement.
A NULLobject object variable can
be useful as an argument to certain class methods.
Example:
.. code-block::
none
objref nil
print nil // prints NULLobject
----
.. data:: this
Syntax:
``objref this``
Description:
Declared inside a template
(see :ref:`begintemplate`).
Allows the object to call a procedure
with itself as one of the arguments.
Example:
.. code-block::
none
begintemplate Demothis
public printname
objref this
proc init() {
printname()
}
proc printname() {
print "I am ", this
}
endtemplate Demothis
objref foo[3]
print "at creation"
for i=0,2 foo[i]=new Demothis()
print "check existing"
for i=0,2 foo[i].printname()
|