Fawkes API Fawkes Development Version
enum_constant.cpp
1
2/***************************************************************************
3 * enum_constant.cpp - Interface generator enum constant representation
4 *
5 * Generated: Wed Oct 11 19:41:56 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/enum_constant.h>
25#include <interfaces/generator/exceptions.h>
26
27/** @class InterfaceEnumConstant interfaces/generator/enum_constant.h
28 * Interface generator internal representation of a enum constant as parsed
29 * from the XML template file.
30 */
31
32/** Constructor.
33 * @param name name of enumeration constant
34 * @param comment comment of enumeration constant.
35 */
36InterfaceEnumConstant::InterfaceEnumConstant(const std::string &name, const std::string &comment)
37{
38 if (!InterfaceChecker::validName(name, reserved_names_interface()))
39 throw InterfaceGeneratorReservedIdentifierException("enum constant", name.c_str());
40 name_ = name;
41 comment_ = comment;
42 items_.clear();
43}
44
45/** Get name of enum constant.
46 * @return name of enum constant.
47 */
48const std::string &
50{
51 return name_;
52}
53
54/** Get comment of enum constant.
55 * @return comment of enum constant.
56 */
57const std::string &
59{
60 return comment_;
61}
62
63/** Get enumeration items.
64 * @return vector of enum items. First item in pair contains item name, second item
65 * the comment.
66 */
67const std::vector<InterfaceEnumConstant::EnumItem> &
69{
70 return items_;
71}
72
73/** Add an item without custom value.
74 * @param name name of item
75 * @param comment comment of item.
76 */
77void
78InterfaceEnumConstant::add_item(std::string name, std::string comment)
79{
80 if (!InterfaceChecker::validName(name, reserved_names_interface()))
81 throw InterfaceGeneratorReservedIdentifierException("enum item", name.c_str());
82 std::vector<EnumItem>::iterator i;
83 for (i = items_.begin(); i != items_.end(); ++i) {
84 if (i->name == name) {
85 throw InterfaceGeneratorAmbiguousNameException(name.c_str(), "enum item");
86 }
87 }
88 EnumItem p = {name, comment, false, 0};
89 items_.push_back(p);
90}
91
92/** Add an item with custom value.
93 * @param name name of item
94 * @param comment comment of item.
95 * @param value custom value
96 */
97void
98InterfaceEnumConstant::add_item(std::string name, std::string comment, int value)
99{
100 std::vector<EnumItem>::iterator i;
101 for (i = items_.begin(); i != items_.end(); ++i) {
102 if (i->name == name) {
103 throw InterfaceGeneratorAmbiguousNameException(name.c_str(), "enum item");
104 }
105 }
106 EnumItem p = {name, comment, true, value};
107 items_.push_back(p);
108}
static bool validName(const std::string &name, const std::set< std::string > &reserved_names)
Check identifiers.
Definition: checker.cpp:147
const std::string & get_name() const
Get name of enum constant.
void add_item(std::string name, std::string comment)
Add an item without custom value.
InterfaceEnumConstant(const std::string &name, const std::string &comment)
Constructor.
const std::vector< EnumItem > & get_items() const
Get enumeration items.
const std::string & get_comment() const
Get comment of enum constant.
Thrown if name is ambiguous.
Definition: exceptions.h:159
Thrown if something is a reserved identifier.
Definition: exceptions.h:173