SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches

Implements the "Model" leg of a Model-View-Controller architecture. More...

#include <rw/model.h>

Public Member Functions

 RWModel ()
 
 RWModel (const RWModel &m)
 
 RWModel (RWModel &&m)
 
void addDependent (RWModelClient *m)
 
virtual void changed (void *d=0)
 
const RWOrdereddependents () const
 
RWModeloperator= (const RWModel &m)
 
RWModeloperator= (RWModel &&m)
 
void removeDependent (RWModelClient *m)
 
void swap (RWModel &m)
 

Detailed Description

This abstract base class has been designed to implement the "Model" leg of a Model-View-Controller architecture. A companion class, RWModelClient, supplies the "View" leg.

It maintains a list of dependent RWModelClient objects. When member function changed(void*) is called, the list of dependents is traversed, calling RWModelClient::updateFrom(RWModel*, void*) for each one, with itself as the first argument. Subclasses of RWModelClient should be prepared to accept such a call.

Synopsis
#include <rw/model.h>
(abstract base class)
Persistence
None
Example
#include <rw/model.h>
#include <iostream>
class Dial : public RWModelClient {
int dialNumber;
public:
explicit Dial(int dialNumber);
virtual void updateFrom(RWModel* m, void* d);
};
class Thermostat : public RWModel {
double setting;
public:
Thermostat(Dial* d) {
addDependent(d);
setting = 0;
}
double temperature() const { return setting; }
void setTemperature(double t) {
setting = t;
changed();
}
};
void Dial::updateFrom(RWModel* m, void*) {
Thermostat* t = (Thermostat*)m;
double temp = t->temperature();
// Redraw graphic.
std::cout << "Dial #" << dialNumber << " says " << temp << std::endl;
}
Dial::Dial(int num) : dialNumber(num) {}
int main() {
Dial one(1);
Dial two(2);
Thermostat therm(&one);
therm.setTemperature(77);
therm.setTemperature(-4);
therm.addDependent(&two);
therm.setTemperature(47);
return 0;
}
Implements the "View" leg of a Model-View-Controller architecture.
Definition model.h:203
Implements the "Model" leg of a Model-View-Controller architecture.
Definition model.h:110

Program output:

Dial #1 says 77
Dial #1 says -4
Dial #1 says 47
Dial #2 says 47

Constructor & Destructor Documentation

◆ RWModel() [1/3]

RWModel::RWModel ( )

Sets up the internal ordered list of dependents when called by the specializing class.

◆ RWModel() [2/3]

RWModel::RWModel ( const RWModel & m)

Copy constructor. The constructed instance gets a copy of the dependents list from m.

◆ RWModel() [3/3]

RWModel::RWModel ( RWModel && m)

Move constructor. The constructed instance takes ownership of the dependents list owned by m.

Condition:
This method is available only on platforms with rvalue reference support.

Member Function Documentation

◆ addDependent()

void RWModel::addDependent ( RWModelClient * m)

Adds the object pointed to by m to the list of dependents of self.

◆ changed()

virtual void RWModel::changed ( void * d = 0)
virtual

Traverse the internal list of dependents, calling member function RWModelClient::updateFrom(RWModel*, void*) for each one, with self as the first argument and d as the second argument.

◆ dependents()

const RWOrdered * RWModel::dependents ( ) const
inline

Allows a peek at the dependent list.

◆ operator=() [1/2]

RWModel & RWModel::operator= ( const RWModel & m)

Assignment operator. Self gets a copy of the dependents list from m.

◆ operator=() [2/2]

RWModel & RWModel::operator= ( RWModel && m)

Move assignment. Self takes ownership of the dependents list owned by m.

Condition:
This method is available only on platforms with rvalue reference support.

◆ removeDependent()

void RWModel::removeDependent ( RWModelClient * m)

Removes the object pointed to by m from the list of dependents of self.

◆ swap()

void RWModel::swap ( RWModel & m)

Swaps the list of dependents in self with that of m.

Copyright © 2024 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.