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/checker.h>
24#include <interfaces/generator/constant.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/** Constructor
33 * @param name name of constant
34 * @param type type of constant
35 * @param value value of constant
36 * @param comment comment of message
37 * @exception InterfaceGeneratorInvalidTypeException thrown if InterfaceDataTypeChecker
38 * reports an invalid type.
39 * @exception InterfaceGeneratorInvalidValueException thrown if InterfaceDataTypeChecker
40 * reports an illegal value for the given type.
41 */
43 const std::string &type,
44 const std::string &value,
45 const std::string &comment)
46{
47 if (!InterfaceChecker::validName(name, reserved_names_interface())) {
48 throw InterfaceGeneratorReservedIdentifierException("constant", name.c_str());
49 }
50 if (!InterfaceChecker::validType(type)) {
51 throw InterfaceGeneratorInvalidTypeException("constant", name.c_str(), type.c_str());
52 }
53 if (!InterfaceChecker::validValue(type, value)) {
54 throw InterfaceGeneratorInvalidValueException(name.c_str(), type.c_str(), value.c_str());
55 }
56
57 this->name = name;
58 this->type = type;
59 if (type == "string") {
60 this->value = std::string("\"") + value + "\"";
61 } else {
62 this->value = value;
63 }
64 this->comment = comment;
65}
66
67/** Get name of constant.
68 * @return name of constant.
69 */
70std::string
72{
73 return name;
74}
75
76/** Get value of constant.
77 * @return value of constant.
78 */
79std::string
81{
82 return value;
83}
84
85/** Get type of constant.
86 * @return type of constnat.
87 */
88std::string
90{
91 if (type == "string") {
92 return "char *";
93 } else if (type == "byte") {
94 return "uint8_t";
95 } else if (type == "float" || type == "double" || type == "bool") {
96 return type;
97 } else {
98 return type + "_t";
99 }
100}
101
102/** Get comment of constant.
103 * @return comment of constant.
104 */
105std::string
107{
108 return comment;
109}
static bool validValue(const std::string &type, const std::string &value)
Check value validity for given type.
Definition: checker.cpp:88
static bool validName(const std::string &name, const std::set< std::string > &reserved_names)
Check identifiers.
Definition: checker.cpp:147
static bool validType(const std::string &type, std::vector< InterfaceEnumConstant > *enum_constants=0)
Decide if a supplied type is correct and in the case of constants if the supplied value matches the f...
Definition: checker.cpp:61
InterfaceConstant(const std::string &name, const std::string &type, const std::string &value, const std::string &comment)
Constructor.
Definition: constant.cpp:42
std::string getComment()
Get comment of constant.
Definition: constant.cpp:106
std::string getType()
Get type of constant.
Definition: constant.cpp:89
std::string getValue()
Get value of constant.
Definition: constant.cpp:80
std::string getName()
Get name of constant.
Definition: constant.cpp:71
Thrown if illegal type is supplied.
Definition: exceptions.h:73
Thrown if illegal value is supplied.
Definition: exceptions.h:91
Thrown if something is a reserved identifier.
Definition: exceptions.h:173