SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CRegisteredPrintables.hpp
1 /* This file is part of sUtil, a random collection of utilities.
2 
3 sUtil is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 
8 Alternatively, you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 sUtil 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 Lesser General Public
19 License and a copy of the GNU General Public License along with
20 sUtil. If not, see <http://www.gnu.org/licenses/>.
21  */
22 /* \file CRegisteredPrintables.hpp
23  *
24  * Created on: Sep 20, 2011
25  *
26  * Copyright (C) 2011, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef CREGISTEREDPRINTABLES_HPP_
30 #define CREGISTEREDPRINTABLES_HPP_
31 
32 #include <sutil/CMappedList.hpp>
33 #include <sutil/CSingleton.hpp>
34 
35 #include <string>
36 
37 namespace sutil
38 {
41  template<typename ObjectType>
42  void printToStream(std::ostream&, const ObjectType&);
43 
44  // Forward declarations. Anything that subclasses this is printable on
45  // stl streams. Ie. Supports the 'stream<<object' operator style.
46  class SPrintableBase;
47 
48  // This helps the map to work properly through typical object orientation.
49  // And saves you from writing redundant code for every type.
50  template<typename ObjectType> class SPrintable;
51 
57 
81  namespace printables
82  {
91  const SPrintableBase* get(const std::string& arg_name)
92  {
93  SPrintableBase** ret = CRegisteredPrintables::getData()->at(arg_name);
94  if(NULL == ret) { return NULL; }
95  return static_cast<const SPrintableBase*>(*ret);
96  }
97 
101  template<typename ObjectType>
102  bool add(const std::string& arg_name,
103  const ObjectType& arg_obj)
104  {
105  SPrintable<ObjectType> tmp_printable_object(arg_obj);
106  SPrintableBase** obj = CRegisteredPrintables::getData()->create(arg_name);
107  if(NULL == obj){ return false; }
108  *obj = tmp_printable_object.createObject();
109  return true;
110  }
111 
115  }
116 
123  {
124  public:
126  virtual ~SPrintableBase(){}
127 
128  virtual void printDataToStream(std::ostream& arg_stream) const =0;
129 
130  virtual SPrintableBase* createObject() const =0;
131 
132  //The only way you can overload << (it is a global function)
133  friend std::ostream& operator<<(std::ostream& outstr, const SPrintableBase& me);
134 
135  protected:
136  SPrintableBase(){}
137  private:
138  SPrintableBase(const SPrintableBase&);
139  SPrintableBase& operator =(const SPrintableBase&);
140  };
141 
143  std::ostream& operator<<(std::ostream& out, const SPrintableBase& me)
144  { me.printDataToStream(out); return out; }
145 
154  template<typename ObjectType>
155  class SPrintable : public SPrintableBase
156  {
157  public:
158  SPrintable(const ObjectType& arg_data) :
159  SPrintableBase(), data_(arg_data) {}
160 
161  virtual void printDataToStream(std::ostream& ostr) const
162  { printToStream<ObjectType>(ostr, data_); }
163 
164  virtual SPrintableBase* createObject() const
165  { return new SPrintable<ObjectType>(data_); }
166 
167  const ObjectType& data_;
168 
169  private: //To enforce implementers to pass an object (which is then registered)
170  SPrintable();
171  SPrintable(const SPrintable&);
172  SPrintable& operator=(const SPrintable&);
173  };
174 }
175 
176 #endif /* CREGISTEREDPRINTABLES_HPP_ */
friend std::ostream & operator<<(std::ostream &outstr, const SPrintableBase &me)
Definition: CRegisteredPrintables.hpp:143
Definition: CSingleton.hpp:47
Definition: CRegisteredPrintables.hpp:122
static SDataStruct * getData()
Definition: CSingleton.hpp:87
Definition: CRegisteredPrintables.hpp:50
bool reset()
Definition: CRegisteredPrintables.hpp:114
static bool resetData()
Definition: CSingleton.hpp:107
virtual ~SPrintableBase()
Definition: CRegisteredPrintables.hpp:126
bool add(const std::string &arg_name, const ObjectType &arg_obj)
Definition: CRegisteredPrintables.hpp:102