(Source List) Add forall_regions function, used by Region and Source lists.
[ardour.git] / libs / ardour / ardour / region_factory.h
1 /*
2     Copyright (C) 2000-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_region_factory_h__
21 #define __ardour_region_factory_h__
22
23 #include <map>
24 #include <set>
25 #include <glibmm/threads.h>
26
27 #include "pbd/id.h"
28 #include "pbd/property_list.h"
29 #include "pbd/signals.h"
30
31 #include "ardour/libardour_visibility.h"
32 #include "ardour/types.h"
33
34 class XMLNode;
35 class RegionNamingTest;
36
37 namespace ARDOUR {
38
39 class Session;
40 class AudioRegion;
41
42 class LIBARDOUR_API RegionFactory {
43 public:
44         typedef std::map<PBD::ID,boost::shared_ptr<Region> > RegionMap;
45
46         static boost::shared_ptr<Region> wholefile_region_by_name (const std::string& name);
47         static boost::shared_ptr<Region> region_by_id (const PBD::ID&);
48         static boost::shared_ptr<Region> region_by_name (const std::string& name);
49         static const RegionMap all_regions() { return region_map; }
50         static void clear_map ();
51
52         /** This is emitted only when a new id is assigned. Therefore,
53            in a pure Region copy, it will not be emitted.
54
55            It must be emitted using a derived instance of Region, not Region
56            itself, to permit dynamic_cast<> to be used to
57            infer the type of Region.
58         */
59         static PBD::Signal1<void,boost::shared_ptr<Region> >  CheckNewRegion;
60
61         /** create a "pure copy" of Region @param other */
62         static boost::shared_ptr<Region> create (boost::shared_ptr<const Region> other, bool announce = false, bool fork = false);
63         static boost::shared_ptr<Region> create (boost::shared_ptr<Region> other, bool announce, bool fork) {
64                 return create (boost::shared_ptr<const Region>(other), announce, fork);
65         }
66
67         /** create a region from a single Source */
68         static boost::shared_ptr<Region> create (boost::shared_ptr<Source>,
69                                                  const PBD::PropertyList&, bool announce = true);
70
71         /** create a region from a multiple sources */
72         static boost::shared_ptr<Region> create (const SourceList &,
73                                                  const PBD::PropertyList&, bool announce = true);
74         /** create a copy of @other starting at zero within @param other's sources */
75         static boost::shared_ptr<Region> create (boost::shared_ptr<Region> other,
76                                                  const PBD::PropertyList&, bool announce = true);
77         /** create a copy of @param other starting at @param offset within @param other */
78         static boost::shared_ptr<Region> create (boost::shared_ptr<Region> other, ARDOUR::MusicSample offset,
79                                                  const PBD::PropertyList&, bool announce = true);
80         /** create a "copy" of @param other but using a different set of sources @param srcs */
81         static boost::shared_ptr<Region> create (boost::shared_ptr<Region> other, const SourceList& srcs,
82                                                  const PBD::PropertyList&, bool announce = true);
83
84         /** create a region with no sources, using XML state */
85         static boost::shared_ptr<Region> create (Session&, XMLNode&, bool);
86         /** create a region with specified sources @param srcs and XML state */
87         static boost::shared_ptr<Region> create (SourceList& srcs, const XMLNode&);
88
89         static boost::shared_ptr<Region> get_whole_region_for_source (boost::shared_ptr<ARDOUR::Source>);
90         
91         static void get_regions_using_source (boost::shared_ptr<Source>, std::set<boost::shared_ptr<Region> >& );
92         static void remove_regions_using_source (boost::shared_ptr<Source>);
93
94         static void map_remove (boost::weak_ptr<Region>);
95         static void delete_all_regions ();
96         static const RegionMap& regions() { return region_map; }
97         static uint32_t nregions ();
98         
99         static void foreach_region (boost::function<void( boost::shared_ptr<Region> )> f) {
100                 Glib::Threads::Mutex::Lock ls (region_map_lock);
101                 for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
102                         f ( (*i).second );
103                 }
104         }
105         
106
107
108         static int region_name (std::string &, std::string, bool new_level = false);
109         static std::string new_region_name (std::string);
110         static std::string compound_region_name (const std::string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source);
111
112         /* when we make a compound region, for every region involved there
113          * are two "instances" - the original, which is removed from this
114          * playlist, and a copy, which is added to the playlist used as
115          * the source for the compound.
116          *
117          * when we uncombine, we want to put the originals back into this
118          * playlist after we remove the compound. this map lets us
119          * look them up easily. note that if the compound was trimmed or
120          * split, we may have to trim the originals
121          * and they may not be added back if the compound was trimmed
122          * or split sufficiently.
123          */
124
125         typedef std::map<boost::shared_ptr<Region>, boost::shared_ptr<Region> > CompoundAssociations;
126         static CompoundAssociations& compound_associations() { return _compound_associations; }
127
128         static void add_compound_association (boost::shared_ptr<Region>, boost::shared_ptr<Region>);
129
130         /* exposed because there may be cases where regions are created with
131          * announce=false but they still need to be in the map soon after
132          * creation.
133          */
134
135         static void map_add (boost::shared_ptr<Region>);
136
137   private:
138         friend class ::RegionNamingTest;
139
140         static void region_changed (PBD::PropertyChange const &, boost::weak_ptr<Region>);
141
142         static Glib::Threads::Mutex region_map_lock;
143
144         static RegionMap region_map;
145
146         static Glib::Threads::Mutex region_name_maps_mutex;
147         /** map of partial region names and suffix numbers */
148         static std::map<std::string, uint32_t> region_name_number_map;
149         /** map of complete region names with their region ID */
150         static std::map<std::string, PBD::ID> region_name_map;
151         static void add_to_region_name_maps (boost::shared_ptr<Region>);
152         static void rename_in_region_name_maps (boost::shared_ptr<Region>);
153         static void update_region_name_number_map (boost::shared_ptr<Region>);
154         static void remove_from_region_name_map (std::string);
155
156         static PBD::ScopedConnectionList* region_list_connections;
157         static CompoundAssociations _compound_associations;
158 };
159
160 }
161
162 #endif /* __ardour_region_factory_h__  */