File: ifile_change_notifier_python.cpp

package info (click to toggle)
k3d 0.8.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 46,148 kB
  • ctags: 27,246
  • sloc: cpp: 166,490; xml: 6,995; python: 5,997; ansic: 4,978; makefile: 32; sh: 21
file content (123 lines) | stat: -rw-r--r-- 4,000 bytes parent folder | download | duplicates (3)
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
123
// K-3D
// Copyright (c) 1995-2008, 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
	\author Timothy M. Shead (tshead@k-3d.com)
*/

#include <boost/python.hpp>

#include <k3dsdk/python/ifile_change_notifier_python.h>
#include <k3dsdk/python/iunknown_python.h>
#include <k3dsdk/python/utility_python.h>

#include <k3dsdk/ifile_change_notifier.h>
#include <k3dsdk/path.h>

using namespace boost::python;

namespace k3d
{

namespace python
{

///////////////////////////////////////////////////////////////////////////////////////////////
// file_change_receiver

class file_change_receiver
{
public:
	void file_changed(const filesystem::path& Path)
	{
		changed_files.push_back(Path);
	}

	boost::python::list changed()
	{
		boost::python::list result;
		for(k3d::uint_t i = 0; i != changed_files.size(); ++i)
			result.append(changed_files[i]);
		return result;
	}

	void clear()
	{
		changed_files.clear();
	}

private:
	std::vector<filesystem::path> changed_files;
};

///////////////////////////////////////////////////////////////////////////////////////////////
// ifile_change_notifier

static uint_t watch_file(iunknown_wrapper& Self, const filesystem::path& Path, file_change_receiver& Receiver)
{
	return Self.wrapped<k3d::ifile_change_notifier>().watch_file(Path, sigc::bind(sigc::mem_fun(Receiver, &file_change_receiver::file_changed), Path));
}

static void unwatch_file(iunknown_wrapper& Self, const uint_t WatchID)
{
	Self.wrapped<k3d::ifile_change_notifier>().unwatch_file(WatchID);
}

static void wait_for_changes(iunknown_wrapper& Self)
{
	Self.wrapped<k3d::ifile_change_notifier>().wait_for_changes();
}

static uint_t change_count(iunknown_wrapper& Self)
{
	return Self.wrapped<k3d::ifile_change_notifier>().change_count();
}

static void signal_change(iunknown_wrapper& Self)
{
	Self.wrapped<k3d::ifile_change_notifier>().signal_change();
}

void define_class_file_change_receiver()
{
	class_<file_change_receiver>("file_change_receiver",
		"Helper class that caches file-change notifications for later retrieval.")
		.def("changed", &file_change_receiver::changed,
			"Retrieves the list of files that have changed.")
		.def("clear", &file_change_receiver::clear,
			"Clears the list of files that have changed.")
		;
}

void define_methods_ifile_change_notifier(k3d::iunknown& Interface, boost::python::object& Instance)
{
	if(!dynamic_cast<k3d::ifile_change_notifier*>(&Interface))
		return;

	utility::add_method(utility::make_function(&watch_file, "Watch a file for changes.\n\n" "@rtype: integer watch identifier\n"), "watch_file", Instance);
	utility::add_method(utility::make_function(&unwatch_file, "Stops watching a file for changes."), "unwatch_file", Instance);
	utility::add_method(utility::make_function(&wait_for_changes, "Blocks indefinitely until at least one file change has been received."), "wait_for_changes", Instance);
	utility::add_method(utility::make_function(&change_count, "Returns the number of file changes that are pending, ready to be signalled.  This method never blocks."), "change_count", Instance);
	utility::add_method(utility::make_function(&signal_change, "Signals the next file change that is pending, if any.  This method never blocks."), "signal_change", Instance);
}

} // namespace python

} // namespace k3d