kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Range Networks, Inc. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This software is distributed under multiple licenses; |
| 6 | * see the COPYING file in the main directory for licensing |
| 7 | * information for this specific distribuion. |
| 8 | * |
| 9 | * This use of this software may be subject to additional restrictions. |
| 10 | * See the LEGAL file in the main directory for details. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SCALARTYPES_H |
| 18 | #define SCALARTYPES_H |
| 19 | #include <iostream> // For size_t |
| 20 | #include <stdint.h> |
| 21 | //#include "GSMCommon.h" // Was included for Z100Timer |
| 22 | |
| 23 | // We dont bother to define *= /= etc.; you'll have to convert: a*=b; to: a=a*b; |
| 24 | #define _INITIALIZED_SCALAR_BASE_FUNCS(Classname,Basetype,Init) \ |
| 25 | Classname() : value(Init) {} \ |
| 26 | Classname(Basetype wvalue) { value = wvalue; } /* Can set from basetype. */ \ |
| 27 | operator Basetype(void) const { return value; } /* Converts from basetype. */ \ |
| 28 | Basetype operator=(Basetype wvalue) { return value = wvalue; } \ |
| 29 | Basetype* operator&() { return &value; } |
| 30 | |
| 31 | #define _INITIALIZED_SCALAR_ARITH_FUNCS(Basetype) \ |
| 32 | Basetype operator++() { return ++value; } \ |
| 33 | Basetype operator++(int) { return value++; } \ |
| 34 | Basetype operator--() { return --value; } \ |
| 35 | Basetype operator--(int) { return value--; } \ |
| 36 | Basetype operator+=(Basetype wvalue) { return value = value + wvalue; } \ |
| 37 | Basetype operator-=(Basetype wvalue) { return value = value - wvalue; } |
| 38 | |
| 39 | #define _INITIALIZED_SCALAR_FUNCS(Classname,Basetype,Init) \ |
| 40 | _INITIALIZED_SCALAR_BASE_FUNCS(Classname,Basetype,Init) \ |
| 41 | _INITIALIZED_SCALAR_ARITH_FUNCS(Basetype) |
| 42 | |
| 43 | |
| 44 | #define _DECLARE_SCALAR_TYPE(Classname_i,Classname_z,Basetype) \ |
| 45 | template <Basetype Init> \ |
| 46 | struct Classname_i { \ |
| 47 | Basetype value; \ |
| 48 | _INITIALIZED_SCALAR_FUNCS(Classname_i,Basetype,Init) \ |
| 49 | }; \ |
| 50 | typedef Classname_i<0> Classname_z; |
| 51 | |
| 52 | |
| 53 | // Usage: |
| 54 | // Where 'classname' is one of the types listed below, then: |
| 55 | // classname_z specifies a zero initialized type; |
| 56 | // classname_i<value> initializes the type to the specified value. |
| 57 | // We also define Float_z. |
| 58 | _DECLARE_SCALAR_TYPE(Int_i, Int_z, int) |
| 59 | _DECLARE_SCALAR_TYPE(Char_i, Char_z, signed char) |
| 60 | _DECLARE_SCALAR_TYPE(Int16_i, Int16_z, int16_t) |
| 61 | _DECLARE_SCALAR_TYPE(Int32_i, Int32_z, int32_t) |
| 62 | _DECLARE_SCALAR_TYPE(UInt_i, UInt_z, unsigned) |
| 63 | _DECLARE_SCALAR_TYPE(UChar_i, UChar_z, unsigned char) |
| 64 | _DECLARE_SCALAR_TYPE(UInt16_i, UInt16_z, uint16_t) |
| 65 | _DECLARE_SCALAR_TYPE(UInt32_i, UInt32_z, uint32_t) |
| 66 | _DECLARE_SCALAR_TYPE(Size_t_i, Size_t_z, size_t) |
| 67 | |
| 68 | // Bool is special because it cannot accept some arithmetic funcs |
| 69 | //_DECLARE_SCALAR_TYPE(Bool_i, Bool_z, bool) |
| 70 | template <bool Init> |
| 71 | struct Bool_i { |
| 72 | bool value; |
| 73 | _INITIALIZED_SCALAR_BASE_FUNCS(Bool_i,bool,Init) |
| 74 | }; |
| 75 | typedef Bool_i<0> Bool_z; |
| 76 | |
| 77 | // float is special, because C++ does not permit the template initalization: |
| 78 | struct Float_z { |
| 79 | float value; |
| 80 | _INITIALIZED_SCALAR_FUNCS(Float_z,float,0) |
| 81 | }; |
| 82 | struct Double_z { |
| 83 | double value; |
| 84 | _INITIALIZED_SCALAR_FUNCS(Double_z,double,0) |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | class ItemWithValueAndWidth { |
| 89 | public: |
| 90 | virtual unsigned getValue() const = 0; |
| 91 | virtual unsigned getWidth() const = 0; |
| 92 | }; |
| 93 | |
| 94 | // A Range Networks Field with a specified width. |
| 95 | // See RLCMessages.h for examples. |
| 96 | template <int Width=32, unsigned Init=0> |
| 97 | class Field_i : public ItemWithValueAndWidth |
| 98 | { |
| 99 | public: |
| 100 | unsigned value; |
| 101 | _INITIALIZED_SCALAR_FUNCS(Field_i,unsigned,Init) |
| 102 | unsigned getWidth() const { return Width; } |
| 103 | unsigned getValue() const { return value; } |
| 104 | }; |
| 105 | |
| 106 | // Synonym for Field_i, but no way to do it. |
| 107 | template <int Width, unsigned Init=0> |
| 108 | class Field_z : public ItemWithValueAndWidth |
| 109 | { |
| 110 | public: |
| 111 | unsigned value; |
| 112 | _INITIALIZED_SCALAR_FUNCS(Field_z,unsigned,Init) |
| 113 | unsigned getWidth() const { return Width; } |
| 114 | unsigned getValue() const { return value; } |
| 115 | }; |
| 116 | |
| 117 | // This is an uninitialized field. |
| 118 | template <int Width=32, unsigned Init=0> |
| 119 | class Field : public ItemWithValueAndWidth |
| 120 | { |
| 121 | public: |
| 122 | unsigned value; |
| 123 | _INITIALIZED_SCALAR_FUNCS(Field,unsigned,Init) |
| 124 | unsigned getWidth() const { return Width; } |
| 125 | unsigned getValue() const { return value; } |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | // A Z100Timer with an initial value specified. |
| 130 | //template <int Init> |
| 131 | //class Z100Timer_i : public GSM::Z100Timer { |
| 132 | // public: |
| 133 | // Z100Timer_i() : GSM::Z100Timer(Init) {} |
| 134 | //}; |
| 135 | |
| 136 | #endif |