Spinning Topp Logo BlackTopp Studios
inc
interval.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _interval_h
42 #define _interval_h
43 
44 #include "exception.h"
45 #include "serialization.h"
46 #include "XML/xml.h"
47 
48 namespace Mezzanine
49 {
50  ///////////////////////////////////////////////////////////////////////////////
51  /// @class Interval
52  /// @brief This class will generate keep track of a pool of unique 32-bit ID's that can be used for distinct object instances.
53  ///////////////////////////////////////
54  template<class NumType>
56  {
57  public:
58  /// @brief Convenience type for the implemented type.
60 
61  ///////////////////////////////////////////////////////////////////////////////
62  // Public Data Members
63 
64  /// @brief The lower numeric boundry of the interval.
65  NumType LowerBound;
66  /// @brief The upper numeric boundry of the interval.
67  NumType UpperBound;
68 
69  ///////////////////////////////////////////////////////////////////////////////
70  // Construction and Destruction
71 
72  /// @brief Blank constructor.
74  LowerBound(0),
75  UpperBound(0)
76  { }
77  /// @brief Copy constructor.
78  /// @param Other The other Interval to be copied.
79  Interval(const SelfType& Other) :
80  LowerBound(Other.LowerBound),
81  UpperBound(Other.UpperBound)
82  { }
83  /// @brief Descriptive constructor.
84  /// @param Lower The lower numeric boundry of the interval.
85  /// @param Upper The upper numeric boundry of the interval.
86  Interval(const NumType& Lower, const NumType& Upper) :
87  LowerBound(Lower),
88  UpperBound(Upper)
89  { }
90  /// @brief Class destructor.
92  { }
93 
94  ///////////////////////////////////////////////////////////////////////////////
95  // Utility
96 
97  /// @brief Sets both values of this Interval.
98  /// @param Lower The lower numeric boundry of the interval.
99  /// @param Upper The upper numeric boundry of the interval.
100  void SetBounds(const NumType& Lower, const NumType& Upper)
101  {
102  this->LowerBound = Lower;
103  this->UpperBound = Upper;
104  }
105 
106  /// @brief Gets whether or not a number is inside the bounds of this Interval.
107  /// @param Num The number to check against this Interval.
108  /// @return Returns true if the provided number is within the bounds of this interval, false otherwise.
109  Boole IsWithinBounds(const NumType& Num) const
110  { return this->LowerBound <= Num && this->UpperBound >= Num; }
111 
112  /// @brief Gets the lower boundry of this Interval.
113  /// @return Returns a reference to the lower boundry of this Interval.
114  NumType& GetLowerBound()
115  { return this->LowerBound; }
116  /// @brief Gets the lower boundry of this Interval.
117  /// @return Returns a const reference to the lower boundry of this Interval.
118  const NumType& GetLowerBound() const
119  { return this->LowerBound; }
120  /// @brief Gets the upper boundry of this Interval.
121  /// @return Returns a reference to the upper boundry of this Interval.
122  NumType& GetUpperBound()
123  { return this->UpperBound; }
124  /// @brief Gets the upper boundry of this Interval.
125  /// @return Returns a const reference to the upper boundry of this Interval.
126  const NumType& GetUpperBound() const
127  { return this->UpperBound; }
128 
129  /// @brief Gets the numeric distance between the lower and upper bounds of this interval.
130  /// @return Returns a NumType representing the difference between both bounds of this interval.
131  NumType GetIntervalSize() const
132  { return this->UpperBound - this->LowerBound; }
133 
134  ///////////////////////////////////////////////////////////////////////////////
135  // Operators
136 
137  /// @brief Assignment operator.
138  /// @param Other The other Interval to being assigned to this.
139  /// @return Returns a reference to this.
140  Interval& operator=(const SelfType& Other)
141  {
142  this->LowerBound = Other.LowerBound;
143  this->UpperBound = Other.UpperBound;
144  return *this;
145  }
146 
147  /// @brief Equality comparison operator.
148  /// @param Other The other Interval to be compared with.
149  /// @return Returns true if both Intervals are the same, false otherwise.
150  Boole operator==(const SelfType& Other) const
151  { return this->LowerBound == Other.LowerBound && this->UpperBound == Other.UpperBound; }
152  /// @brief Inequality comparison operator.
153  /// @param Other The other Interval to be compared with.
154  /// @return Returns true if both Intervals are not equal, false otherwise.
155  Boole operator!=(const SelfType& Other) const
156  { return this->LowerBound != Other.LowerBound || this->UpperBound != Other.UpperBound; }
157 
158  /// @brief Less-than comparison with NumType.
159  /// @param Num The number to be compared with.
160  /// @return Returns true if the highest bounds of this Interval is less than the specified number.
161  Boole operator<(const NumType& Num) const
162  { return this->UpperBound < Num; }
163  /// @brief Greater-than comparison with NumType.
164  /// @param Num The number to be compared with.
165  /// @return Returns true if the lowest bounds of this Interval is greater than the specified number.
166  Boole operator>(const NumType& Num) const
167  { return this->LowerBound < Num; }
168 
169  /// @brief Less-than or equal comparison with NumType.
170  /// @param Num The number to be compared with.
171  /// @return Returns true if the highest bounds of this Interval is less than or equal to the specified number.
172  Boole operator<=(const NumType& Num) const
173  { return this->UpperBound <= Num; }
174  /// @brief Greater-than or equal comparison with NumType.
175  /// @param Num The number to be compared with.
176  /// @return Returns true if the lowest bounds of this Interval is greater than or equal to the specified number.
177  Boole operator>=(const NumType& Num) const
178  { return this->LowerBound <= Num; }
179 
180  ///////////////////////////////////////////////////////////////////////////////
181  // Sorting Operators
182 
183  /// @brief Less-than operator.
184  /// @param Other The other Interval to compare to.
185  /// @return Returns true if this Interval is to be sorted before the other Interval, false otherwise.
186  Boole operator<(const SelfType& Other) const
187  { return this->LowerBound < Other.LowerBound && this->UpperBound < Other.UpperBound; }
188  /// @brief Greater-than operator.
189  /// @param Other The other Interval to compare to.
190  /// @return Returns true if this Interval is to be sorted after the other Interval, false otherwise.
191  Boole operator>(const SelfType& Other) const
192  { return this->LowerBound > Other.LowerBound && this->UpperBound > Other.UpperBound; }
193 
194  ///////////////////////////////////////////////////////////////////////////////
195  // Serialization
196 
197  /// @brief Convert this class to an XML::Node ready for serialization.
198  /// @param ParentNode The point in the XML hierarchy that this Interval should be appended to.
199  void ProtoSerialize(XML::Node& ParentNode) const
200  {
201  XML::Node SelfRoot = ParentNode.AppendChild( this->Interval::GetSerializableName() );
202 
203  if( SelfRoot.AppendAttribute("Version").SetValue("1") &&
204  SelfRoot.AppendAttribute("UpperBound").SetValue( this->UpperBound ) &&
205  SelfRoot.AppendAttribute("LowerBound").SetValue( this->LowerBound ) )
206  {
207  return;
208  }else{
209  SerializeError("Create XML Attribute Values",Interval::GetSerializableName(),true);
210  }
211  }
212  /// @brief Take the data stored in an XML Node and overwrite this object with it.
213  /// @param SelfRoot An XML::Node containing the data to populate this class with.
214  void ProtoDeSerialize(const XML::Node& SelfRoot)
215  {
216  XML::Attribute CurrAttrib;
217  StringStream Converter;
218 
219  if( !SelfRoot.Empty() ) {
220  if( SelfRoot.GetAttribute("Version").AsInt() == 1 ) {
221  CurrAttrib = SelfRoot.GetAttribute("UpperBound");
222  if( !CurrAttrib.Empty() ) {
223  Converter << CurrAttrib.AsString();
224  Converter >> this->UpperBound;
225  }
226 
227  Converter.str("");
228  CurrAttrib = SelfRoot.GetAttribute("LowerBound");
229  if( !CurrAttrib.Empty() ) {
230  Converter << CurrAttrib.AsString();
231  Converter >> this->LowerBound;
232  }
233  }else{
234  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Interval::GetSerializableName() + ": Not Version 1.");
235  }
236  }else{
237  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,Interval::GetSerializableName() + " was not found in the provided XML node, which was expected.");
238  }
239  }
240  /// @brief Get the name of the the XML tag the Interval class will leave behind as its instances are serialized.
241  /// @return A string containing the name of this class.
243  { return "Interval"; }
244  };//Interval
245 }//Mezzanine
246 
247 #endif
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
Boole operator>=(const NumType &Num) const
Greater-than or equal comparison with NumType.
Definition: interval.h:177
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Boole operator==(const SelfType &Other) const
Equality comparison operator.
Definition: interval.h:150
static String GetSerializableName()
Get the name of the the XML tag the Interval class will leave behind as its instances are serialized...
Definition: interval.h:242
void SetBounds(const NumType &Lower, const NumType &Upper)
Sets both values of this Interval.
Definition: interval.h:100
NumType LowerBound
The lower numeric boundry of the interval.
Definition: interval.h:65
Boole IsWithinBounds(const NumType &Num) const
Gets whether or not a number is inside the bounds of this Interval.
Definition: interval.h:109
Thrown when the requested identity could not be found.
Definition: exception.h:94
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
NumType & GetLowerBound()
Gets the lower boundry of this Interval.
Definition: interval.h:114
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
~Interval()
Class destructor.
Definition: interval.h:91
Interval(const NumType &Lower, const NumType &Upper)
Descriptive constructor.
Definition: interval.h:86
bool Empty() const
Is this storing anything at all?
This implements the exception hiearchy for Mezzanine.
Boole operator>(const NumType &Num) const
Greater-than comparison with NumType.
Definition: interval.h:166
std::stringstream StringStream
A Datatype used for streaming operations with strings.
Definition: datatypes.h:176
The interface for serialization.
Interval & operator=(const SelfType &Other)
Assignment operator.
Definition: interval.h:140
bool SetValue(const Char8 *rhs)
Set the value of this.
NumType GetIntervalSize() const
Gets the numeric distance between the lower and upper bounds of this interval.
Definition: interval.h:131
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
bool Empty() const
Is this storing anything at all?
NumType UpperBound
The upper numeric boundry of the interval.
Definition: interval.h:67
NumType & GetUpperBound()
Gets the upper boundry of this Interval.
Definition: interval.h:122
Boole operator<(const SelfType &Other) const
Less-than operator.
Definition: interval.h:186
This class will generate keep track of a pool of unique 32-bit ID's that can be used for distinct obj...
Definition: interval.h:55
const NumType & GetLowerBound() const
Gets the lower boundry of this Interval.
Definition: interval.h:118
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
Interval()
Blank constructor.
Definition: interval.h:73
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: interval.h:199
Boole operator<(const NumType &Num) const
Less-than comparison with NumType.
Definition: interval.h:161
Boole operator!=(const SelfType &Other) const
Inequality comparison operator.
Definition: interval.h:155
Interval< NumType > SelfType
Convenience type for the implemented type.
Definition: interval.h:59
Interval(const SelfType &Other)
Copy constructor.
Definition: interval.h:79
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: interval.h:214
Boole operator<=(const NumType &Num) const
Less-than or equal comparison with NumType.
Definition: interval.h:172
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
const NumType & GetUpperBound() const
Gets the upper boundry of this Interval.
Definition: interval.h:126
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Boole operator>(const SelfType &Other) const
Greater-than operator.
Definition: interval.h:191
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.