File: Setting.h

package info (click to toggle)
pymol 1.8.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,248 kB
  • ctags: 24,095
  • sloc: cpp: 474,635; python: 75,034; ansic: 22,888; sh: 236; makefile: 78; csh: 21
file content (396 lines) | stat: -rw-r--r-- 12,411 bytes parent folder | download | duplicates (2)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/
#ifndef _H_Setting
#define _H_Setting

#include <vector>
#include <string>

#include"os_python.h"
#include"PyMOLGlobals.h"
#include"OVOneToOne.h"

typedef char SettingName[255];

/*
 * Setting record for atom/astate/bond/bstate level settings
 */
typedef struct {
  int setting_id;
  union {
    int int_;
    float float_;
    float float3_[3];
  } value;
  int next;                     /* for per-atom setting lists & memory management */
} SettingUniqueEntry;

struct _CSettingUnique {
  OVOneToOne *id2offset;
  OVOneToOne *old2new;
  SettingUniqueEntry *entry;
  int n_alloc, next_free;
};

/*
 * Setting record for global/object/ostate level settings
 */
struct SettingRec {
  union {
    int int_;
    float float_;
    float float3_[3];
    std::string * str_;
  };

  bool defined;
  bool changed;

private:
  void setChanged() {
    defined = true;
    changed = true;
  }

public:
  void set_i(int value) {
    int_ = value;
    setChanged();
  }

  void set_f(float value) {
    float_ = value;
    setChanged();
  }

  void set_3f(float v0, float v1, float v2) {
    float3_[0] = v0;
    float3_[1] = v1;
    float3_[2] = v2;
    setChanged();
  }

  void set_3f(const float * value) {
    set_3f(value[0], value[1], value[2]);
  }

  void set_s(const char * value) {
    if (!str_) {
      str_ = new std::string(value);
    } else {
      str_->assign(value);
    }
    setChanged();
  }

  void delete_s() {
    if (str_) {
      delete str_;
      str_ = NULL;
    }
  }
};

struct _CSetting {
  PyMOLGlobals *G;
  ov_size size;
  SettingRec *info;
};

#define cSetting_blank       0
#define cSetting_boolean     1
#define cSetting_int         2
#define cSetting_float       3
#define cSetting_float3      4
#define cSetting_color       5
#define cSetting_string      6


/* Atomic Settings */

void SettingUniqueDetachChain(PyMOLGlobals * G, int index);

/* New API 
 * NOTE: get commands are not range-checked, so be careful
 * in contrast, set commands expand the current list 
 */

int SettingUniqueSetTypedValue(PyMOLGlobals * G, int unique_id, int setting_id,
			       int setting_type, const void *value);
#ifndef _PYMOL_NOPY
bool SettingUniqueSetPyObject(PyMOLGlobals * G, int unique_id, int setting_id, PyObject *value);
#endif

int SettingUniqueCheck(PyMOLGlobals * G, int unique_id, int setting_id);
PyObject *SettingUniqueGetPyObject(PyMOLGlobals * G, int unique_id, int index);

void SettingUniqueResetAll(PyMOLGlobals * G);
PyObject *SettingUniqueAsPyList(PyMOLGlobals * G);
int SettingUniqueFromPyList(PyMOLGlobals * G, PyObject * list, int partial_restore);
int SettingUniqueConvertOldSessionID(PyMOLGlobals * G, int old_unique_id);

int SettingUniqueCopyAll(PyMOLGlobals * G, int src_unique_id, int dst_unique_id);
void SettingInitGlobal(PyMOLGlobals * G, int alloc, int reset_gui, int use_default);
void SettingStoreDefault(PyMOLGlobals * G);
void SettingPurgeDefault(PyMOLGlobals * G);

void SettingFreeGlobal(PyMOLGlobals * G);

CSetting *SettingNew(PyMOLGlobals * G);
void SettingFreeP(CSetting * I);
void SettingInit(PyMOLGlobals * G, CSetting * I);
void SettingPurge(CSetting * I);
void SettingCheckHandle(PyMOLGlobals * G, CSetting ** handle);

#define SettingSet_b SettingSet_i
int SettingSet_i(CSetting * I, int index, int value);
int SettingSet_f(CSetting * I, int index, float value);
int SettingSet_s(CSetting * I, int index, const char *value);
int SettingSet_3fv(CSetting * I, int index, const float *value);

int SettingGetTextValue(PyMOLGlobals * G, const CSetting * set1, const CSetting * set2, int index,
                        char *buffer);
