Skip to content

Ghoti-io/Util

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ghoti.io Util

Ghoti.io (pronounced fish) Util is a C++20 library containing the generic, non-OS-specific classes used by the Ghoti project. The classes it contains are as follows:

  • shared_string_view: is a dynamically-linked, C++ library.
  • ErrorOr a wrapper around C++17 std::variants to represent either an error or a valid response.

shared_string_view

This class extends the ideas of the std::string_view introduced in C++17. The drawback to the std::string_view is that it requires the programmer to ensure that the string remains allocated while the string_view itself is in use. For situations in which strings are passed around long-lived objects, this limitation makes the std::string_view useless and its performance advantages tantalizingly out-of-reach.

The features that make this library worth using are:

  • Memory safety via std::shared_ptr<>. As long as there is a Ghoti::shared_string_view referencing the string pointer, then the string is guaranteed to be present.
  • Thread safety. The Ghoti::shared_string_pointer object is thread safe. That is, it is write-only, and the reference counts used by the internal std::shared_ptr<std::string> are themselves thread safe.
  • Minimal but useful methods and operators. Immediately useful access methods are supported such as the [] operator and forward and reverse iterators (allowing for use in ranged-for expressions). For more versatile usage, any shared_string_view object can be cast to a std::string_view, which makes many additional methods available through the STL-provided methods.

Yes, the underscores break the CamelCase approach used throughout the library, in order to be consistent with std::string_view.

Example Code

Assume the file is called main.cpp. Also assume that the library has been compiled and installed per the instructions at the end of this README.md.

#include <ghoti.io/shared_string_view.hpp>
#include <string>

using namespace std;

Ghoti::shared_string_view get_ssv() {
  // Create a string in this scope.
  string s{"abc 123"};

  // By returning from the function, the original string will definitely no
  // longer be in scope.
  return Ghoti::shared_string_view{s};
}

/**
 */
int main() {
  // Get a Ghoti::shared_string_view whose source string is now out of scope.
  auto ssv get_ssv();

  // Iterate through a portion of the string.
  for (auto ch : ssv.substr(2,3)) {
    cout << ch << endl;
  }

  return 0;
}

Compile and run the example

g++ `pkg-config --libs --cflags ghoti.io-shared_string_view` main.cpp -o main

./main

Sample Output

c
 
1

Compiling the code

Download from Github

git clone https://github.com/Ghoti-io/Util.git

Compile and install the library (Ubuntu 22.04+)

make
sudo make install

Make and run the tests

make test

During development, it is helpful to recompile and run tests every time a file is saved. This can be done via:

make test-watch

Compiling documentation

Documentation can be created using Doxygen.

make docs

For PDF documentation:

make docs-pdf

Viewing the documentation

If using the WSL2 (on Windows), the documentation can be viewed using the following command:

wslview docs/html/index.html

or

wslview docs/latex/util-docs.pdf

About

A C++ library providing utility classes/libraries for the Ghoti project.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published