Libmacro  0.2
Libmacro is an extensible macro and hotkey library.
registry.h
Go to the documentation of this file.
1 /* Libmacro - A multi-platform, extendable macro and hotkey C library
2  Copyright (C) 2013 Jonathan Pelletier, New Paradigm Software
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
23 #ifndef __cplusplus
24  #pragma message "C++ support is required for extras module"
25  #include "mcr/err.h"
26 #endif
27 
28 #ifndef MCR_EXTRAS_REGISTRY_H_
29 #define MCR_EXTRAS_REGISTRY_H_
30 
31 #include "mcr/extras/base_cpp.h"
32 
33 namespace mcr
34 {
36 class RegistryPrivate;
37 struct MCR_API Registry
38 {
39  friend class RegistryPrivate;
40  mcr_IRegistry self;
41 
42  Registry();
43  Registry(const Registry &copytron) = delete;
44  virtual ~Registry();
45  Registry &operator =(const Registry &copytron) = delete;
46 
47  inline mcr_IRegistry &operator *()
48  {
49  return self;
50  }
51  inline const mcr_IRegistry &operator *() const
52  {
53  return self;
54  }
55  static inline Registry *offset(mcr_IRegistry *originPt)
56  {
57  return mcr::offset<Registry>(originPt);
58  }
59  static inline const Registry *offset(const mcr_IRegistry *originPt)
60  {
61  return mcr::offset<Registry>(originPt);
62  }
63 // static inline const Registry *self(const mcr_IRegistry *originPt)
64 // {
65 // return (const Registry *)originPt;
66 // }
67 
68  virtual size_t id(struct mcr_Interface *ifacePt) const;
69  virtual struct mcr_Interface *interface(size_t id) const;
70  virtual const char *name(struct mcr_Interface *ifacePt) const;
71  virtual struct mcr_Interface *interface(const char *name) const;
72  virtual size_t setName(struct mcr_Interface *ifacePt, const char *name);
73  virtual void addName(struct mcr_Interface *ifacePt, const char *name);
74  virtual void addNames(struct mcr_Interface *ifacePt, const char * const *addNames, size_t addNamesCount);
75  virtual void remove(struct mcr_Interface *ifacePt);
76  virtual void removeName(const char *removeName);
77  virtual size_t count() const;
78  virtual void trim();
79  virtual void clear();
80  /* Remove all interfaces registerd, but do not modify name mapping. */
81  virtual void unregister();
82 
83 private:
84  /* non-export */
85  RegistryPrivate *_private;
86 
87  static void deinit(struct mcr_IRegistry *iregPt)
88  {
89  if (iregPt)
90  offset(iregPt)->unregister();
91  }
92  static size_t id(const struct mcr_IRegistry *iregPt, struct mcr_Interface *ifacePt)
93  {
94  return iregPt ? offset(iregPt)->id(ifacePt) : (size_t)~0;
95  }
96  static struct mcr_Interface *from_id(const struct mcr_IRegistry *iregPt, size_t id)
97  {
98  return iregPt ? offset(iregPt)->interface(id) : nullptr;
99  }
100  static const char *name(const struct mcr_IRegistry *iregPt, struct mcr_Interface *ifacePt)
101  {
102  return iregPt ? offset(iregPt)->name(ifacePt) : nullptr;
103  }
104  static struct mcr_Interface *from_name(const struct mcr_IRegistry *iregPt, const char *name)
105  {
106  return iregPt ? offset(iregPt)->interface(name) : nullptr;
107  }
108  static size_t set_name(struct mcr_IRegistry *iregPt, struct mcr_Interface *ifacePt, const char *name)
109  {
110  mcr_dassert(iregPt);
111  if (!iregPt) {
112  mcr_err = EFAULT;
113  return (size_t)~0;
114  }
115  try {
116  return offset(iregPt)->setName(ifacePt, name);
117  } catch (int e) {
118  mcr_err = e;
119  return (size_t)~0;
120  }
121  return (size_t)~0;
122  }
123  static int add_names(struct mcr_IRegistry *iregPt, struct mcr_Interface *ifacePt, const char * const*addNames, size_t addNamesCount)
124  {
125  mcr_dassert(iregPt);
126  if (!iregPt)
127  return EFAULT;
128  try {
129  offset(iregPt)->addNames(ifacePt, addNames, addNamesCount);
130  } catch (int e) {
131  return e;
132  }
133  return 0;
134  }
135  static void remove(struct mcr_IRegistry *iregPt, struct mcr_Interface *ifacePt)
136  {
137  if (iregPt)
138  offset(iregPt)->remove(ifacePt);
139  }
140  static void remove_name(struct mcr_IRegistry *iregPt, const char *removeName)
141  {
142  if (iregPt)
143  offset(iregPt)->removeName(removeName);
144  }
145  static size_t count(const struct mcr_IRegistry *iregPt)
146  {
147  return iregPt ? offset(iregPt)->count() : 0;
148  }
149  static void trim(struct mcr_IRegistry *iregPt)
150  {
151  if (iregPt)
152  offset(iregPt)->trim();
153  }
154  static void clear(struct mcr_IRegistry *iregPt)
155  {
156  if (iregPt)
157  offset(iregPt)->clear();
158  }
159 };
160 }
161 
162 #endif
mcr_data_fnc deinit
Definition: interface.h:53
Raise a compiler error. Usage: #include "mcr/err.h"
Libmacro, by Jonathan Pelletier, New Paradigm Software. Alpha version.
Definition: classes.h:31
rT * offset(vT *valuePt)
Definition: functions.h:44
#define mcr_dassert(expression)
Definition: defines.h:298
size_t id
Definition: interface.h:37