CppEnum.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "CppEnum.h"
25 
26 #include <boost/format.hpp>
27 
28 #include <SimoxUtility/algorithm/string/string_tools.h>
29 
30 namespace armarx
31 {
32 
33  CppEnumField::CppEnumField(const std::string& name) : name(name), value(-1), hasValue(false)
34  {
35  }
36 
37  CppEnumField::CppEnumField(const std::string& name, int value) :
38  name(name), value(value), hasValue(true)
39  {
40  }
41 
42  void
43  CppEnumField::writeCpp(const CppWriterPtr& writer, bool addComma)
44  {
45  if (hasValue)
46  {
47  writer->body.line(name + " = " + std::to_string(value) + (addComma ? "," : ""));
48  }
49  return writer->body.line(name + (addComma ? "," : ""));
50  }
51 
52  CppEnum::CppEnum(const std::vector<std::string> namespaces, const std::string& name) :
53  MetaEnum(name), namespaces(namespaces)
54  {
55  }
56 
57  CppEnum::CppEnum(const std::vector<std::string> namespaces,
58  const std::string& name,
59  const std::vector<CppEnumFieldPtr>& fields) :
60  MetaEnum(name), namespaces(namespaces), fields(fields)
61  {
62  }
63 
64  void
66  {
67  CppWriterPtr w = std::dynamic_pointer_cast<CppWriter>(writer);
68  writeCpp(w);
69  }
70 
71  void
73  {
74  fields.push_back(f);
75  }
76 
77  std::vector<std::string>
79  {
80  return namespaces;
81  }
82 
83  void
85  {
86  for (const auto& ns : namespaces)
87  {
88  writer->body.startBlock(std::string("namespace ") + ns);
89  }
90 
91  if (!docString.empty())
92  {
93  std::string delimiters = "\n";
94  std::vector<std::string> doclines = simox::alg::split(docString, delimiters);
95  writer->body.line("/**");
96 
97  for (auto& line : doclines)
98  {
99  writer->body.line(" * " + line);
100  }
101 
102  writer->body.line(" */");
103  }
104 
105  writer->body.line("enum class " + name);
106  writer->body.startBlock();
107  for (unsigned int i = 0; i < fields.size(); ++i)
108  {
109  auto& f = fields[i];
110  f->writeCpp(writer, (i != (fields.size() - 1)));
111  }
112  writer->body.endBlock("; // enum class " + name); // end of enum
113 
114  for (std::vector<std::string>::reverse_iterator it = namespaces.rbegin();
115  it != namespaces.rend();
116  it++)
117  {
118  writer->body.endBlockComment(std::string("namespace ") + *it);
119  }
120  }
121 } // namespace armarx
armarx::CppEnumFieldPtr
std::shared_ptr< CppEnumField > CppEnumFieldPtr
Definition: CppEnum.h:37
armarx::CppEnumField::writeCpp
void writeCpp(const CppWriterPtr &writer, bool addComma=false)
Definition: CppEnum.cpp:43
armarx::CppEnum::getNamespaces
std::vector< std::string > getNamespaces() const
Definition: CppEnum.cpp:78
armarx::CppEnum::writeCpp
void writeCpp(const CppWriterPtr &writer)
Definition: CppEnum.cpp:84
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
CppEnum.h
armarx::MetaEnum::docString
std::string docString
Definition: MetaEnum.h:50
armarx::CppEnum::addField
void addField(const CppEnumFieldPtr &)
Definition: CppEnum.cpp:72
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::CppEnum::CppEnum
CppEnum(const std::vector< std::string > namespaces, const std::string &name)
Definition: CppEnum.cpp:52
armarx::MetaEnum
Definition: MetaEnum.h:36
armarx::MetaWriterPtr
std::shared_ptr< MetaWriter > MetaWriterPtr
Definition: MetaWriter.h:37
armarx::MetaEnum::name
std::string name
Definition: MetaEnum.h:49
armarx::CppWriterPtr
std::shared_ptr< CppWriter > CppWriterPtr
Definition: CppWriter.h:35
armarx::CppEnumField::CppEnumField
CppEnumField(const std::string &name)
Definition: CppEnum.cpp:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38
armarx::CppEnum::write
void write(const MetaWriterPtr &writer) override
Definition: CppEnum.cpp:65