Skip to content

Commit

Permalink
Lots of base work
Browse files Browse the repository at this point in the history
  • Loading branch information
Tectu committed Dec 21, 2020
1 parent 1514925 commit cada3e6
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 43 deletions.
65 changes: 29 additions & 36 deletions content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,32 @@ title: Introduction
type: docs
---

# Acerbo datus maxime

{{< columns >}}
## Astris ipse furtiva

Est in vagis et Pittheus tu arge accipiter regia iram vocatur nurus. Omnes ut
olivae sensit **arma sorori** deducit, inesset **crudus**, ego vetuere aliis,
modo arsit? Utinam rapta fiducia valuere litora _adicit cursu_, ad facies

<--->

## Suis quot vota

Ea _furtique_ risere fratres edidit terrae magis. Colla tam mihi tenebat:
miseram excita suadent es pecudes iam. Concilio _quam_ velatus posset ait quod
nunc! Fragosis suae dextra geruntur functus vulgata.
{{< /columns >}}


## Tempora nisi nunc

Lorem **markdownum** emicat gestu. Cannis sol pressit ducta. **Est** Idaei,
tremens ausim se tutaeque, illi ulnis hausit, sed, lumina cutem. Quae avis
sequens!

var panel = ram_design;
if (backup + system) {
file.readPoint = network_native;
sidebar_engine_device(cell_tftp_raster,
dual_login_paper.adf_vci.application_reader_design(
graphicsNvramCdma, lpi_footer_snmp, integer_model));
}

## Locis suis novi cum suoque decidit eadem

Idmoniae ripis, at aves, ali missa adest, ut _et autem_, et ab?
# CppProperties
Welcome to the official documentation of the CppProperties library.
Let's start off with a couple of useful links:
- [Library GitHub Repository](https://github.com/tectu/cppproperties)

# Overview
This is a C++20 library providing a property system to client classes.

## Features
The library is built with the following aspects in mind:
- Modern C++
- Easy to use
- Providing "raw access" to the properties just as if they were regular class members.
- Easy registration of custom property types.
- Easy integration of optional (de)serialization (XML & JSON already optionally built-in).
- Observer interface for property change notifications.
- Support for linked properties (properties in a base class not implementing this library).

## Notes
A couple of things to be aware of when using this library:
- Requires a C++20 capable compiler
- Properties are stored on the heap
- The memory layout of `struct { MAKE_PROPERTY(a, int) };` is not the same as `struct { int a; };`
- Property change notification observer callbacks are invoked by which ever thread modified the property value.

## License
This library is MIT licenses.

It uses [tinyxml2](https://github.com/leethomason/tinyxml2) for XML (de)serialization. The tinyxml2 library itself is zlib licensed.
40 changes: 40 additions & 0 deletions content/docs/deserialization/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
weight: 4
bookCollapseSection: true
title: "Serialization"
---

# Serialization
Client classes may be (de)serialized without any further intervention by the user. The following class:

```cpp
struct shape :
tct::cppproperties::properties
{
MAKE_PROPERTY(name, std::string);
MAKE_PROPERTY(x, float);
MAKE_PROPERTY(y, float);
};
```
will be automatically serialized:
{{< tabs "serialization_example_01" >}}
{{< tab "XML" >}}
```xml
<properties>
<name>My Shape</name>
<x>12.98</x>
<y>-38.01</y>
</properties>
```
{{< /tab >}}
{{< tab "JSON" >}}
```json
{
"name": "My Shape",
"x": 12.98,
"y": -38.01
}
```
{{< /tab >}}
{{< /tabs >}}
6 changes: 4 additions & 2 deletions content/docs/properties/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
weight: 2
weight: 3
bookCollapseSection: true
---

Expand All @@ -17,9 +17,11 @@ class shape :
}
```
{{< hint info >}}
Note that a property type needs to be registered before it can be used. See [types](../types/_index.md) for more information.
{{< /hint >}}
The following types of properties are available:
- **[Raw properties](./raw_properties.md)**: This is the most common form of a property. Use this if you want to use a regular member variable as a property.
- **[Linked properties](./linked_properties.md)**: Linked properties are used when public or protected member variables of a base class should become properties.
- **[Linked property functions](./linked_property_functions.md)**: Allows to use private members variables of a base class as properties.
72 changes: 72 additions & 0 deletions content/docs/properties/raw_properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ weight: 1
# Raw Properties
Raw properties are the most basic form of properties. They are mimicing regular member variables.


## Creation
A raw property can be created by using the `MAKE_PROPERTY()` macro:

```cpp
Expand All @@ -14,3 +16,73 @@ struct shape :
MAKE_PROPERTY(y, float);
};
```
## Access
Raw properties may be used as if they were normal class members:
```cpp
shape s;
s.x = 24;
s.y = s.x * 2;
std::cout << "x = " << s.x << "\n";
std::cout << "y = " << s.y << "\n";
```

Furthermore, `get_property()` and `set_property()` may be used to access properties:
```cpp
shape s;

s.set_property<float>("x", 0.27f);

std::cout << "x = " << s.get_property<float>("x") << "\n";
```

## Notifications
Raw properties provide the ability to register zero or more observers which will be notified when the property value changed. Notifications are implemented through simple callbacks.

The following example registers `this` as an observer for the `x` and `y` properties:
```cpp
struct shape :
tct::cppproperties::properties
{
MAKE_PROPERTY(x, int);
MAKE_PROPERTY(y, int);

shape()
{
// Register observers
x.register_observer( [](){ std::cout << "x changed!\n"; } );
y.register_observer( [](){ std::cout << "y changed!\n"; } );
}
};
```
Here we're registering an "external" observer:
```cpp
struct shape :
tct::cppproperties::properties
{
MAKE_PROPERTY(x, int);
MAKE_PROPERTY(y, int);
};
void shape_position_changed(const shape& s)
{
std::cout << "Shape position changed to: " << s.x << ", " << s.y << "\n";
}
int main()
{
shape s;
// Register another observer
s.x.register_observer( [&s](){ shape_position_changed(s); } );
s.y.register_observer( [&s](){ shape_position_changed(s); } );
// Set some property values
s.x = 24;
s.y = 48;
return 0;
}
```
2 changes: 1 addition & 1 deletion content/docs/types/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
weight: 1
weight: 2
bookCollapseSection: true
---

Expand Down
10 changes: 6 additions & 4 deletions content/docs/types/custom_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ title: "Custom types"
---

# Custom types
Any type can be registered as a property type by specializing the existing `property<T>` template.
Any type can be registered as a property type by specializing the existing `property<T>` template. This class template provides a member of type `T` named `data`.

Asuming a custom type `color`:

```cpp
// Our custom type
struct color
{
int red, green, blue;

std::string to_string() const { /* ... */ }
void from_string(const std::string& str) { /* ... */ }
}
```
Expand All @@ -32,13 +34,13 @@ struct tct::cppproperties::property<color> :
// Register to_string()
this->to_string = [this]()
{
return this->data;
return data.to_string();
};
// Register from_string()
this->from_string = [this](const std::string& str)
{
*this = str;
return data.to_string();
};
}
};
Expand Down

0 comments on commit cada3e6

Please sign in to comment.