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

View File

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

View File

@ -80,7 +80,7 @@ class ProcTableAsClass {
private:
// 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 {

View File

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

View File

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