1 #ifndef H_CANOPEN_LAYER 2 #define H_CANOPEN_LAYER 5 #include <boost/thread/shared_mutex.hpp> 6 #include <boost/shared_ptr.hpp> 7 #include <boost/atomic.hpp> 8 #include <boost/exception/diagnostic_information.hpp> 20 virtual void set(
const State &s,
const std::string &r){
21 boost::mutex::scoped_lock lock(write_mutex_);
22 if(s > state) state = s;
24 if(reason_.empty()) reason_ = r;
25 else reason_ +=
"; " + r;
35 template<
typename T>
bool bounded()
const{
return state <= T::state; }
39 int get()
const {
return state; }
41 const std::string
reason()
const { boost::mutex::scoped_lock lock(write_mutex_);
return reason_; }
43 const void warn(
const std::string & r) {
set(
WARN, r); }
44 const void error(
const std::string & r) {
set(
ERROR, r); }
45 const void stale(
const std::string & r) {
set(
STALE, r); }
48 std::vector<std::pair<std::string, std::string> >
values_;
50 const std::vector<std::pair<std::string, std::string> > &
values()
const {
return values_; }
51 template<
typename T>
void add(
const std::string &key,
const T &value) {
52 std::stringstream str;
54 values_.push_back(std::make_pair(key,str.str()));
58 #define CATCH_LAYER_HANDLER_EXCEPTIONS(command, status) \ 60 catch(std::exception &e) {status.error(boost::diagnostic_information(e)); } 137 virtual void handleShutdown(
LayerStatus &status) = 0;
140 virtual void handleRecover(
LayerStatus &status) = 0;
152 template<
typename Bound,
typename Data,
typename FuncType>
typename vector_type::iterator
call(FuncType func, Data &status){
153 boost::shared_lock<boost::shared_mutex> lock(mutex);
154 return call<Bound>(func, status, layers.begin(), layers.end());
156 template<
typename Data,
typename FuncType>
typename vector_type::iterator
call(FuncType func, Data &status){
157 boost::shared_lock<boost::shared_mutex> lock(mutex);
158 return call<LayerStatus::Unbounded>(func, status, layers.begin(), layers.end());
160 template<
typename Bound,
typename Data,
typename FuncType>
typename vector_type::reverse_iterator
call_rev(FuncType func, Data &status){
161 boost::shared_lock<boost::shared_mutex> lock(mutex);
162 return call<Bound>(func, status, layers.rbegin(), layers.rend());
164 template<
typename Data,
typename FuncType>
typename vector_type::reverse_iterator
call_rev(FuncType func, Data &status){
165 boost::shared_lock<boost::shared_mutex> lock(mutex);
166 return call<LayerStatus::Unbounded>(func, status, layers.rbegin(), layers.rend());
168 void destroy() { boost::unique_lock<boost::shared_mutex> lock(mutex); layers.clear(); }
171 virtual void add(
const VectorMemberSharedPtr &l) { boost::unique_lock<boost::shared_mutex> lock(mutex); layers.push_back(l); }
173 template<
typename Bound,
typename Data,
typename FuncType>
bool callFunc(FuncType func, Data &status){
174 boost::shared_lock<boost::shared_mutex> lock(mutex);
175 return call<Bound>(func, status, layers.begin(), layers.end()) == layers.end();
181 template<
typename Bound,
typename Iterator,
typename Data,
typename FuncType> Iterator
call(FuncType func, Data &status,
const Iterator &begin,
const Iterator &end){
182 bool okay_on_start = status.template bounded<Bound>();
184 for(Iterator it = begin; it != end; ++it){
185 ((**it).*func)(status);
186 if(okay_on_start && !status.template bounded<Bound>()){
192 template<
typename Iterator,
typename Data,
typename FuncType> Iterator
call(FuncType func, Data &status,
const Iterator &begin,
const Iterator &end){
193 return call<LayerStatus::Unbounded, Iterator, Data>(func, status, begin, end);
199 template<
typename Data,
typename FuncType,
typename FailType>
void call_or_fail(FuncType func, FailType fail, Data &status){
200 this->
template call(func, status);
201 if(!status.template bounded<LayerStatus::Warn>()){
202 this->
template call(fail, status);
203 (this->*fail)(status);
206 template<
typename Data,
typename FuncType,
typename FailType>
void call_or_fail_rev(FuncType func, FailType fail, Data &status){
207 this->
template call_rev(func, status);
208 if(!status.template bounded<LayerStatus::Warn>()){
209 this->
template call_rev(fail, status);
210 (this->*fail)(status);
virtual void add(const VectorMemberSharedPtr &l)
Iterator call(FuncType func, Data &status, const Iterator &begin, const Iterator &end)
void init(LayerStatus &status)
vector_type::reverse_iterator call_rev(FuncType func, Data &status)
virtual void handleWrite(LayerStatus &status, const LayerState ¤t_state)
boost::shared_mutex mutex
LayerGroupNoDiag(const std::string &n)
Iterator call(FuncType func, Data &status, const Iterator &begin, const Iterator &end)
void shutdown(LayerStatus &status)
virtual void diag(LayerReport &report)
void read(LayerStatus &status)
std::vector< std::pair< std::string, std::string > > values_
const void warn(const std::string &r)
vector_type::iterator call(FuncType func, Data &status)
const std::string reason() const
boost::atomic< LayerState > state
void recover(LayerStatus &status)
virtual void handleHalt(LayerStatus &status)
virtual void handleInit(LayerStatus &status)
virtual void handleRecover(LayerStatus &status)
void halt(LayerStatus &status)
LayerGroup(const std::string &n)
LayerStack(const std::string &n)
boost::atomic< State > state
void call_or_fail(FuncType func, FailType fail, Data &status)
virtual void handleRead(LayerStatus &status, const LayerState ¤t_state)
bool callFunc(FuncType func, Data &status)
LayerState getLayerState()
void call_or_fail_rev(FuncType func, FailType fail, Data &status)
const std::vector< std::pair< std::string, std::string > > & values() const
#define CATCH_LAYER_HANDLER_EXCEPTIONS(command, status)
virtual void handleDiag(LayerReport &report)
std::vector< VectorMemberSharedPtr > vector_type
vector_type::iterator call(FuncType func, Data &status)
void add(const std::string &key, const T &value)
void write(LayerStatus &status)
boost::shared_ptr< T > VectorMemberSharedPtr
void diag(LayerReport &report)
const void error(const std::string &r)
vector_type::reverse_iterator call_rev(FuncType func, Data &status)
virtual void handleWrite(LayerStatus &status, const LayerState ¤t_state)
const void stale(const std::string &r)
virtual void handleShutdown(LayerStatus &status)
Layer(const std::string &n)
virtual void handleDiag(LayerReport &report)
virtual void handleShutdown(LayerStatus &status)
boost::mutex write_mutex_