Skip to content

JohnGu9/async_runtime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Async Runtime

A framework write in C++11 (compatible with up to C++17 standard) that similar with Flutter but without build GUI. Just for backend software. With object management, software architecture management and async runtime management.

0.2.7-nullsafety

Welcome to nullsafety world! Now framework provide nullsafety feature. ref for non-null refs of object and option for nullable refs of object.

  1. ref, option and weakref for object management (implement base on std::shared_ptr, cross-platform, less buggy and nullsafety)

  2. widget and context for software architecture management. (Widget tree layout and lifecycle just similar with Flutter. If you familiar with Flutter, there is nothing new for you to learn)

  3. all async apis directly build upon context, init async tasks right after State::initState and dispose async resource when State::dispose (so that async work managed by framework). Ease code and less memory lack problem and less state-unknown conflict problem.

  4. compatible with Windows/Linux/macOS and more. compatible with C++17/14/11. CMake build rule. (Linux/macOS support C++20 but Windows MSVC has a bug on std::string constructor that cause compile error when enable C++20 flag)

Code

// StatelessWidget example
class MyWidget : public StatelessWidget {
    ref<Widget> build(ref<BuildContext> context) override 
    {
        // build your child widget without state
        ...
    }
};
// StatefulWidget example
class MyWidget : public StatefulWidget {
    ref<State<>> createState() override;
};

class _MyWidgetState : public State<MyWidget> {
    ref<Widget> build(ref<BuildContext> context) override 
    {
        // build your child widget with state
        ...
    }
};

inline ref<State<>> MyWidget::createState() { return Object::create<_MyWidgetState>(); }
// run your widget as root widget
// framework will automatically unfold the widget tree
runApp(Object::create<MyWidget>());

Widget tree

Tree layout


Tree lifecycle

  • as if both parent and child are StatefulWidget


State lifecycle

  • as if the widget is StatefulWidget


Async

This framework provides async task management. All async components showup in fundamental folder. The common async tasks already build in framework.

  1. Timer (Callback style async. Also see: Future::delay)
  2. File
  3. Http

Except Timer, all async components take advantage of Future and Stream to implement async.

If you not satisfy with build-in async api. This framework provides Completer for build Future and StreamController for build Stream to achieve async program.


All async api request one more argument than traditional async programming language. They always request ref<State<>> or ref<ThreadPool>, Because this framework is designed for multi-thread application. Async task must need to know which thread it should return to. For example:

class MyState : public State<MyWidget> {
    using super = State<MyWidget>;
    lateref<Timer> _timer;

    void initState() override 
    {
        super::initState();
        _timer = Timer::delay(self() /* timer always request a reference of current State to build a timer object */, 
            Duration(1000), []{ ... });
    }
};

Reference

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published