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++17std::variant
s to represent either an error or a valid response.
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 aGhoti::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 internalstd::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, anyshared_string_view
object can be cast to astd::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
.
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;
}
g++ `pkg-config --libs --cflags ghoti.io-shared_string_view` main.cpp -o main
./main
c
1
git clone https://github.com/Ghoti-io/Util.git
make
sudo make install
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
Documentation can be created using Doxygen.
make docs
For PDF documentation:
make docs-pdf
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