EVE 1.0
static-counter.h
Go to the documentation of this file.
1 #ifndef STATIC_COUNTER_H_INCLUDED
2 #define STATIC_COUNTER_H_INCLUDED
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2014-2015, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * @brief A part of TOC table compile-time generator
36  *
37  * @author DT, Jetro AS
38  */ /******************************************************************/
39 
40 #include "int2type.h"
41 
42 template <class T, class U>
43 class CStaticCounter
44  : public CStaticCounter<IntToType<T::value - 1>, U>
45 {};
46 
47 template<class U> \
48 class CStaticCounter<IntToType<0>, U> \
49 { unsigned char space; };
50 
51 #define DEF_STATIC_COUNTER_EX(type, line) \
52  class CStaticCounterType_ ## type;\
53  template<> \
54  class CStaticCounter<IntToType<line>, CStaticCounterType_ ## type> \
55  { unsigned char space; }
56 
57 #define DEF_STATIC_COUNTER(type) \
58  DEF_STATIC_COUNTER_EX(type, __LINE__)
59 
60 #define INC_STATIC_COUNTER_EX(type, line) \
61  template<> \
62  class CStaticCounter<IntToType<line>, CStaticCounterType_ ## type> \
63  { unsigned char space[GET_STATIC_COUNTER(type) + 2]; }
64 
65 #define INC_STATIC_COUNTER(type) \
66  INC_STATIC_COUNTER_EX(type, __LINE__)
67 
68 #define ADD_STATIC_COUNTER_EX(type, val, line) \
69  template<> \
70  class CStaticCounter<IntToType<line>, CStaticCounterType_ ## type> \
71  { unsigned char space[GET_STATIC_COUNTER(type) + 1 + (val)]; }
72 
73 #define ADD_STATIC_COUNTER(type, val) \
74  ADD_STATIC_COUNTER_EX(type, val, __LINE__)
75 
76 #define SET_STATIC_COUNTER_EX(type, val, line) \
77  template<> \
78  class CStaticCounter<IntToType<line>, CStaticCounterType_ ## type> \
79  { unsigned char space[1 + (val)]; }
80 
81 #define SET_STATIC_COUNTER(type, val) \
82  SET_STATIC_COUNTER_EX(type, val, __LINE__)
83 
84 #define GET_STATIC_COUNTER_EX(type, line) \
85  (sizeof(CStaticCounter<IntToType<line - 1>, CStaticCounterType_ ## type>) - 1)
86 
87 #define GET_STATIC_COUNTER(type) \
88  GET_STATIC_COUNTER_EX(type, __LINE__)
89 
90 #if 0
91 // Usage:
92 DEF_STATIC_COUNTER(test);
93 static const int cnt0 = GET_STATIC_COUNTER(test); // -> 0
94 INC_STATIC_COUNTER(test);
95 static const int cnt1 = GET_STATIC_COUNTER(test); // -> 1
96 INC_STATIC_COUNTER(test);
97 INC_STATIC_COUNTER(test);
98 static const int cnt2 = GET_STATIC_COUNTER(test); // -> 3
99 ADD_STATIC_COUNTER(test, -1);
100 static const int cnt3 = GET_STATIC_COUNTER(test); // -> 2
101 SET_STATIC_COUNTER(test, 4);
102 static const int cnt4 = GET_STATIC_COUNTER(test); // -> 4
103 #endif
104 
105 #endif /* STATIC_COUNTER_H_INCLUDED */
A part of TOC table compile-time generator.