15b949ca12425b59efb74b137ceaa60a9fa513e3
[anna.git] / include / anna / core / util / MultiRangeExpression.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_core_util_MultiRangeExpression_hpp
38 #define anna_core_util_MultiRangeExpression_hpp
39
40
41 // STL
42 #include <string>
43 #include <map>
44
45
46 namespace anna {
47
48 /**
49 * Class helper to manage multi-range expression like '1-4,23,45-46' (1,2,3,4,23,45,46)
50 */
51 class MultiRangeExpression {
52
53   std::string a_literal;
54   std::map < unsigned int, int/*dummy*/ > a_data; // expands literal
55
56
57   void refresh(void) throw();  // keep coherence between 'a_data' and 'a_literal'
58
59
60 public:
61
62   MultiRangeExpression() {};
63   ~MultiRangeExpression() {};
64
65
66   /**
67   * Gets the configured literal by mean #setLiteral or #addLiteral
68   *
69   * @return Literal
70   */
71   const char * getLiteral(void) const throw() { return a_literal.c_str(); }
72
73   /**
74   * Gets expanded representation for stored literal. E.g.: '1-3,8,10' => '1,2,3,7,8,10'
75   *
76   * @return Expanded literal
77   */
78   std::string getExpandedLiteral(void) const throw();
79
80   /**
81   * Simplify stored literal. E.g.: '1,1,1,2,3,7,8,10' => '1-3,8,10' and returns it.
82   *
83   * @return Simplified literal
84   */
85   const char * simplifyLiteral(void) throw();
86
87   // helpers
88
89   /**
90   * Returns true if the value provided is contained in the multirange expression literal
91   *
92   * @param value Value to be tested
93   * @return True or false
94   */
95   bool contain(const unsigned int & value) const throw() { return (a_data.find(value) != a_data.end()); }
96
97   // setters
98
99   /**
100   * Configures a new literal
101   *
102   * @param l Literal to be stored
103   */
104   void setLiteral(const char * l) throw() {
105     a_literal = l ? l : "";
106     refresh();
107   }
108
109   /**
110   * Accumulates the provided literal over the stored literal
111   * You could simplify with #simplifyLiteral, because perhaps there is overlapping between current literal and provided one.
112   *
113   * @param l Literal to be added
114   */
115   void addLiteral(const char * l) throw() {
116     if(!l) return;
117
118     if(std::string(l) != "") {
119       a_literal += ",";
120       a_literal += l;
121     }
122
123     refresh();
124   }
125 };
126
127 };
128
129
130 #endif