Skip to content

Commit

Permalink
Merge pull request #7 from vsg-dev/serialization
Browse files Browse the repository at this point in the history
Added basic serialization support
  • Loading branch information
vsg-dev authored Nov 19, 2018
2 parents 2ff0bbe + f18dbce commit 5cae707
Show file tree
Hide file tree
Showing 51 changed files with 1,800 additions and 84 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if(NOT CMAKE_BUILD_TYPE)
endif(NOT CMAKE_BUILD_TYPE)

if(CMAKE_COMPILER_IS_GNUCXX)
set(VSG_WARNING_FLAGS -Wall -Wparentheses -Wno-long-long -Wno-import -Wreturn-type -Wmissing-braces -Wunknown-pragmas -Wmaybe-uninitialized -Wshadow -Wunused -Wno-misleading-indentation -Wextra -pedantic)
set(VSG_WARNING_FLAGS -Wall -Wparentheses -Wno-long-long -Wno-import -Wreturn-type -Wmissing-braces -Wunknown-pragmas -Wmaybe-uninitialized -Wshadow -Wunused -Wno-misleading-indentation -Wextra) # removed -pedantic due to macro(); warnings
endif()
set(VSG_WARNING_FLAGS ${VSG_WARNING_FLAGS} CACHE STRING "Compiler flags to use." FORCE)
add_compile_options(${VSG_WARNING_FLAGS})
Expand Down
2 changes: 2 additions & 0 deletions build/build_all_h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ macro(BUILD_ALL_H)
file(GLOB MATHS_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/include include/vsg/maths/*.h )
file(GLOB NODES_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/include include/vsg/nodes/*.h )
file(GLOB TRAVERSAL_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/include include/vsg/traversals/*.h )
file(GLOB IO_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/include include/vsg/io/*.h )
file(GLOB UTILS_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/include include/vsg/utils/*.h )
file(GLOB VIEWER_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/include include/vsg/viewer/*.h )
file(GLOB VK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/include include/vsg/vk/*.h )
Expand All @@ -27,6 +28,7 @@ macro(BUILD_ALL_H)
APPEND_INCLUDES(ALL_H_CONTENTS TRAVERSAL_HEADERS "// Traversal header files\n")
APPEND_INCLUDES(ALL_H_CONTENTS VIEWER_HEADERS "// Viewer header files\n")
APPEND_INCLUDES(ALL_H_CONTENTS VK_HEADERS "// Vulkan related header files\n")
APPEND_INCLUDES(ALL_H_CONTENTS IO_HEADERS "// Input/Output header files\n")
APPEND_INCLUDES(ALL_H_CONTENTS UTILS_HEADERS "// Utiltiy header files\n")
APPEND_INCLUDES(ALL_H_CONTENTS INTROSPECTION_HEADERS "// Introspection header files\n")

Expand Down
6 changes: 4 additions & 2 deletions include/vsg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ The headers that provide the library classes and definitions are organized in su

* [include/vsg/maths](maths/) - GLSL style maths classes

* [include/vsg/nodes](nodes/) - Graph node classes
* [include/vsg/nodes](nodes/) - Graph node classes

* [include/vsg/traversals](traversals/) - Graph traversals

* [include/vsg/vk](vk/) - Vulkan integration classes

* [include/vsg/viewer](viewer/) - Viewer/Windowing classes

* [include/vsg/io](io/) - File system, stream and native file format support

* [include/vsg/utils](utils/) - Utility classes/template functions

* [include/vsg/introspection](introspection) - introspection/reflection classes/functions
* [include/vsg/introspection](introspection) - introspection/reflection classes/functions
13 changes: 11 additions & 2 deletions include/vsg/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
// Core header files
#include <vsg/core/Allocator.h>
#include <vsg/core/Array.h>
#include <vsg/core/Array2D.h>
#include <vsg/core/Auxiliary.h>
#include <vsg/core/ConstVisitor.h>
#include <vsg/core/Data.h>
Expand All @@ -27,6 +28,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/core/Visitor.h>
#include <vsg/core/observer_ptr.h>
#include <vsg/core/ref_ptr.h>
#include <vsg/core/type_name.h>

// Maths header files
#include <vsg/maths/mat4.h>
Expand Down Expand Up @@ -88,10 +90,17 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/vk/Surface.h>
#include <vsg/vk/Swapchain.h>

// Input/Output header files
#include <vsg/io/AsciiInput.h>
#include <vsg/io/AsciiOutput.h>
#include <vsg/io/FileSystem.h>
#include <vsg/io/Input.h>
#include <vsg/io/ObjectFactory.h>
#include <vsg/io/Output.h>
#include <vsg/io/stream.h>

// Utiltiy header files
#include <vsg/utils/CommandLine.h>
#include <vsg/utils/FileSystem.h>
#include <vsg/utils/stream.h>

// Introspection header files
#include <vsg/introspection/c_interface.h>
2 changes: 1 addition & 1 deletion include/vsg/core/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/core/Visitor.h>
#include <vsg/core/ref_ptr.h>

#include <vsg/utils/stream.h>
#include <vsg/io/stream.h>

#include <vsg/traversals/CullTraversal.h>
#include <vsg/traversals/DispatchTraversal.h>
Expand Down
75 changes: 59 additions & 16 deletions include/vsg/core/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/maths/vec3.h>
#include <vsg/maths/vec4.h>

#include <vsg/io/Input.h>
#include <vsg/io/Output.h>

#define VSG_array(N, T) \
using N = Array<T>; \
template<> \
constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }

namespace vsg
{
template<typename T>
Expand Down Expand Up @@ -46,12 +54,46 @@ namespace vsg
_size(numElements),
_data(new value_type[numElements]) {}

std::size_t sizeofObject() const noexcept override { return sizeof(Data); }
std::size_t sizeofObject() const noexcept override { return sizeof(Array); }

// implementation provided by Visitor.h
void accept(Visitor& visitor) override;
void accept(ConstVisitor& visitor) const override;

const char* className() const noexcept override { return type_name<Array>(); }

void read(Input& input) override
{
Data::read(input);
size_t size = input.readValue<uint32_t>("Size");
if (input.matchPropertyName("Data"))
{
if (_data) // if data already may be able to reuse it
{
if (_size != size) // if existing data is a different size delete old, and create new
{
delete[] _data;
_size = size;
_data = new value_type[size];
}
}
else // allocate space for data
{
_size = size;
_data = new value_type[size];
}
input.read(_size, _data);
}
}

void write(Output& output) const override
{
Data::write(output);
output.writeValue<uint32_t>("Size", _size);
output.writePropertyName("Data");
output.write(_size, _data);
}

std::size_t size() const { return _size; }
bool empty() const { return _size == 0; }

Expand Down Expand Up @@ -138,19 +180,20 @@ namespace vsg
value_type* _data;
};

using ubyteArray = Array<std::uint8_t>;
using ushortArray = Array<std::uint16_t>;
using uintArray = Array<std::uint32_t>;
using floatArray = Array<float>;
using doubleArray = Array<double>;

using vec2Array = Array<vec2>;
using vec3Array = Array<vec3>;
using vec4Array = Array<vec4>;
using mat4Array = Array<mat4>;

using dvec2Array = Array<dvec2>;
using dvec3Array = Array<dvec3>;
using dvec4Array = Array<dvec4>;
using dmat4Array = Array<dmat4>;
VSG_array(ubyteArray, std::uint8_t);
VSG_array(ushortArray, std::uint16_t);
VSG_array(uintArray, std::uint32_t);
VSG_array(floatArray, float);
VSG_array(doubleArray, double);

VSG_array(vec2Array, vec2);
VSG_array(vec3Array, vec3);
VSG_array(vec4Array, vec4);
VSG_array(mat4Array, mat4);

VSG_array(dvec2Array, dvec2);
VSG_array(dvec3Array, dvec3);
VSG_array(dvec4Array, dvec4);
VSG_array(dmat4Array, dmat4);

} // namespace vsg
188 changes: 188 additions & 0 deletions include/vsg/core/Array2D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#pragma once

/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */

#include <vsg/core/Data.h>

#include <vsg/maths/mat4.h>
#include <vsg/maths/vec2.h>
#include <vsg/maths/vec3.h>
#include <vsg/maths/vec4.h>

#include <vsg/io/Input.h>
#include <vsg/io/Output.h>

#define VSG_array2D(N, T) \
using N = Array2D<T>; \
template<> \
constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }

