A tool that returns the variable type (char, uint8_t, ...)

Dependents:   GetTypeName_demo

Get a variable type using type traits - a replacement for RTTI typeid().

Sources
https://developer.mbed.org/forum/bugs-suggestions/topic/4494/?page=1#comment-22386
http://stackoverflow.com/questions/81870/print-variable-type-in-c

In RTTI, we were able to use something like.

// Depending on the compiler used, this returns 'int', 'i', ....
    int main() {
        int a= 4;
        printf("%s", typeid(a).name());
    }


To reduce the code overhead, this can no longer be used in the mbed compiler.
However, we can replace this with type traits.
Example

#include "mbed.h"
#include "GetTypeName.h"

int main()
{
    char a = 65;
    printf("Type name for <char> '%c' is %s\r\n", a, GetTypeName(a));
    // Check whether GetTypeName is of 'char' type.
    // Note that strcmp returns 0 when both strings are equal.
    if(!strcmp(GetTypeName(a),"char")) printf("'%c' is of 'char' type\r\n", a);
}

Supported declarations

VariableReturns
charchar
uint8_tuint8_t
signed charint8_t
int8_tint8_t
unsigned shortuint16_t
uint16_tuint16_t
shortint16_t
int16_tint16_t
unsigned intuint32_t
uint32_tuint32_t
intint32_t
int32_tint32_t
unsigned long longuint64_t
uint64_tuint64_t
long longint64_t
int64_tint64_t
floatfloat
doubledouble
boolbool
Committer:
frankvnk
Date:
Sun Feb 15 21:00:05 2015 +0000
Revision:
0:9723189e1955
Initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frankvnk 0:9723189e1955 1 /* GetTypeName.h */
frankvnk 0:9723189e1955 2 /* Copyright (C) 2015 mbed.org, MIT License
frankvnk 0:9723189e1955 3 *
frankvnk 0:9723189e1955 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
frankvnk 0:9723189e1955 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
frankvnk 0:9723189e1955 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
frankvnk 0:9723189e1955 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
frankvnk 0:9723189e1955 8 * furnished to do so, subject to the following conditions:
frankvnk 0:9723189e1955 9 *
frankvnk 0:9723189e1955 10 * The above copyright notice and this permission notice shall be included in all copies or
frankvnk 0:9723189e1955 11 * substantial portions of the Software.
frankvnk 0:9723189e1955 12 *
frankvnk 0:9723189e1955 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
frankvnk 0:9723189e1955 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
frankvnk 0:9723189e1955 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
frankvnk 0:9723189e1955 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
frankvnk 0:9723189e1955 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
frankvnk 0:9723189e1955 18 *
frankvnk 0:9723189e1955 19 */
frankvnk 0:9723189e1955 20 #ifndef TYPE_NAME_H_
frankvnk 0:9723189e1955 21 #define TYPE_NAME_H_
frankvnk 0:9723189e1955 22
frankvnk 0:9723189e1955 23 #define DECLARE_TYPE_NAME(x) template<> const char *type_name<x>::name = #x;
frankvnk 0:9723189e1955 24 #define GetTypeName(x) (type_name<typeof(x)>::name)
frankvnk 0:9723189e1955 25
frankvnk 0:9723189e1955 26 namespace mbed {
frankvnk 0:9723189e1955 27 /** <H1>Get a variable type using type traits - a replacement for RTTI typeid().</H1>
frankvnk 0:9723189e1955 28 *
frankvnk 0:9723189e1955 29 * Sources:<BR>
frankvnk 0:9723189e1955 30 *
frankvnk 0:9723189e1955 31 * https://developer.mbed.org/forum/bugs-suggestions/topic/4494/?page=1#comment-22386<BR>
frankvnk 0:9723189e1955 32 * http://stackoverflow.com/questions/81870/print-variable-type-in-c<BR>
frankvnk 0:9723189e1955 33 * <BR>
frankvnk 0:9723189e1955 34 * In RTTI, we were able to use something like.
frankvnk 0:9723189e1955 35 * @code
frankvnk 0:9723189e1955 36 * // Depending on the compiler used, this returns 'int', 'i', ....
frankvnk 0:9723189e1955 37 * int main() {
frankvnk 0:9723189e1955 38 * int a= 4;
frankvnk 0:9723189e1955 39 * printf("%s", typeid(a).name());
frankvnk 0:9723189e1955 40 * }
frankvnk 0:9723189e1955 41 * @endcode
frankvnk 0:9723189e1955 42 * To reduce the code overhead, this can no longer be used in the mbed compiler, however, we can replace this with type traits.<BR>
frankvnk 0:9723189e1955 43 * <BR>
frankvnk 0:9723189e1955 44 * Example:<BR>
frankvnk 0:9723189e1955 45 * @code
frankvnk 0:9723189e1955 46 * #include "mbed.h"
frankvnk 0:9723189e1955 47 * #include "GetTypeName.h"
frankvnk 0:9723189e1955 48 *
frankvnk 0:9723189e1955 49 * int main()
frankvnk 0:9723189e1955 50 * {
frankvnk 0:9723189e1955 51 * char a = 65;
frankvnk 0:9723189e1955 52 * printf("Type name for <char> '%c' is %s\r\n", a, GetTypeName(a));
frankvnk 0:9723189e1955 53 * // Check whether GetTypeName is of 'char' type.
frankvnk 0:9723189e1955 54 * // Note that strcmp returns 0 when both strings are equal.
frankvnk 0:9723189e1955 55 * if(!strcmp(GetTypeName(a),"char")) printf("'%c' is of 'char' type\r\n", a);
frankvnk 0:9723189e1955 56 * }
frankvnk 0:9723189e1955 57 * @endcode
frankvnk 0:9723189e1955 58 * <BR>
frankvnk 0:9723189e1955 59 * The DECLARE_TYPE_NAME define exists to make your life easier in declaring this traits class for all the types you expect to need.<BR>
frankvnk 0:9723189e1955 60 * This might be more useful than the solutions involving typeid because you get to control the output.<BR>
frankvnk 0:9723189e1955 61 * <BR>
frankvnk 0:9723189e1955 62 * Supported declarations
frankvnk 0:9723189e1955 63 * @verbatim
frankvnk 0:9723189e1955 64 * | Variable | Returns |
frankvnk 0:9723189e1955 65 * |--------------------|------------|
frankvnk 0:9723189e1955 66 * | char | char |
frankvnk 0:9723189e1955 67 * | uint8_t | uint8_t |
frankvnk 0:9723189e1955 68 * | signed char | int8_t |
frankvnk 0:9723189e1955 69 * | int8_t | int8_t |
frankvnk 0:9723189e1955 70 * | unsigned short | uint16_t |
frankvnk 0:9723189e1955 71 * | uint16_t | uint16_t |
frankvnk 0:9723189e1955 72 * | short | int16_t |
frankvnk 0:9723189e1955 73 * | int16_t | int16_t |
frankvnk 0:9723189e1955 74 * | unsigned int | uint32_t |
frankvnk 0:9723189e1955 75 * | uint32_t | uint32_t |
frankvnk 0:9723189e1955 76 * | int | int32_t |
frankvnk 0:9723189e1955 77 * | int32_t | int32_t |
frankvnk 0:9723189e1955 78 * | unsigned long long | uint64_t |
frankvnk 0:9723189e1955 79 * | uint64_t | uint64_t |
frankvnk 0:9723189e1955 80 * | long long | int64_t |
frankvnk 0:9723189e1955 81 * | int64_t | int64_t |
frankvnk 0:9723189e1955 82 * | float | float |
frankvnk 0:9723189e1955 83 * | double | double |
frankvnk 0:9723189e1955 84 * | bool | bool |
frankvnk 0:9723189e1955 85 * @endverbatim
frankvnk 0:9723189e1955 86 */
frankvnk 0:9723189e1955 87
frankvnk 0:9723189e1955 88 template <typename T> class type_name
frankvnk 0:9723189e1955 89 {
frankvnk 0:9723189e1955 90 public:
frankvnk 0:9723189e1955 91 static const char *name;
frankvnk 0:9723189e1955 92 };
frankvnk 0:9723189e1955 93
frankvnk 0:9723189e1955 94 DECLARE_TYPE_NAME(char);
frankvnk 0:9723189e1955 95 DECLARE_TYPE_NAME(uint8_t);
frankvnk 0:9723189e1955 96 DECLARE_TYPE_NAME(int8_t); // also valid for 'signed char'
frankvnk 0:9723189e1955 97 DECLARE_TYPE_NAME(uint16_t); // also valid for 'unsigned short'
frankvnk 0:9723189e1955 98 DECLARE_TYPE_NAME(int16_t); // also valid for 'short'
frankvnk 0:9723189e1955 99 DECLARE_TYPE_NAME(uint32_t); // also valid for 'unsigned int'
frankvnk 0:9723189e1955 100 DECLARE_TYPE_NAME(int32_t); // also valid for 'int'
frankvnk 0:9723189e1955 101 DECLARE_TYPE_NAME(uint64_t); // also valid for 'unsigned long long'
frankvnk 0:9723189e1955 102 DECLARE_TYPE_NAME(int64_t); // also valid for 'long long
frankvnk 0:9723189e1955 103 DECLARE_TYPE_NAME(float);
frankvnk 0:9723189e1955 104 DECLARE_TYPE_NAME(double);
frankvnk 0:9723189e1955 105 DECLARE_TYPE_NAME(bool);
frankvnk 0:9723189e1955 106
frankvnk 0:9723189e1955 107 } // namespace mbed
frankvnk 0:9723189e1955 108
frankvnk 0:9723189e1955 109 #endif //TYPE_NAME_H_