libFAUDES

Sections

Index

cfl_attributes.h

Go to the documentation of this file.
00001 /** @file cfl_attributes.h Classes AttributeVoid and AttributeFlags  */
00002 
00003 
00004 /* FAU Discrete Event Systems Library (libfaudes)
00005 
00006 Copyright (C) 2007  Thomas Moor
00007 Exclusive copyright is granted to Klaus Schmidt
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00022 
00023 
00024 #ifndef FAUDES_ATTRIBUTES_H
00025 #define FAUDES_ATTRIBUTES_H
00026 
00027 #include "cfl_types.h"
00028 #include "cfl_tokenreader.h"
00029 #include "cfl_tokenwriter.h"
00030 
00031 namespace faudes {
00032 
00033 
00034 /**
00035  * Minimal Attribute. Attributes are used as template parameters for
00036  * faudes containers and generators and facilitate the modelling of customized  
00037  * properties of events, states and transitions.  The AttributeVoid class defines
00038  * the minimal interface of faudes attributes and therefore is the designated
00039  * base class for all attribute implementations. The AttributeVoid class does
00040  * not define any actual properties. See AttributeFlags for a non-trivial example.
00041  *
00042  * To derive a class from AttributeVoid you should reimplement the virtual
00043  * interface
00044  * - virtual methody DoRead and DoWrtie for token io (as in faudes::Type)
00045  * - virtual methods for DoAssign() (as in faudes::Type) 
00046  * - the factory method New() (use provided macro from faudes::Type)
00047  * - the rti typecast method Cast() (use provided macro from faudes::Type)
00048  * - user level Assign() method (use provided macro from faudes::Type)
00049  */
00050 
00051 class AttributeVoid : public Type {
00052  
00053 FAUDES_TYPE_DECLARATION(AttributeVoid,Type)
00054 
00055 public:
00056 
00057   /** Constructor */
00058   AttributeVoid(void);
00059 
00060   /** Destructor */
00061   virtual ~AttributeVoid(void);
00062 
00063   /** 
00064    * Test for default value.
00065    */
00066   virtual bool IsDefault(void) const {return true;};
00067 
00068   /**
00069    * Set to default value.
00070    * Derived classes must reimplement this function for Clear to operate properly.
00071    */
00072   virtual void SetDefault(void) {};
00073 
00074   /**
00075    *   Synonym for SetDefault to match std interface
00076    */
00077   virtual void Clear(void) { SetDefault(); };
00078 
00079   /**
00080    * Skip attribute tokens.
00081    *
00082    * Helper method to be called after all sttribute derived classes had their
00083    * chance to read their data. It skips all tokens and sections until it reaches a 
00084    * String or decimal Integer.
00085    *
00086    * @param rTr
00087    *   TokenReader to read from
00088    * @exception Exception
00089    *   - IO error (id 1)
00090    */
00091   static void Skip(TokenReader& rTr);
00092 
00093 protected:
00094 
00095   /**
00096    * Assign attribute members. 
00097    * Since AttributeVoid has no members, this method does nothing.
00098    * Derived classes are meant to reimplement DoAssign by first calling
00099    * their base and then assigning additional member variables.
00100    *
00101    * @param rSrcAttr
00102    *    Source to assign from
00103    */
00104   virtual AttributeVoid& DoAssign(const AttributeVoid& rSrcAttr) { (void) rSrcAttr; return *this;};
00105 
00106   /**
00107    * Test equality of configuration data.
00108    * Derived attributes should reimplement this class to compare
00109    * configuration data. The base AttributeVoid returns true as a default.
00110    * @param rOther 
00111    *    Other object to compare with.
00112    * @return 
00113    *   True on match.
00114    */
00115   virtual bool DoEqual(const AttributeVoid& rOther) const { (void) rOther; return true; }
00116 
00117   /**
00118    * Actual read method to read attribute from tokenreader.
00119    *
00120    * For derived attributue classes, this method must either read all tokens that
00121    * belong to the respective attribute, or none. It may throw exceptions on token
00122    * mismatch within the relevant attribute, but it may not throw exceptions when
00123    * it encounters tokens that possibly belong to another attribute. The latter are
00124    * to be skipped by the provided Skip method. See Type::Read
00125    * for public wrappers.
00126    *
00127    * @param rTr
00128    *   TokenReader to read from
00129    * @param rLabel
00130    *   Section to read
00131    * @param pContext
00132    *   Read context to provide contextual information
00133    *
00134    * @exception Exception
00135    *   - IO error (id 1)
00136    */
00137   virtual void DoRead(TokenReader& rTr,  const std::string& rLabel = "", const Type* pContext=0);
00138  
00139   /**
00140    * Actual write method to write the attribute to a TokenWriter.
00141    *
00142    * Reimplement this method for derived attribute classes to define the token io 
00143    * format. See Type::Write for public wrappers.
00144    *
00145    * @param rTw
00146    *   Reference to TokenWriter
00147    * @param rLabel
00148    *   Label of section to write
00149    * @param pContext
00150    *   Write context to provide contextual information
00151    *
00152    * @exception Exception 
00153    *   - IO errors (id 2)
00154    */
00155   virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="",const Type* pContext=0) const;
00156 
00157 
00158 
00159 }; // class AttributeVoid
00160 
00161 
00162 /** Convenience typdef flag data */
00163 typedef unsigned long int fType;
00164 
00165 /**
00166  * Boolean flags Attribute. This attribute class uses a flag word to represent 
00167  * boolean values. The current implementation uses a long int type and hence handles
00168  * up to 32 flags. Each flag is accessed by the according bit mask. 
00169  *
00170  * The current version
00171  * of libFAUDES uses bits 0,1,2 of event attributes for controllability properties 
00172  * (see AttributeCFlags) and the bits 31 and 32 of state attributes for the Qt based
00173  * GUI project. Further versions may use the lower 8 bits of the event aflags and the higher 8
00174  * bits of state flags. You are free to use any flags for your application, but if possible try
00175  * to avoid the above mentioned. For extensive use of flags, you may also consider to
00176  * define a separate attribute class, perhaps with a different fType.
00177  */
00178 
00179 
00180 
00181 class AttributeFlags : public AttributeVoid {
00182 
00183 FAUDES_TYPE_DECLARATION(AttributeFlags,AttributeVoid)
00184 
00185 public:
00186 
00187   /** Default constructor */
00188   AttributeFlags(void) : AttributeVoid(), mFlags(mDefFlags) {};
00189 
00190   /** Copy constructor */
00191   AttributeFlags(const AttributeFlags& rOther) : AttributeVoid(), mFlags(rOther.mFlags) {};
00192 
00193   /** Destructor */
00194   virtual ~AttributeFlags(void) {};
00195 
00196   /**
00197    * Test a flag
00198    */
00199   bool Test(fType mask) const { return ( (mFlags & mask) != 0 ); };
00200 
00201   /**
00202    * Test multible flags, combine by "and"
00203    */
00204   bool TestAll(fType mask) const { return ( (mFlags & mask) == mask ); };
00205 
00206   /**
00207    * Test multible flags, combine by "or"
00208    */
00209   bool TestSome(fType mask) const { return ( (mFlags & mask) != 0 ); };
00210 
00211   /**
00212    * Test multible flags, combine by "not or"
00213    */
00214   bool TestNone(fType mask) const { return ( (mFlags & mask) == 0 ); };
00215 
00216   /**
00217    * Set multiple flags
00218    */
00219   void Set(fType mask) {mFlags |= mask; };
00220 
00221   /**
00222    * Clear multiple flags
00223    */
00224   void Clr(fType mask) {mFlags &= ~mask; };
00225 
00226   /** 
00227    * Test for default value
00228    */
00229   virtual bool IsDefault(void) const {return mFlags==mDefFlags;};
00230 
00231   /** Flags (public access for convenience)  */
00232   fType mFlags;
00233 
00234   /* default flags */
00235   const static fType mDefFlags=0x0;
00236 
00237 
00238 protected:
00239 
00240   /**
00241    * Assignment method. 
00242    *
00243    * @param rSrcAttr
00244    *    Source to assign from
00245    */
00246   AttributeFlags& DoAssign(const AttributeFlags& rSrcAttr);
00247 
00248   /**
00249    * Test equality of configuration data.
00250    *
00251    * @param rOther 
00252    *    Other object to compare with.
00253    * @return 
00254    *   True on match.
00255    */
00256   virtual bool DoEqual(const AttributeFlags& rOther) const;
00257 
00258 
00259   /**
00260    * Reads attribute from TokenReader, see Type for public wrappers.
00261    *
00262    * Test whether the current token is an integer with base 16. If so, read the token to the
00263    * flags value. Else, dont take any tokens. The label and context arguments are ignored.
00264    *
00265    * @param rTr
00266    *   TokenReader to read from
00267    * @param rLabel
00268    *   Section to read
00269    * @param pContext
00270    *   Read context to provide contextual information
00271    *
00272    * @exception Exception
00273    *   - IO error (id 1)
00274    */
00275   virtual void DoRead(TokenReader& rTr,  const std::string& rLabel = "", const Type* pContext=0);
00276  
00277   /**
00278    * Write to TokenWriter, see Type for public wrappers.
00279    *
00280    * If not the defult value, write the flag value as base 16 integer token. Else,
00281    * do nothing. The label and context arguments are ignored.
00282    *
00283    * @param rTw
00284    *   Reference to TokenWriter
00285    * @param rLabel
00286    *   Label of section to write
00287    * @param pContext
00288    *   Write context to provide contextual information
00289    *
00290    * @exception Exception 
00291    *   - IO errors (id 2)
00292    */
00293   virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="",const Type* pContext=0) const;
00294 
00295 
00296 
00297 }; // class AttributeFlags
00298 
00299 
00300 } // namespace faudes
00301 
00302 #endif

libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3