namespace vsg
{
template<typename T>
class Array2D : public Data
{
public:
using value_type = T;
using iterator = value_type*;
using const_iterator = const value_type*;

Array2D() :
_width(0),
_height(),
_data(nullptr) {}
Array2D(std::size_t width, std::size_t height, value_type* data) :
_width(width),
_height(height),
_data(data) {}
explicit Array2D(std::size_t width, std::size_t height) :
_width(width),
_height(height),
_data(new value_type[width * height]) {}

std::size_t sizeofObject() const noexcept override { return sizeof(Array2D); }

// implementation provided by Visitor.h
void accept(Visitor& visitor) override;
void accept(ConstVisitor& visitor) const override;

const char* className() const noexcept override { return type_name<Array2D>(); }

void read(Input& input) override
{
Data::read(input);
size_t width = input.readValue<uint32_t>("Width");
size_t height = input.readValue<uint32_t>("Height");
size_t new_size = width * height;
if (input.matchPropertyName("Data"))
{
if (_data) // if data already may be able to reuse it
{
size_t original_size = width * height;
if (original_size != new_size) // if existing data is a different size delete old, and create new
{
delete[] _data;
_data = new value_type[new_size];
}
}
else // allocate space for data
{
_data = new value_type[new_size];
}
_width = width;
_height = height;
input.read(new_size, _data);
}
}

void write(Output& output) const override
{
Data::write(output);
output.writeValue<uint32_t>("Width", _width);
output.writeValue<uint32_t>("Height", _height);
output.writePropertyName("Data");
output.write(_width * _height, _data);
}

std::size_t size() const { return _width * _height; }
bool empty() const { return _width == 0 && _height == 0; }

void clear()
{
_width = 0;
_height = 0;
if (_data) { delete[] _data; }
_data = nullptr;
}

void assign(std::size_t width, std::size_t height, value_type* data)
{
if (_data) delete[] _data;

_width = width;
_height = height;
_data = data;
}

// release the data so that owneership can be passed on, the local data pointer and size is set to 0 and destruction of Array will no result in the data being deleted.
void* dataRelease() override
{
void* tmp = _data;
_data = nullptr;
_width = 0;
_height = 0;
return tmp;
}

std::size_t valueSize() const override { return sizeof(value_type); }
std::size_t valueCount() const override { return _width * _height; }

std::size_t dataSize() const override { return (_width * _height) * sizeof(value_type); }
void* dataPointer() override { return _data; }
const void* dataPointer() const override { return _data; }

size_t width() const { return _width; }
size_t height() const { return _height; }

value_type* data() { return _data; }
const value_type* data() const { return _data; }

value_type& operator[](std::size_t i) { return _data[i]; }
const value_type& operator[](std::size_t i) const { return _data[i]; }

value_type& at(std::size_t i) { return _data[i]; }
const value_type& at(std::size_t i) const { return _data[i]; }

value_type& operator()(std::size_t i, std::size_t j) { return _data[i + j * _width]; }
const value_type& operator()(std::size_t i, std::size_t j) const { return _data[i + j * _width]; }

value_type& at(std::size_t i, std::size_t j) { return _data[i + j * _width]; }
const value_type& at(std::size_t i, std::size_t j) const { return _data[i + j * _width]; }

void set(std::size_t i, const value_type& v) { _data[i] = v; }
void set(std::size_t i, std::size_t j, const value_type& v) { _data[i + j * _width] = v; }

iterator begin() { return _data; }
const_iterator begin() const { return _data; }

iterator end() { return _data + (_width * _height); }
const_iterator end() const { return _data + (_width * _height); }

protected:
virtual ~Array2D()
{
if (_data) delete[] _data;
}

private:
std::size_t _width;
std::size_t _height;
value_type* _data;
};

VSG_array2D(ubyteArray2D, std::uint8_t);
VSG_array2D(ushortArray2D, std::uint16_t);
VSG_array2D(uintArray2D, std::uint32_t);
VSG_array2D(floatArray2D, float);
VSG_array2D(doubleArray2D, double);

VSG_array2D(vec2Array2D, vec2);
VSG_array2D(vec3Array2D, vec3);
VSG_array2D(vec4Array2D, vec4);
VSG_array2D(mat4Array2D, mat4);

VSG_array2D(dvec2Array2D, dvec2);
VSG_array2D(dvec3Array2D, dvec3);
VSG_array2D(dvec4Array2D, dvec4);

} // namespace vsg
Loading

0 comments on commit 5cae707

Please sign in to comment.