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 <SimoxUtility/algorithm/string/string_tools.h>
27 #include <boost/format.hpp>
28 
29 namespace armarx
30 {
31 
32  CppEnumField::CppEnumField(const std::string& name) :
33  name(name), value(-1), hasValue(false)
34  {
35 
36  }
37 
38  CppEnumField::CppEnumField(const std::string& name, int value) :
39  name(name), value(value), hasValue(true)
40  {
41 
42  }
43 
44  void CppEnumField::writeCpp(const CppWriterPtr& writer, bool addComma)
45  {
46  if (hasValue)
47  {
48  writer->body.line(name + " = " + std::to_string(value) + (addComma ? "," : ""));
49  }
50  return writer->body.line(name + (addComma ? "," : ""));
51  }
52 
53  CppEnum::CppEnum(const std::vector<std::string> namespaces, const std::string& name) :
54  MetaEnum(name),
55  namespaces(namespaces)
56  {
57  }
58 
59  CppEnum::CppEnum(const std::vector<std::string> namespaces, const std::string& name, const std::vector<CppEnumFieldPtr>& fields) :
60  MetaEnum(name),
61  namespaces(namespaces), fields(fields)
62  {
63 
64  }
65 
66  void CppEnum::write(const MetaWriterPtr& writer)
67  {
68  CppWriterPtr w = std::dynamic_pointer_cast<CppWriter>(writer);
69  writeCpp(w);
70  }
71 
73  {
74  fields.push_back(f);
75  }
76 
77  std::vector<std::string> CppEnum::getNamespaces() const
78  {
79  return namespaces;
80  }
81 
82  void CppEnum::writeCpp(const CppWriterPtr& writer)
83  {
84  for (const auto& ns : namespaces)
85  {
86  writer->body.startBlock(std::string("namespace ") + ns);
87  }
88 
89  if (!docString.empty())
90  {
91  std::string delimiters = "\n";
92  std::vector<std::string> doclines = simox::alg::split(docString, delimiters);
93  writer->body.line("/**");
94 
95  for (auto& line : doclines)
96  {
97  writer->body.line(" * " + line);
98  }
99 
100  writer->body.line(" */");
101  }
102 
103  writer->body.line("enum class " + name);
104  writer->body.startBlock();
105  for (unsigned int i = 0; i < fields.size(); ++i)
106  {
107  auto& f = fields[i];
108  f->writeCpp(writer, (i != (fields.size()-1)));
109  }
110  writer->body.endBlock("; // enum class " + name); // end of enum
111 
112  for (std::vector<std::string>::reverse_iterator it = namespaces.rbegin(); it != namespaces.rend(); it++)
113  {
114  writer->body.endBlockComment(std::string("namespace ") + *it);
115  }
116  }
117 }
armarx::CppEnumFieldPtr
std::shared_ptr< CppEnumField > CppEnumFieldPtr
Definition: CppEnum.h:38
armarx::CppEnumField::writeCpp
void writeCpp(const CppWriterPtr &writer, bool addComma=false)
Definition: CppEnum.cpp:44
armarx::CppEnum::getNamespaces
std::vector< std::string > getNamespaces() const
Definition: CppEnum.cpp:77
armarx::CppEnum::writeCpp
void writeCpp(const CppWriterPtr &writer)
Definition: CppEnum.cpp:82
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
CppEnum.h
armarx::MetaEnum::docString
std::string docString
Definition: MetaEnum.h:49
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:40
armarx::CppEnum::CppEnum
CppEnum(const std::vector< std::string > namespaces, const std::string &name)
Definition: CppEnum.cpp:53
armarx::MetaEnum
Definition: MetaEnum.h:35
armarx::MetaWriterPtr
std::shared_ptr< MetaWriter > MetaWriterPtr
Definition: MetaWriter.h:37
armarx::MetaEnum::name
std::string name
Definition: MetaEnum.h:48
armarx::CppWriterPtr
std::shared_ptr< CppWriter > CppWriterPtr
Definition: CppWriter.h:35
armarx::CppEnumField::CppEnumField
CppEnumField(const std::string &name)
Definition: CppEnum.cpp:32
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36
armarx::CppEnum::write
void write(const MetaWriterPtr &writer) override
Definition: CppEnum.cpp:66