[apps/shared] Move function store class to shared/ to be used by

sequence

Change-Id: I89380d59e3450e9c5af4bf05f520ed1a5205ba29
This commit is contained in:
Émilie Feral
2017-02-06 16:07:51 +01:00
parent 59553e69e0
commit 3164939c9d
5 changed files with 118 additions and 68 deletions

View File

@@ -1,8 +1,8 @@
#include "function_store.h"
extern "C" {
#include <assert.h>
}
#include <stddef.h>
}
#include <ion.h>
namespace Graph {
@@ -12,7 +12,7 @@ constexpr KDColor FunctionStore::k_defaultColors[k_numberOfDefaultColors];
constexpr const char * FunctionStore::k_functionNames[k_maxNumberOfFunctions];
FunctionStore::FunctionStore() :
m_numberOfFunctions(0)
Shared::FunctionStore()
{
addEmptyFunction();
}
@@ -29,35 +29,14 @@ Function * FunctionStore::functionAtIndex(int i) {
}
Function * FunctionStore::activeFunctionAtIndex(int i) {
assert(i>=0 && i<m_numberOfFunctions);
int index = 0;
for (int k = 0; k < m_numberOfFunctions; k++) {
if (m_functions[k].isActive() && m_functions[k].layout() != nullptr) {
if (i == index) {
return &m_functions[k];
}
index++;
}
}
assert(false);
return nullptr;
return (Function *)Shared::FunctionStore::activeFunctionAtIndex(i);
}
Function * FunctionStore::definedFunctionAtIndex(int i) {
assert(i>=0 && i<m_numberOfFunctions);
int index = 0;
for (int k = 0; k < m_numberOfFunctions; k++) {
if (m_functions[k].layout() != nullptr) {
if (i == index) {
return &m_functions[k];
}
index++;
}
}
assert(false);
return nullptr;
return (Function *)Shared::FunctionStore::definedFunctionAtIndex(i);
}
Function * FunctionStore::addEmptyFunction() {
assert(m_numberOfFunctions < k_maxNumberOfFunctions);
const char * name = firstAvailableName();
@@ -69,7 +48,7 @@ Function * FunctionStore::addEmptyFunction() {
return result;
}
void FunctionStore::removeFunction(Function * f) {
void FunctionStore::removeFunction(Shared::Function * f) {
int i = 0;
while (&m_functions[i] != f && i < m_numberOfFunctions) {
i++;
@@ -81,30 +60,6 @@ void FunctionStore::removeFunction(Function * f) {
}
}
int FunctionStore::numberOfFunctions() {
return m_numberOfFunctions;
}
int FunctionStore::numberOfActiveFunctions() {
int result = 0;
for (int i = 0; i < m_numberOfFunctions; i++) {
if (m_functions[i].layout() != nullptr && m_functions[i].isActive()) {
result++;
}
}
return result;
}
int FunctionStore::numberOfDefinedFunctions() {
int result = 0;
for (int i = 0; i < m_numberOfFunctions; i++) {
if (m_functions[i].layout() != nullptr) {
result++;
}
}
return result;
}
const char * FunctionStore::firstAvailableName() {
for (int k = 0; k < k_maxNumberOfFunctions; k++) {
int j = 0;

View File

@@ -2,29 +2,24 @@
#define GRAPH_FUNCTION_STORE_H
#include "function.h"
#include "../shared/function_store.h"
#include <stdint.h>
namespace Graph {
/* FunctionStore is a dumb class.
* Its only job is to store functions and to give them a color. */
class FunctionStore {
class FunctionStore : public Shared::FunctionStore {
public:
FunctionStore();
uint32_t storeChecksum();
Function * functionAtIndex(int i);
Function * activeFunctionAtIndex(int i);
Function * definedFunctionAtIndex(int i);
Function * addEmptyFunction();
void removeFunction(Function * f);
int numberOfFunctions();
// Functions can be undefined when they have a color and a name but no content
int numberOfDefinedFunctions();
// An active function must be defined to be counted
int numberOfActiveFunctions();
uint32_t storeChecksum() override;
Function * functionAtIndex(int i) override;
Function * activeFunctionAtIndex(int i) override;
Function * definedFunctionAtIndex(int i) override;
Function * addEmptyFunction() override;
void removeFunction(Shared::Function * f) override;
static constexpr int k_maxNumberOfFunctions = 8;
private:
const char * firstAvailableName();
const KDColor firstAvailableColor();
const char * firstAvailableName() override;
const KDColor firstAvailableColor() override;
static constexpr int k_numberOfDefaultColors = 8;
static constexpr KDColor k_defaultColors[k_numberOfDefaultColors] = {
KDColor::RGB24(0xbe2727), KDColor::RGB24(0x3e6f3c), KDColor::RGB24(0x656975), KDColor::RGB24(0x9ec580),
@@ -33,7 +28,6 @@ private:
static constexpr const char * k_functionNames[k_maxNumberOfFunctions] = {
"f", "g", "h", "p", "q", "r", "s", "t"
};
int m_numberOfFunctions;
Function m_functions[k_maxNumberOfFunctions];
};

View File

@@ -9,6 +9,7 @@ app_objs += $(addprefix apps/shared/,\
float_pair_store.o\
float_parameter_controller.o\
function.o\
function_store.o\
interactive_curve_view_controller.o\
interactive_curve_view_range.o\
memoized_curve_view_range.o\

View File

@@ -0,0 +1,65 @@
#include "function_store.h"
#include <assert.h>
namespace Shared {
FunctionStore::FunctionStore() :
m_numberOfFunctions(0)
{
}
Function * FunctionStore::activeFunctionAtIndex(int i) {
assert(i>=0 && i<m_numberOfFunctions);
int index = 0;
for (int k = 0; k < m_numberOfFunctions; k++) {
if (functionAtIndex(k)->isActive() && functionAtIndex(k)->layout() != nullptr) {
if (i == index) {
return functionAtIndex(k);
}
index++;
}
}
assert(false);
return nullptr;
}
Function * FunctionStore::definedFunctionAtIndex(int i) {
assert(i>=0 && i<m_numberOfFunctions);
int index = 0;
for (int k = 0; k < m_numberOfFunctions; k++) {
if (functionAtIndex(k)->layout() != nullptr) {
if (i == index) {
return functionAtIndex(k);
}
index++;
}
}
assert(false);
return nullptr;
}
int FunctionStore::numberOfFunctions() {
return m_numberOfFunctions;
}
int FunctionStore::numberOfActiveFunctions() {
int result = 0;
for (int i = 0; i < m_numberOfFunctions; i++) {
if (functionAtIndex(i)->layout() != nullptr && functionAtIndex(i)->isActive()) {
result++;
}
}
return result;
}
int FunctionStore::numberOfDefinedFunctions() {
int result = 0;
for (int i = 0; i < m_numberOfFunctions; i++) {
if (functionAtIndex(i)->layout() != nullptr) {
result++;
}
}
return result;
}
}

View File

@@ -0,0 +1,35 @@
#ifndef SHARED_FUNCTION_STORE_H
#define SHARED_FUNCTION_STORE_H
#include "function.h"
#include <stdint.h>
namespace Shared {
/* FunctionStore is a dumb class.
* Its only job is to store functions and to give them a color. */
class FunctionStore {
public:
FunctionStore();
virtual uint32_t storeChecksum() = 0;
virtual Function * functionAtIndex(int i) = 0;
virtual Function * activeFunctionAtIndex(int i);
virtual Function * definedFunctionAtIndex(int i);
virtual Function * addEmptyFunction() = 0;
virtual void removeFunction(Function * f) = 0;
int numberOfFunctions();
// Functions can be undefined when they have a color and a name but no content
int numberOfDefinedFunctions();
// An active function must be defined to be counted
int numberOfActiveFunctions();
protected:
int m_numberOfFunctions;
private:
virtual const char * firstAvailableName() = 0;
virtual const KDColor firstAvailableColor() = 0;
};
}
#endif