An open API service indexing awesome lists of open source software.

https://github.com/horance-liu/libval


https://github.com/horance-liu/libval

Last synced: 14 days ago
JSON representation

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);
```