const char * SettingGetTextPtr(PyMOLGlobals * G, const CSetting * set1, const CSetting * set2,
                               int index, char *buffer);

int SettingUnset(CSetting * I, int index);

void SettingRestoreDefault(CSetting * I, int index, const CSetting * src=NULL);

bool SettingIsDefaultZero(int index);

int SettingGetType(int index);
inline int SettingGetType(PyMOLGlobals *, int index) {
  return SettingGetType(index);
}

template <typename V> inline int SettingGetType();
template <> inline int SettingGetType<bool>()           { return cSetting_boolean; }
template <> inline int SettingGetType<int>()            { return cSetting_int; }
template <> inline int SettingGetType<float>()          { return cSetting_float; }
template <> inline int SettingGetType<const float*>()   { return cSetting_float3; }
template <> inline int SettingGetType<float*>()         { return cSetting_float3; }
template <> inline int SettingGetType<const char*>()    { return cSetting_string; }
template <> inline int SettingGetType<char*>()          { return cSetting_string; }

#define SettingSetGlobal_b SettingSet<bool>
#define SettingSetGlobal_i SettingSet<int>
#define SettingSetGlobal_s SettingSet<const char *>
#define SettingSetGlobal_f SettingSet<float>

int SettingSetSmart_i(PyMOLGlobals * G, CSetting * set1, CSetting * set2, int index,
                      int value);

int SettingSet_color(CSetting * I, int index, const char *value);

int SettingSetFromString(PyMOLGlobals * G, CSetting * I, int index, const char *st);
int SettingStringToTypedValue(PyMOLGlobals * G, int index, const char *st, int *type,
                              int *value);

#ifndef _PYMOL_NOPY
int SettingSetFromTuple(PyMOLGlobals * G, CSetting * I, int index, PyObject * tuple);
PyObject *SettingGetPyObject(PyMOLGlobals * G, const CSetting * set1, const CSetting * set2, int index);
PyObject *SettingGetTuple(PyMOLGlobals * G, const CSetting * set1, const CSetting * set2, int index);       /* (type,(value,)) */
PyObject *SettingGetSettingIndices();
PyObject *SettingUniqueGetIndicesAsPyList(PyMOLGlobals * G, int unique_id);
#endif

std::vector<int> SettingGetUpdateList(PyMOLGlobals * G, const char *, int);

void SettingGenerateSideEffects(PyMOLGlobals * G, int index, const char *sele, int state, int quiet);

int SettingGetIndex(PyMOLGlobals * G, const char *name);
int SettingGetName(PyMOLGlobals * G, int index, SettingName name);
const char * SettingGetName(int index);

PyObject *SettingAsPyList(CSetting * I, bool incl_blacklisted=false);
int SettingFromPyList(CSetting * I, PyObject * list);

int SettingSetGlobalsFromPyList(PyMOLGlobals * G, PyObject * list);
PyObject *SettingGetGlobalsAsPyList(PyMOLGlobals * G);


CSetting *SettingNewFromPyList(PyMOLGlobals * G, PyObject * list);


// The following defines the enum with all cSetting_<settingname> indices
#include "SettingInfo.h"

#define cStereo_default              0  /* stereo_mode=0 only used for startup */
#define cStereo_quadbuffer           1
#define cStereo_crosseye             2
#define cStereo_walleye              3
#define cStereo_geowall              4
#define cStereo_sidebyside           5
#define cStereo_stencil_by_row       6
#define cStereo_stencil_by_column    7
#define cStereo_stencil_checkerboard 8
#define cStereo_stencil_custom       9  /* for hardware developers to use */
#define cStereo_anaglyph            10
#define cStereo_dynamic             11  /* dynamic polarization */
#define cStereo_clone_dynamic       12

/*
 * State index iterator which iterates either over a single state (state >= 0),
 * the current state (state == -2), or all states (state == -1). Takes
 * static singletons into account. Zero iterations if state >= nstate.
 *
 * StateIterator iter(G, I->Obj.Setting, state, I->NState);
 * while(iter.next()) {
 *   printf("in state %d\n", iter.state);
 * }
 */
class StateIterator {
  int end;

public:
  int state;

  StateIterator(PyMOLGlobals * G, CSetting * set, int state_, int nstate);

  bool next() {
    return (++state < end);
  };
};

/*
 * Setting levels (global, object, ...)
 */

