1 /*
2  *  connection_label.h
3  *
4  *  This file is part of NEST.
5  *
6  *  Copyright (C) 2004 The NEST Initiative
7  *
8  *  NEST is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation, either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  NEST is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with NEST.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef CONNECTION_LABEL_H
24 #define CONNECTION_LABEL_H
25 
26 #include "nest.h"
27 #include "nest_names.h"
28 #include "dictdatum.h"
29 #include "dictutils.h"
30 
31 namespace nest
32 {
33 class ConnectorModel;
34 
35 /**
36  * Connections are unlabeled by default. Unlabeled connections cannot be
37  * specified
38  * as a search criterion in the `GetConnections` function.
39  * @see ConnectionLabel
40  */
41 const static long UNLABELED_CONNECTION = -1;
42 
43 /**
44  * The class ConnectionLabel enables synapse model to be labeled by a positive
45  * integer. The label can be set / retrieved with the `names::synapse_label`
46  * property in the parameter dictionary of `Set/GetStatus` or `Connect`.
47  * Using the `GetConnections` function, synapses with the same label can be
48  * specified.
49  *
50  * The name of synapse models, which can be labeled, end with '_lbl'.
51  * @see nest::ConnectionManager::get_connections
52  */
53 template < typename ConnectionT >
54 class ConnectionLabel : public ConnectionT
55 {
56 public:
57   ConnectionLabel();
58 
59   /**
60    * Get all properties of this connection and put them into a dictionary.
61    */
62   void get_status( DictionaryDatum& d ) const;
63 
64   /**
65    * Set properties of this connection from the values given in dictionary.
66    *
67    * @note Target and Rport cannot be changed after a connection has been
68    * created.
69    */
70   void set_status( const DictionaryDatum& d, ConnectorModel& cm );
71 
72   long get_label() const;
73 
74 private:
75   long label_;
76 };
77 
78 template < typename ConnectionT >
ConnectionLabel()79 ConnectionLabel< ConnectionT >::ConnectionLabel()
80   : ConnectionT()
81   , label_( UNLABELED_CONNECTION )
82 {
83 }
84 
85 template < typename ConnectionT >
86 void
get_status(DictionaryDatum & d)87 ConnectionLabel< ConnectionT >::get_status( DictionaryDatum& d ) const
88 {
89   ConnectionT::get_status( d );
90   def< long >( d, names::synapse_label, label_ );
91   // override names::size_of from ConnectionT,
92   // as the size from ConnectionLabel< ConnectionT > is
93   // one long larger
94   def< long >( d, names::size_of, sizeof( *this ) );
95 }
96 
97 template < typename ConnectionT >
98 void
set_status(const DictionaryDatum & d,ConnectorModel & cm)99 ConnectionLabel< ConnectionT >::set_status( const DictionaryDatum& d, ConnectorModel& cm )
100 {
101   long lbl;
102   if ( updateValue< long >( d, names::synapse_label, lbl ) )
103   {
104     if ( lbl >= 0 )
105     {
106       label_ = lbl;
107     }
108     else
109     {
110       throw BadProperty( "Connection label must not be negative." );
111     }
112   }
113   ConnectionT::set_status( d, cm );
114 }
115 
116 template < typename ConnectionT >
117 inline long
get_label()118 ConnectionLabel< ConnectionT >::get_label() const
119 {
120   return label_;
121 }
122 
123 
124 } // namespace nest
125 
126 
127 #endif /* CONNECTION_LABEL_H */
128