Fawkes API  Fawkes Development Version
constant.cpp
1 
2 /***************************************************************************
3  * constant.cpp - Interface generator constant representation
4  *
5  * Generated: Wed Oct 11 15:33:39 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it 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 Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <interfaces/generator/constant.h>
24 #include <interfaces/generator/type_checker.h>
25 #include <interfaces/generator/exceptions.h>
26 
27 /** @class InterfaceConstant interfaces/generator/constant.h
28  * Interface generator internal representation of a constant as parsed from
29  * the XML template file.
30  */
31 
32 
33 /** Constructor
34  * @param name name of constant
35  * @param type type of constant
36  * @param value value of constant
37  * @param comment comment of message
38  * @exception InterfaceGeneratorInvalidTypeException thrown if InterfaceDataTypeChecker
39  * reports an invalid type.
40  * @exception InterfaceGeneratorInvalidValueException thrown if InterfaceDataTypeChecker
41  * reports an illegal value for the given type.
42  */
43 InterfaceConstant::InterfaceConstant(const std::string &name, const std::string &type,
44  const std::string &value, const std::string &comment)
45 {
47  throw InterfaceGeneratorInvalidTypeException("constant", name.c_str(), type.c_str());
48  }
49  if ( ! InterfaceDataTypeChecker::validValue(type, value) ) {
50  throw InterfaceGeneratorInvalidValueException(name.c_str(), type.c_str(), value.c_str());
51  }
52 
53  this->name = name;
54  this->type = type;
55  if ( type == "string" ) {
56  this->value = std::string("\"") + value + "\"";
57  } else {
58  this->value = value;
59  }
60  this->comment = comment;
61 }
62 
63 
64 /** Get name of constant.
65  * @return name of constant.
66  */
67 std::string
69 {
70  return name;
71 }
72 
73 
74 /** Get value of constant.
75  * @return value of constant.
76  */
77 std::string
79 {
80  return value;
81 }
82 
83 
84 /** Get type of constant.
85  * @return type of constnat.
86  */
87 std::string
89 {
90  if (type == "string") {
91  return "char *";
92  } else if (type == "byte") {
93  return "uint8_t";
94  } else if (type == "float" || type == "double" || type == "bool") {
95  return type;
96  } else {
97  return type + "_t";
98  }
99 }
100 
101 
102 /** Get comment of constant.
103  * @return comment of constant.
104  */
105 std::string
107 {
108  return comment;
109 }
std::string getName()
Get name of constant.
Definition: constant.cpp:68
static bool validValue(const std::string &type, const std::string &value)
Check value validity for given type.
Thrown if illegal value is supplied.
Definition: exceptions.h:90
std::string getType()
Get type of constant.
Definition: constant.cpp:88
std::string getComment()
Get comment of constant.
Definition: constant.cpp:106
std::string getValue()
Get value of constant.
Definition: constant.cpp:78
static bool validType(const std::string &type, std::vector< InterfaceEnumConstant > *enum_constants=0)
Check type validity.
Thrown if illegal type is supplied.
Definition: exceptions.h:73
InterfaceConstant(const std::string &name, const std::string &type, const std::string &value, const std::string &comment)
Constructor.
Definition: constant.cpp:43