COFFI
1.2
               
coffi_utils.hpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2014-2014 by Serge Lamikhov-Center
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 /*! @file coffi_utils.hpp
24  * @brief COFFI library utilities.
25  *
26  * Do not include this file directly. This file is included by coffi.hpp.
27  */
28 
29 #ifndef COFFI_UTILS_HPP
30 #define COFFI_UTILS_HPP
31 
32 #include <stdexcept>
33 #include <coffi/coffi_types.hpp>
34 
35 #define STRINGIFY(NAME) #NAME
36 
37 /*! @name Macros for accessing the COFF structures fields
38  * @anchor group_access
39  *
40  * These macros provide a way to declare and define the get/set accessors to the COFF structures fields.
41  * <br>The list of the classes using these macro, and the COFF structures accessed is given by: @ref accessors.
42  *
43  * The declaration functions <b>*_DECL</b> are used in virtual template classes.
44  * <br>They declare the interface for all the classes, derived from the same virtual template class.
45  * <br>This is useful when a COFF structure has several flavors, depending on the COFF file architecture.
46  *
47  * @{
48  */
49 
50 //! Declares a **get_NAME** function for accessing the **NAME** structure field.
51 #define COFFI_GET_ACCESS_DECL(TYPE, NAME) virtual TYPE get_##NAME() const = 0
52 
53 //! Declares a **set_NAME** function for accessing the **NAME** structure field.
54 #define COFFI_SET_ACCESS_DECL(TYPE, NAME) \
55  virtual void set_##NAME(TYPE value) = 0
56 
57 //! Declares a **get_NAME** and a **set_NAME** functions for accessing the **NAME** structure field.
58 #define COFFI_GET_SET_ACCESS_DECL(TYPE, NAME) \
59  virtual TYPE get_##NAME() const = 0; \
60  virtual void set_##NAME(TYPE value) = 0
61 
62 //! Defines a **get_NAME** function for accessing the **NAME** structure field.
63 #define COFFI_GET_ACCESS(TYPE, NAME) \
64  TYPE get_##NAME() const { return header.NAME; }
65 
66 //! Defines a **set_NAME** function for accessing the **NAME** structure field.
67 #define COFFI_SET_ACCESS(TYPE, NAME) \
68  void set_##NAME(TYPE value) { header.NAME = value; }
69 
70 //! Defines a **get_NAME** and a **set_NAME** functions for accessing the **NAME** structure field.
71 #define COFFI_GET_SET_ACCESS(TYPE, NAME) \
72  TYPE get_##NAME() const { return header.NAME; } \
73  void set_##NAME(TYPE value) { header.NAME = value; }
74 
75 //! Disables the **get_NAME** function for prohibiting read accesses to the **NAME** structure field.
76 #define COFFI_GET_ACCESS_NONE(TYPE, NAME) \
77  TYPE get_##NAME() const \
78  { \
79  throw std::runtime_error("The header field '" STRINGIFY( \
80  NAME) "' is not applicable to this COFF version"); \
81  }
82 
83 //! Disables the **set_NAME** function for prohibiting write accesses to the **NAME** structure field.
84 #define COFFI_SET_ACCESS_NONE(TYPE, NAME) \
85  void set_##NAME(TYPE value) \
86  { \
87  throw std::runtime_error("The header field '" STRINGIFY( \
88  NAME) "' is not applicable to this COFF version"); \
89  }
90 
91 //! Disables the **get_NAME** and the **set_NAME** function for prohibiting all accesses to the **NAME** structure field.
92 #define COFFI_GET_SET_ACCESS_NONE(TYPE, NAME) \
93  TYPE get_##NAME() const \
94  { \
95  throw std::runtime_error("The header field '" STRINGIFY( \
96  NAME) "' is not applicable to this COFF version"); \
97  } \
98  void set_##NAME(TYPE) \
99  { \
100  throw std::runtime_error("The header field '" STRINGIFY( \
101  NAME) "' is not applicable to this COFF version"); \
102  }
103 
104 //! Declares the **get_sizeof** function for returning the size of the COFF file structure.
105 #define COFFI_GET_SIZEOF_DECL() virtual size_t get_sizeof() const = 0
106 
107 //! Defines the **get_sizeof** function for returning the size of the COFF file structure.
108 #define COFFI_GET_SIZEOF() \
109  size_t get_sizeof() const { return sizeof(header); }
110 
111 //! @}
112 
113 #endif // COFFI_UTILS_HPP
COFFI library basic structures and types.