Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Attribute.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright and trademark 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
44 #ifndef __ATTRIBUTE_H__
45 #define __ATTRIBUTE_H__
46 
47 #include "CoreConfig.h"
48 #include <string>
49 #include "MengeException.h"
50 #include "Math/Vector2.h"
51 
52 // forward declaration
53 class TiXmlElement;
54 
55 namespace Menge {
56 
57  namespace Math {
58  class FloatGenerator;
59  class IntGenerator;
60  class Vec2DGenerator;
61  }
62  using namespace Math;
63 
69  class MENGE_API AttributeDefinitionException : public virtual Menge::MengeException {
70  public:
75 
81  AttributeDefinitionException( const std::string & s ): Menge::MengeException(s) {}
82  };
83 
88  public:
93 
100  };
101 
114  class MENGE_API Attribute {
115  public:
123  Attribute( const std::string & name, bool required ): _name(name), _required(required) {}
124 
128  virtual ~Attribute(){}
129 
138  virtual bool extract( TiXmlElement * node ) = 0;
139 
145  inline const std::string & getName() const { return _name; }
146 
152  inline bool isRequired() const { return _required; }
153 
159  inline bool isValid() const { return _valid; }
160 
164  void clear();
165 
169  virtual void setDefault() = 0;
170 
179  virtual int getInt() { throw AttributeDefinitionException("This Attribute can't provide an int value."); }
180 
189  virtual bool getBool() { throw AttributeDefinitionException("This Attribute can't provide a boolean value."); }
190 
199  virtual float getFloat() { throw AttributeDefinitionException("This Attribute can't provide anfloat value."); }
200 
209  virtual std::string getString() { throw AttributeDefinitionException("This Attribute can't provide a string value."); }
210 
219  virtual size_t getSizeT() { throw AttributeDefinitionException("This Attribute can't provide a size_t value."); }
220 
230  virtual FloatGenerator * getFloatGenerator(){ throw AttributeDefinitionException("This Attribute can't provide a float generator."); }
231 
239  virtual Vec2DGenerator * getVec2DGenerator(){ throw AttributeDefinitionException("This Attribute can't provide a 2D float generator."); }
240 
250  virtual IntGenerator * getIntGenerator(){ throw AttributeDefinitionException("This Attribute can't provide an int generator."); }
251 
252  protected:
256  std::string _name;
257 
261  bool _required;
262 
268  bool _valid;
269 
270  };
271 
273 
277  class MENGE_API StringAttribute : public Attribute {
278  public:
287  StringAttribute( const std::string & name, bool required, const std::string & defValue="" ): Attribute(name,required), _default(defValue) {}
288 
297  virtual bool extract( TiXmlElement * node );
298 
302  virtual void setDefault() { _value = _default; }
303 
310  virtual std::string getString() { return _value; }
311 
312  protected:
316  std::string _default;
317 
321  std::string _value;
322  };
323 
325 
329  class MENGE_API IntAttribute : public Attribute {
330  public:
339  IntAttribute( const std::string & name, bool required, int defValue ): Attribute(name,required), _default(defValue) {}
340 
349  virtual bool extract( TiXmlElement * node );
350 
354  virtual void setDefault() { _value = _default; }
355 
362  virtual int getInt() { return _value; }
363 
364  protected:
368  int _default;
369 
373  int _value;
374  };
375 
377 
381  class MENGE_API FloatAttribute : public Attribute {
382  public:
391  FloatAttribute( const std::string & name, bool required, float defValue ): Attribute(name,required), _default(defValue) {}
392 
401  virtual bool extract( TiXmlElement * node );
402 
406  virtual void setDefault() { _value = _default; }
407 
414  virtual float getFloat() { return _value; }
415  protected:
419  float _default;
420 
424  float _value;
425  };
426 
428 
432  class MENGE_API BoolAttribute : public Attribute {
433  public:
442  BoolAttribute( const std::string & name, bool required, bool defValue ): Attribute(name,required), _default(defValue) {}
443 
452  virtual bool extract( TiXmlElement * node );
453 
457  virtual void setDefault() { _value = _default; }
458 
465  virtual bool getBool() { return _value; }
466 
467  protected:
471  bool _default;
472 
476  bool _value;
477  };
478 
480 
484  class MENGE_API SizeTAttribute : public Attribute {
485  public:
494  SizeTAttribute( const std::string & name, bool required, size_t defValue ): Attribute(name,required), _default(defValue) {}
495 
504  virtual bool extract( TiXmlElement * node );
505 
509  virtual void setDefault() { _value = _default; }
510 
517  virtual size_t getSizeT() { return _value; }
518 
519  protected:
523  size_t _default;
524 
528  size_t _value;
529  };
530 
532 
536  class MENGE_API FloatDistributionAttribute : public Attribute {
537  public:
549  FloatDistributionAttribute( const std::string & name, bool required, float defValue, float scale=1.f ): Attribute(name,required), _default(defValue), _scale(scale), _generator(0x0) {}
550 
554  virtual ~FloatDistributionAttribute();
555 
564  virtual bool extract( TiXmlElement * node );
565 
569  virtual void setDefault() { _generator = 0x0; }
570 
578  virtual FloatGenerator * getFloatGenerator(){ FloatGenerator * val = _generator; _generator=0x0; return val; }
579 
580  protected:
584  float _default;
585 
590  float _scale;
591 
596  };
597 
599 
603  class MENGE_API Vec2DDistributionAttribute : public Attribute {
604  public:
617  Vec2DDistributionAttribute( bool required, const Vector2 & defValue, float scale=1.f ): Attribute("",required), _default(defValue), _scale(scale), _generator(0x0) {}
618 
622  virtual ~Vec2DDistributionAttribute();
623 
632  virtual bool extract( TiXmlElement * node );
633 
637  virtual void setDefault() { _generator = 0x0; }
638 
646  virtual Vec2DGenerator * getVec2DGenerator(){ Vec2DGenerator * val = _generator; _generator=0x0; return val; }
647 
648  protected:
653 
658  float _scale;
659 
664  };
665 
667 
671  class MENGE_API IntDistributionAttribute : public Attribute {
672  public:
683  IntDistributionAttribute( const std::string & name, bool required, int defValue ): Attribute(name,required), _default(defValue), _generator(0x0) {}
684 
688  virtual ~IntDistributionAttribute();
689 
698  virtual bool extract( TiXmlElement * node );
699 
703  virtual void setDefault() { _generator = 0x0; }
704 
712  virtual IntGenerator * getIntGenerator(){ IntGenerator * val = _generator; _generator=0x0; return val; }
713 
714  protected:
718  int _default;
719 
724  };
725 } // namespace Menge
726 #endif // __ATTRIBUTE_H__
const std::string & getName() const
Returns a const reference to the name of the attribute.
Definition: Attribute.h:145
AttributeDefinitionException()
Default constructor.
Definition: Attribute.h:74
virtual ~Attribute()
Destructor.
Definition: Attribute.h:128
IntAttribute(const std::string &name, bool required, int defValue)
Constructor.
Definition: Attribute.h:339
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
Generic abstract class which generates a scalar float value.
Definition: RandGenerator.h:99
float _default
The default float value. Meaningless if _required is true.
Definition: Attribute.h:419
virtual bool getBool()
Retrieve the parsed boolean value.
Definition: Attribute.h:465
An element attribute.
Definition: Attribute.h:114
Sets up the proper compiler directives for platform and dll export/import.
bool _required
Determines if the XML must specify this attribute.
Definition: Attribute.h:261
virtual IntGenerator * getIntGenerator()
Retrieve the parsed int generator.
Definition: Attribute.h:712
virtual void setDefault()
If defined, sets the default value for the attribute.
Definition: Attribute.h:703
std::string _default
The default string value. Meaningless if _required is true.
Definition: Attribute.h:316
FloatAttribute(const std::string &name, bool required, float defValue)
Constructor.
Definition: Attribute.h:391
virtual void setDefault()
If defined, sets the default value for the attribute.
Definition: Attribute.h:569
std::string _value
The parsed attribute value.
Definition: Attribute.h:321
virtual std::string getString()
Retrieve the parsed string value.
Definition: Attribute.h:209
The class which specifies an xml attribute with a float-type value.
Definition: Attribute.h:381
virtual int getInt()
Retrieve the parsed int value.
Definition: Attribute.h:362
Attribute(const std::string &name, bool required)
Constructor.
Definition: Attribute.h:123
The fatal attribute definition exception.
Definition: Attribute.h:87
The base definition for exceptions in Menge.
Generic abstract class which generates a 2D vector float values.
Definition: RandGenerator.h:706
float _value
The parsed attribute value.
Definition: Attribute.h:424
The class which specifies an xml attribute with a int-type value.
Definition: Attribute.h:329
AttributeDefinitionFatalException()
Default constructor.
Definition: Attribute.h:92
virtual void setDefault()
If defined, sets the default value for the attribute.
Definition: Attribute.h:637
float _default
The default float value. Meaningless if _required is true.
Definition: Attribute.h:584
Exception class for attribute definition problems (i.e., using the wrong type of attribute for the wr...
Definition: Attribute.h:69
virtual size_t getSizeT()
Retrieve the parsed size_t value.
Definition: Attribute.h:517
The class which specifies an xml attribute with a boolean-type value.
Definition: Attribute.h:432
virtual void setDefault()
If defined, sets the default value for the attribute.
Definition: Attribute.h:457
virtual void setDefault()
If defined, sets the default value for the attribute.
Definition: Attribute.h:406
The class which specifies an xml attribute with a float distribution value.
Definition: Attribute.h:536
int _default
The default int value. Meaningless if _required is true.
Definition: Attribute.h:718
virtual void setDefault()
If defined, sets the default value for the attribute.
Definition: Attribute.h:509
Base exception class for menge operations.
Definition: MengeException.h:58
AttributeDefinitionException(const std::string &s)
Constructor with message.
Definition: Attribute.h:81
virtual FloatGenerator * getFloatGenerator()
Retrieve the parsed float generator.
Definition: Attribute.h:578
Vec2DDistributionAttribute(bool required, const Vector2 &defValue, float scale=1.f)
Constructor.
Definition: Attribute.h:617
The namespace for math primitives for simulation and visualization.
Definition of a vector in R2.
FloatDistributionAttribute(const std::string &name, bool required, float defValue, float scale=1.f)
Constructor.
Definition: Attribute.h:549
The class which specifies an xml attribute with an int distribution value.
Definition: Attribute.h:671
bool isValid() const
Reports if the attribute is valid.
Definition: Attribute.h:159
virtual void setDefault()
If defined, sets the default value for the attribute.
Definition: Attribute.h:354
float _scale
Scale value. Useful for converting units at parse time (i.e. from degrees to radians.
Definition: Attribute.h:590
IntGenerator * _generator
The parsed generator.
Definition: Attribute.h:723
virtual FloatGenerator * getFloatGenerator()
Retrieve the parsed float generator.
Definition: Attribute.h:230
int _default
The default int value. Meaningless if _required is true.
Definition: Attribute.h:368
Vector2 _default
The default float value. Meaningless if _required is true.
Definition: Attribute.h:652
virtual float getFloat()
Retrieve the parsed float value.
Definition: Attribute.h:414
virtual Vec2DGenerator * getVec2DGenerator()
Retrieve the parsed 2D float generator.
Definition: Attribute.h:239
IntDistributionAttribute(const std::string &name, bool required, int defValue)
Constructor.
Definition: Attribute.h:683
bool isRequired() const
Reports if this attribute is required to be specified.
Definition: Attribute.h:152
virtual Vec2DGenerator * getVec2DGenerator()
Retrieve the parsed 2D float generator.
Definition: Attribute.h:646
The class which specifies an xml attribute with a 2D float distribution value.
Definition: Attribute.h:603
Generic abstract class which generates a scalar integer value.
Definition: RandGenerator.h:486
virtual void setDefault()
If defined, sets the default value for the attribute.
Definition: Attribute.h:302
size_t _default
The default bool value. Meaningless if _required is true.
Definition: Attribute.h:523
Base class for fatal exceptions.
Definition: MengeException.h:99
FloatGenerator * _generator
The parsed generator.
Definition: Attribute.h:595
float _scale
Scale value. Useful for converting units at parse time (i.e. from degrees to radians.
Definition: Attribute.h:658
virtual bool getBool()
Retrieve the parsed boolean value.
Definition: Attribute.h:189
bool _value
The parsed attribute value.
Definition: Attribute.h:476
virtual IntGenerator * getIntGenerator()
Retrieve the parsed int generator.
Definition: Attribute.h:250
int _value
The parsed attribute value.
Definition: Attribute.h:373
StringAttribute(const std::string &name, bool required, const std::string &defValue="")
Constructor.
Definition: Attribute.h:287
virtual size_t getSizeT()
Retrieve the parsed size_t value.
Definition: Attribute.h:219
The class which specifies an xml attribute with a size_t-type value.
Definition: Attribute.h:484
std::string _name
The name of the xml attribute.
Definition: Attribute.h:256
The class which specifies an xml attribute with a string-type value.
Definition: Attribute.h:277
size_t _value
The parsed attribute value.
Definition: Attribute.h:528
bool _default
The default bool value. Meaningless if _required is true.
Definition: Attribute.h:471
SizeTAttribute(const std::string &name, bool required, size_t defValue)
Constructor.
Definition: Attribute.h:494
virtual std::string getString()
Retrieve the parsed string value.
Definition: Attribute.h:310
BoolAttribute(const std::string &name, bool required, bool defValue)
Constructor.
Definition: Attribute.h:442
bool _valid
Reports if the attribute is valid. An attribute is always valid if it is not required. Otherwise, it is only valid if it was able to be initialized from the XML.
Definition: Attribute.h:268
Vec2DGenerator * _generator
The parsed generator.
Definition: Attribute.h:663
virtual float getFloat()
Retrieve the parsed float value.
Definition: Attribute.h:199
virtual int getInt()
Retrieve the parsed int value.
Definition: Attribute.h:179
AttributeDefinitionFatalException(const std::string &s)
Constructor with message.
Definition: Attribute.h:99