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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
// culltrans_impl.cpp
// write implementation file
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <fstream>
#include "culltrans_repository.h"
#include "culltrans.h"
#ifdef HAVE_STD
using namespace std;
#endif
// global variables
static string prefix;
static string key;
static int key_type = lEndT;
static string indent(" ");
// write Header
static bool writeHeader(ofstream& impl, map<string, List>::iterator& elem) {
vector<Elem>::iterator it;
map<string, List>::iterator list = lists.end();
impl << "// " << elem->second.name << "_implementation.cpp" << endl;
impl << "// this file is automatically generated. DO NOT EDIT" << endl << endl;
// nothing to do from now on if it's not an interface
if(!elem->second.interf)
return false;
impl << "#include <OB/CORBA.h>" << endl;
impl << "#include \"qidl_common.h\"" << endl;
impl << "#include \"" << elem->second.name << "_implementation.h\"" << endl;
for(it=elem->second.elems.begin(); it != elem->second.elems.end(); ++it) {
if(it->type == lListT && (list = lists.find(it->listType)) != lists.end() && list->second.interf)
impl << "#include \"" << list->second.name << "_implementation.h\"" << endl;
}
impl << "#include \"Master_impl.h\"" << endl;
impl << "extern \"C\" {" << endl;
impl << "#include \"sge_api.h\"" << endl;
impl << "#include \"" << elem->second.file << "\"" << endl;
impl << "}" << endl;
impl << endl;
return true;
}
// get type prefix (e.g. QU_, JB_, CAL_) and key element
bool getType(map<string, List>::iterator& elem) {
vector<Elem>::iterator it;
prefix = elem->second.type.substr(0, elem->second.type.find('_')+1);
for(it=elem->second.elems.begin(); it != elem->second.elems.end(); ++it)
if(it->key) {
key = it->name;
key_type = it->type;
break;
}
if(key.empty()) {
cerr << "No key element found for " << elem->second.name << "." << endl;
return false;
}
return true;
}
// write c'tor and d'tor
void writeCtorDtor(ofstream& impl, map<string, List>::iterator& elem) {
impl << "GE_" << elem->second.name << "_implementation::GE_";
impl << elem->second.name << "_implementation(const ";
if(key_type == lUlongT)
impl << "GE_sge_ulong ";
else
impl << "char* ";
impl << "_key, CORBA_ORB_var o)" << endl;
//if(key_type == lUlongT)
impl << indent << ": key(_key), ";
//else
//impl << indent << ": key(CORBA_string_dup(_key)), ";
impl << "self(0), newSelf(0), orb(o), creation(0), lastEvent(0), ";
impl << "apiListType(" << elem->second.sge_list_type << ") {}" << endl << endl;
impl << "GE_" << elem->second.name << "_implementation::GE_";
impl << elem->second.name << "_implementation(const ";
if(key_type == lUlongT)
impl << "GE_sge_ulong ";
else
impl << "char* ";
impl << "_key, const time_t& tm, CORBA_ORB_var o)" << endl;
//if(key_type == lUlongT)
impl << indent << ": key(_key), ";
//else
//impl << indent << ": key(CORBA_string_dup(_key)), ";
impl << "self(0), newSelf(0), orb(o), creation(tm), lastEvent(0) {}" << endl << endl;
impl << "GE_" << elem->second.name << "_implementation::~GE_";
impl << elem->second.name << "_implementation() {}" << endl << endl;
}
// write get functions
bool writeGetFuncs(ofstream& impl, map<string, List>::iterator& elem) {
vector<Elem>::iterator it;
map<string, List>::iterator list = lists.end();
for(it=elem->second.elems.begin(); it != elem->second.elems.end(); ++it) {
if(it->idlonly || it->key || ((it->type == lListT) && ((list = lists.find(it->listType)) != lists.end()) && list->second.interf))
continue;
if(it->type == lListT) {
list = lists.find(it->listType);
if(list != lists.end())
if(it->object)
impl << "GE_" << list->second.name << "* ";
else
impl << "GE_" << list->second.name << "Seq* ";
else {
cerr << "Could not find " << it->listType << "." << endl;
return false;
}
}
else
impl << multiType2sgeType[it->type] << ' ';
impl << "GE_" << elem->second.name << "_implementation::get_" << it->name;
impl << "(CORBA_Context* ctx) {" << endl;
impl << indent << "QENTER(\"" << elem->second.name;
impl << "::get_" << it->name << "\");" << endl;
impl << indent << "AUTO_LOCK_MASTER;" << endl;
impl << indent << "qidl_authenticate(ctx);" << endl;
impl << indent << "getSelf();" << endl << endl;
if(it->type == lListT) {
list = lists.find(it->listType);
if(!list->second.interf) {
impl << indent << "lList* lp = lGetList(self, ";
impl << prefix << it->name << ");" << endl;
impl << indent << "GE_" << list->second.name << "Seq* foo = ";
impl << "cull2" << list->second.name << "Seq(lp);" << endl;
}
else {
// get other's type prefix (e.g. QU_, JB_, CAL_) and key element
string other_prefix = list->second.type.substr(0, list->second.type.find('_')+1);
vector<Elem>::iterator other;
for(other=list->second.elems.begin(); other != list->second.elems.end(); ++other)
if(other->key)
break;
if(other == list->second.elems.end()) {
cerr << "No key element found for " << list->second.name << "." << endl;
return false;
}
impl << indent << "// build the list of object refs" << endl;
impl << indent << "lList* lp = lGetList(self, ";
impl << prefix << it->name << ");" << endl;
impl << indent << "lListElem* lep;" << endl;
impl << indent << "GE_" << list->second.name << "_implementation* x;" << endl << endl;
impl << indent << "GE_" << list->second.name << "Seq* foo =";
impl << " new GE_" << list->second.name << "Seq();" << endl;
impl << indent << "for_each_cpp(lep, lp) {" << endl;
impl << indent << indent << "x = GE_Master_impl::instance()";
impl << "->get" << list->second.name << "Impl(lGet";
impl << multiType2getType[other->type] << "(lep, ";
impl << other_prefix << other->name << "));" << endl;
impl << endl << indent << indent << "if(x)" << endl;
impl << indent << indent << indent << "foo->append(GE_";
impl << list->second.name << "_implementation::_duplicate(x));" << endl;
impl << indent << "}" << endl << endl;
}
}
else if(it->type == lStringT) {
impl << indent << "char* temp = lGetString(self, ";
impl << prefix << it->name << ");" << endl;
impl << indent << "GE_sge_string foo = CORBA_string_dup(temp?temp:\"\");" << endl;
}
else {
impl << indent << multiType2sgeType[it->type] << " foo = lGet";
impl << multiType2getType[it->type] << "(self, ";
impl << prefix << it->name << ");" << endl;
}
impl << indent << "return foo;" << endl;
impl << "}" << endl << endl;
}
return true;
}
// write set functions
bool writeSetFuncs(ofstream& impl, map<string, List>::iterator& elem) {
vector<Elem>::iterator it;
map<string, List>::iterator list = lists.end();
for(it=elem->second.elems.begin(); it != elem->second.elems.end(); ++it) {
if(it->idlonly || it->readonly || it->key || ((it->type == lListT) && ((list = lists.find(it->listType)) != lists.end()) && list->second.interf))
continue;
impl << "GE_sge_ulong GE_" << elem->second.name << "_implementation::set_" << it->name << "(";
if(it->type == lListT) {
list = lists.find(it->listType);
if(list != lists.end()) {
impl << "const GE_" << list->second.name << "Seq& ";
}
else {
cerr << "Could not find " << it->listType << "." << endl;
return false;
}
}
else if(it->type == lStringT)
impl << "const char* ";
else
impl << multiType2sgeType[it->type] << ' ';
impl << "val, CORBA_Context* ctx) {" << endl;
impl << indent << "QENTER(\"" << elem->second.name;
impl << "::set_" << it->name << "\");" << endl;
impl << indent << "AUTO_LOCK_MASTER;" << endl;
impl << indent << "qidl_authenticate(ctx);" << endl;
impl << indent << "getSelf();" << endl << endl;
impl << indent << "if(creation != 0) {" << endl;
impl << indent << indent << "lSet" << multiType2getType[it->type];
impl << "(self, " << prefix << it->name << ", ";
if(it->type == lListT) {
impl << list->second.name << "Seq2cull((GE_" << list->second.name;
impl << "Seq&)val));" << endl;
}
else if(it->type == lStringT)
impl << "(char*)val);" << endl;
else
impl << "val);" << endl;
impl << indent << indent << "return 0;" << endl;
impl << indent << "}" << endl << endl;
impl << indent << "lListPtr lp;" << endl;
impl << indent << "lListPtr alp;" << endl;
impl << indent << "lListElem* lep;" << endl;
impl << indent << "lEnumeration* what;" << endl << endl;
impl << indent << "lp = lCreateList(\"My " << it->name << " list\", ";
impl << prefix << "Type);" << endl;
impl << indent << "lAppendElem(lp, (lep = lCopyElem(self)));" << endl;
impl << indent << "lSet" << multiType2getType[it->type] << "(lep, ";
impl << prefix << it->name << ", ";
if(it->type == lListT) {
impl << list->second.name << "Seq2cull((GE_" << list->second.name;
impl << "Seq&)val));" << endl;
}
else if(it->type == lStringT)
impl << "(char*)val);" << endl;
else
impl << "val);" << endl;
impl << indent << "what = lWhat(\"%T(%I %I)\", " << prefix << "Type, ";
impl << prefix << key << ", " << prefix << it->name << ");" << endl;
impl << indent << "alp = sge_api(apiListType";
impl << ", SGE_API_MOD, &lp, NULL, what);" << endl;
impl << indent << "lFreeWhat(&what);" << endl;
impl << indent << "throwErrors(alp);" << endl;
impl << indent << "return lastEvent;" << endl;
impl << "}" << endl << endl;
}
return true;
}
// write get content
bool writeGetContent(ofstream& impl, map<string, List>::iterator& elem) {
vector<Elem>::iterator it;
map<string, List>::iterator list = lists.end();
impl << "GE_contentSeq* GE_" << elem->second.name << "_implementation::get_content(CORBA_Context* ctx) {" << endl;
impl << indent << "QENTER(\"" << elem->second.name;
impl << "::get_content\");" << endl;
impl << indent << "AUTO_LOCK_MASTER;" << endl;
impl << indent << "qidl_authenticate(ctx);" << endl;
impl << indent << "getSelf();" << endl << endl;
impl << indent << "// build content sequence" << endl;
impl << indent << "GE_contentSeq* cs = new GE_contentSeq;" << endl;
impl << indent << "char* dummy;" << endl << endl;
impl << indent << "cs->length(" << elem->second.elems.size() << ");" << endl << endl;
unsigned long i=0;
for(it=elem->second.elems.begin(), i=0; it != elem->second.elems.end(); ++it, i++) {
impl << indent << "// " << it->name << endl;
impl << indent << "cs->operator[](" << i << ").elem = GE_" << prefix << it->name << ";" << endl;
impl << indent << "cs->operator[](" << i << ").value <<= ";
if(it->type == lListT) {
list = lists.find(it->listType);
if(list != lists.end())
impl << "get_" << it->name << "(GE_Master_impl::instance()->getMasterContext());" << endl;
else {
cerr << "Could not find " << it->listType << "." << endl;
return false;
}
}
else if(it->idlonly)
impl << "get_" << it->name << "(GE_Master_impl::instance()->getMasterContext());" << endl;
else if(it->type == lBoolT)
impl << "CORBA_Any::from_boolean(" << "(" << multiType2sgeType[it->type] << ")lGet" << multiType2getType[it->type] << "(self, " << prefix << it->name << "));" << endl;
else if(it->type == lStringT)
impl << "(dummy = lGetString(self, " << prefix << it->name << "))?dummy:\"\";" << endl;
else
impl << "(" << multiType2sgeType[it->type] << ")lGet" << multiType2getType[it->type] << "(self, " << prefix << it->name << ");" << endl;
}
impl << indent << "return cs;" << endl;
impl << "}" << endl << endl;
return true;
}
// write set content
bool writeSetContent(ofstream& impl, map<string, List>::iterator& elem) {
vector<Elem>::iterator it;
map<string, List>::iterator list = lists.end();
impl << "GE_sge_ulong GE_" << elem->second.name << "_implementation::set_content(const GE_contentSeq& cs, CORBA_Context* ctx) {" << endl;
impl << indent << "QENTER(\"" << elem->second.name;
impl << "::set_content\");" << endl;
impl << indent << "AUTO_LOCK_MASTER;" << endl;
impl << indent << "qidl_authenticate(ctx);" << endl;
impl << indent << "getSelf();" << endl << endl;
impl << indent << "// intermediate variables" << endl;
for(list=lists.begin(); list != lists.end(); ++list)
for(it=elem->second.elems.begin(); it != elem->second.elems.end(); ++it)
if(it->type == lListT && it->listType == list->second.type) {
impl << indent << "GE_" << list->second.name;
if(!it->object)
impl << "Seq";
impl << "* " << list->second.name << "_val;" << endl;
break;
}
for(int x=1; x<lListT; x++)
impl << indent << multiType2sgeType[x] << " " << multiType2getType[x] << "_val;" << endl;
impl << indent << "lList* " << multiType2getType[lListT] << "_val;" << endl;
impl << indent << "int* nameVector = new int[cs.length()+1];" << endl;
impl << indent << "lListPtr lp = lCreateList(\"set content\", " << elem->second.type << ");" << endl;
impl << indent << "lListElem* lep = lCreateElem(" << elem->second.type << ");" << endl;
impl << indent << "lAppendElem(lp, lep);" << endl;
impl << indent << "// now pretend we're CORBA-only" << endl;
impl << indent << "time_t old_creation = creation;" << endl;
impl << indent << "creation = 1;" << endl;
impl << indent << "lListElem* oldSelf = self;" << endl;
impl << indent << "self = lep;" << endl << endl;
impl << indent << "// check the sequence" << endl;
impl << indent << "for(CORBA_ULong i=0; i<cs.length(); i++) {" << endl;
impl << indent << indent << "switch(cs[i].elem) {" << endl;
for(it=elem->second.elems.begin(); it != elem->second.elems.end(); ++it) {
// don't try to set readonly elems
// maybe throwing an exception would be better
// but for now these are handled in default:
if(it->readonly)
continue;
impl << indent << indent << "case " << prefix << it->name << ":" << endl;
if(it->type == lListT) {
list = lists.find(it->listType);
impl << indent << indent << indent << "cs[i].value >>= " << list->second.name << "_val;" << endl;
if(!list->second.interf)
impl << indent << indent << indent << multiType2getType[it->type] << "_val = " << list->second.name << "Seq2cull(*" << list->second.name << "_val);" << endl;
else {
impl << indent << indent << indent << "set_" << it->name << "(";
if(!it->object)
impl << "*";
impl << list->second.name << "_val, GE_Master_impl::instance()->getMasterContext());" << endl;
}
}
else
impl << indent << indent << indent << "cs[i].value >>= " << multiType2getType[it->type] << "_val;" << endl;
if(it->type != lListT || (it->type == lListT && !list->second.interf))
impl << indent << indent << indent << "lSet" << multiType2getType[it->type] << "(lep, " << prefix << it->name << ", " << multiType2getType[it->type] << "_val);" << endl;
else if(it->type != lListT && it->idlonly)
impl << indent << indent << indent << "set_" << it->name << "(" << multiType2getType[it->type] << "_val , GE_Master_impl::instance()->getMasterContext());" << endl;
impl << indent << indent << indent << "break;" << endl;
}
impl << indent << indent << "default:" << endl;
impl << indent << indent << indent << "break;" << endl;
impl << indent << indent << "}" << endl;
impl << indent << indent << "nameVector[i] = cs[i].elem;" << endl;
impl << indent << "}" << endl;
impl << indent << "nameVector[cs.length()] = NoName;" << endl << endl;
impl << indent << "// don't forget to set back" << endl;
impl << indent << "creation = old_creation;" << endl;
impl << indent << "self = oldSelf;" << endl << endl;
impl << indent << "// now set the state" << endl;
impl << indent << "if(creation) {" << endl;
impl << indent << indent << "lFreeElem(&self);" << endl;
impl << indent << indent << "self = lCopyElem(lep);" << endl;
impl << indent << "}" << endl;
impl << indent << "else {" << endl;
impl << indent << indent << "lListPtr alp;" << endl;
impl << indent << indent << "lEnumeration* what = lIntVector2What(" << elem->second.type << ", nameVector);" << endl;
impl << indent << indent << "alp = sge_api(apiListType, SGE_API_MOD, &lp, NULL, what);" << endl;
impl << indent << indent << "lFreeWhat(&what);" << endl;
impl << indent << indent << "throwErrors(alp);" << endl;
impl << indent << "}" << endl << endl;
impl << indent << "return lastEvent;" << endl;
impl << "}" << endl << endl;
return true;
}
// writeImplementation
// writes out the implementation file for a given interface or struct
// this file contains only those functions that can be generated
// automatically by the meta data
bool writeImplementation(map<string, List>::iterator& elem) {
cout << "Creating implementation file for " << elem->second.name << endl;
vector<Elem>::iterator it;
map<string, List>::iterator list = lists.end();
// open output file
string file(elem->second.name);
file += "_implementation.cpp";
ofstream impl(file.c_str());
if(!impl) {
cerr << "Could not open output file for " << elem->second.name << endl;
return false;
}
// write header and #includes
// returns false if not an interface
if(!writeHeader(impl, elem))
return true;
// get type prefix (e.g. QU_, JB_, CAL_) and key element
if(!getType(elem))
return false;
// write c'tor and d'tor
writeCtorDtor(impl, elem);
// write get functions
writeGetFuncs(impl, elem);
// write set functions
writeSetFuncs(impl, elem);
// write get content
writeGetContent(impl, elem);
// write set content
writeSetContent(impl, elem);
impl.close();
return true;
}
|