CppMethod.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 "CppMethod.h"
25 
26 #include <SimoxUtility/algorithm/string/string_tools.h>
29 #include <boost/format.hpp>
30 
31 using namespace armarx;
32 
33 CppMethod::CppMethod(const std::string& header, const std::string& doc, const bool enforceBlockGeneration) :
34  header(header), // may be suffixed with ;
35  doc(doc),
36  enforceBlockGeneration(enforceBlockGeneration)
37 {
38 }
39 
40 void CppMethod::writeCpp(const CppWriterPtr& writer)
41 {
42  if (!doc.empty())
43  {
44  std::string delimiters = "\n";
45  std::vector<std::string> doclines = simox::alg::split(doc, delimiters);
46  writer->body.line("/**");
47 
48  for (auto& line : doclines)
49  {
50  writer->body.line(" * " + line);
51  }
52 
53  writer->body.line(" */");
54  }
55 
56  writer->body.line(header);
57 
58  if (simox::alg::ends_with(simox::alg::trim_copy(header), ";"))
59  {
60  ARMARX_CHECK(!enforceBlockGeneration);
61  ARMARX_CHECK(!block);
62  return;
63  }
64 
65  auto writeBlock = [this, writer](){
66  if (block)
67  {
68  block->writeCpp(writer);
69  }
70  else
71  {
72  CppBlockPtr b = std::make_shared<CppBlock>();
73  b->writeCpp(writer);
74  }
75  };
76 
77  //if (enforceBlockGeneration)
78  //{
79  writeBlock();
80  //}
81  //else if (block)
82  //{
83  // block->writeCpp(writer);
84  //}
85 }
86 
87 void CppMethod::addLine(const std::string& line)
88 {
89  if (!block)
90  {
91  this->block.reset(new CppBlock());
92  }
93  block->addLine(line);
94 }
95 
96 void CppMethod::addLine(const boost::basic_format<char>& line)
97 {
98  if (!block)
99  {
100  this->block.reset(new CppBlock());
101  }
102  block->addLine(line);
103 }
104 
105 void CppMethod::addBlock(const CppBlockPtr& new_block)
106 {
107  if (!block)
108  {
109  this->block.reset(new CppBlock());
110  }
111  block->addBlock(new_block);
112 }
113 
114 void CppMethod::setBlock(const CppBlockPtr& new_block)
115 {
116  block = new_block;
117 }
118 
119 void CppMethod::setCompact(const bool compact)
120 {
121  this->compact = compact;
122 }
123 
124 void CppMethod::setEnforceBlockGeneration(const bool enforceBlockGeneration)
125 {
126  this->enforceBlockGeneration = enforceBlockGeneration;
127 }
armarx::CppMethod::writeCpp
void writeCpp(const CppWriterPtr &writer)
Definition: CppMethod.cpp:40
armarx::CppMethod::addBlock
void addBlock(const CppBlockPtr &block)
Definition: CppMethod.cpp:105
CppMethod.h
armarx::CppBlockPtr
std::shared_ptr< CppBlock > CppBlockPtr
Definition: CppBlock.h:35
armarx::CppBlock
Definition: CppBlock.h:73
armarx::CppMethod::setCompact
void setCompact(bool compact)
Definition: CppMethod.cpp:119
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::CppMethod::setBlock
void setBlock(const CppBlockPtr &block)
Definition: CppMethod.cpp:114
armarx::CppMethod::CppMethod
CppMethod(const std::string &header, const std::string &doc="", const bool enforceBlockGeneration=false)
Definition: CppMethod.cpp:33
ExpressionException.h
armarx::CppMethod::addLine
void addLine(const std::string &line)
Definition: CppMethod.cpp:87
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:50
armarx::CppWriterPtr
std::shared_ptr< CppWriter > CppWriterPtr
Definition: CppWriter.h:35
Logging.h
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::CppMethod::setEnforceBlockGeneration
void setEnforceBlockGeneration(bool enforceBlockGeneration)
Definition: CppMethod.cpp:124