File: property.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (122 lines) | stat: -rw-r--r-- 3,878 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
		\brief Implements helper functions for working with object properties
		\author Tim Shead (tshead@k-3d.com)
*/

#include "idag.h"
#include "idocument.h"
#include "iobject.h"

#include "property.h"

namespace k3d
{

iproperty* get_property(iunknown& Object, const std::string& Name)
{
	// If the object doesn't have properties, we're done ...
	iproperty_collection* const property_collection = dynamic_cast<iproperty_collection*>(&Object);
	if(!property_collection)
		return 0;

	// Get the object's properties ...
	const iproperty_collection::properties_t properties(property_collection->properties());

	// Look for a property with a matching name ...
	for(iproperty_collection::properties_t::const_iterator property_iterator = properties.begin(); property_iterator != properties.end(); ++property_iterator)
		{
			// if the name matches, we're done ...
			if((*property_iterator)->name() == Name)
				return *property_iterator;
		}
		
	// No property by that name!
	return 0;
}

boost::any get_property_value(idag& DAG, iproperty& Property)
{
	// Found the property, try following dependencies ...
	iproperty* property = &Property;
	for(iproperty* dependency = DAG.dependency(*property); dependency; dependency = DAG.dependency(*property))
		property = dependency;
	
	return property->value();
}

boost::any get_property_value(iunknown& Object, const std::string& Name)
{
	// Look for the property by name ...
	iproperty* const property = get_property(Object, Name);
	return property ? get_property_value(dynamic_cast<iobject*>(&Object)->document().dag(), *property) : boost::any();
}

boost::any get_internal_property_value(iunknown& Object, const std::string& Name)
{
	// Look for the property by name ...
	iproperty* const property = get_property(Object, Name);
	if(!property)
		return boost::any();

	return property->value();	
}

bool set_property_value(iproperty& Property, const boost::any& Value)
{
	// If the property type doesn't match, we're done ...
	if(Value.type() != Property.type())
		return false;
	
	// If the property is read-only, we're done ...
	iwritable_property* const writable_property = dynamic_cast<iwritable_property*>(&Property);
	if(!writable_property)
		return false;
	
	return writable_property->set_value(Value);
}

bool set_property_value(iunknown& Object, const std::string Name, const boost::any& Value)
{
	// If the object isn't a property collection, we're done ...
	iproperty_collection* const property_collection = dynamic_cast<iproperty_collection*>(&Object);
	if(!property_collection)
		return false;

	// Look for a property with a matching name ...
	const iproperty_collection::properties_t properties(property_collection->properties());
	for(iproperty_collection::properties_t::const_iterator property = properties.begin(); property != properties.end(); ++property)
		{
			// Name doesn't match, so keep going ...
			if((*property)->name() != Name)
				continue;

			// Success ...
			return set_property_value(**property, Value);
		}

	// Couldn't find a property by that name!
	return false;
}

} // namespace k3d