Goby v2
message_var.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #include <boost/foreach.hpp>
24 
25 #include "goby/moos/moos_string.h"
26 #include "goby/util/as.h"
27 
28 #include "message.h"
29 #include "message_algorithms.h"
30 #include "message_val.h"
31 #include "message_var.h"
32 
33 goby::transitional::DCCLMessageVar::DCCLMessageVar()
34  : array_length_(1), is_key_frame_(true), sequence_number_(-1), source_set_(false),
35  ap_(DCCLAlgorithmPerformer::getInstance())
36 {
37 }
38 
39 void goby::transitional::DCCLMessageVar::initialize(const DCCLMessage& msg)
40 {
41  // add trigger_var_ as source_var for any message_vars without a source
42  if (!source_set_)
43  source_var_ = msg.trigger_var();
44 
45  BOOST_FOREACH (const std::string& alg, algorithms_)
46  ap_->check_algorithm(alg, msg);
47 
48  initialize_specific();
49 }
50 
51 void goby::transitional::DCCLMessageVar::set_defaults(
52  std::map<std::string, std::vector<DCCLMessageVal> >& vals, unsigned modem_id, unsigned id)
53 {
54  vals[name_].resize(array_length_);
55 
56  std::vector<DCCLMessageVal>& vm = vals[name_];
57 
58  for (std::vector<DCCLMessageVal>::size_type i = 0, n = vm.size(); i < n; ++i)
59  set_defaults_specific(vm[i], modem_id, id);
60 }
61 
62 void goby::transitional::DCCLMessageVar::write_schema_to_dccl2(std::ofstream* proto_file,
63  int sequence_number)
64 {
65  sequence_number_ = sequence_number;
66 
67  *proto_file << "\t";
68 
69  if (array_length_ == 1)
70  *proto_file << "optional ";
71  else
72  *proto_file << "repeated ";
73 
74  std::string enum_name;
75  if (type() == dccl_enum)
76  {
77  enum_name = name_ + "Enum";
78  enum_name[0] = toupper(enum_name[0]);
79  *proto_file << enum_name;
80  }
81  else
82  {
83  *proto_file << type_to_protobuf_type(type());
84  }
85 
86  *proto_file << " " << name_ << " = " << sequence_number;
87 
88  int count = 0;
89  try
90  {
91  double max_tmp = max();
92 
93  *proto_file << (count ? ", " : " [");
94  ++count;
95  *proto_file << "(dccl.field).max="
96  << goby::util::as<std::string>(max_tmp, precision(), goby::util::FLOAT_FIXED);
97  }
98  catch (...)
99  {
100  }
101  try
102  {
103  double min_tmp = min();
104  *proto_file << (count ? ", " : " [");
105  ++count;
106  *proto_file << "(dccl.field).min="
107  << goby::util::as<std::string>(min_tmp, precision(), goby::util::FLOAT_FIXED);
108  }
109  catch (...)
110  {
111  }
112  try
113  {
114  if (type() == dccl_float)
115  {
116  int precision_tmp = precision();
117  *proto_file << (count ? ", " : " [");
118  ++count;
119  *proto_file << "(dccl.field).precision=" << precision_tmp;
120  }
121  }
122  catch (...)
123  {
124  }
125  try
126  {
127  unsigned max_length_tmp = max_length();
128  *proto_file << (count ? ", " : " [");
129  ++count;
130  *proto_file << "(dccl.field).max_length=" << max_length_tmp;
131  }
132  catch (...)
133  {
134  }
135  try
136  {
137  unsigned num_bytes_tmp = num_bytes();
138  *proto_file << (count ? ", " : " [");
139  ++count;
140  *proto_file << "(dccl.field).max_length=" << num_bytes_tmp;
141  }
142  catch (...)
143  {
144  }
145  try
146  {
147  std::string static_val_tmp = static_val();
148  *proto_file << (count ? ", " : " [");
149  ++count;
150  *proto_file << "default=\"" << static_val_tmp << "\", (dccl.field).static_value=\""
151  << static_val_tmp << "\", "
152  << "(dccl.field).codec=\"_static\"";
153  }
154  catch (...)
155  {
156  }
157 
158  if (array_length_ != 1)
159  {
160  *proto_file << (count ? ", " : " [");
161  ++count;
162  *proto_file << "(dccl.field).max_repeat=" << array_length_;
163  }
164 
165  std::string more_options = additional_option_extensions();
166  if (!more_options.empty())
167  {
168  *proto_file << (count ? ", " : " [");
169  ++count;
170  *proto_file << more_options;
171  }
172 
173  if (count)
174  *proto_file << "]";
175 
176  *proto_file << ";" << std::endl;
177 
178  if (type() == dccl_enum)
179  {
180  *proto_file << "\t"
181  << "enum " << enum_name << "{ \n";
182 
183  int enum_value = 0;
184  BOOST_FOREACH (const std::string& e, *enums())
185  *proto_file << "\t\t " << boost::to_upper_copy(name_) << "_" << e << " = "
186  << enum_value++ << "; \n";
187 
188  *proto_file << "\t} \n";
189  }
190 }
DCCLCppType type() const
what type is the original type of this DCCLMessageVal?
Definition: message_val.h:139
STL namespace.
The global namespace for the Goby project.