MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
bufmod.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/bufmod.h,v 1.5 2017/08/28 10:34:28 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 /* socket driver */
33 
34 #ifndef BUFMOD_H
35 #define BUFMOD_H
36 
37 typedef std::map<std::string, bool> TypeMap_t;
38 
39 
40 /* BufCast - begin */
41 
42 class BufCast {
43 protected:
44  size_t m_offset;
45 
46 public:
47  BufCast(size_t offset);
48  virtual ~BufCast(void);
49 
50  virtual size_t size(void) const = 0;
51  virtual size_t offset(void) const = 0;
52  virtual doublereal cast(const void *p) const = 0;
53  virtual void uncast(void *pTo, doublereal d) const = 0;
54  virtual BufCast *copy(size_t offset) const = 0;
55 };
56 
57 template <class T>
58 class TBufCast : public BufCast {
59 public:
60  TBufCast(size_t offset) : BufCast(offset) { NO_OP; };
61 
62  size_t size(void) const {
63  return sizeof(T);
64  };
65 
66  size_t offset(void) const {
67  return m_offset;
68  };
69 
70  // "cast" in the sense that whatever type comes from the stream,
71  // it is cast into a doublereal
72  doublereal cast(const void *pFrom) const {
73  const char *p = &((const char *)pFrom)[m_offset];
74  return doublereal(*((T *)p));
75  };
76 
77  // "uncast" in the sense that a doublereal is transformed into
78  // the correct type for the stream
79  void uncast(void *pTo, doublereal d) const {
80  char *p = &((char *)pTo)[m_offset];
81  ((T *)p)[0] = T(d);
82  };
83 
84  BufCast *copy(size_t offset) const {
85  return new TBufCast<T>(offset);
86  };
87 };
88 
89 template <typename T>
90 static T
91 mbswap(const T in)
92 {
93  const char *pin = (const char *)&in;
94  T out;
95  char *pout = (char *)&out;
96 
97  for (unsigned int i = 0; i < sizeof(T)/2; i++) {
98  pout[i] = pin[sizeof(T) - 1 - i];
99  pout[sizeof(T) - 1 - i] = pin[i];
100  }
101 
102  return out;
103 }
104 
105 template <int8_t>
106 static int8_t
107 mbswap(const int8_t in)
108 {
109  return in;
110 }
111 
112 template <uint8_t>
113 static uint8_t
114 mbswap(const uint8_t in)
115 {
116  return in;
117 }
118 
119 template <class T>
120 class TBufCastHToN : public TBufCast<T> {
121 public:
122  TBufCastHToN(size_t offset) : TBufCast<T>(offset) {};
123 
124  // "cast" in the sense that whatever type comes from the stream,
125  // it is cast into a doublereal
126  doublereal cast(const void *pFrom) const {
127  const char *p = &((const char *)pFrom)[TBufCast<T>::m_offset];
128  return mbswap<T>(*((T *)p));
129  };
130 
131  // "uncast" in the sense that a doublereal is transformed into
132  // the correct type for the stream
133  void uncast(void *pTo, doublereal d) const {
134  char *p = &((char *)pTo)[TBufCast<T>::m_offset];
135  ((T *)p)[0] = mbswap<T>(T(d));
136  };
137 
138  BufCast *copy(size_t offset) const {
139  return new TBufCastHToN<T>(offset);
140  };
141 };
142 
143 extern void
144 ReadBufCast(HighParser& HP, std::vector<BufCast *>& data);
145 
146 extern bool
147 bIsLittleEndian(void);
148 
149 extern void
150 SwapMapInit(TypeMap_t& swapmap);
151 
152 /* BufCast - end */
153 
154 #endif // BUFMOD_H
size_t size(void) const
Definition: bufmod.h:62
virtual void uncast(void *pTo, doublereal d) const =0
doublereal cast(const void *pFrom) const
Definition: bufmod.h:126
virtual ~BufCast(void)
Definition: bufmod.cc:51
virtual doublereal cast(const void *p) const =0
void uncast(void *pTo, doublereal d) const
Definition: bufmod.h:79
virtual size_t size(void) const =0
virtual size_t offset(void) const =0
#define NO_OP
Definition: myassert.h:74
TBufCastHToN(size_t offset)
Definition: bufmod.h:122
size_t offset(void) const
Definition: bufmod.h:66
BufCast(size_t offset)
Definition: bufmod.cc:45
bool bIsLittleEndian(void)
Definition: bufmod.cc:259
TBufCast(size_t offset)
Definition: bufmod.h:60
void uncast(void *pTo, doublereal d) const
Definition: bufmod.h:133
BufCast * copy(size_t offset) const
Definition: bufmod.h:138
doublereal cast(const void *pFrom) const
Definition: bufmod.h:72
BufCast * copy(size_t offset) const
Definition: bufmod.h:84
void ReadBufCast(HighParser &HP, std::vector< BufCast * > &data)
Definition: bufmod.cc:287
std::map< std::string, bool > TypeMap_t
Definition: bufmod.h:37
static T mbswap(const T in)
Definition: bufmod.h:91
size_t m_offset
Definition: bufmod.h:44
virtual BufCast * copy(size_t offset) const =0
void SwapMapInit(TypeMap_t &swapmap)
Definition: bufmod.cc:266
double doublereal
Definition: colamd.c:52
Definition: bufmod.h:42