Member rename: generator/templates

This commit is contained in:
Corentin Wallez 2017-11-23 16:04:26 -05:00 committed by Corentin Wallez
parent d5d77af5b6
commit 42dbde1b65
5 changed files with 89 additions and 89 deletions

View File

@ -59,52 +59,52 @@ namespace nxt {
class ObjectBase { class ObjectBase {
public: public:
ObjectBase() = default; ObjectBase() = default;
ObjectBase(CType handle): handle(handle) { ObjectBase(CType handle): mHandle(handle) {
if (handle) Derived::NxtReference(handle); if (mHandle) Derived::NxtReference(mHandle);
} }
~ObjectBase() { ~ObjectBase() {
if (handle) Derived::NxtRelease(handle); if (mHandle) Derived::NxtRelease(mHandle);
} }
ObjectBase(ObjectBase const& other) = delete; ObjectBase(ObjectBase const& other) = delete;
Derived& operator=(ObjectBase const& other) = delete; Derived& operator=(ObjectBase const& other) = delete;
ObjectBase(ObjectBase&& other) { ObjectBase(ObjectBase&& other) {
handle = other.handle; mHandle = other.mHandle;
other.handle = 0; other.mHandle = 0;
} }
Derived& operator=(ObjectBase&& other) { Derived& operator=(ObjectBase&& other) {
if (&other == this) return static_cast<Derived&>(*this); if (&other == this) return static_cast<Derived&>(*this);
if (handle) Derived::NxtRelease(handle); if (mHandle) Derived::NxtRelease(mHandle);
handle = other.handle; mHandle = other.mHandle;
other.handle = 0; other.mHandle = 0;
return static_cast<Derived&>(*this); return static_cast<Derived&>(*this);
} }
explicit operator bool() const { explicit operator bool() const {
return handle != nullptr; return mHandle != nullptr;
} }
CType Get() const { CType Get() const {
return handle; return mHandle;
} }
CType Release() { CType Release() {
CType result = handle; CType result = mHandle;
handle = 0; mHandle = 0;
return result; return result;
} }
Derived Clone() const { Derived Clone() const {
return Derived(handle); return Derived(mHandle);
} }
static Derived Acquire(CType handle) { static Derived Acquire(CType handle) {
Derived result; Derived result;
result.handle = handle; result.mHandle = handle;
return result; return result;
} }
protected: protected:
CType handle = nullptr; CType mHandle = nullptr;
}; };
{% macro render_cpp_method_declaration(type, method) %} {% macro render_cpp_method_declaration(type, method) %}

View File

@ -53,7 +53,7 @@ void ProcTableAsClass::DeviceSetErrorCallback(nxtDevice self, nxtDeviceErrorCall
object->deviceErrorCallback = callback; object->deviceErrorCallback = callback;
object->userdata1 = userdata; object->userdata1 = userdata;
this->OnDeviceSetErrorCallback(self, callback, userdata); OnDeviceSetErrorCallback(self, callback, userdata);
} }
void ProcTableAsClass::BufferMapReadAsync(nxtBuffer self, uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata) { void ProcTableAsClass::BufferMapReadAsync(nxtBuffer self, uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata) {
@ -61,7 +61,7 @@ void ProcTableAsClass::BufferMapReadAsync(nxtBuffer self, uint32_t start, uint32
object->mapReadCallback = callback; object->mapReadCallback = callback;
object->userdata1 = userdata; object->userdata1 = userdata;
this->OnBufferMapReadAsyncCallback(self, start, size, callback, userdata); OnBufferMapReadAsyncCallback(self, start, size, callback, userdata);
} }
void ProcTableAsClass::CallDeviceErrorCallback(nxtDevice device, const char* message) { void ProcTableAsClass::CallDeviceErrorCallback(nxtDevice device, const char* message) {
@ -84,14 +84,14 @@ void ProcTableAsClass::CallMapReadCallback(nxtBuffer buffer, nxtBufferMapReadSta
object->userdata1 = userdata1; object->userdata1 = userdata1;
object->userdata2 = userdata2; object->userdata2 = userdata2;
this->OnBuilderSetErrorCallback(reinterpret_cast<nxtBufferBuilder>(self), callback, userdata1, userdata2); OnBuilderSetErrorCallback(reinterpret_cast<nxtBufferBuilder>(self), callback, userdata1, userdata2);
} }
{% endfor %} {% endfor %}
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
{{as_cType(type.name)}} ProcTableAsClass::GetNew{{type.name.CamelCase()}}() { {{as_cType(type.name)}} ProcTableAsClass::GetNew{{type.name.CamelCase()}}() {
objects.emplace_back(new Object); mObjects.emplace_back(new Object);
objects.back()->procs = this; mObjects.back()->procs = this;
return reinterpret_cast<{{as_cType(type.name)}}>(objects.back().get()); return reinterpret_cast<{{as_cType(type.name)}}>(mObjects.back().get());
} }
{% endfor %} {% endfor %}

View File

@ -80,7 +80,7 @@ class ProcTableAsClass {
private: private:
// Remembers the values returned by GetNew* so they can be freed. // Remembers the values returned by GetNew* so they can be freed.
std::vector<std::unique_ptr<Object>> objects; std::vector<std::unique_ptr<Object>> mObjects;
}; };
class MockProcTable : public ProcTableAsClass { class MockProcTable : public ProcTableAsClass {

View File

@ -124,65 +124,65 @@ namespace wire {
uint32_t serial; uint32_t serial;
}; };
ObjectAllocator(Device* device) : device(device) { ObjectAllocator(Device* device) : mDevice(device) {
// ID 0 is nullptr // ID 0 is nullptr
objects.emplace_back(nullptr, 0); mObjects.emplace_back(nullptr, 0);
} }
ObjectAndSerial* New() { ObjectAndSerial* New() {
uint32_t id = GetNewId(); uint32_t id = GetNewId();
T* result = new T(device, 1, id); T* result = new T(mDevice, 1, id);
auto object = std::unique_ptr<T>(result); auto object = std::unique_ptr<T>(result);
if (id >= objects.size()) { if (id >= mObjects.size()) {
ASSERT(id == objects.size()); ASSERT(id == mObjects.size());
objects.emplace_back(std::move(object), 0); mObjects.emplace_back(std::move(object), 0);
} else { } else {
ASSERT(objects[id].object == nullptr); ASSERT(mObjects[id].object == nullptr);
//* TODO(cwallez@chromium.org): investigate if overflows could cause bad things to happen //* TODO(cwallez@chromium.org): investigate if overflows could cause bad things to happen
objects[id].serial++; mObjects[id].serial++;
objects[id].object = std::move(object); mObjects[id].object = std::move(object);
} }
return &objects[id]; return &mObjects[id];
} }
void Free(T* obj) { void Free(T* obj) {
FreeId(obj->id); FreeId(obj->id);
objects[obj->id].object = nullptr; mObjects[obj->id].object = nullptr;
} }
T* GetObject(uint32_t id) { T* GetObject(uint32_t id) {
if (id >= objects.size()) { if (id >= mObjects.size()) {
return nullptr; return nullptr;
} }
return objects[id].object.get(); return mObjects[id].object.get();
} }
uint32_t GetSerial(uint32_t id) { uint32_t GetSerial(uint32_t id) {
if (id >= objects.size()) { if (id >= mObjects.size()) {
return 0; return 0;
} }
return objects[id].serial; return mObjects[id].serial;
} }
private: private:
uint32_t GetNewId() { uint32_t GetNewId() {
if (freeIds.empty()) { if (mFreeIds.empty()) {
return currentId ++; return mCurrentId ++;
} }
uint32_t id = freeIds.back(); uint32_t id = mFreeIds.back();
freeIds.pop_back(); mFreeIds.pop_back();
return id; return id;
} }
void FreeId(uint32_t id) { void FreeId(uint32_t id) {
freeIds.push_back(id); mFreeIds.push_back(id);
} }
// 0 is an ID reserved to represent nullptr // 0 is an ID reserved to represent nullptr
uint32_t currentId = 1; uint32_t mCurrentId = 1;
std::vector<uint32_t> freeIds; std::vector<uint32_t> mFreeIds;
std::vector<ObjectAndSerial> objects; std::vector<ObjectAndSerial> mObjects;
Device* device; Device* mDevice;
}; };
//* The client wire uses the global NXT device to store its global data such as the serializer //* The client wire uses the global NXT device to store its global data such as the serializer
@ -194,11 +194,11 @@ namespace wire {
{% for type in by_category["object"] if not type.name.canonical_case() == "device" %} {% for type in by_category["object"] if not type.name.canonical_case() == "device" %}
{{type.name.camelCase()}}(this), {{type.name.camelCase()}}(this),
{% endfor %} {% endfor %}
serializer(serializer) { mSerializer(serializer) {
} }
void* GetCmdSpace(size_t size) { void* GetCmdSpace(size_t size) {
return serializer->GetCmdSpace(size); return mSerializer->GetCmdSpace(size);
} }
{% for type in by_category["object"] if not type.name.canonical_case() == "device" %} {% for type in by_category["object"] if not type.name.canonical_case() == "device" %}
@ -215,7 +215,7 @@ namespace wire {
nxtCallbackUserdata errorUserdata; nxtCallbackUserdata errorUserdata;
private: private:
CommandSerializer* serializer = nullptr; CommandSerializer* mSerializer = nullptr;
}; };
//* Implementation of the client API functions. //* Implementation of the client API functions.
@ -404,7 +404,7 @@ namespace wire {
class Client : public CommandHandler { class Client : public CommandHandler {
public: public:
Client(Device* device) : device(device) { Client(Device* device) : mDevice(device) {
} }
const uint8_t* HandleCommands(const uint8_t* commands, size_t size) override { const uint8_t* HandleCommands(const uint8_t* commands, size_t size) override {
@ -441,7 +441,7 @@ namespace wire {
} }
private: private:
Device* device = nullptr; Device* mDevice = nullptr;
//* Helper function for the getting of the command data in command handlers. //* Helper function for the getting of the command data in command handlers.
//* Checks there is enough data left, updates the buffer / size and returns //* Checks there is enough data left, updates the buffer / size and returns
@ -475,7 +475,7 @@ namespace wire {
return false; return false;
} }
device->HandleError(cmd->GetMessage()); mDevice->HandleError(cmd->GetMessage());
return true; return true;
} }
@ -492,8 +492,8 @@ namespace wire {
return false; return false;
} }
auto* builtObject = device->{{type.built_type.name.camelCase()}}.GetObject(cmd->builtObjectId); auto* builtObject = mDevice->{{type.built_type.name.camelCase()}}.GetObject(cmd->builtObjectId);
uint32_t objectSerial = device->{{type.built_type.name.camelCase()}}.GetSerial(cmd->builtObjectId); uint32_t objectSerial = mDevice->{{type.built_type.name.camelCase()}}.GetSerial(cmd->builtObjectId);
//* The object might have been deleted or a new object created with the same ID. //* The object might have been deleted or a new object created with the same ID.
if (builtObject == nullptr || objectSerial != cmd->builtObjectSerial) { if (builtObject == nullptr || objectSerial != cmd->builtObjectSerial) {
@ -504,7 +504,7 @@ namespace wire {
// Unhandled builder errors are forwarded to the device // Unhandled builder errors are forwarded to the device
if (!called && cmd->status != NXT_BUILDER_ERROR_STATUS_SUCCESS && cmd->status != NXT_BUILDER_ERROR_STATUS_UNKNOWN) { if (!called && cmd->status != NXT_BUILDER_ERROR_STATUS_SUCCESS && cmd->status != NXT_BUILDER_ERROR_STATUS_UNKNOWN) {
builtObject->device->HandleError(("Unhandled builder error: " + std::string(cmd->GetMessage())).c_str()); mDevice->HandleError(("Unhandled builder error: " + std::string(cmd->GetMessage())).c_str());
} }
return true; return true;
@ -517,8 +517,8 @@ namespace wire {
return false; return false;
} }
auto* buffer = device->buffer.GetObject(cmd->bufferId); auto* buffer = mDevice->buffer.GetObject(cmd->bufferId);
uint32_t bufferSerial = device->buffer.GetSerial(cmd->bufferId); uint32_t bufferSerial = mDevice->buffer.GetSerial(cmd->bufferId);
//* The buffer might have been deleted or recreated so this isn't an error. //* The buffer might have been deleted or recreated so this isn't an error.
if (buffer == nullptr || bufferSerial != cmd->bufferSerial) { if (buffer == nullptr || bufferSerial != cmd->bufferSerial) {

View File

@ -67,17 +67,17 @@ namespace wire {
nullObject.handle = nullptr; nullObject.handle = nullptr;
nullObject.valid = true; nullObject.valid = true;
nullObject.allocated = true; nullObject.allocated = true;
known.push_back(nullObject); mKnown.push_back(nullObject);
} }
//* Get a backend objects for a given client ID. //* Get a backend objects for a given client ID.
//* Returns nullptr if the ID hasn't previously been allocated. //* Returns nullptr if the ID hasn't previously been allocated.
Data* Get(uint32_t id) { Data* Get(uint32_t id) {
if (id >= known.size()) { if (id >= mKnown.size()) {
return nullptr; return nullptr;
} }
Data* data = &known[id]; Data* data = &mKnown[id];
if (!data->allocated) { if (!data->allocated) {
return nullptr; return nullptr;
@ -90,7 +90,7 @@ namespace wire {
//* Returns nullptr if the ID is already allocated, or too far ahead. //* Returns nullptr if the ID is already allocated, or too far ahead.
//* Invalidates all the Data* //* Invalidates all the Data*
Data* Allocate(uint32_t id) { Data* Allocate(uint32_t id) {
if (id > known.size()) { if (id > mKnown.size()) {
return nullptr; return nullptr;
} }
@ -99,27 +99,27 @@ namespace wire {
data.valid = false; data.valid = false;
data.handle = nullptr; data.handle = nullptr;
if (id >= known.size()) { if (id >= mKnown.size()) {
known.push_back(data); mKnown.push_back(data);
return &known.back(); return &mKnown.back();
} }
if (known[id].allocated) { if (mKnown[id].allocated) {
return nullptr; return nullptr;
} }
known[id] = data; mKnown[id] = data;
return &known[id]; return &mKnown[id];
} }
//* Marks an ID as deallocated //* Marks an ID as deallocated
void Free(uint32_t id) { void Free(uint32_t id) {
ASSERT(id < known.size()); ASSERT(id < mKnown.size());
known[id].allocated = false; mKnown[id].allocated = false;
} }
private: private:
std::vector<Data> known; std::vector<Data> mKnown;
}; };
void ForwardDeviceErrorToServer(const char* message, nxtCallbackUserdata userdata); void ForwardDeviceErrorToServer(const char* message, nxtCallbackUserdata userdata);
@ -133,9 +133,9 @@ namespace wire {
class Server : public CommandHandler { class Server : public CommandHandler {
public: public:
Server(nxtDevice device, const nxtProcTable& procs, CommandSerializer* serializer) Server(nxtDevice device, const nxtProcTable& procs, CommandSerializer* serializer)
: procs(procs), serializer(serializer) { : mProcs(procs), mSerializer(serializer) {
//* The client-server knowledge is bootstrapped with device 1. //* The client-server knowledge is bootstrapped with device 1.
auto* deviceData = knownDevice.Allocate(1); auto* deviceData = mKnownDevice.Allocate(1);
deviceData->handle = device; deviceData->handle = device;
deviceData->valid = true; deviceData->valid = true;
@ -155,7 +155,7 @@ namespace wire {
{% for type in by_category["object"] if type.is_builder%} {% for type in by_category["object"] if type.is_builder%}
{% set Type = type.name.CamelCase() %} {% set Type = type.name.CamelCase() %}
void On{{Type}}Error(nxtBuilderErrorStatus status, const char* message, uint32_t id, uint32_t serial) { void On{{Type}}Error(nxtBuilderErrorStatus status, const char* message, uint32_t id, uint32_t serial) {
auto* builder = known{{Type}}.Get(id); auto* builder = mKnown{{Type}}.Get(id);
if (builder == nullptr || builder->serial != serial) { if (builder == nullptr || builder->serial != serial) {
return; return;
@ -206,7 +206,7 @@ namespace wire {
} }
const uint8_t* HandleCommands(const uint8_t* commands, size_t size) override { const uint8_t* HandleCommands(const uint8_t* commands, size_t size) override {
procs.deviceTick(knownDevice.Get(1)->handle); mProcs.deviceTick(mKnownDevice.Get(1)->handle);
while (size > sizeof(WireCmd)) { while (size > sizeof(WireCmd)) {
WireCmd cmdId = *reinterpret_cast<const WireCmd*>(commands); WireCmd cmdId = *reinterpret_cast<const WireCmd*>(commands);
@ -246,16 +246,16 @@ namespace wire {
} }
private: private:
nxtProcTable procs; nxtProcTable mProcs;
CommandSerializer* serializer = nullptr; CommandSerializer* mSerializer = nullptr;
void* GetCmdSpace(size_t size) { void* GetCmdSpace(size_t size) {
return serializer->GetCmdSpace(size); return mSerializer->GetCmdSpace(size);
} }
//* The list of known IDs for each object type. //* The list of known IDs for each object type.
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
KnownObjects<{{as_cType(type.name)}}> known{{type.name.CamelCase()}}; KnownObjects<{{as_cType(type.name)}}> mKnown{{type.name.CamelCase()}};
{% endfor %} {% endfor %}
//* Helper function for the getting of the command data in command handlers. //* Helper function for the getting of the command data in command handlers.
@ -300,7 +300,7 @@ namespace wire {
//* Unpack 'self' //* Unpack 'self'
{% set Type = type.name.CamelCase() %} {% set Type = type.name.CamelCase() %}
{{as_cType(type.name)}} self; {{as_cType(type.name)}} self;
auto* selfData = known{{Type}}.Get(cmd->self); auto* selfData = mKnown{{Type}}.Get(cmd->self);
{ {
if (selfData == nullptr) { if (selfData == nullptr) {
return false; return false;
@ -314,7 +314,7 @@ namespace wire {
{% set Type = arg.type.name.CamelCase() %} {% set Type = arg.type.name.CamelCase() %}
{{as_cType(arg.type.name)}} arg_{{as_varName(arg.name)}}; {{as_cType(arg.type.name)}} arg_{{as_varName(arg.name)}};
{ {
auto* data = known{{Type}}.Get(cmd->{{as_varName(arg.name)}}); auto* data = mKnown{{Type}}.Get(cmd->{{as_varName(arg.name)}});
if (data == nullptr) { if (data == nullptr) {
return false; return false;
} }
@ -340,7 +340,7 @@ namespace wire {
auto {{argName}}Ids = reinterpret_cast<const uint32_t*>(cmd->GetPtr_{{argName}}()); auto {{argName}}Ids = reinterpret_cast<const uint32_t*>(cmd->GetPtr_{{argName}}());
for (size_t i = 0; i < cmd->{{as_varName(arg.length.name)}}; i++) { for (size_t i = 0; i < cmd->{{as_varName(arg.length.name)}}; i++) {
{% set Type = arg.type.name.CamelCase() %} {% set Type = arg.type.name.CamelCase() %}
auto* data = known{{Type}}.Get({{argName}}Ids[i]); auto* data = mKnown{{Type}}.Get({{argName}}Ids[i]);
if (data == nullptr) { if (data == nullptr) {
return false; return false;
} }
@ -361,7 +361,7 @@ namespace wire {
{% set returns = return_type.name.canonical_case() != "void" %} {% set returns = return_type.name.canonical_case() != "void" %}
{% if returns %} {% if returns %}
{% set Type = method.return_type.name.CamelCase() %} {% set Type = method.return_type.name.CamelCase() %}
auto* resultData = known{{Type}}.Allocate(cmd->resultId); auto* resultData = mKnown{{Type}}.Allocate(cmd->resultId);
if (resultData == nullptr) { if (resultData == nullptr) {
return false; return false;
} }
@ -388,7 +388,7 @@ namespace wire {
{% if returns %} {% if returns %}
auto result ={{" "}} auto result ={{" "}}
{%- endif %} {%- endif %}
procs.{{as_varName(type.name, method.name)}}(self mProcs.{{as_varName(type.name, method.name)}}(self
{%- for arg in method.arguments -%} {%- for arg in method.arguments -%}
{%- if arg.annotation == "value" and arg.type.category != "object" -%} {%- if arg.annotation == "value" and arg.type.category != "object" -%}
, cmd->{{as_varName(arg.name)}} , cmd->{{as_varName(arg.name)}}
@ -408,7 +408,7 @@ namespace wire {
if (result != nullptr) { if (result != nullptr) {
uint64_t userdata1 = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(this)); uint64_t userdata1 = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(this));
uint64_t userdata2 = (uint64_t(resultData->serial) << uint64_t(32)) + cmd->resultId; uint64_t userdata2 = (uint64_t(resultData->serial) << uint64_t(32)) + cmd->resultId;
procs.{{as_varName(return_type.name, Name("set error callback"))}}(result, Forward{{return_type.name.CamelCase()}}ToClient, userdata1, userdata2); mProcs.{{as_varName(return_type.name, Name("set error callback"))}}(result, Forward{{return_type.name.CamelCase()}}ToClient, userdata1, userdata2);
} }
{% endif %} {% endif %}
{% endif %} {% endif %}
@ -431,16 +431,16 @@ namespace wire {
return false; return false;
} }
auto* data = known{{type.name.CamelCase()}}.Get(cmd->objectId); auto* data = mKnown{{type.name.CamelCase()}}.Get(cmd->objectId);
if (data == nullptr) { if (data == nullptr) {
return false; return false;
} }
if (data->valid) { if (data->valid) {
procs.{{as_varName(type.name, Name("release"))}}(data->handle); mProcs.{{as_varName(type.name, Name("release"))}}(data->handle);
} }
known{{type.name.CamelCase()}}.Free(cmd->objectId); mKnown{{type.name.CamelCase()}}.Free(cmd->objectId);
return true; return true;
} }
{% endfor %} {% endfor %}
@ -453,7 +453,7 @@ namespace wire {
return false; return false;
} }
auto* buffer = knownBuffer.Get(cmd->bufferId); auto* buffer = mKnownBuffer.Get(cmd->bufferId);
if (buffer == nullptr) { if (buffer == nullptr) {
return false; return false;
} }
@ -473,7 +473,7 @@ namespace wire {
return true; return true;
} }
procs.bufferMapReadAsync(buffer->handle, cmd->start, cmd->size, ForwardBufferMapReadAsync, userdata); mProcs.bufferMapReadAsync(buffer->handle, cmd->start, cmd->size, ForwardBufferMapReadAsync, userdata);
return true; return true;
} }