Remove dynamic exceptions
[anna.git] / include / anna / core / util / MultiRangeExpression.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_core_util_MultiRangeExpression_hpp
10 #define anna_core_util_MultiRangeExpression_hpp
11
12
13 // STL
14 #include <string>
15 #include <map>
16
17
18 namespace anna {
19
20 /**
21 * Class helper to manage multi-range expression like '1-4,23,45-46' (1,2,3,4,23,45,46)
22 */
23 class MultiRangeExpression {
24
25   std::string a_literal;
26   std::map < unsigned int, int/*dummy*/ > a_data; // expands literal
27
28
29   void refresh(void) ;  // keep coherence between 'a_data' and 'a_literal'
30
31
32 public:
33
34   MultiRangeExpression() {};
35   ~MultiRangeExpression() {};
36
37
38   /**
39   * Gets the configured literal by mean #setLiteral or #addLiteral
40   *
41   * @return Literal
42   */
43   const char * getLiteral(void) const { return a_literal.c_str(); }
44
45   /**
46   * Gets expanded representation for stored literal. E.g.: '1-3,8,10' => '1,2,3,7,8,10'
47   *
48   * @return Expanded literal
49   */
50   std::string getExpandedLiteral(void) const ;
51
52   /**
53   * Simplify stored literal. E.g.: '1,1,1,2,3,7,8,10' => '1-3,8,10' and returns it.
54   *
55   * @return Simplified literal
56   */
57   const char * simplifyLiteral(void) ;
58
59   // helpers
60
61   /**
62   * Returns true if the value provided is contained in the multirange expression literal
63   *
64   * @param value Value to be tested
65   * @return True or false
66   */
67   bool contain(const unsigned int & value) const { return (a_data.find(value) != a_data.end()); }
68
69   // setters
70
71   /**
72   * Configures a new literal
73   *
74   * @param l Literal to be stored
75   */
76   void setLiteral(const char * l) {
77     a_literal = l ? l : "";
78     refresh();
79   }
80
81   /**
82   * Accumulates the provided literal over the stored literal
83   * You could simplify with #simplifyLiteral, because perhaps there is overlapping between current literal and provided one.
84   *
85   * @param l Literal to be added
86   */
87   void addLiteral(const char * l) {
88     if(!l) return;
89
90     if(std::string(l) != "") {
91       a_literal += ",";
92       a_literal += l;
93     }
94
95     refresh();
96   }
97 };
98
99 };
100
101
102 #endif