Suuports clang compiler
[anna.git] / include / anna / diameter / stack / Command.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // http://redmine.teslayout.com/projects/anna-suite
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     *  Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_diameter_stack_Command_hpp
38 #define anna_diameter_stack_Command_hpp
39
40
41 // Local
42 #include <anna/diameter/defines.hpp>
43 #include <anna/diameter/stack/AvpRule.hpp>
44 #include <anna/diameter/codec/Format.hpp>
45
46 #include <anna/core/RuntimeException.hpp>
47 #include <anna/config/defines.hpp>
48
49 // STL
50 #include <string>
51 #include <map>
52
53
54
55 namespace anna {
56 namespace xml {
57 class Node;
58 }
59 }
60
61
62 namespace anna {
63
64 namespace diameter {
65
66 namespace stack {
67
68 class Dictionary;
69
70 //------------------------------------------------------------------------------
71 //---------------------------------------------------------------- class Command
72 //------------------------------------------------------------------------------
73 /**
74 * Command information
75 */
76 class Command {
77
78 public:
79
80   //typedef std::map<AvpId, AvpRule> avprule_container;
81   typedef std::map < int /*position*/, AvpRule > avprule_container;
82   typedef avprule_container::iterator avprule_iterator;
83   typedef avprule_container::const_iterator const_avprule_iterator;
84
85
86 private:
87
88   const Dictionary *a_dictionary;
89   CommandId a_id;
90   std::string a_name;
91
92   // Children:
93   avprule_container a_avprules;
94   bool a_allowFixedRule;
95   int a_avprulePosition;
96
97   void _initializeRules() throw() {
98     a_avprules.clear();
99     a_allowFixedRule = true;
100     a_avprulePosition = 0;
101   }
102
103   void _initialize(const Dictionary *d) throw() {
104     a_dictionary = d;
105     _initializeRules();
106   }
107
108 public:
109
110   Command(const Dictionary *d = NULL) { _initialize(d); }
111   ~Command() {};
112
113
114   // get
115   const CommandId & getId(void) const throw() { return a_id; }
116   const std::string & getName(void) const throw() { return a_name; }
117
118
119   // containers
120   const_avprule_iterator avprule_begin() const throw() { return a_avprules.begin(); }
121   const_avprule_iterator avprule_end() const throw() { return a_avprules.end(); }
122   int avprule_size() const throw() { return a_avprules.size(); }
123
124
125   // helpers
126   bool isEmpty(void) const throw() { return (!a_avprules.size()); }
127   bool isRequest(void) const throw() { return (a_id.second); }
128   bool isAnswer(void) const throw() { return (!a_id.second); }
129   bool isChild(const AvpId & avp) const throw();
130
131   std::string asString(void) const throw();
132   anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
133
134   // operators
135
136   // set
137   void initialize(const Dictionary *d = NULL) throw() { _initialize(d); }
138   void setCode(const U24 & c) throw(anna::RuntimeException) {
139
140     a_id.first = c;
141   }
142   void setRequest(bool r = true) throw() { a_id.second = r; }
143   void setAnswer(bool a = true) throw() { a_id.second = a; }
144   void setName(const std::string & n) throw(anna::RuntimeException) {
145     if(n == "") throw anna::RuntimeException("Empty command-name string not allowed", ANNA_FILE_LOCATION);
146
147     a_name = n;
148   }
149   void addAvpRule(const AvpRule & avpRule) throw(anna::RuntimeException);
150 };
151
152
153 }
154 }
155 }
156
157
158 #endif