iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RemoteCommandImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_CORE_BLACKBOARD_NETWORK_DETAILS_REMOTECOMMANDIMPL_H
36 #define INCLUDE_NRT_CORE_BLACKBOARD_NETWORK_DETAILS_REMOTECOMMANDIMPL_H
37 
39 #include <boost/lexical_cast.hpp>
40 
41 // Define a bunch of macros
43 
44 // ######################################################################
45 inline nrt::blackboard::RemoteCommand::RemoteCommand(nrt::blackboard::Command c) :
46  command(c)
47 { }
48 
49 // ######################################################################
51 { args.clear(); }
52 
53 // ######################################################################
54 template <typename T> inline
55 T nrt::blackboard::RemoteCommand::get(std::string const & key) const
56 {
57 #define BBWHAT "command [" + boost::lexical_cast<std::string>(command) + "]: key [" + key + "] not found."
58 
59  auto itr = args.find(key);
60  if (itr == args.end()) BBTHROW(InternalInconsistency);
61 
62  // Found it, try to cast to desired type:
63  try { return boost::lexical_cast<T>(itr->second); }
64  catch (boost::bad_lexical_cast const & e)
65  { BBTHROWX(InternalInconsistency, std::string("Bad lexical_cast: ") + e.what()); }
66  catch(...)
67  { BBTHROWX(InternalInconsistency, "Unknown error during lexical_cast"); }
68 
69 #undef BBWHAT
70 }
71 
72 // ######################################################################
73 template <typename T> inline
74 T nrt::blackboard::RemoteCommand::serget(std::string const & key) const
75 {
76 #define BBWHAT "command [" + boost::lexical_cast<std::string>(command) + "]: key [" + key + "] not found."
77 
78  auto itr = args.find(key);
79  if (itr == args.end()) BBTHROW(InternalInconsistency);
80 
81  // Found it, try to cast to desired type:
82  try { T value; std::istringstream is(itr->second); nrt::iarchive archive(is); archive(value); return value; }
83  catch (cereal::Exception const & e)
84  { BBTHROWX(MessageSerializationError, "Could not deserialize data :" + std::string(e.what())); }
85  catch(...)
86  { BBTHROWX(InternalInconsistency, "Unknown error during serget"); }
87 
88 #undef BBWHAT
89 }
90 
91 // ######################################################################
92 template <typename T> inline
93 void nrt::blackboard::RemoteCommand::set(std::string const & key, T const & value)
94 {
95 #define BBWHAT "command [" + boost::lexical_cast<std::string>(command) + "]: key [" + key + "] trying to set value."
96 
97  try { args[key] = boost::lexical_cast<std::string>(value); }
98  catch (boost::bad_lexical_cast const & e)
99  { BBTHROWX(InternalInconsistency, std::string("Bad lexical_cast: ") + e.what()); }
100  catch(...)
101  { BBTHROWX(InternalInconsistency, "Unknown error during lexical_cast"); }
102 
103 #undef BBWHAT
104 }
105 
106 // ######################################################################
107 template <typename T> inline
108 void nrt::blackboard::RemoteCommand::serset(std::string const & key, T const & value)
109 {
110 #define BBWHAT "command [" + boost::lexical_cast<std::string>(command) + "]: key [" + key + "] trying to set value."
111 
112  try { std::ostringstream os; nrt::oarchive archive(os); archive(value); args[key] = os.str(); }
113  catch (cereal::Exception const & e)
114  { BBTHROWX(MessageSerializationError, "Could not serialize data :" + std::string(e.what())); }
115  catch(...)
116  { BBTHROWX(InternalInconsistency, "Unknown error during serialization"); }
117 
118 #undef BBWHAT
119 }
120 
121 // Un-define a bunch of macros
123 
124 
125 #endif // INCLUDE_NRT_CORE_BLACKBOARD_NETWORK_DETAILS_REMOTECOMMANDIMPL_H