https://github.com/horance-liu/libval
https://github.com/horance-liu/libval
Last synced: 14 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/horance-liu/libval
- Owner: horance-liu
- License: apache-2.0
- Created: 2020-04-30T14:13:38.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-02T13:39:03.000Z (almost 5 years ago)
- Last Synced: 2025-04-15T01:16:49.149Z (14 days ago)
- Language: C++
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# libval: type-safe container for single value of any type.
## Build
### CMake
```bash
$ mkdir build && cd build
$ cmake ..
$ make && make test
```libval use xunit cctest:
```bash
$ git clone https://github.com/ccup/cctest.git
$ cd cctest && mkdir build && cd build
$ cmake ..
$ make && sudo make install
```### Bazel
```bash
$ bazel test //val/...
```Bazel will download xunit cctest.
## Usage
libval's Value object will contains one single value of any type, and then you can cast to the value for the original type.
```bash
TEST("uint -> uint") {
Value a((unsigned int) 2);
ASSERT_TRUE(value_castable(a));
ASSERT_EQ(2, value_cast(a));
}
```libval supports type conversions, and allows all of rules for type conversion in C/C++ programming langugae.
```cpp
TEST("char -> ushort") {
Value a((char) 2);ASSERT_TRUE(value_castable(a));
ASSERT_EQ(2, value_cast(a));
}
```libval will not castable if value is overflow or underflow.
```cpp
TEST("int(max_ushort + 1) -> ushort: overflow") {
Value a(std::numeric_limits::max() + 1);ASSERT_TRUE(!value_castable(a));
}
```libval supports the conversions between enum constants and integers.
```cpp
enum Enum {
Enum10 = 10
};
TEST("enum") {
Value e(Enum10);ASSERT_EQ(Enum10, value_cast(e));
}TEST("long -> enum") {
Value l((long) 10);ASSERT_EQ(Enum10, value_cast(l));
}TEST("enum -> long") {
Value l(Enum10);ASSERT_TRUE(value_castable(l));
ASSERT_EQ(10, value_cast(l));
}
```## TODO
### universal constructor.
```cpp
template
Value(T&&);template
explicit Value(std::in_place_type_t, Args&&... args);
```### universal operator=
```cpp
template
Value& operator=(T&&);
```### make_value factory
```cpp
template
Value make_value(Args&&... args);
```