SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
GenericCallbacks.hpp
1 /* This file is part of scl, a control and simulation library
2 for robots and biomechanical models.
3 
4 scl is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8 
9 Alternatively, you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
13 
14 scl is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public
20 License and a copy of the GNU General Public License along with
21 scl. If not, see <http://www.gnu.org/licenses/>.
22  */
23 /* \file GenericCallbacks.hpp
24  *
25  * Created on: Sep 18, 2011
26  *
27  * Copyright (C) 2011
28  *
29  * Author: Samir Menon <smenon@stanford.edu>
30  */
31 
32 #ifndef GENERICCALLBACKS_HPP_
33 #define GENERICCALLBACKS_HPP_
34 
35 #include <sutil/CRegisteredCallbacks.hpp>
36 #include <sutil/CRegisteredPrintables.hpp>
37 
38 #include <string>
39 #include <iostream>
40 
41 
42 namespace scl
43 {
51  class CCallbackEcho : public sutil::CCallbackBase<std::string, std::vector<std::string> >
53  public:
54  virtual void call(std::vector<std::string>& arg)
55  {//Print out all the arguments (except the first, which is the command)
56  if(1 >= arg.size()) {}
57  else if("--help" == arg[1])
58  { std::cout<<" >>echo\n Echoes a string back on to the screen"; }
59  else
60  {
61  std::vector<std::string>::iterator it, ite;
62  for(it = arg.begin()+1, ite = arg.end(); it != ite; ++it)
63  { std::cout<<*it<<" "; }
64  }
65  }
66 
67  virtual base* createObject()
68  { return dynamic_cast<base*>(new CCallbackEcho()); }
69  };
70 
77  class CCallbackPrint : public sutil::CCallbackBase<std::string, std::vector<std::string> >
79  public:
80  virtual void call(std::vector<std::string>& arg)
81  {//Print out all the arguments (except the first, which is the command)
82  if(1 >= arg.size())
83  { std::cout<<"Object not found"; }
84  else if("--help" == arg[1])
85  { std::cout<<" >>print <object_name>\n Finds the object in the print registry and prints its contents"; }
86  else
87  {//Prints the contents of the object
88  std::vector<std::string>::iterator it, ite;
89  for(it = arg.begin()+1, ite = arg.end(); it != ite; ++it)
90  {
92  if(NULL == obj)
93  { std::cout<<"Object not found"; }
94  else
95  { std::cout<<(*obj); }
96  }
97  }
98  }
99 
100  virtual base* createObject()
101  { return dynamic_cast<base*>(new CCallbackPrint()); }
102  };
103 
104 
111  class CCallbackSet : public sutil::CCallbackBase<std::string, std::vector<std::string> >
113  public:
114  virtual void call(std::vector<std::string>& arg)
115  {//Set out all the arguments (except the first, which is the command)
116  if(1 >= arg.size())
117  { std::cout<<"Object not found"; }
118  else if("--help" == arg[1])
119  { std::cout<<" >>set <object_name> <arg_str1> <arg_str2> ... <arg_strN>"
120  <<"\n Sets the contents of an object in the database"; }
121  else
122  {//Sets the contents of the object
123 // const scl::SObject** obj = scl::CObjectMap::getData()->at_const(*(arg.begin()+1));
124 // if(NULL == obj) { std::cout<<"Object not found"; }
125 // else
126 // { std::cout<<(*obj); }
127  }
128  }
129 
130  virtual base* createObject()
131  { return dynamic_cast<base*>(new CCallbackSet()); }
132  };
133 
136  class CCallbackHelp : public sutil::CCallbackBase<std::string, std::vector<std::string> >
138  public:
139  virtual void call(std::vector<std::string>& arg)
140  {//Help out all the arguments (except the first, which is the command)
141  std::vector<std::string> idxlist;
142  //Get a list of all the callbacks in the registry
143  if(false == sutil::callbacks::list(idxlist))
144  { std::cout<<"Callback registry is empty"; }
145  else
146  {
147  if(1 >= idxlist.size())
148  { std::cout<<"Available commands:"; }
149  std::vector<std::string>::iterator it,ite;
150  for(it = idxlist.begin(), ite = idxlist.end(); it!=ite; ++it)
151  { std::cout<<" "<<(*it); } //Print all the callbacks
152  std::cout<<"\nFor details, type: <command> --help\nTo switch to char mode, type: x\nTo exit, type: exit";
153  }
154  }
155 
156  virtual base* createObject()
157  { return dynamic_cast<base*>(new CCallbackHelp()); }
158  };
159 
162  class CCallbackDecrement : public sutil::CCallbackBase<char, bool, double>
163  {
165  public:
167  virtual void call(bool& arg) { if(arg) (*data_)-=0.1; else (*data_)-=0.02; }
168  virtual base* createObject() { return dynamic_cast<base*>(new CCallbackDecrement()); }
169  };
170 
173  class CCallbackIncrement : public sutil::CCallbackBase<char, bool, double>
174  {
176  public:
177  CCallbackIncrement() {}
178  virtual void call(bool& arg) { if(arg) (*data_)+=0.1; else (*data_)+=0.02; }
179  virtual base* createObject() { return dynamic_cast<base*>(new CCallbackIncrement()); }
180  };
181 }
182 
183 #endif /* GENERICCALLBACKS_HPP_ */
virtual base * createObject()
Definition: GenericCallbacks.hpp:67
virtual void call(std::vector< std::string > &arg)
Definition: GenericCallbacks.hpp:139
virtual void call(bool &arg)
Definition: GenericCallbacks.hpp:178
bool list(std::vector< Idx > &idxlist)
Definition: CRegisteredCallbacks.hpp:134
Definition: GenericCallbacks.hpp:173
virtual void call(std::vector< std::string > &arg)
Definition: GenericCallbacks.hpp:54
virtual base * createObject()
Definition: GenericCallbacks.hpp:100
virtual base * createObject()
Definition: GenericCallbacks.hpp:168
virtual void call(bool &arg)
Definition: GenericCallbacks.hpp:167
Definition: GenericCallbacks.hpp:111
const SPrintableBase * get(const std::string &arg_name)
Definition: CRegisteredPrintables.hpp:91
Definition: GenericCallbacks.hpp:162
Definition: CRegisteredPrintables.hpp:122
Definition: GenericCallbacks.hpp:51
Definition: CRegisteredCallbacks.hpp:44
virtual base * createObject()
Definition: GenericCallbacks.hpp:130
Definition: GenericCallbacks.hpp:77
virtual void call(std::vector< std::string > &arg)
Definition: GenericCallbacks.hpp:80
virtual base * createObject()
Definition: GenericCallbacks.hpp:156
Definition: GenericCallbacks.hpp:136
virtual base * createObject()
Definition: GenericCallbacks.hpp:179
virtual void call(std::vector< std::string > &arg)
Definition: GenericCallbacks.hpp:114