### Automated EASTL Build and Test Script (Batch) Source: https://github.com/electronicarts/eastl/blob/master/CONTRIBUTING.md An example batch script to automate the process of cloning, building, and running EASTL unit tests across multiple configurations (Release, Debug, RelWithDebInfo, MinSizeRel). ```batch set build_folder=out mkdir %build_folder% pushd %build_folder% call cmake .. -DEASTL_BUILD_TESTS:BOOL=ON -DEASTL_BUILD_BENCHMARK:BOOL=OFF call cmake --build . --config Release call cmake --build . --config Debug call cmake --build . --config RelWithDebInfo call cmake --build . --config MinSizeRel pushd test call ctest -C Release call ctest -C Debug call ctest -C RelWithDebInfo call ctest -C MinSizeRel popd popd ``` -------------------------------- ### Install EASTL with vcpkg Source: https://github.com/electronicarts/eastl/blob/master/README.md Steps to install EASTL using the vcpkg dependency manager. This involves cloning the vcpkg repository, bootstrapping it, integrating it with the system, and then installing the eastl package. ```shell git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install vcpkg install eastl ``` -------------------------------- ### Automated EASTL Build and Test Script (Bash) Source: https://github.com/electronicarts/eastl/blob/master/CONTRIBUTING.md An example bash script to automate the process of cloning, building, and running EASTL unit tests across multiple configurations (Release, Debug, RelWithDebInfo, MinSizeRel). ```bash build_folder=out mkdir $build_folder pushd $build_folder cmake .. -DEASTL_BUILD_TESTS:BOOL=ON -DEASTL_BUILD_BENCHMARK:BOOL=OFF cmake --build . --config Release cmake --build . --config Debug cmake --build . --config RelWithDebInfo cmake --build . --config MinSizeRel pushd test ctest -C Release ctest -C Debug ctest -C RelWithDebInfo ctest -C MinSizeRel popd popd ``` -------------------------------- ### Install EASTL with Conan Source: https://github.com/electronicarts/eastl/blob/master/README.md Instructions for installing the EASTL library using the Conan package manager. This command fetches the latest version available in the Conan Center Index. ```shell conan install eastl/3.15.00@ ``` -------------------------------- ### EASTL CMake Configuration Options Source: https://github.com/electronicarts/eastl/blob/master/CONTRIBUTING.md Documentation for key CMake options used when configuring the EASTL build. This includes enabling tests and benchmarks. ```cmake -DEASTL_BUILD_TESTS:BOOL=ON Enables the building of EASTL unit tests. -DEASTL_BUILD_BENCHMARK:BOOL=ON Enables the building of EASTL benchmark programs. Can be toggled ON or OFF. ``` -------------------------------- ### CMake Minimum Version and Project Setup Source: https://github.com/electronicarts/eastl/blob/master/test/CMakeLists.txt Specifies the minimum required CMake version and defines the project name and language. This is a standard starting point for any CMake project. ```cmake cmake_minimum_required(VERSION 3.11) project(EASTLTest CXX) ``` -------------------------------- ### EASTL List Size Implementation Example Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Demonstrates the assembly output for calling the size() method on an EASTL list, showing compiler optimization where a single implementation is used. ```assembly eastl_size_t n2 = toPtrList.size(); 0042D288  lea       edx, [esp+14h] 0042D28C  call     eastl::list::size (414180h) 0042D291  push     eax     0042D292  lea       edx, [esp+24h] 0042D296  call     eastl::list::size (414180h) ``` -------------------------------- ### Basic String Copy-on-Write Example Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Design.html Demonstrates the concept of copy-on-write (CoW) for strings, showing how string data can be shared between objects until one of them is modified. This example illustrates the sharing mechanism and how assignment triggers detachment. ```C++ string a("hello"); string b(a); a = "world"; ``` -------------------------------- ### Clone EASTL Repository Source: https://github.com/electronicarts/eastl/blob/master/CONTRIBUTING.md Instructions for cloning the EASTL project repository from GitHub using Git. This is the first step to obtaining the source code. ```bash git clone https://github.com/electronicarts/EASTL ``` -------------------------------- ### C++ Count Algorithm Example Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Maintenance.html An example implementation of the C++ count algorithm, demonstrating the critical difference between using '<' and '!=' for iterator comparisons, especially with InputIterators versus RandomAccessIterators. This highlights the importance of understanding iterator categories. ```cpp template int count(InputIterator first, InputIterator last, const T& value) { int result = 0; for(; first < last; ++first){ if(*first == value) ++result; } return result; } ``` -------------------------------- ### Installation Rules Source: https://github.com/electronicarts/eastl/blob/master/CMakeLists.txt Defines how the EASTL library and its headers are installed. The library is installed to 'lib', headers to 'include/EASTL', and MSVC Natvis files to 'doc'. ```cmake install(TARGETS EASTL DESTINATION lib) install(DIRECTORY include/EASTL DESTINATION include) if (MSVC) install(FILES ${EASTL_NATVIS_FILE} DESTINATION ${EASTL_NATVIS_DIR}) endif() ``` -------------------------------- ### EASTL String Resize and strcpy Pitfalls Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Gotchas.html Demonstrates the correct and incorrect ways to use `strcpy` with EASTL strings when resizing. Incorrect usage can lead to data corruption due to zero-initialization during `resize` after `reserve` and `strcpy`. - **Broken Example**: Shows how `reserve` followed by `strcpy` and then `resize` can overwrite copied data with null characters. - **OK Example**: Illustrates the correct approach by using `resize` first to allocate and zero-initialize, then `strcpy` to fill the data, followed by a final `resize` to the actual content length. ```cpp string mDataDir; // Broken example: mDataDir.reserve(kMaxPathLength); strcpy(&mDataDir[0], "blah/blah/blah");mDataDir.resize(strlen(&mDataDir[0])); // Overwrites your blah/... with 00000... ``` ```cpp string mDataDir; // OK example:mDataDir.resize(kMaxPathLength); strcpy(&mDataDir[0], "blah/blah/blah");mDataDir.resize(strlen(&mDataDir[0])); ``` -------------------------------- ### Using Algorithms for Container Operations Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Provides examples of using standard algorithms like 'find' and 'quick_sort' with EASTL containers. This approach centralizes functionality and allows for sub-range operations. ```cpp // Find an element in a list list::iterator i = find(list.begin(), list.end(), 3); // Sort a vector quick_sort(v.begin(), v.end()); // Sort the entire array. quick_sort(&v[3], &v[8]); // Sort the items at the indexes in the range of [3, 8). ``` -------------------------------- ### Copy-on-Write (CoW) String Concept Example Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Demonstrates the basic concept of copy-on-write (CoW) for strings, where data is shared between objects until a modification occurs. This example shows string assignment and copy construction. ```cpp string a("hello"); string b(a); a = "world"; ``` -------------------------------- ### Build and Run EASTL Unit Tests with CMake Source: https://github.com/electronicarts/eastl/blob/master/CONTRIBUTING.md Detailed steps for building EASTL and its unit tests using CMake. This includes generating build scripts, compiling tests for different configurations, and running them via ctest. ```bash mkdir your_build_folder && cd your_build_folder cmake .. -DEASTL_BUILD_TESTS:BOOL=ON cmake --build . --config Release cmake --build . --config Debug cmake --build . --config RelWithDebInfo cmake --build . --config MinSizeRel cd test ctest -C Release ctest -C Debug ctest -C RelWithDebInfo ctest -C MinSizeRel ``` -------------------------------- ### C++ String Initialization and Sharing Example Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Illustrates how two string objects can share data using copy initialization, a prerequisite for copy-on-write optimization. ```cpp string a("hello"); string b(a); ``` -------------------------------- ### Template Efficiency and Code Bloat Example Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Illustrates how template instantiation can lead to code bloat by generating specialized code for each data type used. This example shows a generic 'min' function and its specialized instantiations for 'int' and 'double'. ```cpp template const T min(const T a, const T b) { return b < a ? b : a; } int i = min(3, 4); double d = min(3.0, 4.0); ``` ```cpp int min(const int a, const int b) { return b < a ? b : a; } double min(const double a, const double b) { return b < a ? b : a; } ``` -------------------------------- ### Project Setup and Options Source: https://github.com/electronicarts/eastl/blob/master/CMakeLists.txt Configures the CMake minimum version, project name, and defines various build options that control features like benchmark generation, test execution, iterator category compatibility, and deprecation handling. ```cmake cmake_minimum_required(VERSION 3.11) project(EASTL CXX) # Options option(EASTL_BUILD_BENCHMARK "Enable generation of build files for benchmark" OFF) option(EASTL_BUILD_TESTS "Enable generation of build files for tests" OFF) option(EASTL_STD_ITERATOR_CATEGORY_ENABLED "Enable compatibility with std:: iterator categories" OFF) option(EASTL_DISABLE_APRIL_2024_DEPRECATIONS "Enable use of API marked for removal in April 2024." OFF) option(EASTL_DISABLE_SEPT_2024_DEPRECATIONS "Enable use of API marked for removal in September 2024." OFF) option(EASTL_DISABLE_APRIL_2025_DEPRECATIONS "Enable use of API marked for removal in April 2025." OFF) ``` -------------------------------- ### EASTL Custom Allocator Usage Examples Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Demonstrates how to declare EASTL containers (`list`) with a custom allocator, both by default construction and by copying an existing allocator, and how to assign an underlying implementation after construction. ```cpp // Declare a Widget list and have it default construct. list widgetList; // Declare a Widget list and have it construct with a copy of some global allocator. list widgetList2(gSomeGlobalAllocator); // Declare a Widget list and have it default construct, but assign // an underlying implementation after construction. list widgetList; widgetList.get_allocator().mpIAllocator = new WidgetAllocatorImpl; ``` -------------------------------- ### EASTL hash_map Example Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Best Practices.html Demonstrates the creation and basic usage of an EASTL hash_map for mapping integers to strings. Hash containers offer fast lookups and reduced memory usage compared to associative containers, but do not maintain sorted order. ```c++ hash_map stringTable; stringTable[37] = "hello"; ``` -------------------------------- ### EASTL Custom Allocator Usage Examples Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Demonstrates how to declare EASTL containers like 'list' using a custom allocator. It shows default construction and assignment of an underlying allocator implementation. ```cpp // Declare a Widget list and have it default construct. list widgetList; // Declare a Widget list and have it construct with a copy of some global allocator. list widgetList2(gSomeGlobalAllocator); // Declare a Widget list and have it default construct, but assign // an underlying implementation after construction. list widgetList; widgetList.get_allocator().mpIAllocator = new WidgetAllocatorImpl; ``` -------------------------------- ### EASTL List Size Function Call Example Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Demonstrates the assembly-level view of calling the `eastl::list::size()` function. This snippet highlights how compiler optimizations might fold multiple calls into a single implementation. ```Assembly 0042D288  lea         edx,[esp+14h] 0042D28C  call        eastl::list::size (414180h) 0042D291  push        eax  0042D292  lea         edx,[esp+24h] 0042D296  call        eastl::list::size (414180h) ``` -------------------------------- ### Explicit Template Instantiation Example Source: https://github.com/electronicarts/eastl/blob/master/doc/Glossary.md Demonstrates the syntax for explicitly instantiating templated classes and functions in C++. This is useful for creating library files where uninstantiated template definitions are not desired in object files. ```cpp template class vector; template void min(int, int); template void min(int, int); ``` -------------------------------- ### Visual Studio Project Setup for EASTL Source: https://github.com/electronicarts/eastl/blob/master/doc/CMake/EASTL_Project_Integration.md Steps to configure a Visual Studio project to use EASTL. This includes adding include directories, library paths, and the library dependency. It also explains how to optionally add the EASTL.natvis file for improved debugger visualization. ```C++ // Add to C/C++ -> General -> Additional include directories: // ${EASTL_ROOT_DIR}/include // ${EASTL_ROOT_DIR}/test/packages/EAAssert/include // ${EASTL_ROOT_DIR}/test/packages/EABase/include/Common // ${EASTL_ROOT_DIR}/test/packages/EAMain/include // ${EASTL_ROOT_DIR}/test/packages/EAStdC/include // ${EASTL_ROOT_DIR}/test/packages/EATest/include // ${EASTL_ROOT_DIR}/test/packages/EAThread/include // Add to Linker -> General -> Additional Library Directories: // ${EASTL_ROOT_DIR}/build/$(Configuration) // Add to Linker -> Input -> Additional Dependencies: // EASTL.lib // Or in code: #pragma comment(lib, "EASTL.lib") // To add natvis (optional): // Right-click your project: Add -> Existing item and add the file: // ${EASTL_ROOT_DIR}/doc/EASTL.natvis ``` -------------------------------- ### begins_with C++ String Utility Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Checks if a string container starts with a specified prefix. It takes the string by const reference and the prefix string to check against. ```cpp template bool begins_with(const String& s, const typename String::value_type* p) { return s.compare(0, eastl::CharStrlen(p), p) == 0; } ``` ```cpp if(begins_with(s, "log: ")) ... ``` -------------------------------- ### EASTL list with pointer types example Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Demonstrates the declaration of EASTL lists containing pointer types, illustrating how EASTL handles specialized containerizations for pointer types, similar to some STL implementations. ```cpp eastl::list intPtrList; eastl::list toPtrList; eastl_size_t n1 = intPtrList.size(); ``` -------------------------------- ### Using Standard Algorithms with EASTL Containers Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Illustrates how to use standard C++ algorithms like 'find' and 'quick_sort' with EASTL containers. EASTL often relies on external algorithms for functionality not directly implemented as member functions. ```cpp list::iterator i = find(list.begin(), list.end(), 3); quick_sort(v.begin(), v.end()); quick_sort(&v[3], &v[8]); ``` -------------------------------- ### EASTL Vector Set Capacity Source: https://github.com/electronicarts/eastl/blob/master/doc/BestPractices.md Shows how to reduce the capacity of an EASTL vector to free up unused memory. Includes examples for trimming and resizing to zero. ```cpp vector v; ... v.set_capacity(); vector v; ... v.set_capacity(0); ``` -------------------------------- ### VC++ Debugger Integration for EASTL Containers Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Explains how to use the `AutoExp.dat` file with Visual Studio to enable tooltips for EASTL container data in the debugger. It also discusses potential issues related to whitespace sensitivity in debugger expressions. ```APIDOC Visual Studio Debugger Integration for EASTL: Purpose: Configure Visual Studio to display EASTL container data with tooltips in the debugger. Method: Use the `AutoExp.dat` file. Configuration: - An example `AutoExp.dat` file is provided with the EASTL documentation. Troubleshooting: - Debugger sensitivity to whitespace in template expressions can cause issues. - Ensure exact matching of whitespace and `const` placement in template expressions. - For `eastl::list<>`, separate sections for different value types might be necessary due to whitespace rules. ``` -------------------------------- ### EASTL Coding Conventions Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Maintenance.html Key coding conventions for maintaining and contributing to EASTL, emphasizing efficiency, readability, and avoiding certain C++ features. ```APIDOC Coding Conventions: - No RTTI use. - No use of exception specifications (e.g. appending the 'throw' declarator to a function). - No use of exception handling itself except where explicitly required by the implementation (e.g. vector::at). - Exception use needs to be savvy to EASTL_EXCEPTIONS_ENABLED. - No use of macros (outside of config.h). - No use of static or global variables. - No use of global new, delete, malloc, or free. Memory must be user-specifyable via an Allocator parameter. - Containers use protected member data and functions as opposed to private, to allow subclasses to extend containers. - No use of multithreading primitives. - No use of the export keyword. - Preference for C++ casts (e.g., static_cast) when debuggers can evaluate them; C-style casts are used when necessary for debugging. - No external library dependencies whatsoever, including standard STL. Dependent only on EABase and the C++ compiler. - All code must be const-correct. - Algorithms refer only to iterators, not containers. - Algorithms generally do not allocate memory; if they do, a version allowing user-provided allocators should exist. - No inferior implementations; all facilities must be of professional quality. - Emulate EASTL style of code layout (4 spaces for indents). - No major changes without consulting a peer group. ``` -------------------------------- ### Iterating and Erasing from EASTL Containers Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Provides an example of safely removing elements from an EASTL container (like `set`) while iterating, by correctly handling iterator invalidation. ```cpp set intSet; set::iterator i = intSet.begin(); while(i != intSet.end()) { if(*i & 1) { // Erase all odd integers from the container. i = intSet.erase(i); } else { ++i; } } ``` -------------------------------- ### Copy-less Element Insertion in EASTL Containers Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Best Practices.html Explains and demonstrates the use of copy-less insertion methods like push_back(void) to construct elements in-place, avoiding copy construction costs and supporting types without copy constructors. ```cpp vector widgetArray; widgetArray.push_back(); // Constructs Widget in-place widgetArray.back().x = 0; // Example of accessing the new object. ``` -------------------------------- ### Handle Global EASTL Container Crashes with Initialization Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Demonstrates solutions for crashes caused by uninitialized global EASTL containers. Covers pointer-based initialization and manual constructor calls to ensure proper object construction. ```cpp eastl::list gIntList; // Global variable. void DoSomething() { gIntList.push_back(1); // Crash. gIntList was never constructed. } ``` ```cpp eastl::list* gIntList = NULL; void DoSomething() { if(!gIntList) // Or move this to an init function. gIntList = new eastl::list; gIntList->push_back(1); // Success } ``` ```cpp eastl::list gIntList; void InitSystem() { new(&gIntList) eastl::list; } void DoSomething() { gIntList.push_back(1); // Success } ``` -------------------------------- ### Search EASTL hash_map with find_as() Source: https://github.com/electronicarts/eastl/blob/master/doc/BestPractices.md Explains how to use the `find_as()` function in EASTL's hash tables for efficient searching with different types, such as string literals or char pointers, avoiding temporary object creation. ```cpp hash_map hashMap; hash_map::iterator it = hashMap.find_as("hello"); // Using default hash and compare. ``` -------------------------------- ### C++ String Assignment and Sharing Example Source: https://github.com/electronicarts/eastl/blob/master/doc/Design.md Demonstrates how string assignment can lead to data sharing between string objects in C++, illustrating the concept of Copy-On-Write (CoW) behavior. ```cpp string a("hello"); string b(a); a = "world"; ``` -------------------------------- ### EASTL Type Traits: is_floating_point Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Best Practices.html An example of using EASTL's type traits library to check if a given type T is a floating-point number at compile time. ```cpp template void DoSomething(T t) { assert(is_floating_point::value); } ``` -------------------------------- ### mapped_type Definition Source: https://github.com/electronicarts/eastl/blob/master/doc/Glossary.md mapped_type is a typedef used by associative containers to identify the container object accessed by a key. For example, in a dictionary mapping integer IDs to strings, the strings are the mapped types. ```cpp mapped_type A mapped_type is a typedef used by associative containers to identify the container object which is accessed by a key. If you have a dictionary of strings that you access by an integer id, the ids are the keys and the strings are the mapped types. ``` -------------------------------- ### EASTL Smart Pointers Comparison Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Compares EASTL smart pointers to Boost smart pointers, highlighting their features, differences, and suitability for various use cases. ```cpp // EASTL smart pointers (e.g., eastl::unique_ptr, eastl::shared_ptr) are designed // to be similar to C++11's std::unique_ptr and std::shared_ptr, and often // offer performance advantages or specific features tailored for game development. // Comparison with Boost smart pointers: // - Functionality: Both provide RAII for resource management (pointers, files, etc.). // - Performance: EASTL aims for high performance, potentially outperforming Boost // in specific scenarios due to tailored optimizations. // - Dependencies: Boost is a large, external library. EASTL is often integrated // directly into projects, potentially with fewer external dependencies. // - Features: Specific features like custom deleters, array support, and ownership // semantics are generally comparable, but implementation details may differ. // EASTL smart pointers are generally a good choice when using EASTL containers // or when seeking a lightweight, high-performance smart pointer solution. ``` -------------------------------- ### EASTL Compiler Testing and Portability Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Maintenance.html Guidelines for ensuring EASTL code correctness and portability across different C++ compilers, highlighting common pitfalls and necessary testing procedures. ```APIDOC Compiler Issues and Portability: - EASTL requires compilers that properly support templates. - VC++ may allow illegal statements not permitted by other compilers (e.g., neglecting 'typename' keyword in template references). Testing Requirements: - Test under at least VS2005, GCC 3.4+, GCC 4.4+, EDG, and clang. - Test all functions written, as compilers may skip compilation of unused template functions. ``` -------------------------------- ### EASTL Custom Allocator Skeleton Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Provides a C++ code example for creating a custom allocator class to be used with EASTL containers. It includes constructor and destructor signatures and placeholders for allocator logic. ```cpp class custom_allocator { public: custom_allocator(const char* pName = EASTL_NAME_VAL("custom allocator")) { #if EASTL_NAME_ENABLED mpName = pName ? pName : EASTL_ALLOCATOR_DEFAULT_NAME; #endif // Possibly do something here. } custom_allocator(const allocator& x, const char* pName = EASTL_NAME_VAL("custom allocator")); { #if EASTL_NAME_ENABLED mpName = pName ? pName : EASTL_ALLOCATOR_DEFAULT_NAME; #endif // Possibly copy from x here. } ~custom_allocator(); { // Destructor logic } }; ``` -------------------------------- ### Name EASTL Containers for Memory Tracking Source: https://github.com/electronicarts/eastl/blob/master/doc/BestPractices.md Demonstrates how to assign a name to EASTL containers using `set_name` or a constructor argument for improved memory usage tracking and categorization. ```cpp list collisionList(allocator("collision list")); ``` ```cpp list collisionList; collisionList.get_allocator().set_name("collision list"); ``` -------------------------------- ### EASTL HashMap Search with find_as() Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Best Practices.html Illustrates using the EASTL hash_map's find_as() method to search using string literals or char pointers, avoiding inefficient temporary string object creation. ```cpp hash_map hashMap; // Using default hash and compare. hash_map::iterator it = hashMap.find_as("hello"); ``` -------------------------------- ### EASTL Vector Initialization and Reservation Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL Best Practices.html Demonstrates two common ways to initialize an EASTL vector: pre-allocating capacity upon construction or reserving capacity after default construction. This helps in managing memory efficiently by avoiding multiple reallocations. ```cpp vector v(37);  // Reserve space to hold up to 37 items.    or vector v;     // This empty construction causes to memory to be allocated or reserved. v.reserve(37); ``` -------------------------------- ### EASTL String Begins With Utility Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html A template function to check if a string starts with a specified prefix. It compares the beginning portion of the string with the prefix. Requires the string type to support `compare` and `size_type`. ```cpp template bool begins_with(const String& s, const typename String::value_type* p) { return s.compare(0, eastl::CharStrlen(p), p) == 0; } ``` -------------------------------- ### C++ String Assignment and Detachment Example Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Shows how assigning a new value to a string object can cause it to release its shared data reference, a key aspect in CoW behavior. ```cpp a = "world"; ``` -------------------------------- ### Global EASTL Container Initialization Failure Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Illustrates a common problem where global EASTL containers may not be properly constructed, leading to crashes. It shows a failing example and provides two common solutions: using pointers with manual initialization or calling the constructor directly using placement new. ```C++ // Failing code: eastl::list gIntList; // Global variable. void DoSomething() { gIntList.push_back(1); // Crash. gIntList was never constructed. } ``` ```C++ // Declaring a pointer solution: eastl::list* gIntList = NULL; void DoSomething() { if(!gIntList) // Or move this to an init function. gIntList = new eastl::list; gIntList->push_back(1); // Success } ``` ```C++ // Manual constructor call solution: eastl::list gIntList; void InitSystem() { new(&gIntList) eastl::list; } void DoSomething() { gIntList.push_back(1); // Success } ``` -------------------------------- ### CMake Project Initialization Source: https://github.com/electronicarts/eastl/blob/master/benchmark/CMakeLists.txt Sets the minimum required CMake version, defines the project name and language, and includes the CTest module for testing integration. ```cmake cmake_minimum_required(VERSION 3.1) project(EASTLBenchmarks CXX) include(CTest) ``` -------------------------------- ### Get Element Type from Iterator (C++) Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Demonstrates how to determine the type of elements an iterator points to using `iterator_traits`. This is useful in generic programming to work with different container types. ```cpp template void DoSomething(Iterator first, Iterator last) { typedef typename iterator_traits::value_type ValueType; // Use ValueType for operations // For example: ValueType element = *first; } ``` -------------------------------- ### EASTL String: Get String Length Source: https://github.com/electronicarts/eastl/blob/master/doc/BestPractices.md Highlights the efficient and clear way to obtain the length of an EASTL string. Using the length() member function is preferred over converting to a C-style string and using strlen(). ```cpp len = strlen(s.c_str()); // Instead, use: len = s.length(); ``` -------------------------------- ### EASTL Fixed Pool Allocator Initialization Source: https://github.com/electronicarts/eastl/blob/master/doc/FAQ.md Shows how to use the eastl::fixed_pool allocator with a list by initializing it with a pre-allocated buffer. This approach is useful for managing memory for node-based containers. ```cpp char buffer[256]; list myList; myList.get_allocator().init(buffer, 256); ``` -------------------------------- ### EASTL Containers with In-Place Insertion Source: https://github.com/electronicarts/eastl/blob/master/doc/BestPractices.md Lists EASTL containers and their methods that support in-place construction of elements, offering an alternative to copy-based insertion. ```APIDOC Container In-Place Insertion Methods: vector::push_back() - Constructs an element in-place at the end of the vector. deque::push_back() - Constructs an element in-place at the end of the deque. deque::push_front() - Constructs an element in-place at the beginning of the deque. list::push_back() - Constructs an element in-place at the end of the list. list::push_front() - Constructs an element in-place at the beginning of the list. slist::push_front() - Constructs an element in-place at the beginning of the singly linked list. map::insert(const key_type& key) - Inserts a new element with the given key and a default-constructed value. This differs from other map insert functions that require a value_type. multimap::insert(const key_type& key) - Inserts a new element with the given key and a default-constructed value. Allows duplicate keys. hash_map::insert(const key_type& key) - Inserts a new element with the given key and a default-constructed value into a hash map. hash_multimap::insert(const key_type& key) - Inserts a new element with the given key and a default-constructed value into a hash multimap. Allows duplicate keys. ``` -------------------------------- ### node Class Definition Source: https://github.com/electronicarts/eastl/blob/master/doc/Glossary.md A node is a small holder class used by many containers to store contained items. For example, a linked list node typically contains pointers to the previous and next nodes, and the data element. ```cpp node A node is a little holder class used by many containers to hold the contained items. A linked-list, for example, defines a node which has three members: mpPrev, mpNext, and T (the contained object). ``` -------------------------------- ### EASTL Sorting Algorithms Source: https://github.com/electronicarts/eastl/blob/master/doc/html/EASTL FAQ.html Covers EASTL's sorting algorithms, including how to sort in reverse order and common issues encountered. ```cpp // EASTL Sorting Algorithms: // EASTL provides sorting algorithms similar to the STL, often found in . // Common algorithms include sort, stable_sort, partial_sort. // Sorting in reverse order: // You can sort in reverse order by providing a custom comparator (e.g., eastl::greater). // Example: eastl::vector vec = {5, 2, 8, 1, 9}; // Sort in ascending order (default) eastl::sort(vec.begin(), vec.end()); // Sort in descending order eastl::sort(vec.begin(), vec.end(), eastl::greater()); // Issues with sorting algorithms often stem from incorrect comparison functions // or elements that do not meet the requirements (e.g., missing operator<). // Ensure your element type has a valid comparison operator or provide a correct comparator. ``` -------------------------------- ### Implement Count Algorithm in C++ Source: https://github.com/electronicarts/eastl/blob/master/doc/Maintenance.md Demonstrates a C++ implementation of the count algorithm. It highlights a common pitfall with InputIterators, specifically the use of operator< which is not guaranteed for all iterator types. The correct comparison for InputIterators is first != last. ```cpp int count(InputIterator first, InputIterator last, const T& value) { int result = 0; for(; first < last; ++first){ if(*first == value) ++result; } return result; } ```