| 1 | #include "foo.h" |
| 2 | #include <iostream> |
| 3 | #include <string.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdexcept> |
| 6 | |
| 7 | int print_something(const char *message) |
| 8 | { |
| 9 | std::cout << "MESSAGE1: " << message << std::endl; |
| 10 | return strlen(message); |
| 11 | } |
| 12 | |
| 13 | int print_something_else(const char *message2) |
| 14 | { |
| 15 | std::cout << "MESSAGE2: " << message2 << std::endl; |
| 16 | return strlen(message2); |
| 17 | } |
| 18 | |
| 19 | int get_int_from_string(const char *from_string, int multiplier) |
| 20 | { |
| 21 | return atoi(from_string)*multiplier; |
| 22 | } |
| 23 | |
| 24 | int get_int_from_float(double from_float, int multiplier) |
| 25 | { |
| 26 | return (int) from_float*multiplier; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | std::string SomeObject::staticData = std::string("Hello Static World!"); |
| 31 | |
| 32 | SomeObject::~SomeObject () |
| 33 | { |
| 34 | SomeObject::instance_count--; |
| 35 | delete m_foo_ptr; |
| 36 | if (m_zbr) |
| 37 | m_zbr->Unref (); |
| 38 | if (m_internal_zbr) { |
| 39 | m_internal_zbr->Unref (); |
| 40 | m_internal_zbr = NULL; |
| 41 | } |
| 42 | delete m_foobar; |
| 43 | m_foobar = NULL; |
| 44 | } |
| 45 | |
| 46 | SomeObject::SomeObject (const SomeObject &other) |
| 47 | : m_prefix (other.m_prefix), |
| 48 | m_foobar (NULL) |
| 49 | { |
| 50 | if (other.m_foo_ptr) |
| 51 | m_foo_ptr = new Foo (*other.m_foo_ptr); |
| 52 | else |
| 53 | m_foo_ptr = NULL; |
| 54 | m_foo_shared_ptr = NULL; |
| 55 | |
| 56 | if (other.m_zbr) { |
| 57 | m_zbr = other.m_zbr; |
| 58 | m_zbr->Ref(); |
| 59 | } else |
| 60 | m_zbr = NULL; |
| 61 | |
| 62 | m_internal_zbr = new Zbr; |
| 63 | m_pyobject = NULL; |
| 64 | m_foobar = NULL; |
| 65 | SomeObject::instance_count++; |
| 66 | } |
| 67 | |
| 68 | SomeObject::SomeObject (std::string const prefix) |
| 69 | : m_prefix (prefix), m_foo_ptr (0), |
| 70 | m_foo_shared_ptr (0), m_zbr (0), |
| 71 | m_internal_zbr (new Zbr), |
| 72 | m_pyobject (NULL), |
| 73 | m_foobar (NULL) |
| 74 | { |
| 75 | SomeObject::instance_count++; |
| 76 | } |
| 77 | |
| 78 | SomeObject::SomeObject (int prefix_len) |
| 79 | : m_prefix (prefix_len, 'X'), m_foo_ptr (0), |
| 80 | m_foo_shared_ptr (0), m_zbr (0), |
| 81 | m_internal_zbr (new Zbr), |
| 82 | m_pyobject (NULL), |
| 83 | m_foobar (NULL) |
| 84 | { |
| 85 | SomeObject::instance_count++; |
| 86 | } |
| 87 | |
| 88 | int SomeObject::get_int (const char *from_string) |
| 89 | { |
| 90 | return atoi(from_string); |
| 91 | } |
| 92 | |
| 93 | int SomeObject::get_int (double from_float) |
| 94 | { |
| 95 | return (int) from_float; |
| 96 | } |
| 97 | |
| 98 | std::string |
| 99 | SomeObject::protected_method_that_is_not_virtual (std::string arg) const |
| 100 | { |
| 101 | return m_prefix + arg; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | int SomeObject::instance_count = 0; |
| 106 | |
| 107 | class HiddenClass : public Bar |
| 108 | { |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | Foo* |
| 113 | get_hidden_subclass_pointer () |
| 114 | { |
| 115 | return new HiddenClass; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | static SomeObject *g_someObject = 0; |
| 120 | |
| 121 | // Transfer ownership of 'obj' to the library |
| 122 | void store_some_object(SomeObject *obj) |
| 123 | { |
| 124 | delete g_someObject; |
| 125 | g_someObject = obj; |
| 126 | } |
| 127 | |
| 128 | // Invokes the virtual method in the stored SomeObject |
| 129 | std::string invoke_some_object_get_prefix() |
| 130 | { |
| 131 | if (g_someObject) |
| 132 | return g_someObject->get_prefix(); |
| 133 | else |
| 134 | return std::string(); |
| 135 | } |
| 136 | |
| 137 | // Transfer ownership of 'obj' away from the library |
| 138 | SomeObject* take_some_object() |
| 139 | { |
| 140 | SomeObject *retval = g_someObject; |
| 141 | g_someObject = 0; |
| 142 | return retval; |
| 143 | } |
| 144 | |
| 145 | // Deletes the contained object, if any |
| 146 | void delete_some_object() |
| 147 | { |
| 148 | delete g_someObject; |
| 149 | g_someObject = 0; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | namespace xpto |
| 154 | { |
| 155 | std::string some_function() |
| 156 | { |
| 157 | return "hello"; |
| 158 | } |
| 159 | |
| 160 | std::string get_foo_datum(FooXpto const &foo) |
| 161 | { |
| 162 | return foo.get_datum(); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | |
| 168 | Foo g_foo; |
| 169 | |
| 170 | void function_that_takes_foo(Foo foo) |
| 171 | { |
| 172 | g_foo = foo; |
| 173 | } |
| 174 | |
| 175 | Foo function_that_returns_foo() |
| 176 | { |
| 177 | return g_foo; |
| 178 | } |
| 179 | |
| 180 | int Foo::instance_count = 0; |
| 181 | int SomeObject::NestedClass::instance_count = 0; |
| 182 | int Foobar::instance_count = 0; |
| 183 | int Box::instance_count = 0; |
| 184 | |
| 185 | Foobar* get_foobar_with_other_as_custodian (SomeObject *other) |
| 186 | { |
| 187 | return other->get_foobar_with_self_as_custodian(); |
| 188 | } |
| 189 | |
| 190 | Foobar* create_new_foobar() |
| 191 | { |
| 192 | return new Foobar; |
| 193 | } |
| 194 | |
| 195 | void set_foobar_with_other_as_custodian(Foobar *foobar, SomeObject *other) |
| 196 | { |
| 197 | other->set_foobar_with_self_as_custodian(foobar); |
| 198 | } |
| 199 | |
| 200 | SomeObject * set_foobar_with_return_as_custodian(Foobar *foobar) |
| 201 | { |
| 202 | SomeObject *some = new SomeObject("xxx"); |
| 203 | some->set_foobar_with_self_as_custodian(foobar); |
| 204 | return some; |
| 205 | } |
| 206 | |
| 207 | std::string some_object_get_something_prefixed(const SomeObject *obj, const std::string something) |
| 208 | { |
| 209 | return obj->get_prefix() + something; |
| 210 | } |
| 211 | |
| 212 | std::string some_object_val_get_something_prefixed(SomeObject obj, const std::string something) |
| 213 | { |
| 214 | return obj.get_prefix() + something; |
| 215 | } |
| 216 | |
| 217 | std::string some_object_ref_get_something_prefixed(const SomeObject &obj, const std::string something) |
| 218 | { |
| 219 | return obj.get_prefix() + something; |
| 220 | } |
| 221 | |
| 222 | namespace xpto |
| 223 | { |
| 224 | FooType g_fooType; |
| 225 | FooType get_foo_type () |
| 226 | { |
| 227 | return g_fooType; |
| 228 | } |
| 229 | void set_foo_type (FooType type) |
| 230 | { |
| 231 | g_fooType = type; |
| 232 | } |
| 233 | void set_foo_type_inout (FooType &type) |
| 234 | { |
| 235 | FooType oldfooType = g_fooType; |
| 236 | g_fooType = type; |
| 237 | type = oldfooType; |
| 238 | } |
| 239 | |
| 240 | void set_foo_type_ptr (FooType *type) |
| 241 | { |
| 242 | FooType oldfooType = g_fooType; |
| 243 | g_fooType = *type; |
| 244 | *type = oldfooType; |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | |
| 249 | |
| 250 | SingletonClass *SingletonClass::m_instance = NULL; |
| 251 | |
| 252 | InterfaceId make_interface_id () |
| 253 | { |
| 254 | return InterfaceId (); |
| 255 | } |
| 256 | |
| 257 | template <> std::string TypeNameGet<int> (void) |
| 258 | { |
| 259 | return "int"; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | static Zbr *g_zbr = NULL; |
| 264 | int Zbr::instance_count = 0; |
| 265 | |
| 266 | void store_zbr (Zbr *zbr) |
| 267 | { |
| 268 | if (g_zbr) |
| 269 | g_zbr->Unref (); |
| 270 | g_zbr = zbr; |
| 271 | } |
| 272 | |
| 273 | int invoke_zbr (int x) |
| 274 | { |
| 275 | return g_zbr->get_int (x); |
| 276 | } |
| 277 | |
| 278 | void delete_stored_zbr (void) |
| 279 | { |
| 280 | if (g_zbr) |
| 281 | g_zbr->Unref (); |
| 282 | g_zbr = NULL; |
| 283 | } |
| 284 | |
| 285 | float matrix_sum_of_elements (float *matrix) |
| 286 | { |
| 287 | float result = 0; |
| 288 | for (int i = 0; i < 6; i++) |
| 289 | result += matrix[i]; |
| 290 | return result; |
| 291 | } |
| 292 | |
| 293 | void matrix_identity_new (float *matrix) |
| 294 | { |
| 295 | matrix[0] = 1; |
| 296 | matrix[1] = 0; |
| 297 | matrix[2] = 0; |
| 298 | matrix[3] = 0; |
| 299 | matrix[4] = 1; |
| 300 | matrix[5] = 0; |
| 301 | } |
| 302 | |
| 303 | static SimpleStructList g_simpleList; |
| 304 | |
| 305 | SimpleStructList get_simple_list () |
| 306 | { |
| 307 | SimpleStructList retval; |
| 308 | for (int i = 0; i < 10; i++) |
| 309 | { |
| 310 | simple_struct_t val = {i}; |
| 311 | retval.push_back(val); |
| 312 | } |
| 313 | return retval; |
| 314 | } |
| 315 | |
| 316 | int set_simple_list (SimpleStructList list) |
| 317 | { |
| 318 | int count = 0; |
| 319 | g_simpleList = list; |
| 320 | for (SimpleStructList::iterator iter = g_simpleList.begin(); iter != g_simpleList.end(); iter++) |
| 321 | count += iter->xpto; |
| 322 | return count; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | SimpleStructList |
| 327 | TestContainer::get_simple_list () |
| 328 | { |
| 329 | SimpleStructList retval; |
| 330 | for (int i = 0; i < 10; i++) |
| 331 | { |
| 332 | simple_struct_t val = {i}; |
| 333 | retval.push_back(val); |
| 334 | } |
| 335 | return retval; |
| 336 | } |
| 337 | |
| 338 | int |
| 339 | TestContainer::set_simple_list (SimpleStructList list) |
| 340 | { |
| 341 | int count = 0; |
| 342 | m_simpleList = list; |
| 343 | for (SimpleStructList::iterator iter = m_simpleList.begin(); iter != m_simpleList.end(); iter++) |
| 344 | count += iter->xpto; |
| 345 | return count; |
| 346 | } |
| 347 | |
| 348 | int |
| 349 | TestContainer::set_simple_list_by_ref (SimpleStructList &inout_list) |
| 350 | { |
| 351 | int count = 0; |
| 352 | m_simpleList = inout_list; |
| 353 | for (SimpleStructList::iterator iter = inout_list.begin(); iter != inout_list.end(); iter++) |
| 354 | { |
| 355 | iter->xpto *= 2; |
| 356 | count += iter->xpto; |
| 357 | } |
| 358 | return count; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | std::vector<simple_struct_t> |
| 364 | TestContainer::get_simple_vec () |
| 365 | { |
| 366 | std::vector<simple_struct_t> retval; |
| 367 | for (int i = 0; i < 10; i++) |
| 368 | { |
| 369 | simple_struct_t val = {i}; |
| 370 | retval.push_back(val); |
| 371 | } |
| 372 | return retval; |
| 373 | } |
| 374 | |
| 375 | int |
| 376 | TestContainer::set_simple_vec (std::vector<simple_struct_t> list) |
| 377 | { |
| 378 | int count = 0; |
| 379 | m_simpleList = list; |
| 380 | for (std::vector<simple_struct_t>::iterator iter = m_simpleList.begin(); iter != m_simpleList.end(); iter++) |
| 381 | count += iter->xpto; |
| 382 | return count; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | SimpleStructMap |
| 387 | TestContainer::get_simple_map () |
| 388 | { |
| 389 | SimpleStructMap retval; |
| 390 | for (int i = 0; i < 10; i++) |
| 391 | { |
| 392 | simple_struct_t val = {i}; |
| 393 | std::ostringstream os; |
| 394 | os << i; |
| 395 | retval[os.str()] = val; |
| 396 | } |
| 397 | return retval; |
| 398 | } |
| 399 | |
| 400 | int |
| 401 | TestContainer::set_simple_map (SimpleStructMap map) |
| 402 | { |
| 403 | int count = 0; |
| 404 | m_simpleMap = map; |
| 405 | for (SimpleStructMap::iterator iter = m_simpleMap.begin(); iter != m_simpleMap.end(); iter++) |
| 406 | count += iter->second.xpto; |
| 407 | return count; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | void |
| 412 | TestContainer::get_vec (std::vector<std::string> &outVec) |
| 413 | { |
| 414 | outVec.clear (); |
| 415 | outVec.push_back ("hello"); |
| 416 | outVec.push_back ("world"); |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | TestContainer::set_vec_ptr (std::vector<std::string> *inVec) |
| 421 | { |
| 422 | m_vec = inVec; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | void |
| 427 | TestContainer::get_vec_ptr (std::vector<std::string> *outVec) |
| 428 | { |
| 429 | *outVec = *m_vec; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | std::map<std::string, int> |
| 434 | get_map () |
| 435 | { |
| 436 | std::map<std::string, int> rv; |
| 437 | rv["123"] = 123; |
| 438 | rv["456"] = 456; |
| 439 | return rv; |
| 440 | } |
| 441 | |
| 442 | std::set<uint32_t> |
| 443 | get_set () |
| 444 | { |
| 445 | std::set<uint32_t> rv; |
| 446 | return rv; |
| 447 | } |
| 448 | |
| 449 | namespace xpto |
| 450 | { |
| 451 | FlowId |
| 452 | get_flow_id (FlowId flowId) |
| 453 | { |
| 454 | return flowId + 1; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | double |
| 459 | my_inverse_func (double x) throw (DomainError) |
| 460 | { |
| 461 | if (x == 0) |
| 462 | { |
| 463 | DomainError ex; |
| 464 | ex.message = "value must be != 0"; |
| 465 | throw ex; |
| 466 | } |
| 467 | return 1/x; |
| 468 | } |
| 469 | |
| 470 | double |
| 471 | ClassThatThrows::my_inverse_method (double x) throw (DomainError) |
| 472 | { |
| 473 | if (x == 0) |
| 474 | { |
| 475 | DomainError ex; |
| 476 | ex.message = "value must be != 0"; |
| 477 | throw ex; |
| 478 | } |
| 479 | return 1/x; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | ClassThatThrows::ClassThatThrows (double x) throw (DomainError) |
| 484 | { |
| 485 | if (x == 0) |
| 486 | { |
| 487 | DomainError ex; |
| 488 | ex.message = "value must be != 0"; |
| 489 | throw ex; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | int |
| 494 | ClassThatThrows::throw_out_of_range() throw (std::out_of_range) |
| 495 | { |
| 496 | throw std::out_of_range("imaginary index out of range"); |
| 497 | } |
| 498 | |
| 499 | int |
| 500 | ClassThatThrows::throw_error () const throw (std::exception) |
| 501 | { |
| 502 | throw std::runtime_error ("an error"); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | double |
| 507 | my_inverse_func2 (double x) throw (std::exception) |
| 508 | { |
| 509 | if (x == 0) |
| 510 | { |
| 511 | throw std::runtime_error ("value must be != 0"); |
| 512 | } |
| 513 | return 1/x; |
| 514 | } |
| 515 | |
| 516 | double |
| 517 | my_inverse_func3 (double x) |
| 518 | { |
| 519 | if (x == 0) |
| 520 | { |
| 521 | throw std::runtime_error ("value must be != 0"); |
| 522 | } |
| 523 | return 1/x; |
| 524 | } |
| 525 | |
| 526 | double |
| 527 | ClassThatThrows::my_inverse_method2 (double x) throw (std::exception) |
| 528 | { |
| 529 | if (x == 0) |
| 530 | { |
| 531 | throw std::runtime_error ("value must be != 0"); |
| 532 | } |
| 533 | return 1/x; |
| 534 | } |
| 535 | |
| 536 | double |
| 537 | ClassThatThrows::my_inverse_method3 (double x) |
| 538 | { |
| 539 | if (x == 0) |
| 540 | { |
| 541 | throw std::runtime_error ("value must be != 0"); |
| 542 | } |
| 543 | return 1/x; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | Tupl |
| 548 | my_throwing_func () throw (std::exception) |
| 549 | { |
| 550 | Tupl retval; |
| 551 | return retval; |
| 552 | } |
| 553 | |
| 554 | int |
| 555 | test_args_kwargs(const char *args, const char *kwargs) |
| 556 | { |
| 557 | return (int) (kwargs - args); |
| 558 | } |
| 559 |