1Chromium Embedded Framework (CEF) Translator Tool -- translator.py 2------------------------------------------------------------------------------- 3 4Document Last Updated: February 14, 2012 5 6 7OVERVIEW 8-------- 9 10The CEF translator tool automatically generates CEF source code based on the 11contents of the CEF header file (cef.h). The generated source code includes the 12main C API header file (cef_capi.h) and all files in the libcef_dll/cpptoc and 13libcef_dll/ctocpp directories. 14 15If any differences are detected between the new translator-generated output and 16the file that currently exists on disk a backup of the existing file will be 17created before the new file is written (this behavior can be controlled using 18a command-line switch -- see 'translator.py -h' for more information). Header 19files (*.h) are completely generated by the translator and should never be 20edited by hand. Implementation files (*.cc) may contain user-created content 21within method and function body blocks. The user-created content is extracted 22from the existing file and inserted into the new translator-generated file. Any 23differences between existing method/function prototypes and new method/function 24prototypes in manually edited implementations will be noted as a warning in new 25output file. 26 27 // WARNING - CHANGED ATTRIBUTES 28 // REMOVED: const wchar_t* key 29 // ADDED: int index 30 // WARNING - CHANGED RETURN VALUE 31 // WAS: void 32 // NOW: int 33 #pragma message("Warning: "__FILE__": MyFunction prototype has changed") 34 35Auto-generated implementations will be added in the new output file for any 36methods/functions that exist in the CEF header file but did not exist in the 37current on-disk implementation file. Each time the translator re-generates the 38implementation file it will warn if an implementation could not be auto- 39generated. Delete the indicated portion of the generated code after adding the 40implementation manually. 41 42 size_t CEF_CALLBACK frame_new_func(struct _cef_frame_t* self) 43 { 44 // BEGIN DELETE BEFORE MODIFYING 45 // AUTO-GENERATED CONTENT 46 #pragma message("Warning: "__FILE__": frame_new_func is not implemented") 47 // END DELETE BEFORE MODIFYING 48 } 49 50If the complete function or method implementation has been auto-generated the 51body of the function or method will contain the following comment. 52 53 // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING 54 55If you edit the implementation manually you should remove this comment so that 56CEF will not discard your changes on the next run of the translator tool. 57 58The 'translator.[bat|sh]' file can be used to run the translator tool with 59command- line arguments that match the default CEF directory structure and 60output options. Run 'translator.py -h' for a complete list of available command- 61line arguments. 62 63 64HEADER ATTRIBUTES 65----------------- 66 67Comment-based attribute tags are added before each function, class and method 68definition in the CEF header file to provide the translator with additional 69information about how the output should be generated. The attribute tags must 70be in the form of a comma-delimited list of name=value pairs. Attribute names 71and values must contain only alpha-numeric characters, numbers and underscores, 72and must all exist on a single line. 73 74 /*--cef(name1=value1,name2=value2,name3=value3)--*/ 75 76Supported method/function attributes: 77 78 capi_name=[string] (Optional) Force a specific output name for the 79 resulting C API function. 80 optional_param=[param] (Optional) Parameter name that will be optional 81 instead of required. 82 index_param=[param] (Optional) Parameter name representing an index 83 value that will be verified as >= 0. 84 default_retval=[string] (Required for enumeration types, Optional for other 85 types) Specify the default return value. 86 count_func=[param:func] (Required for non-const non-string std::vector 87 types) Specify the C++ function that returns the 88 count of elements for a vector parameter. 89 api_hash_check (Optional) If set an API hash check will be added 90 to the CToCpp version of the method/function. 91 92Supported class attributes: 93 94 source=[library|client] (Required) Indicates whether the class 95 implementation is provided by the library or the 96 client. This effects the generation of guard 97 blocks in the cpptoc and ctocpp header files. 98 no_debugct_check (Optional) If set the debug reference count 99 of the object will not be checked on shutdown. 100 101 102TRANSLATION RULES 103----------------- 104 105All C++ names in the CEF header file are written in CamelCaps format and all 106C API translations are generated in lowercase_underscore format. 107 108 109Translating Classes and Methods 110------------------------------- 111 112Class names and global function names must be prefixed with the 'Cef' string. 113 114 Global function translation 115 C++: void CefShutdown() 116 C API: void cef_shutdown() 117 118The translation of a C++ class name to a C API structure name is prefixed with 119'_' and postfixed with '_t'. A typedef of the C API structure to a value 120without the prefixed '_' is also provided and may be used interchangeably. 121 122 Class name translation 123 C++: class CefPostData 124 C API: typedef struct _cef_post_data_t { ... } cef_post_data_t 125 126The translation of a C++ virtual class method to a C API member function adds a 127'self' structure pointer as the first parameter. This will always be a pointer 128to the structure that contains the member function. 129 130 Virtual method translation 131 C++: virtual void SetFocus(bool enable) 132 C API: void set_focus(struct _cef_browser_t* self, int enable) 133 134The translation of a C++ static class method to a C API global function 135is prefixed with 'cef_classname_' where 'classname' is the 136lowercase_underscore name of the class that contains the static method. Any 137repeat of 'classname' in the function name is removed. 138 139 Static method translation 140 C++: static CefRefPtr<CefRequest> CreateRequest() 141 C API: struct _cef_request_t* cef_request_create() 142 143Implementation of the wrapper method/function body is generally formatted as 144follows. 145 146 Static/Global CppToC (without Return): 147 148 CEF_EXPORT void cef_function(capi_params) 149 { 150 // Parameter Verification (Optional) 151 // Verify the C parameter values. 152 // ... 153 154 // Parameter Translation (Optional) 155 // Convert C parameter values to C++ parameter values. 156 // ... 157 158 // Execution 159 CefFunction(cpp_arams); 160 161 // Parameter Restoration (Optional) 162 // Retore the C parameter values if changed. 163 // ... 164 } 165 166 Static/Global CppToC (with Return): 167 168 CEF_EXPORT capi_retval cef_function(capi_params) 169 { 170 // Parameter Verification (Optional) 171 // Verify the C parameter values. 172 // ... 173 174 // Parameter Translation (Optional) 175 // Convert C parameter values to C++ parameter values. 176 // ... 177 178 // Execution 179 cpp_retval _rv = CefFunction(cpp_params); 180 181 // Parameter Restoration (Optional) 182 // Restore the C parameter values if changed. 183 // ... 184 185 // Return Translation 186 // Convert the C++ return value to a C return value. 187 return ...; 188 } 189 190 Static/Global CToCpp (without Return): 191 192 void CefFunction(cpp_params) 193 { 194 // Parameter Verification (Optional) 195 // Verify the C++ parameter values. 196 // ... 197 198 // Parameter Translation (Optional) 199 // Convert C++ parameter values to C parameter values. 200 // ... 201 202 // Execution 203 cef_function(capi_params); 204 205 // Parameter Restoration (Optional) 206 // Restore the C++ parameter values if changed. 207 // ... 208 } 209 210 Static/Global CToCpp (with Return): 211 212 cpp_retval CefFunction(cpp_params) 213 { 214 // Parameter Verification (Optional) 215 // Verify the C++ parameter values. 216 // ... 217 218 // Parameter Translation (Optional) 219 // Convert C++ parameter values to C parameter values. 220 // ... 221 222 // Execution 223 capi_retval _rv = cef_function(capi_params); 224 225 // Parameter Restoration (Optional) 226 // Restore the C++ parameter values if changed. 227 // ... 228 229 // Return Translation 230 // Convert the C return value to a C++ return value. 231 return ...; 232 } 233 234 Member CppToC (without Return): 235 236 CEF_CALLBACK void class_function(cef_class_t* self, capi_params) 237 { 238 // Parameter Verification. 239 // Verify the C parameter values. 240 DCHECK(self); 241 DCHECK(...); 242 if (!self || ...) 243 return; 244 245 // Parameter Translation (Optional) 246 // Convert the C parameter values to C++ parameter values. 247 // ... 248 249 // Execution 250 CefClassCppToC::Get(self)->CefFunction(cpp_params); 251 252 // Parameter Restoration (Optional) 253 // Restore the C parameter values if changed. 254 // ... 255 } 256 257 Member CppToC (with Return): 258 259 CEF_CALLBACK capi_retval class_function(cef_class_t* self, capi_params) 260 { 261 // Parameter Verification. 262 // Verify the C parameter values. 263 DCHECK(self); 264 DCHECK(...); 265 if (!self || ...) 266 return default_retval; // Configured or defaulted automatically. 267 268 // Parameter Translation (Optional) 269 // Convert the C parameter values to C++ parameter values. 270 // ... 271 272 // Execution 273 cpp_retval _rv = CefClassCppToC::Get(self)->CefFunction(cpp_params); 274 275 // Parameter Restoration (Optional) 276 // Restore the C parameter values if changed. 277 // ... 278 279 // Return Translation 280 // Convert the C++ return value to a C return value. 281 return ...; 282 } 283 284 Member CToCpp (without Return): 285 286 void CefClassCToCpp::Function(cpp_params) 287 { 288 // Structure Verification 289 if (CEF_MEMBER_MISSING(struct_, function)) 290 return; 291 292 // Parameter Verification (Optional) 293 // Verify the C++ parameter values. 294 // ... 295 296 // Parameter Translation (Optional) 297 // Convert C++ parameter values to C parameter values. 298 // ... 299 300 // Execution 301 struct_->class_function(struct_, capi_params); 302 303 // Parameter Restoration (Optional) 304 // Restore the C++ parameter values if changed. 305 // ... 306 } 307 308 Member CToCpp (with Return): 309 310 cpp_retval CefClassCToCpp::Function(cpp_params) 311 { 312 // Structure Verification 313 if (CEF_MEMBER_MISSING(struct_, function)) 314 return default_retval; // Configured or defaulted automatically. 315 316 // Parameter Verification (Optional) 317 // Verify the C++ parameter values. 318 // ... 319 320 // Parameter Translation (Optional) 321 // Convert C++ parameter values to C parameter values. 322 // ... 323 324 // Execution 325 capi_retval _rv = struct_->class_function(struct_, capi_params); 326 327 // Parameter Restoration (Optional) 328 // Restore the C++ parameter values if changed. 329 // ... 330 331 // Return Translation 332 // Convert the C return value to a C++ return value. 333 return ...; 334 } 335 336 337Translating Data Types 338---------------------- 339 340Data types that are available in both C++ and C are left unchanged. This 341includes the 'double', 'int', 'long', 'size_t' and 'void' basic types. Other 342data types have differing levels of support as indicated below. The translation 343tool will terminate with an exception if it encounters a data type that it 344cannot translate. 345 346Parameters: 347 348 Simple/enumeration type by value (simple_byval): 349 C++: int value 350 C API: int value 351 352 // CppToC Example 353 CEF_EXPORT void cef_function(int value) 354 { 355 // Execution 356 CefFunction(value); 357 } 358 359 // CToCpp Example 360 void CefFunction(int value) 361 { 362 // Execution 363 cef_function(value); 364 } 365 366 Simple/enumeration type by reference (simple_byref): 367 C++: int& value 368 C API: int* value 369 370 // CppToC Example 371 CEF_EXPORT void cef_function(int* value) 372 { 373 // Parameter Verification 374 DHECK(value); 375 if (!value) 376 return; 377 378 // Parameter Translation 379 int valueVal = value?*value:0; 380 381 // Execution 382 CefFunction(valueVal); 383 384 // Parameter Restoration 385 if (value) 386 *value = valueVal; 387 } 388 389 // CToCpp Example 390 void CefFunction(int& value) 391 { 392 // Execution 393 cef_function(&value); 394 } 395 396 Simple/enumeration const type by reference (simple_byref_const): 397 C++: const int& value 398 C API: const int* value 399 400 // CppToC Example 401 CEF_EXPORT void cef_function(const int* value) 402 { 403 // Parameter Verification 404 DHECK(value); 405 if (!value) 406 return; 407 408 // Parameter Translation 409 int valueVal = value?*value:0; 410 411 // Execution 412 CefFunction(valueVal); 413 } 414 415 // CToCpp Example 416 void CefFunction(const int& value) 417 { 418 // Execution 419 cef_function(&value); 420 } 421 422 Simple/enumeration type by address (simple_byaddr): 423 C++: int* value 424 C API: int* value 425 426 // CppToC Example 427 CEF_EXPORT void cef_function(int* value) 428 { 429 // Parameter Verification 430 DHECK(value); 431 if (!value) 432 return; 433 434 // Execution 435 CefFunction(value); 436 } 437 438 // CToCpp Example 439 void CefFunction(int* value) 440 { 441 // Parameter Verification 442 DHECK(value); 443 if (!value) 444 return; 445 446 // Execution 447 cef_function(value); 448 } 449 450 Boolean type by value (bool_byval): 451 C++: bool value 452 C API: int value 453 454 // CppToC Example 455 CEF_EXPORT void cef_function(int value) 456 { 457 // Execution 458 CefFunction(value?true:false); 459 } 460 461 // CToCpp Example 462 void CefFunction(bool value) 463 { 464 // Execution 465 cef_function(value); 466 } 467 468 Boolean type by reference (bool_byref): 469 C++: bool& value 470 C API: int* value 471 472 // CppToC Example 473 CEF_EXPORT void cef_function(int* value) 474 { 475 // Parameter Verification 476 DHECK(value); 477 if (!value) 478 return; 479 480 // Parameter Translation 481 bool valueBool = (value && *value)?true:false; 482 483 // Execution 484 CefFunction(valueBool); 485 486 // Parameter Restoration 487 if (value) 488 *value = valueBool?true:false; 489 } 490 491 // CToCpp Example 492 void CefFunction(bool& value) 493 { 494 // Parameter Translation 495 int valueInt = value; 496 497 // Execution 498 cef_function(&valueInt); 499 500 // Parameter Restoration 501 value = valueInt?true:false; 502 } 503 504 Boolean type by address (bool_byaddr): 505 C++: bool* value 506 C API: int* value 507 508 // CppToC Example 509 CEF_EXPORT void cef_function(int* value) 510 { 511 // Parameter Verification 512 DHECK(value); 513 if (!value) 514 return; 515 516 // Parameter Translation 517 bool valueBool = (value && *value)?true:false; 518 519 // Execution 520 CefFunction(&valueBool); 521 522 // Parameter Restoration 523 if (value) 524 *value = valueBool?true:false; 525 } 526 527 // CToCpp Example 528 void CefFunction(bool* value) 529 { 530 // Parameter Verification 531 DHECK(value); 532 if (!value) 533 return; 534 535 // Parameter Translation 536 int valueInt = value?*value:0; 537 538 // Execution 539 cef_function(&valueInt); 540 541 // Parameter Restoration 542 if (value) 543 *value = valueInt?true:false; 544 } 545 546 Structure const type by reference (struct_byref_const): 547 C++: const CefPopupFeatures& value 548 C API: const cef_popup_features_t* value 549 550 // CppToC Example 551 CEF_EXPORT void cef_function(const cef_popup_features_t* value) 552 { 553 // Parameter Verification 554 DHECK(value); 555 if (!value) 556 return; 557 558 // Parameter Translation 559 CefPopupFeatures valueObj; 560 // Reference the existing values instead of copying. 561 if (value) 562 valueObj.Set(*value, false); 563 564 // Execution 565 CefFunction(valueObj); 566 } 567 568 // CToCpp Example 569 void CefFunction(const CefPopupFeatures& value) 570 { 571 // Execution 572 cef_function(&value); 573 } 574 575 Structure non-const type by reference (struct_byref): 576 C++: CefWindowInfo& value 577 C API: cef_window_info_t* value 578 579 // CppToC Example 580 CEF_EXPORT void cef_function(cef_window_info_t* value) 581 { 582 // Parameter Verification 583 DHECK(value); 584 if (!value) 585 return; 586 587 // Parameter Translation 588 CefWindowInfo valueObj; 589 // Take ownership of the values. 590 if (value) 591 valueObj.AttachTo(*value); 592 593 // Execution 594 CefFunction(valueObj); 595 596 // Parameter Restoration 597 // Return the values to the structure. 598 if (value) 599 valueObj.DetachTo(*value); 600 } 601 602 // CToCpp Example 603 void CefFunction(CefWindowInfo& value) 604 { 605 // Execution 606 cef_function(&value); 607 } 608 609 String const type by reference (string_byref_const): 610 C++: const CefString& value 611 C API: const cef_string_t* value 612 613 // CppToC Example 614 CEF_EXPORT void cef_function(const cef_string_t* value) 615 { 616 // Parameter Verification 617 DHECK(value); 618 if (!value) 619 return; 620 621 // Execution 622 CefFunction(CefString(value)); 623 } 624 625 // CToCpp Example 626 void CefFunction(const CefString& value) 627 { 628 // Execution 629 cef_function(value.GetStruct()); 630 } 631 632 String non-const type by reference (string_byref): 633 C++: CefString& value 634 C API: cef_string_t* value 635 636 // CppToC Example 637 CEF_EXPORT void cef_function(cef_string_t* value) 638 { 639 // Parameter Verification 640 DHECK(value); 641 if (!value) 642 return; 643 644 // Parameter Translation 645 CefString valueStr(value); 646 647 // Execution 648 CefFunction(valueStr); 649 } 650 651 // CToCpp Example 652 void CefFunction(CefString& value) 653 { 654 // Execution 655 cef_function(value.GetWritableStruct()); 656 } 657 658 Smart pointer type same boundary side (refptr_same): 659 C++: CefRefPtr<CefBrowser> value 660 C API: cef_browser_t* value 661 662 // CppToC Example 663 CEF_EXPORT void cef_function(cef_browser_t* value) 664 { 665 // Parameter Verification 666 DHECK(value); 667 if (!value) 668 return; 669 670 // Execution 671 CefFunction(CefBrowserCppToC::Unwrap(value)); 672 } 673 674 // CToCpp Example 675 void CefFunction(CefRefPtr<CefBrowser> value) 676 { 677 // Execution 678 cef_function(CefBrowserCToCpp::Unwrap(value)); 679 } 680 681 Smart pointer type same boundary side by reference (refptr_same_byref): 682 C++: CefRefPtr<CefClient>& value 683 C API: cef_client_t** value 684 685 // CppToC Example 686 CEF_EXPORT void cef_function(cef_client_t** value) 687 { 688 // Parameter Verification 689 DHECK(value); 690 if (!value) 691 return; 692 693 // Parameter Translation 694 CefRefPtr<CefClient> valuePtr; 695 if (value && *value) 696 valuePtr = CefClientCppToC::Unwrap(*value); 697 CefClient* valueOrig = valuePtr.get(); 698 699 // Execution 700 CefFunction(valuePtr); 701 702 // Parameter Restoration 703 if (value) { 704 if (valuePtr.get()) { 705 if (valuePtr.get() != valueOrig) { 706 // The value has been changed. 707 *value = CefClientCppToC::Wrap(valuePtr); 708 } 709 } else { 710 *value = NULL; 711 } 712 } 713 } 714 715 // CToCpp Example 716 void CefFunction(CefRefPtr<CefClient>& value) 717 { 718 // Parameter Translation 719 cef_client_t* valueStruct = NULL; 720 if(value.get()) 721 valueStruct = CefClientCToCpp::Unwrap(value); 722 cef_client_t* valueOrig = valueStruct; 723 724 // Execution 725 cef_function(valueStuct); 726 727 // Parameter Restoration 728 if (valueStruct) { 729 if (valueStruct != valueOrig) { 730 // The value was changed. 731 value = CefClientCToCpp::Wrap(valueStruct); 732 } 733 } else { 734 value = NULL; 735 } 736 } 737 738 Smart pointer type different boundary side (refptr_diff): 739 C++: CefRefPtr<CefBrowser> value 740 C API: cef_browser_t* value 741 742 // CppToC Example 743 CEF_EXPORT void cef_function(cef_browser_t* value) 744 { 745 // Parameter Verification 746 DHECK(value); 747 if (!value) 748 return; 749 750 // Execution 751 CefFunction(CefBrowserCToCpp::Wrap(value)); 752 } 753 754 // CToCpp Example 755 void CefFunction(CefRefPtr<CefBrowser> value) 756 { 757 // Execution 758 cef_function(CefBrowserCppToC::Wrap(value)); 759 } 760 761 Smart pointer type different boundary side by reference (refptr_diff_byref): 762 C++: CefRefPtr<CefClient>& value 763 C API: cef_client_t** value 764 765 // CppToC Example 766 CEF_EXPORT void cef_function(cef_client_t** value) 767 { 768 // Parameter Verification 769 DHECK(value); 770 if (!value) 771 return; 772 773 // Parameter Translation 774 CefRefPtr<CefClient> valuePtr; 775 if (value && *value) 776 valuePtr = CefClientCToCpp::Wrap(*value); 777 CefClient* valueOrig = valuePtr.get(); 778 779 // Execution 780 CefFunction(valuePtr); 781 782 // Parameter Restoration 783 if (value) { 784 if (valuePtr.get()) { 785 if (valuePtr.get() != valueOrig) { 786 // The value has been changed. 787 *value = CefClientCToCpp::Unwrap(valuePtr); 788 } 789 } else { 790 *value = NULL; 791 } 792 } 793 } 794 795 // CToCpp Example 796 void CefFunction(CefRefPtr<CefClient>& value) 797 { 798 // Parameter Translation 799 cef_client_t* valueStruct = NULL; 800 if(value.get()) 801 valueStruct = CefClientCppToC::Wrap(value); 802 cef_client_t* valueOrig = valueStruct; 803 804 // Execution 805 cef_function(valueStuct); 806 807 // Parameter Restoration 808 if (valueStruct) { 809 if (valueStruct != valueOrig) { 810 // The value was changed. 811 value = CefClientCppToC::Unwrap(valueStruct); 812 } 813 } else { 814 value = NULL; 815 } 816 } 817 818 String vector type by reference (string_vec_byref): 819 C++: std::vector<CefString>& value 820 C API: cef_string_list_t value 821 822 // CppToC Example 823 CEF_EXPORT void cef_function(cef_string_list_t value) 824 { 825 // Parameter Verification 826 DHECK(value); 827 if (!value) 828 return; 829 830 // Parameter Translation 831 std::vector<CefString> valueList; 832 transfer_string_list_contents(value, valueList); 833 834 // Execution 835 CefFunction(valueList); 836 837 // Parameter Restoration 838 cef_string_list_clear(value); 839 transfer_string_list_contents(valueList, value); 840 } 841 842 // CToCpp Example 843 void CefFunction(std::vector<CefString>& value) 844 { 845 // Parameter Translation 846 cef_string_list_t valueList = cef_string_list_alloc(); 847 DCHECK(valueList); 848 if (valueList) 849 transfer_string_list_contents(value, valueList); 850 851 // Execution 852 cef_function(valueList); 853 854 // Parameter Restoration 855 if (valueList) { 856 value.clear(); 857 transfer_string_list_contents(valueList, value); 858 cef_string_list_free(valueList); 859 } 860 } 861 862 String vector const type by reference (string_vec_byref_const): 863 C++: const std::vector<CefString>& value 864 C API: cef_string_list_t value 865 866 // CppToC Example 867 CEF_EXPORT void cef_function(cef_string_list_t value) 868 { 869 // Parameter Verification 870 DHECK(value); 871 if (!value) 872 return; 873 874 // Parameter Translation 875 std::vector<CefString> valueList; 876 transfer_string_list_contents(value, valueList); 877 878 // Execution 879 CefFunction(valueList); 880 } 881 882 // CToCpp Example 883 void CefFunction(const std::vector<CefString>& value) 884 { 885 // Parameter Translation 886 cef_string_list_t valueList = cef_string_list_alloc(); 887 DCHECK(valueList); 888 if (valueList) 889 transfer_string_list_contents(value, valueList); 890 891 // Execution 892 cef_function(valueList); 893 894 // Parameter Restoration 895 if (valueList) 896 cef_string_list_free(valueList); 897 } 898 899 String-to-string single map type by reference (string_map_single_byref): 900 C++: std::map<CefString,CefString>& value 901 C API: cef_string_map_t value 902 903 // CppToC Example 904 CEF_EXPORT void cef_function(cef_string_map_t value) 905 { 906 // Parameter Verification 907 DHECK(value); 908 if (!value) 909 return; 910 911 // Parameter Translation 912 std::map<CefString,CefString> valueMap; 913 transfer_string_map_contents(value, valueMap); 914 915 // Execution 916 CefFunction(valueMap); 917 918 // Parameter Restoration 919 cef_string_map_clear(value); 920 transfer_string_map_contents(valueMap, value); 921 } 922 923 // CToCpp Example 924 void CefFunction(std::map<CefString,CefString>& value) 925 { 926 // Parameter Translation 927 cef_string_map_t valueMap = cef_string_map_alloc(); 928 DCHECK(valueMap); 929 if (valueMap) 930 transfer_string_map_contents(value, valueMap); 931 932 // Execution 933 cef_function(valueMap); 934 935 // Parameter Restoration 936 if (valueMap) { 937 value.clear(); 938 transfer_string_map_contents(valueMap, value); 939 cef_string_map_free(valueMap); 940 } 941 } 942 943 String-to-string single map const type by reference 944 (string_map_single_byref_const): 945 C++: const std::map<CefString,CefString>& value 946 C API: cef_string_map_t value 947 948 // CppToC Example 949 CEF_EXPORT void cef_function(cef_string_map_t value) 950 { 951 // Parameter Verification 952 DHECK(value); 953 if (!value) 954 return; 955 956 // Parameter Translation 957 std::map<CefString,CefString> valueMap; 958 transfer_string_map_contents(value, valueMap); 959 960 // Execution 961 CefFunction(valueMap); 962 } 963 964 // CToCpp Example 965 void CefFunction(const std::map<CefString,CefString>& value) 966 { 967 // Parameter Translation 968 cef_string_map_t valueMap = cef_string_map_alloc(); 969 DCHECK(valueMap); 970 if (valueMap) 971 transfer_string_map_contents(value, valueMap); 972 973 // Execution 974 cef_function(valueMap); 975 976 // Parameter Restoration 977 if (valueMap) 978 cef_string_map_free(valueMap); 979 } 980 981 String-to-string multi map type by reference (string_map_multi_byref): 982 C++: std::multimap<CefString,CefString>& value 983 C API: cef_string_multimap_t value 984 985 // CppToC Example 986 CEF_EXPORT void cef_function(cef_string_multimap_t value) 987 { 988 // Parameter Verification 989 DHECK(value); 990 if (!value) 991 return; 992 993 // Parameter Translation 994 std::multimap<CefString,CefString> valueMultimap; 995 transfer_string_multimap_contents(value, valueMultimap); 996 997 // Execution 998 CefFunction(valueMultimap); 999 1000 // Parameter Restoration 1001 cef_string_multimap_clear(value); 1002 transfer_string_multimap_contents(valueMultimap, value); 1003 } 1004 1005 // CToCpp Example 1006 void CefFunction(std::multimap<CefString,CefString>& value) 1007 { 1008 // Parameter Translation 1009 cef_string_multimap_t valueMultimap = cef_string_multimap_alloc(); 1010 DCHECK(valueMultimap); 1011 if (valueMultimap) 1012 transfer_string_multimap_contents(value, valueMultimap); 1013 1014 // Execution 1015 cef_function(valueMultimap); 1016 1017 // Parameter Restoration 1018 if (valueMultimap) { 1019 value.clear(); 1020 transfer_string_multimap_contents(valueMultimap, value); 1021 cef_string_multimap_free(valueMultimap); 1022 } 1023 } 1024 1025 String-to-string multi map const type by reference 1026 (string_map_multi_byref_const): 1027 C++: const std::multimap<CefString,CefString>& value 1028 C API: cef_string_multimap_t value 1029 1030 // CppToC Example 1031 CEF_EXPORT void cef_function(cef_string_multimap_t value) 1032 { 1033 // Parameter Verification 1034 DHECK(value); 1035 if (!value) 1036 return; 1037 1038 // Parameter Translation 1039 std::multimap<CefString,CefString> valueMultimap; 1040 transfer_string_multimap_contents(value, valueMultimap); 1041 1042 // Execution 1043 CefFunction(valueMultimap); 1044 } 1045 1046 // CToCpp Example 1047 void CefFunction(const std::multimap<CefString,CefString>& value) 1048 { 1049 // Parameter Translation 1050 cef_string_multimap_t valueMultimap = cef_string_multimap_alloc(); 1051 DCHECK(valueMultimap); 1052 if (valueMultimap) 1053 transfer_string_multimap_contents(value, valueMultimap); 1054 1055 // Execution 1056 cef_function(valueMultimap); 1057 1058 // Parameter Restoration 1059 if (valueMultimap) 1060 cef_string_multimap_free(valueMultimap); 1061 } 1062 1063 Simple/Enumeration vector non-const type by reference (simple_vec_byref): 1064 C++: std::vector<int>& value 1065 C API: size_t* valueCount, int* value 1066 1067 // CppToC Example 1068 CEF_EXPORT void cef_function(size_t* valueCount, int* value) 1069 { 1070 // Parameter Verification 1071 DCHECK(valueCount && (*valueCount == 0 || value)); 1072 if (!valueCount || (*valueCount > 0 && !value)) 1073 return; 1074 1075 // Parameter Translation 1076 std::vector<int> valueList; 1077 if (valueCount && *valueCount > 0 && value) { 1078 for (size_t i = 0; i < *valueCount; ++i) 1079 valueList.push_back(value[i]); 1080 } 1081 1082 // Execution 1083 CefFunction(valueList); 1084 1085 // Parameter Restoration 1086 if (valueCount && value) { 1087 *valueCount = std::min(valueList.size(), *valueCount); 1088 if (*valueCount > 0) { 1089 for (size_t i = 0; i < *valueCount; ++i) 1090 value[i] = valueList[i]; 1091 } 1092 } 1093 } 1094 1095 // CToCpp Example 1096 void CefFunction(std::vector<int>& value) 1097 { 1098 // Parameter Translation 1099 // Function identified by the "count_func" method attribute. 1100 size_t valueSize = value.size(); 1101 size_t valueCount = std::max(GetFunctionCount(), valueSize); 1102 int* valueList = NULL; 1103 if (valueCount > 0) { 1104 valueList = new int[valueCount]; 1105 DCHECK(valueList); 1106 if (valueList) 1107 memset(valueList, 0, sizeof(int)*valueCount); 1108 if (valueList && valueSize > 0) { 1109 for (size_t i = 0; i < valueSize; ++i) { 1110 valueList[i] = value[i]; 1111 } 1112 } 1113 } 1114 1115 // Execution 1116 cef_function(&valueCount, valueList); 1117 1118 // Parameter Restoration 1119 value.clear(); 1120 if (valueCount > 0 && valueList) { 1121 for (size_t i = 0; i < valueCount; ++i) 1122 value.push_back(valueList[i]); 1123 delete [] valueList; 1124 } 1125 } 1126 1127 Simple/Enumeration vector const type by reference (simple_vec_byref_const): 1128 C++: const std::vector<int>& value 1129 C API: size_t valueCount, int const* value 1130 1131 // CppToC Example 1132 CEF_EXPORT void cef_function(size_t valueCount, int const* value) 1133 { 1134 // Parameter Verification 1135 DCHECK(valueCount == 0 || value); 1136 if (valueCount > 0 && !value) 1137 return; 1138 1139 // Parameter Translation 1140 std::vector<int> valueList; 1141 if (valueCount > 0) { 1142 for (size_t i = 0; i < valueCount; ++i) 1143 valueList.push_back(value[i]); 1144 } 1145 1146 // Execution 1147 CefFunction(valueList); 1148 } 1149 1150 // CToCpp Example 1151 void CefFunction(const std::vector<int>& value) 1152 { 1153 // Parameter Translation 1154 const size_t valueCount = value.size(); 1155 int* valueList = NULL; 1156 if (valueCount > 0) { 1157 valueList = new int[valueCount]; 1158 DCHECK(valueList); 1159 if (valueList) { 1160 for (size_t i = 0; i < valueCount; ++i) 1161 valueList[i] = value[i]; 1162 } 1163 } 1164 1165 // Execution 1166 cef_function(valueCount, valueList); 1167 1168 // Parameter Restoration 1169 if (valueList) 1170 delete [] valueList; 1171 } 1172 1173 Boolean vector non-const type by reference (bool_vec_byref): 1174 C++: std::vector<bool>& value 1175 C API: size_t* valueCount, int* value 1176 1177 // CppToC Example 1178 CEF_EXPORT void cef_function(size_t* valueCount, int* value) 1179 { 1180 // Parameter Verification 1181 DCHECK(valueCount && (*valueCount == 0 || value)); 1182 if (!valueCount || (*valueCount > 0 && !value)) 1183 return; 1184 1185 // Parameter Translation 1186 std::vector<bool> valueList; 1187 if (valueCount && *valueCount > 0 && value) { 1188 for (size_t i = 0; i < *valueCount; ++i) 1189 valueList.push_back(value[i]?true:false); 1190 } 1191 1192 // Execution 1193 CefFunction(valueList); 1194 1195 // Parameter Restoration 1196 if (valueCount && value) { 1197 *valueCount = std::min(valueList.size(), *valueCount); 1198 if (*valueCount > 0) { 1199 for (size_t i = 0; i < *valueCount; ++i) 1200 value[i] = valueList[i]; 1201 } 1202 } 1203 } 1204 1205 // CToCpp Example 1206 void CefFunction(std::vector<bool>& value) 1207 { 1208 // Parameter Translation 1209 // Function identified by the "count_func" method attribute. 1210 size_t valueSize = value.size(); 1211 size_t valueCount = std::max(GetFunctionCount(), valueSize); 1212 int* valueList = NULL; 1213 if (valueCount > 0) { 1214 valueList = new int[valueCount]; 1215 DCHECK(valueList); 1216 if (valueList) 1217 memset(valueList, 0, sizeof(int)*valueCount); 1218 if (valueList && valueSize > 0) { 1219 for (size_t i = 0; i < valueSize; ++i) { 1220 valueList[i] = value[i]; 1221 } 1222 } 1223 } 1224 1225 // Execution 1226 cef_function(&valueCount, valueList); 1227 1228 // Parameter Restoration 1229 value.clear(); 1230 if (valueCount > 0 && valueList) { 1231 for (size_t i = 0; i < valueCount; ++i) 1232 value.push_back(valueList[i]?true:false); 1233 delete [] valueList; 1234 } 1235 } 1236 1237 Boolean vector const type by reference (bool_vec_byref_const): 1238 C++: const std::vector<bool>& value 1239 C API: size_t valueCount, int const* value 1240 1241 // CppToC Example 1242 CEF_EXPORT void cef_function(size_t valueCount, int const* value) 1243 { 1244 // Parameter Verification 1245 DCHECK(valueCount == 0 || value); 1246 if (valueCount > 0 && !value) 1247 return; 1248 1249 // Parameter Translation 1250 std::vector<bool> valueList; 1251 if (valueCount > 0) { 1252 for (size_t i = 0; i < valueCount; ++i) 1253 valueList.push_back(value[i]?true:false); 1254 } 1255 1256 // Execution 1257 CefFunction(valueList); 1258 } 1259 1260 // CToCpp Example 1261 void CefFunction(const std::vector<bool>& value) 1262 { 1263 // Parameter Translation 1264 const size_t valueCount = value.size(); 1265 int* valueList = NULL; 1266 if (valueCount > 0) { 1267 valueList = new int[valueCount]; 1268 DCHECK(valueList) 1269 if (valueList) { 1270 for (size_t i = 0; i < valueCount; ++i) 1271 valueList[i] = value[i]; 1272 } 1273 } 1274 1275 // Execution 1276 cef_function(valueCount, valueList); 1277 1278 // Parameter Restoration 1279 if (valueList) 1280 delete [] valueList; 1281 } 1282 1283 Smart pointer vector non-const type same boundary side by reference 1284 (refptr_vec_same_byref): 1285 C++: std::vector<CefRefPtr<CefPostDataElement>>& value 1286 C API: size_t* valueCount, cef_post_data_element_t** value 1287 1288 // CppToC Example 1289 CEF_EXPORT void cef_function(size_t* valueCount, 1290 cef_post_data_element_t** value) 1291 { 1292 // Parameter Verification 1293 DCHECK(valueCount && (*valueCount == 0 || value)); 1294 if (!valueCount || (*valueCount > 0 && !value)) 1295 return; 1296 1297 // Parameter Translation 1298 std::vector<CefRefPtr<CefPostDataElement>> valueList; 1299 if (valueCount && *valueCount > 0 && value) { 1300 for (size_t i = 0; i < *valueCount; ++i) 1301 valueList.push_back(CefPostDataElementCppToC::Unwrap(value[i])); 1302 } 1303 1304 // Execution 1305 CefFunction(valueList); 1306 1307 // Parameter Restoration 1308 if (valueCount && value) { 1309 *valueCount = std::min(valueList.size(), *valueCount); 1310 if (*valueCount > 0) { 1311 for (size_t i = 0; i < *valueCount; ++i) 1312 value[i] = CefPostDataElementCppToC::Wrap(valueList[i]); 1313 } 1314 } 1315 } 1316 1317 // CToCpp Example 1318 void CefFunction(std::vector<bool>& value) 1319 { 1320 // Parameter Translation 1321 // Function identified by the "count_func" method attribute. 1322 size_t valueSize = value.size(); 1323 size_t valueCount = std::max(GetFunctionCount(), valueSize); 1324 cef_post_data_element_t** valueList = NULL; 1325 if (valueCount > 0) { 1326 valueList = new cef_post_data_element_t*[valueCount]; 1327 DCHECK(valueList); 1328 if (valueList) 1329 memset(valueList, 0, sizeof(cef_post_data_element_t*)*valueCount); 1330 if (valueList && valueSize > 0) { 1331 for (size_t i = 0; i < valueSize; ++i) { 1332 valueList[i] = CefPostDataElementCToCpp::Unwrap(value[i]); 1333 } 1334 } 1335 } 1336 1337 // Execution 1338 cef_function(&valueCount, valueList); 1339 1340 // Parameter Restoration 1341 value.clear(); 1342 if (valueCount > 0 && valueList) { 1343 for (size_t i = 0; i < valueCount; ++i) 1344 value.push_back(CefPostDataElementCToCpp::Wrap(valueList[i])); 1345 delete [] valueList; 1346 } 1347 } 1348 1349 Smart pointer vector const type same boundary side by reference 1350 (refptr_vec_same_byref_const): 1351 C++: const std::vector<CefRefPtr<CefV8Value>>& value 1352 C API: size_t valueCount, const cef_v8value_t** value 1353 1354 // CppToC Example 1355 CEF_EXPORT void cef_function(size_t valueCount, 1356 const cef_v8value_t** value) 1357 { 1358 // Parameter Verification 1359 DCHECK(valueCount == 0 || value); 1360 if (valueCount > 0 && !value) 1361 return; 1362 1363 // Parameter Translation 1364 std::vector<CefRefPtr<CefV8Value>> valueList; 1365 if (valueCount > 0) { 1366 for (size_t i = 0; i < valueCount; ++i) 1367 valueList.push_back(CefV8ValueCppToC::Unwrap(value[i])); 1368 } 1369 1370 // Execution 1371 CefFunction(valueList); 1372 } 1373 1374 // CToCpp Example 1375 void CefFunction(const std::vector<bool>& value) 1376 { 1377 // Parameter Translation 1378 const size_t valueCount = value.size(); 1379 cef_v8value_t** valueList = NULL; 1380 if (valueCount > 0) { 1381 valueList = new int[valueCount]; 1382 DCHECK(valueList); 1383 if (valueList) { 1384 for (size_t i = 0; i < valueCount; ++i) 1385 valueList[i] = CefV8ValueCToCpp::Unwrap(value[i]); 1386 } 1387 } 1388 1389 // Execution 1390 cef_function(valueCount, valueList); 1391 1392 // Parameter Restoration 1393 if (valueList) 1394 delete [] valueList; 1395 } 1396 1397 Smart pointer vector non-const type different boundary side by reference 1398 (refptr_vec_diff_byref): 1399 C++: std::vector<CefRefPtr<CefPostDataElement>>& value 1400 C API: size_t* valueCount, cef_post_data_element_t** value 1401 1402 // CppToC Example 1403 CEF_EXPORT void cef_function(size_t* valueCount, 1404 cef_post_data_element_t** value) 1405 { 1406 // Parameter Verification 1407 DCHECK(valueCount && (*valueCount == 0 || value)); 1408 if (!valueCount || (*valueCount > 0 && !value)) 1409 return; 1410 1411 // Parameter Translation 1412 std::vector<CefRefPtr<CefPostDataElement>> valueList; 1413 if (valueCount && *valueCount > 0 && value) { 1414 for (size_t i = 0; i < *valueCount; ++i) 1415 valueList.push_back(CefPostDataElementCToCpp::Wrap(value[i])); 1416 } 1417 1418 // Execution 1419 CefFunction(valueList); 1420 1421 // Parameter Restoration 1422 if (valueCount && value) { 1423 *valueCount = std::min(valueList.size(), *valueCount); 1424 if (*valueCount > 0) { 1425 for (size_t i = 0; i < *valueCount; ++i) 1426 value[i] = CefPostDataElementCToCpp::Unwrap(valueList[i]); 1427 } 1428 } 1429 } 1430 1431 // CToCpp Example 1432 void CefFunction(std::vector<bool>& value) 1433 { 1434 // Parameter Translation 1435 // Function identified by the "count_func" method attribute. 1436 size_t valueSize = value.size(); 1437 size_t valueCount = std::max(GetFunctionCount(), valueSize); 1438 cef_post_data_element_t** valueList = NULL; 1439 if (valueCount > 0) { 1440 valueList = new cef_post_data_element_t*[valueCount]; 1441 DCHECK(valueList); 1442 if (valueList) 1443 memset(valueList, 0, sizeof(cef_post_data_element_t*)*valueCount); 1444 if (valueList && valueSize > 0) { 1445 for (size_t i = 0; i < valueSize; ++i) { 1446 valueList[i] = CefPostDataElementCppToC::Wrap(value[i]); 1447 } 1448 } 1449 } 1450 1451 // Execution 1452 cef_function(&valueCount, valueList); 1453 1454 // Parameter Restoration 1455 value.clear(); 1456 if (valueCount > 0 && valueList) { 1457 for (size_t i = 0; i < valueCount; ++i) 1458 value.push_back(CefPostDataElementCppToC::Unwrap(valueList[i])); 1459 delete [] valueList; 1460 } 1461 } 1462 1463 Smart pointer vector const type different boundary side by reference 1464 (refptr_vec_diff_byref_const): 1465 C++: const std::vector<CefRefPtr<CefV8Value>>& value 1466 C API: size_t valueCount, const cef_v8value_t** value 1467 1468 // CppToC Example 1469 CEF_EXPORT void cef_function(size_t valueCount, 1470 const cef_v8value_t** value) 1471 { 1472 // Parameter Verification 1473 DCHECK(valueCount == 0 || value); 1474 if (valueCount > 0 && !value) 1475 return; 1476 1477 // Parameter Translation 1478 std::vector<CefRefPtr<CefV8Value>> valueList; 1479 if (valueCount > 0) { 1480 for (size_t i = 0; i < valueCount; ++i) 1481 valueList.push_back(CefV8ValueCToCpp::Wrap(value[i])); 1482 } 1483 1484 // Execution 1485 CefFunction(valueList); 1486 } 1487 1488 // CToCpp Example 1489 void CefFunction(const std::vector<bool>& value) 1490 { 1491 // Parameter Translation 1492 const size_t valueCount = value.size(); 1493 cef_v8value_t** valueList = NULL; 1494 if (valueCount > 0) { 1495 valueList = new int[valueCount]; 1496 DCHECK(valueList); 1497 if (valueList) { 1498 for (size_t i = 0; i < valueCount; ++i) 1499 valueList[i] = CefV8ValueCppToC::Wrap(value[i]); 1500 } 1501 } 1502 1503 // Execution 1504 cef_function(valueCount, valueList); 1505 1506 // Parameter Restoration 1507 if (valueList) 1508 delete [] valueList; 1509 } 1510 1511Return Values: 1512 1513 Simple/Enumeration type (simple): 1514 C++: int 1515 C API: int 1516 1517 // CppToC Example 1518 CEF_EXPORT int cef_function() 1519 { 1520 // Execution 1521 int _rv = CefFunction(); 1522 1523 // Return Translation 1524 return _rv; 1525 } 1526 1527 // CToCpp Example 1528 int CefFunction() 1529 { 1530 // Execution 1531 int _rv = cef_function(); 1532 1533 // Return Translation 1534 return _rv; 1535 } 1536 1537 Boolean type (bool): 1538 C++: bool 1539 C API: int 1540 1541 // CppToC Example 1542 CEF_EXPORT int cef_function() 1543 { 1544 // Execution 1545 bool _rv = CefFunction(); 1546 1547 // Return Translation 1548 return _rv; 1549 } 1550 1551 // CToCpp Example 1552 bool CefFunction() 1553 { 1554 // Execution 1555 int _rv = cef_function(); 1556 1557 // Return Translation 1558 return _rv?true:false; 1559 } 1560 1561 String non-const by reference type (string): 1562 C++: CefString 1563 C API: cef_string_userfree_t 1564 1565 // CppToC Example 1566 CEF_EXPORT cef_string_userfree_t cef_function() 1567 { 1568 // Execution 1569 CefString _rv = CefFunction(); 1570 1571 // Return Translation 1572 return _rv.DetachToUserFree(); 1573 } 1574 1575 // CToCpp Example 1576 CefString CefFunction() 1577 { 1578 // Execution 1579 cef_string_userfree_t _rv = cef_function(); 1580 1581 // Return Translation 1582 CefString _rvStr; 1583 _rvStr.AttachToUserFree(_rv); 1584 return _rvStr; 1585 } 1586 1587 Smart pointer type same boundary side (refptr_same): 1588 C++: CefRefPtr<CefBrowser> 1589 C API: cef_browser_t* 1590 1591 // CppToC Example 1592 CEF_EXPORT cef_browser_t* cef_function() 1593 { 1594 // Execution 1595 CefRefPtr<CefBrowser> _rv = CefFunction(); 1596 1597 // Return Translation 1598 return CefBrowserCppToC::Wrap(_rv); 1599 } 1600 1601 // CToCpp Example 1602 CefString CefFunction() 1603 { 1604 // Execution 1605 cef_browser_t* _rv = cef_function(); 1606 1607 // Return Translation 1608 return CefBrowserCToCpp::Wrap(_rv); 1609 } 1610 1611 Smart pointer type different boundary side (refptr_diff): 1612 C++: CefRefPtr<CefBrowser> 1613 C API: cef_browser_t* 1614 1615 // CppToC Example 1616 CEF_EXPORT cef_browser_t* cef_function() 1617 { 1618 // Execution 1619 CefRefPtr<CefBrowser> _rv = CefFunction(); 1620 1621 // Return Translation 1622 return CefBrowserCToCpp::Unwrap(_rv); 1623 } 1624 1625 // CToCpp Example 1626 CefString CefFunction() 1627 { 1628 // Execution 1629 cef_browser_t* _rv = cef_function(); 1630 1631 // Return Translation 1632 return CefBrowserCppToC::Unwrap(_rv); 1633 } 1634 1635 1636Translating Comments 1637-------------------- 1638 1639Comments from the CEF header file are reproduced in the C API header file with 1640any referenced C++ types and terminology changed to reflect C API types and 1641terminology. 1642 1643C++: 1644// Create a new CefV8Value object of the specified type. These methods 1645// should only be called from within the JavaScript context -- either in a 1646// CefV8Handler::Execute() callback or a CefHandler::HandleJSBinding() 1647// callback. 1648 1649C API: 1650// Create a new cef_v8value_t object of the specified type. These functions 1651// should only be called from within the JavaScript context -- either in a 1652// cef_v8handler_t::execute() callback or a cef_handler_t::handle_jsbinding() 1653// callback. 1654 1655Situations where the user is responsible for freeing strings allocated and 1656returned by the library are also noted by comments in the C API header file. 1657 1658C API: 1659 // The resulting string must be freed by calling cef_string_free(). 1660 1661A comment must occur immediately before the function, class or method that it 1662documents with no extra space in between. Comments may span multiple lines 1663but each line must start with the '//' comment identifier. 1664 1665C++: 1666 // Set focus for the browser window. If |enable| is true focus will be set 1667 // to the window. Otherwise, focus will be removed. 1668 /*--cef()--*/ 1669 virtual void SetFocus(bool enable) =0; 1670 1671If two comments are separated by an empty line it will be assumed that the 1672higher comment represents a section header and additional space will be added 1673before it in the translated output. 1674 1675C++: 1676 // ARRAY METHODS - These methods are only available on arrays. 1677 1678 // Returns the number of elements in the array. 1679 /*--cef()--*/ 1680 virtual int GetArrayLength() =0; 1681 1682Empty lines and lines with the comment identifier but no content are considered 1683paragraph breaks for the purposes of wrapping the translated text. Any content 1684indented more than one space is reproduced as-is without content translation 1685or wrapping. 1686 1687C++: 1688// Register a new V8 extension with the specified JavaScript extension code and 1689// handler. Functions implemented by the handler are prototyped using the 1690// keyword 'native'. The calling of a native function is restricted to the scope 1691// in which the prototype of the native function is defined. 1692// 1693// Example JavaScript extension code: 1694// 1695// // create the 'example' global object if it doesn't already exist. 1696// if (!example) 1697// example = {}; 1698