summaryrefslogtreecommitdiffstats
path: root/mcop/common.h
blob: 2dbde7070512bd54ca697d4e15958cf97e9726d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       [email protected]

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#ifndef COMMON_H
#define COMMON_H

#include "buffer.h"
#include "type.h"
#include "dispatcher.h"
#include "object.h"
#include "connection.h"
#include "objectmanager.h"
#include "idlfilereg.h"
#include "asyncstream.h"
#include "mcoputils.h"
#include "anyref.h"
#include "connect.h"

/*
 * BC - Status (2002-03-08): Read/WriteObject(Seq)
 *
 * Will be kept binary compatible by NOT TOUCHING AT ALL. Do not change this.
 * (Interaction with generated code).
 */

/* define this to see what mcop transfers around */
#undef DEBUG_IO

/* define this to see what mcop messages are processed */
#undef DEBUG_MESSAGES

#include "core.h"

namespace Arts {
/* some marshalling helpers */

template<class T>
void readTypeSeq(Buffer& stream, std::vector<T>& sequence) {
	sequence.clear();

	unsigned long l = stream.readLong();
	while(l--) sequence.push_back(T(stream));
}

template<class T>
void writeTypeSeq(Buffer& stream, const std::vector<T>& sequence) {
	stream.writeLong(sequence.size());
	for(unsigned long l=0;l<sequence.size();l++)
		sequence[l].writeType(stream);
}

template<class T>
void writeObject(Buffer& stream, T* object) {
	if(object)
	{
		/*
	 	 * perhaps reimplement directly (without conversion to/from string)
	 	 * for more speed
	 	 */

		std::string s = object->_toString();

		Buffer buffer;
		buffer.fromString(s,"MCOP-Object");
		ObjectReference reference(buffer);

		object->_copyRemote();	// prevents that the object is freed for a while
		reference.writeType(stream);
	}
	else
	{
		ObjectReference null_reference;

		null_reference.serverID = "null";
		null_reference.objectID = 0;
		null_reference.writeType(stream);
	}
}

template<class T>
void readObject(Buffer& stream, T*& result) {
	ObjectReference reference(stream);

	if(reference.serverID == "null")
		result = 0;		// null reference?
	else
		result = T::_fromReference(reference,false);
}

template<class T>
void readObjectSeq(Buffer& stream, std::vector<T>& sequence)
{
	sequence.clear();

	unsigned long l = stream.readLong();
	while(l--)
	{
		typename T::_base_class *temp;
		readObject(stream, temp);
		sequence.push_back(T::_from_base(temp));
	}
}

template<class T>
void writeObjectSeq(Buffer& stream, const std::vector<T>& sequence)
{
	stream.writeLong(sequence.size());

	for(unsigned long l=0;l<sequence.size();l++)
	{
		T object = sequence[l];
		writeObject(stream,object._base());
	}
}

#ifndef MCOPBYTE_DEFINED
#define MCOPBYTE_DEFINED
typedef unsigned char mcopbyte;
#endif

}
#endif