summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXSceneGraph.h
blob: c01243a5c37bd181b1e36fe4a84f8e581d0abb1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#ifndef NX_SCENEGRAPH_H
#define NX_SCENEGRAPH_H

#include <list>
#include <string>
#include <iostream>
#include <cassert>
#include <Nanorex/Utility/NXCommandResult.h>

// SceneGraph abstraction to bridge platforms

namespace Nanorex {

/* CLASS: NXSGNode */
/**
 * Base class for all scenegraph nodes
 *
 * @ingroup NanorexInterface, PluginArchitecture, GraphicsArchitecture
 */
class NXSGNode {
	
	friend class NXSceneGraphTest;
	
public:
    typedef std::list<NXSGNode*> ChildrenList;
    
    NXSGNode() throw();
    virtual ~NXSGNode();

    /// Initialize the scenegraph context, if any
	virtual bool initializeContext(void) { return true; }
    
    /// Cleanup the scenegraph context, if any
    virtual bool cleanupContext(void) { return true; }

    int getRefCount(void) const throw() { return ref_count; }
    
    /// Return true if child could be added successfully
    virtual bool addChild(NXSGNode *const child);
    
    /// Return true if child was found and was removed from children-list.
    /// In the process, if child's reference count becomes zero, it is deleted
    virtual bool removeChild(NXSGNode *const child);
    
    /// Give up parenthood of all existing children and recursively delete if
    /// their reference count becomes zero
    virtual void removeAllChildren(void);
    
    ChildrenList const& getChildren(void) const throw() { return children; }
    
    ChildrenList::size_type getNumChildren(void) const throw()
    { return children.size(); }
    
    virtual bool apply(void) const { return true; }
    
    /// Apply the effect of this node and its children
    /// Return true if successful. Abort at the first failure
    virtual bool applyRecursive(void) const;
    
    bool isLeaf(void) const throw() { return (children.size()==0); }
    
#ifdef NX_DEBUG
	/// Non-destructively remove all children and set ref-count to zero
	void reset(void);
    
    /// Get a name for the node
    virtual std::string const getName(void) const;
	void setName(std::string const& _name) { name = _name; }
	
    /// write scenegraph structure in GraphViz format
    void writeDotGraph(std::ostream& o) const;
	
	static void ResetIdSource(void) { idSource = 0; }
#endif
        
    /// Last error in the context
    static NXCommandResult* GetCommandResult(void) { return &_s_commandResult; }
    
private:
    int ref_count;
    int incrementRefCount(void) throw() { ++ref_count; return ref_count; }
    int decrementRefCount(void) throw()
	{ --ref_count; return ref_count; }
    
    void removeChildWithoutCheck(NXSGNode *const child);
    
protected:
    ChildrenList children;
    
#ifdef NX_DEBUG
	int id;
	static int idSource;
	std::string name;
#endif
	
    /// Most recent error - to be set by failing node
    /// All calling nodes propagate boolean result back up to root
    static NXCommandResult _s_commandResult;
    
    static void SetError(int errCode, char const *const errMsg);
	static void ClearResult(void);
    
};


} // Nanorex

#endif // NX_SCENEGRAPH_H