COVISE Core
tokenbuffer.h
Go to the documentation of this file.
1/* This file is part of COVISE.
2
3 You can use it under the terms of the GNU Lesser General Public License
4 version 2.1 or later, see lgpl-2.1.txt.
5
6 * License: LGPL 2+ */
7
8#ifndef TokenBuffer_H
9#define TokenBuffer_H
10
11#include "dataHandle.h"
12
13#include <string.h>
14#include <stdio.h>
15#include <iostream>
16#include <string>
17
18
19#include <util/coExport.h>
20#include <util/byteswap.h>
21
22#ifdef _WIN64
23#define __WORDSIZE 64
24#endif
25
26/*
27 $Log: $
28 * Revision 1.4 1994/03/23 18:07:03 zrf30125
29 * Modifications for multiple Shared Memory segments have been finished
30 * (not yet for Cray)
31 *
32 * Revision 1.3 93/10/11 09:22:19 zrhk0125
33 * new types DM_CONTACT_DM and APP_CONTACT_DM included
34 *
35 * Revision 1.2 93/10/08 19:18:06 zrhk0125
36 * data type sizes introduced
37* some fixed type sizes with sizeof calls replaced
38 *
39 * Revision 1.1 93/09/25 20:47:03 zrhk0125
40 * Initial revision
41 *
42 */
43
44/***********************************************************************\
45 ** **
46 ** Message classes Version: 1.1 **
47 ** **
48 ** **
49 ** Description : The basic message structure as well as ways to **
50 ** initialize messages easily are provided. **
51 ** Subclasses for special types of messages **
52 ** can be introduced. **
53 ** **
54 ** Classes : Message, ShmMessage **
55 ** **
56 ** Copyright (C) 1993 by University of Stuttgart **
57 ** Computer Center (RUS) **
58 ** Allmandring 30 **
59 ** 7000 Stuttgart 80 **
60 ** HOSTID **
61 ** **
62 ** Author : A. Wierse (RUS) **
63 ** **
64 ** History : **
65 ** 15.04.93 Ver 1.0 **
66 ** 15.04.93 Ver 1.1 new Messages and type added **
67 ** sender and send_type added **
68 ** **
69 ** **
70\***********************************************************************/
71
72namespace covise
73{
74class UdpMessage;
75class MessageBase;
76class DataHandle;
77
78class NETEXPORT TokenBuffer// class for tokens
79{
80private:
81 enum Types
82 {
83 TbBool = 7,
90 TbTB, //TokenBuffer or DataHandle
92 };
94 void puttype(Types t);
95 bool checktype(Types t);
96
97 bool debug = false;
98 int buflen = 0; // number of allocated bytes
99
100 char *currdata = nullptr; // pointer to the tokens
101 bool networkByteOrder = false;
102
103 void incbuf(int size = 100);
104
105public:
106 TokenBuffer();
107 explicit TokenBuffer(bool nbo);
108 //creates a TokenBuffer with allocated memory
109 explicit TokenBuffer(int al, bool nbo = false);
110
111 TokenBuffer(const MessageBase *msg, bool nbo = false);
112 TokenBuffer(const DataHandle& dh, bool nbo = false);
113 TokenBuffer(const char *dat, int len, bool nbo = false);
114 virtual ~TokenBuffer();
115 TokenBuffer &operator=(const TokenBuffer &other);
116 bool operator==(const TokenBuffer &other) const;
117
118 const DataHandle& getData();
119 const char *getBinary(int n);
120 void addBinary(const char *buf, int n);
121 const char *allocBinary(int n);
122
123 TokenBuffer &operator<<(const bool b);
124 TokenBuffer& operator<<(const uint64_t i);
125 TokenBuffer& operator<<(const time_t i);
126#ifndef WIN32 // it does not work on win32 as size_t == int
127 //TokenBuffer& operator << (const size_t s){return (*this<<(uint64_t)s);}
128#endif
129 TokenBuffer &operator<<(const uint32_t i);
130 TokenBuffer &operator<<(const int i);
132 TokenBuffer &operator<<(const char c);
133 TokenBuffer &operator<<(const float f);
134 TokenBuffer &operator<<(const double f);
135 TokenBuffer &operator<<(const char *c);
138
139 TokenBuffer &operator>>(bool &b);
140 TokenBuffer& operator>>(uint64_t& i);
141 TokenBuffer& operator>>(time_t& i);
142#ifndef WIN32 // it does not work on win32 as size_t == int
143 //TokenBuffer& operator >> (size_t &s){uint64_t i; *this>>i; s=i; return *this; }
144#endif
145 TokenBuffer &operator>>(uint32_t &i);
146 TokenBuffer &operator>>(int &i);
147 TokenBuffer &operator>>(char &c);
148 TokenBuffer &operator>>(unsigned char &c);
149 TokenBuffer &operator>>(float &f);
150 TokenBuffer &operator>>(double &f);
152 TokenBuffer &operator>>(const char *&c);
155
157 protected:
158 struct State {
159 State(const TokenBuffer& tb);
160 void apply(TokenBuffer& tb);
162 int buflen = 0, datalen = 0;
163 };
166 State rewind();
167 void resetRewind(State s);
168 private:
170 };
172 template <typename T>
173 class PlaceHolder : private PlaceHolderBase {
175 public:
176 void replace(const T& value) {
177 auto s = rewind();
178 m_tb << value;
179 resetRewind(s);
180 }
181 private:
183
184 };
185
186 template<typename T>
188 static_assert(std::is_pod<T>::value, "TokenBuffer::addPlaceHolder only works for playi old data types");
189 if (!data.data())
190 {
191 incbuf();
192 }
193 PlaceHolder<T> p(*this);
194 *this << t;
195 return p;
196 }
197 uint32_t get_int_token();
198 char get_char_token();
199 float get_float_token();
200 char *get_charp_token();
201
202 void reset();
203 void rewind();
204};
205}
206
207#endif
#define NETEXPORT
Definition: coExport.h:373
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
GLdouble n
Definition: khronos-glext.h:8447
GLboolean reset
Definition: khronos-glext.h:6369
const GLubyte * c
Definition: khronos-glext.h:9864
GLsizeiptr size
Definition: khronos-glext.h:6610
GLsizei const GLfloat * value
Definition: khronos-glext.h:6760
GLdouble GLdouble t
Definition: khronos-glext.h:6449
GLfloat f
Definition: khronos-glext.h:8258
GLboolean GLboolean GLboolean b
Definition: khronos-glext.h:6895
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: khronos-glext.h:8469
GLfloat GLfloat p
Definition: khronos-glext.h:9861
GLenum GLsizei len
Definition: khronos-glext.h:7440
GLdouble s
Definition: khronos-glext.h:6441
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: khronos-glext.h:6354
list of all chemical elements
Definition: coConfig.h:27
NETEXPORT covise::TokenBuffer & operator>>(covise::TokenBuffer &tb, covise::Program &userType)
Definition: program_type.cpp:34
QTextStream & operator<<(QTextStream &out, const coConfigEntryStringList list)
Definition: coConfigEntryString.cpp:159
INLINE bool operator==(const coObjID &a, const coObjID &b)
Definition: coObjID.h:166
Definition: dataHandle.h:18
Definition: message.h:65
Definition: tokenbuffer.h:79
DataHandle data
Definition: tokenbuffer.h:93
friend PlaceHolderBase
Definition: tokenbuffer.h:171
Types
Definition: tokenbuffer.h:82
@ TbString
Definition: tokenbuffer.h:88
@ TbBinary
Definition: tokenbuffer.h:91
@ TbTB
Definition: tokenbuffer.h:90
@ TbChar
Definition: tokenbuffer.h:89
@ TbFloat
Definition: tokenbuffer.h:86
@ TbDouble
Definition: tokenbuffer.h:87
@ TbInt64
Definition: tokenbuffer.h:84
@ TbInt32
Definition: tokenbuffer.h:85
PlaceHolder< T > addPlaceHolder(const T &t=T())
Definition: tokenbuffer.h:187
Definition: tokenbuffer.h:156
TokenBuffer & m_tb
Definition: tokenbuffer.h:164
State m_state
Definition: tokenbuffer.h:169
Definition: tokenbuffer.h:158
int currdata
Definition: tokenbuffer.h:161
Definition: tokenbuffer.h:173
friend TokenBuffer
Definition: tokenbuffer.h:174
void replace(const T &value)
Definition: tokenbuffer.h:176
PlaceHolder(TokenBuffer &tb)
Definition: tokenbuffer.h:182