summaryrefslogtreecommitdiffstats
path: root/libdimensionxx/dimensionxx
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-07-07 04:23:05 +0000
committerTavian Barnes <tavianator@gmail.com>2009-07-07 04:23:05 +0000
commit81c84a38992ce8e38106d86ce85ac3e88ed91a31 (patch)
tree08fb5913e141b7e208737b799b45921d477d45cc /libdimensionxx/dimensionxx
parentd7b7b4b3391cf99ca63d8311eac3957df7a862ed (diff)
downloaddimension-81c84a38992ce8e38106d86ce85ac3e88ed91a31.tar.xz
Add shallow copy semantics to Camera's, Object's, and Scene's.
Diffstat (limited to 'libdimensionxx/dimensionxx')
-rw-r--r--libdimensionxx/dimensionxx/camera.hpp20
-rw-r--r--libdimensionxx/dimensionxx/cameras.hpp9
-rw-r--r--libdimensionxx/dimensionxx/object.hpp20
-rw-r--r--libdimensionxx/dimensionxx/objects.hpp18
-rw-r--r--libdimensionxx/dimensionxx/scene.hpp53
5 files changed, 98 insertions, 22 deletions
diff --git a/libdimensionxx/dimensionxx/camera.hpp b/libdimensionxx/dimensionxx/camera.hpp
index 950e16d..a883969 100644
--- a/libdimensionxx/dimensionxx/camera.hpp
+++ b/libdimensionxx/dimensionxx/camera.hpp
@@ -29,12 +29,15 @@ namespace Dimension
class Camera
{
public:
- // No-op, made pure virtual
- virtual ~Camera() = 0;
+ // No-op
+ virtual ~Camera();
// Camera callback
virtual Line ray(const Canvas& canvas, unsigned int x, unsigned int y);
+ // Shallow-copy a derived camera
+ virtual Camera* copy() const = 0;
+
// Access the wrapped C camera.
dmnsn_camera* dmnsn();
const dmnsn_camera* dmnsn() const;
@@ -42,15 +45,22 @@ namespace Dimension
protected:
// No-op
Camera();
+ // Shallow-copy
+ Camera(const Camera& camera);
// Wrap an existing camera
explicit Camera(dmnsn_camera* camera);
- dmnsn_camera* m_camera;
+ // Is m_camera unique?
+ bool unique() const;
+
+ // Set the wrapped C camera
+ void dmnsn(dmnsn_camera* camera);
private:
- // Copying prohibited
- Camera(const Camera&);
+ // Copy-assignment prohibited
Camera& operator=(const Camera&);
+
+ std::tr1::shared_ptr<dmnsn_camera*> m_camera;
};
// A custom camera abstract base class, for creating your own camera types
diff --git a/libdimensionxx/dimensionxx/cameras.hpp b/libdimensionxx/dimensionxx/cameras.hpp
index 4e67d64..cabae97 100644
--- a/libdimensionxx/dimensionxx/cameras.hpp
+++ b/libdimensionxx/dimensionxx/cameras.hpp
@@ -30,10 +30,17 @@ namespace Dimension
{
public:
Perspective_Camera(const Matrix& trans);
- virtual ~Perspective_Camera();
+ ~Perspective_Camera();
Matrix trans();
void trans(const Matrix& trans);
+
+ Camera* copy() const;
+
+ private:
+ // Copying prohibited, but used internally
+ Perspective_Camera(const Perspective_Camera& camera);
+ Perspective_Camera& operator=(const Perspective_Camera&);
};
}
diff --git a/libdimensionxx/dimensionxx/object.hpp b/libdimensionxx/dimensionxx/object.hpp
index 1b9c848..c56d358 100644
--- a/libdimensionxx/dimensionxx/object.hpp
+++ b/libdimensionxx/dimensionxx/object.hpp
@@ -29,8 +29,8 @@ namespace Dimension
class Object
{
public:
- // No-op, made pure virtual
- virtual ~Object() = 0;
+ // No-op
+ virtual ~Object();
// Get/set the transformation matrix
Matrix trans();
@@ -40,6 +40,9 @@ namespace Dimension
virtual Array<double> intersections(const Line& l);
virtual bool inside(const Vector& point);
+ // Shallow-copy a derived
+ virtual Object* copy() const = 0;
+
// Access the wrapped C object.
dmnsn_object* dmnsn();
const dmnsn_object* dmnsn() const;
@@ -47,15 +50,22 @@ namespace Dimension
protected:
// No-op
Object();
+ // Shallow copy
+ Object(const Object& object);
// Wrap an existing object.
explicit Object(dmnsn_object* object);
- dmnsn_object* m_object;
+ // Is m_object unique?
+ bool unique() const;
+
+ // Set the wrapped object
+ void dmnsn(dmnsn_object* object);
private:
- // Copying prohibited
- Object(const Object&);
+ // Copy-assignment prohibited
Object& operator=(const Object&);
+
+ std::tr1::shared_ptr<dmnsn_object*> m_object;
};
// A custom object abstract base class, for creating your own object types
diff --git a/libdimensionxx/dimensionxx/objects.hpp b/libdimensionxx/dimensionxx/objects.hpp
index 89aab14..5b43dc4 100644
--- a/libdimensionxx/dimensionxx/objects.hpp
+++ b/libdimensionxx/dimensionxx/objects.hpp
@@ -30,7 +30,14 @@ namespace Dimension
{
public:
Sphere();
- virtual ~Sphere();
+ ~Sphere();
+
+ Object* copy() const;
+
+ private:
+ // Copying prohibited, but used internally
+ Sphere(const Sphere& sphere);
+ Sphere& operator=(const Sphere&);
};
// A cube
@@ -38,7 +45,14 @@ namespace Dimension
{
public:
Cube();
- virtual ~Cube();
+ ~Cube();
+
+ Object* copy() const;
+
+ private:
+ // Copying prohibited, but used internally
+ Cube(const Cube& cube);
+ Cube& operator=(const Cube&);
};
}
diff --git a/libdimensionxx/dimensionxx/scene.hpp b/libdimensionxx/dimensionxx/scene.hpp
index 256de29..b5adde0 100644
--- a/libdimensionxx/dimensionxx/scene.hpp
+++ b/libdimensionxx/dimensionxx/scene.hpp
@@ -25,14 +25,20 @@
namespace Dimension
{
+ // Iterator class for scene objects
+ class Scene_Iterator;
+
// Base scene class. Wraps a dmnsn_scene*.
class Scene
{
public:
- // Allocate a dmnsn_scene
+ typedef Scene_Iterator Iterator;
+
+ // Allocate a dmnsn_scene*
Scene(const Color& background, Camera& camera, Canvas& canvas);
- // Wrap an existing scene
- explicit Scene(dmnsn_scene* scene);
+
+ // Scene(const Scene& scene);
+
// Delete the scene
~Scene();
@@ -43,7 +49,11 @@ namespace Dimension
Canvas& canvas();
const Canvas& canvas() const;
- // Add objects
+ // Object access
+
+ Iterator begin();
+ Iterator end();
+
void push_object(Object& object);
// Access the wrapped C object.
@@ -51,13 +61,38 @@ namespace Dimension
const dmnsn_scene* dmnsn() const;
private:
- // Copying prohibited
- Scene(const Scene&);
+ // Copy-assignment prohibited
Scene& operator=(const Scene&);
- dmnsn_scene* m_scene;
- Camera* m_camera;
- Canvas* m_canvas;
+ std::tr1::shared_ptr<dmnsn_scene*> m_scene;
+ std::tr1::shared_ptr<Camera> m_camera;
+ std::tr1::shared_ptr<Canvas> m_canvas;
+ std::list<std::tr1::shared_ptr<Object> > m_objects;
+ };
+
+ class Scene_Iterator
+ {
+ public:
+ Scene_Iterator(std::list<std::tr1::shared_ptr<Object> >::iterator i)
+ : m_i(i) { }
+ // Scene_Iterator(const Scene_Iterator& i);
+ // ~Scene_Iterator();
+
+ // Scene_Iterator& operator=(const Scene_Iterator& i);
+
+ Object& operator*() const { return **m_i; }
+ Object* operator->() const { return &**m_i; }
+
+ bool operator==(Scene_Iterator i) const { return m_i == i.m_i; }
+ bool operator!=(Scene_Iterator i) const { return m_i != i.m_i; }
+
+ Scene_Iterator& operator++() { ++m_i; return *this; }
+ Scene_Iterator operator++(int) { return Scene_Iterator(m_i++); }
+ Scene_Iterator& operator--() { --m_i; return *this; }
+ Scene_Iterator operator--(int) { return Scene_Iterator(m_i--); }
+
+ private:
+ std::list<std::tr1::shared_ptr<Object> >::iterator m_i;
};
}