Savarese Software Research Corporation
GroupList.h
Go to the documentation of this file.
1 /* Copyright 2006 Savarese Software Research Corporation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.savarese.com/software/ApacheLicense-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
21 #ifndef __SSRC_SPREAD_GROUP_LIST_H
22 #define __SSRC_SPREAD_GROUP_LIST_H
23 
24 #include <string>
25 #include <ostream>
26 
27 #include <ssrc/spread/detail/Buffer.h>
28 
29 // sp.h includes stddef.h, so we need to include it before sp.h in
30 // order to ensure it gets skipped while inside the Spread namespace.
31 #include <cstddef>
32 
34 # include <sp.h>
36  typedef char group_type[MAX_GROUP_NAME];
38 
40 
41 using std::string;
42 
43 enum {
51  MaxSizePrivateName = MAX_PRIVATE_NAME,
53  MaxSizeProcessName = MAX_PROC_NAME,
55  MaxSizeGroupName = MAX_GROUP_NAME,
62 };
63 
64 
75 inline
76 std::pair<string,string> split_private_group(const string & private_group) {
77  string::size_type length = private_group.find(GroupNameSeparatorChar, 1) - 1;
78  return std::pair<string,string>(string(private_group, 1, length),
79  string(private_group, length + 2,
80  private_group.size() - length - 2));
81 }
82 
90 class GroupList {
91 private:
92  struct group_type {
94  };
95 
96  typedef detail::Buffer<group_type> group_vector;
97 
98  group_vector _groups;
99 
100  const Spread::group_type *groups() const {
101  return reinterpret_cast<const Spread::group_type *>(&_groups[0]);
102  }
103 
104  Spread::group_type *groups() {
105  return
106  const_cast<Spread::group_type *>(reinterpret_cast<const Spread::group_type *>(&_groups[0]));
107  }
108 
109  unsigned int capacity() const {
110  return _groups.capacity();
111  }
112 
113  void resize(const unsigned int size) {
114  _groups.resize(size);
115  }
116 
117 public:
118 
122  explicit GroupList(const unsigned int capacity = 10) : _groups(capacity) { }
123 
129  void add(const string & group) {
130  int size;
131  group_type gname;
132  size = group.copy(gname.group, sizeof(Spread::group_type) - 1);
133  gname.group[size] = 0;
134  _groups.add(gname);
135  }
136 
142  void add(const GroupList & groups) {
143  if(&groups != this) {
144  unsigned int offset = size();
145  resize(size() + groups.size());
146  std::memcpy(GroupList::groups() + offset, groups.groups(),
147  groups.size()*sizeof(group_type));
148  }
149  }
150 
159  string group(const unsigned int index) const {
160  return _groups[index].group;
161  }
162 
164  string operator[](const unsigned int index) const {
165  return group(index);
166  }
167 
174  GroupList & operator=(const GroupList & groups) {
175  if(&groups != this) {
176  resize(groups.size());
177  std::memcpy(GroupList::groups(), groups.groups(),
178  size()*sizeof(group_type));
179  }
180  return *this;
181  }
182 
184  void clear() {
185  _groups.clear();
186  }
187 
192  unsigned int size() const {
193  return _groups.size();
194  }
195 
196 private:
197  friend class Mailbox;
198  friend class BaseMessage;
199 
210  friend bool operator==(const GroupList & groups1,
211  const GroupList & groups2)
212  {
213  unsigned int size = groups1.size();
214 
215  if(size != groups2.size())
216  return false;
217  else
218  while(size-- > 0) {
219  if(std::strncmp(groups1.groups()[size], groups2.groups()[size],
221  return false;
222  }
223 
224  return true;
225  }
226 
234  friend std::ostream & operator<<(std::ostream & output,
235  const GroupList & groups)
236  {
237  for(unsigned int i = 0; i < groups.size(); ++i)
238  output << groups._groups[i].group << std::endl;
239  return output;
240  }
241 };
242 
244 
245 #endif

Savarese Software Research Corporation
Copyright © 2006-2015 Savarese Software Research Corporation. All rights reserved.