enum {
  cSettingLevel_unused = 0,
  cSettingLevel_global,
  cSettingLevel_object,
  cSettingLevel_ostate,
  cSettingLevel_atom,
  cSettingLevel_astate,
  cSettingLevel_bond,
  cSettingLevel_bstate
};

// Setting level info table
extern const struct SettingLevelInfoType {
  // name of this level, for feedback
  const char * name;
  // bitmask of valid (sub-)levels
  unsigned char mask;
} SettingLevelInfo[];

const char * SettingLevelGetName(PyMOLGlobals * G, int index);
bool SettingLevelCheckMask(PyMOLGlobals * G, int index, unsigned char mask);
bool SettingLevelCheck(PyMOLGlobals * G, int index, unsigned char level);

bool CPyMOLInitSetting(OVLexicon * Lex, OVOneToOne * Setting);
extern "C" OVreturn_word get_setting_id(CPyMOL * I, const char *setting);

/*
 * Overloaded setters for templatted programming
 */

inline void SettingSet(CSetting * s, int i, bool v)         { SettingSet_b(s, i, v); }
inline void SettingSet(CSetting * s, int i, int v)          { SettingSet_i(s, i, v); }
inline void SettingSet(CSetting * s, int i, long int v)     { SettingSet_i(s, i, v); }
inline void SettingSet(CSetting * s, int i, float v)        { SettingSet_f(s, i, v); }
inline void SettingSet(CSetting * s, int i, const char *v)  { SettingSet_s(s, i, v); }
inline void SettingSet(CSetting * s, int i, const float *v) { SettingSet_3fv(s, i, v); }

template <typename V>
void SettingUniqueSet(PyMOLGlobals * G, int uid, int index, V value) {
  SettingUniqueSetTypedValue(G, uid, index, SettingGetType<V>(), &value);
}

template <typename V> void SettingSet(PyMOLGlobals * G, CSetting ** handle, int index, V value) {
  SettingCheckHandle(G, handle);
  SettingSet(*handle, index, value);
}

// global setting
template <typename V> bool SettingSet(PyMOLGlobals * G, int index, V value) {
  SettingSet(G->Setting, index, value);
  return true;
}

// light setting array
extern int light_setting_indices[];

/*
 * Getters for templatted programming
 */

const CSetting * _SettingGetFirstDefined(int index,
    PyMOLGlobals * G,
    const CSetting * set1,
    const CSetting * set2);

template <typename V> V SettingGet(int index, const CSetting *);
template <typename V> V SettingGet(PyMOLGlobals * G,
    const CSetting * set1,
    const CSetting * set2, int index) {
  return SettingGet<V>(index, _SettingGetFirstDefined(index, G, set1, set2));
}
template <typename V> V SettingGet(PyMOLGlobals * G, int index) {
  return SettingGet<V>(index, G->Setting);
}

/*
 * Get setting value if it's defined in the given set, and assign value to
 * `out` variable and return true. Otherwise return false and leave `out`
 * untouched.
 */
template <typename V>
bool SettingGetIfDefined(const CSetting * s, int index, V * out) {
  if (s && s->info[index].defined) {
    *out = SettingGet<V>(index, s);
    return true;
  }
  return false;
}

#define SettingGet_b            SettingGet<bool>
#define SettingGet_i            SettingGet<int>
#define SettingGet_color        SettingGet<int>
#define SettingGet_f            SettingGet<float>
#define SettingGet_s            SettingGet<const char *>
#define SettingGet_3fv          SettingGet<const float *>

#define SettingGetGlobal_b      SettingGet<bool>
#define SettingGetGlobal_i      SettingGet<int>
#define SettingGetGlobal_color  SettingGet<int>
#define SettingGetGlobal_f      SettingGet<float>
#define SettingGetGlobal_s      SettingGet<const char *>
#define SettingGetGlobal_3fv    SettingGet<const float *>

#define SettingGetIfDefined_b(G, s, i, o) SettingGetIfDefined(s, i, o)
#define SettingGetIfDefined_i(G, s, i, o) SettingGetIfDefined(s, i, o)

/*
 * templatted unique settings
 */

bool SettingUniqueGetTypedValuePtr(PyMOLGlobals * G, int unique_id, int index,
    int setting_type, void * out);

/*
 * `SettingGetIfDefined` equivalent for unique settings.
 */
template <typename V>
bool SettingUniqueGetIfDefined(PyMOLGlobals * G, int unique_id, int index, V * out) {
  return SettingUniqueGetTypedValuePtr(G, unique_id, index,
      SettingGetType<V>(), out);
}

#endif