Orocos Real-Time Toolkit  2.9.0
ConnPolicy.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:08 CEST 2009 ConnPolicy.cpp
3 
4  ConnPolicy.cpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Peter Soetens
8  email : peter@thesourcworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 /*
40  * ConnPolicy.cpp
41  *
42  * Created on: Oct 20, 2009
43  * Author: kaltan
44  */
45 
46 #include "ConnPolicy.hpp"
47 #include "Property.hpp"
48 #include "PropertyBag.hpp"
49 
50 #include <boost/lexical_cast.hpp>
51 #include <iostream>
52 
53 using namespace std;
54 
55 namespace RTT
56 {
58  ConnPolicy::ConnPolicy(const ConnPolicyDefault &)
59  : type(DATA)
60  , size(0)
61  , lock_policy(LOCK_FREE)
62  , init(false)
63  , pull(false)
64  , buffer_policy(PerConnection)
65  , max_threads(0)
66  , mandatory(true)
67  , transport(0)
68  , data_size(0)
69  {}
70 
71  ConnPolicy &ConnPolicy::Default()
72  {
73  static ConnPolicy *s_default_policy = new ConnPolicy(ConnPolicyDefault());
74  return *s_default_policy;
75  }
76 
77  ConnPolicy ConnPolicy::buffer(int size, int lock_policy /*= LOCK_FREE*/, bool init_connection /*= false*/, bool pull /*= false*/)
78  {
79  ConnPolicy result;
80  result.type = BUFFER;
81  result.size = size;
82  result.lock_policy = lock_policy;
83  result.init = init_connection;
84  result.pull = pull;
85  return result;
86  }
87 
88  ConnPolicy ConnPolicy::circularBuffer(int size, int lock_policy /*= LOCK_FREE*/, bool init_connection /*= false*/, bool pull /*= false*/)
89  {
90  ConnPolicy result;
91  result.type = CIRCULAR_BUFFER;
92  result.size = size;
93  result.lock_policy = lock_policy;
94  result.init = init_connection;
95  result.pull = pull;
96  return result;
97  }
98 
99  ConnPolicy ConnPolicy::data(int lock_policy /*= LOCK_FREE*/, bool init_connection /*= true*/, bool pull /*= false*/)
100  {
101  ConnPolicy result;
102  result.type = DATA;
103  result.lock_policy = lock_policy;
104  result.init = init_connection;
105  result.pull = pull;
106  return result;
107  }
108 
109  ConnPolicy::ConnPolicy()
110  : type(Default().type)
111  , size(Default().size)
112  , lock_policy(Default().lock_policy)
113  , init(Default().init)
114  , pull(Default().pull)
115  , buffer_policy(Default().buffer_policy)
116  , max_threads(Default().max_threads)
117  , mandatory(Default().mandatory)
118  , transport(Default().transport)
119  , data_size(Default().data_size)
120  {}
121 
123  : type(type)
124  , size(Default().size)
126  , init(Default().init)
127  , pull(Default().pull)
133  {}
134 
136  : type(type)
137  , size(Default().size)
138  , lock_policy(lock_policy)
139  , init(Default().init)
140  , pull(Default().pull)
146  {}
147 
148  std::ostream &operator<<(std::ostream &os, const ConnPolicy &cp)
149  {
150  std::string type;
151  switch(cp.type) {
152  case ConnPolicy::UNBUFFERED: type = "UNBUFFERED"; break;
153  case ConnPolicy::DATA: type = "DATA"; break;
154  case ConnPolicy::BUFFER: type = "BUFFER"; break;
155  case ConnPolicy::CIRCULAR_BUFFER: type = "CIRCULAR_BUFFER"; break;
156  default: type = "(unknown type)"; break;
157  }
158  if (cp.size > 0) {
159  type += "[" + boost::lexical_cast<std::string>(cp.size) + "]";
160  }
161 
162  std::string lock_policy;
163  switch(cp.lock_policy) {
164  case ConnPolicy::UNSYNC: lock_policy = "UNSYNC"; break;
165  case ConnPolicy::LOCKED: lock_policy = "LOCKED"; break;
166  case ConnPolicy::LOCK_FREE: lock_policy = "LOCK_FREE"; break;
167  default: lock_policy = "(unknown lock policy)"; break;
168  }
169 
170  std::string pull;
171  // note: cast to int to suppress clang "warning: switch condition has boolean value"
172  switch(int(cp.pull)) {
173  case int(ConnPolicy::PUSH): pull = "PUSH"; break;
174  case int(ConnPolicy::PULL): pull = "PULL"; break;
175  }
176 
177  os << pull << " ";
178  os << BufferPolicy(cp.buffer_policy) << " ";
179  os << lock_policy << " ";
180  os << type;
181  if (!cp.name_id.empty()) os << " (name_id=" << cp.name_id << ")";
182  if (cp.max_threads > 0) os << " (max_threads=" << cp.max_threads << ")";
183 
184  return os;
185  }
186 }
static const int CIRCULAR_BUFFER
Definition: ConnPolicy.hpp:113
static ConnPolicy & Default()
Returns the process-wide default ConnPolicy that serves as a template for new ConnPolicy instances...
Definition: ConnPolicy.cpp:71
ConnPolicy()
Constructs a new ConnPolicy instance based on the current default settings as returned by ConnPolicy:...
Definition: ConnPolicy.cpp:109
int data_size
Suggest the payload size of the data sent over this channel.
Definition: ConnPolicy.hpp:248
static const bool PULL
Definition: ConnPolicy.hpp:120
STL namespace.
int lock_policy
This is the locking policy on the connection.
Definition: ConnPolicy.hpp:196
static const int LOCKED
Definition: ConnPolicy.hpp:116
int type
DATA, BUFFER or CIRCULAR_BUFFER.
Definition: ConnPolicy.hpp:190
static const bool PUSH
Definition: ConnPolicy.hpp:119
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:107
static const int DATA
Definition: ConnPolicy.hpp:111
int size
If the connection is a buffered connection, the size of the buffer.
Definition: ConnPolicy.hpp:193
bool pull
If true, then the sink will have to pull data.
Definition: ConnPolicy.hpp:209
bool mandatory
Whether the connection described by this connection policy is mandatory, which means that write opera...
Definition: ConnPolicy.hpp:232
int max_threads
The maximum number of threads that will access the connection data or buffer object.
Definition: ConnPolicy.hpp:224
static const int LOCK_FREE
Definition: ConnPolicy.hpp:117
bool init
If true, one should initialize the connection&#39;s value with the last value written on the writer port...
Definition: ConnPolicy.hpp:203
std::ostream & operator<<(std::ostream &os, const BufferPolicy &bp)
BufferPolicy
The BufferPolicy controls how multiple connections to the same input or output port are handled in ca...
int transport
The prefered transport used.
Definition: ConnPolicy.hpp:238
static const int UNSYNC
Definition: ConnPolicy.hpp:115
int buffer_policy
The policy on how buffer elements will be installed for this connection, which influences the behavio...
Definition: ConnPolicy.hpp:216
static const int BUFFER
Definition: ConnPolicy.hpp:112
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
std::string name_id
The name of this connection.
Definition: ConnPolicy.hpp:256
static const int UNBUFFERED
Definition: ConnPolicy.hpp:110