www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - An Idea - New data stucture for D

I want to offer to introduce new data structures into D language.
It should help to write user interfaces more easly and faster.
Acording design goals 5 "Make doing things the right way easier than the wrong
These stuctures very simple, but I belive them could make a small revolution.

The idea is simple and based on MVC conception. Now I have to use it meta
language to compile it to language used.

The first type of structures let call it "model". It looks like simple stucture.
But the main difference from stucture in it's additionl behaviour it should

Let's look on implementation in pseudo language

NotifyDelegate - is a pointer to function without parameters

struct Notifier // can notify list of subsctibers
	Modify()    // mark modified
	Lock()      // lock notification (recursive)
	Unlock()    // unlock notification
	Subscribe(NotifyDelegate)   // subscribe listner and call it first time
	Unsubscribe(NotifyDelegate) // unsubscribe listner
	AddChild(Notifier)          // subscribe child(group) listner
	RemoveChild(Notifier)       // unsubscribe child

This class contains list of subscribers and subnotifiers. Lock/Unlock can delay
notification until lock_level=0 for all subscribers and subnotifiers.
This class is very flexible and allows to have very complex notification
More over then it call notify subscribers it goes to lock_level=-1. It means
no Lock/Unlock allowed. It means read only mode. This mode is very usefull
to avoiding bugs. Because views that use model in notify must have read only

struct Model
	Notifier rootNotifier;  -- notify any change of model and submodels if any
	Notifier scopeNotifier; -- notify any change of scope fields

Base interface it has only to notification source scopeNotifier to notify
change in model fields. And rootNotifier to notify any change of model.
It means if field chage or if submodel change it should notify all subscriber
about it (when lock_level drops to zero).

Typical declaration in speudocode:

struct Model1 : Model
	private type1 _FieldName1=default_value1;
	property type1 FieldName1 {
		get { return _FieldName1; }
		set { if (_FieldName1!=value) { _Name1=value; scopeNotifier.Modify(); }
	private typeN _NameN=default_valueN;
	property typeN NameN {
		get { return _NameN; }
		set { if (_NameN!=value) { _Name1=value; scopeNotifier.Modify(); }
	SubModel1 submodel1;
	SubModelN submodelN;
	ctor() { // constructor
	dtor() { // destructor
	assign_scope(Model1 src) {
	assign(Model1 src) { // this function allows to make full copy

Here a lot of dead-level code. I offer to introduce new struct type to have same
result with smaller line of code.

struct_model Model1
	type1 FieldName1=def_value1;
	typeN FieldNameN=def_valueN;
	SumModel1 submodel1
	SumModelN submodelN

All other method should be generated by compiler to have save functional.
So we can simple declare field and submodels and use it. Easy read, easy modify.

Typical use:

void change_model_fn1(Model m) { // simple change
void change_model_fn2(Model m) { // group change
	ModelLocker locker(m);
void change_model_fn3(Model m) {
	ModelLocker locker(m);
	change_model_fn2(m); // reuse

One one possible views for model:

struct SomeView : SomeVisualControl {
	Model m;
	void attach(Model model) {
		model.rootNotifier.Subscribe(onModelChange); // add and call it first time
		//or may be like this: model.OnChange+=onModelChange;
		//or model.OnLocalChange+=onSimpleChange;
	void dettach() {
		model.rootNotifier.Unsubscribe(onModelChage); // remove
		// model.OnChange-=onModelChange;
	void onModelChange() { // update view
		bool en=m.field1>0;

I beleave this greately improve ability of language for writing user interfaces.

Design notes:
1. This structure must not be thread safe. It must be simple.
2. Multithread use could be implemented by "assign" operation and model

If we have working thread that work with model. We can subsribe on model in this
thread and synchronously make a full copy of model to passit for other threads

	Model1 model;
	void main() {
		here work with model
	// addeditional
	Model1 model_copy;
	Mutex  mutex;
	void onModelChange() {
		ModelLocker locker(model_copy);
		lock(mutex) {
	void init() {
	void done() {
	public void getModel(Model m) {
		ModelLocker locker(m);
		lock(mutex) {

So we can time to time call getModel() from gui thread. This model would work as
dummy. And views can subscribe on it and show it state. Here no write access
required. But if we want to ineract with it. We need to use model controller.
Special class that can pass action we neeed into thread that work with this
So we need another construction "model_controller".

This construction is greately depends on serialize of function call.
If we have controller with function of special type that did not return result,
but only do action or initiate them. Indeed only this type of functions we need.

interface Controller
	void fn1(params);
	void fnN(params);

Additional structure to serialize call may look like this:

struct ControllerFnSerializer : Controller
	ThreadSafeQueue<Call> queue;
	void Execute(Controller ctrl,Call call);
	void Serialize(Call call);
	void ExecuteAll(Controller ctrl);

	void fn1(params) { Serialize(Call(fn1,params)); }
	void fnN(params) { Serialize(Call(fnN,params)); }

So there is a need in ability to declare such function call
It allows to pass parameters for inter-thread inter-process and delayed calls

struct ControllerFnSerializer : dispatcher<Controller> {};

or simply

disp_interface Controller
	void fn1(params);
	void fnN(params);

automatically generate interface and interface call dispatcher class.

So that is the idea.
I offer build in D language "sturct_model" and "disp_interface" or something
Syntax may vary but it should be brief, simple, clear and pictorial.

Thanks you for reading.
Nov 04 2010