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
|
/*
* This file contains source code for the PyMOL computer program
* Copyright (c) Schrodinger, LLC.
*
* Handling of "interned" strings in PyMOLGlobals::Lexicon
*/
#pragma once
#include "PyMOLGlobals.h"
#include "OVLexicon.h"
#include "pymol/zstring_view.h"
#define LexDec(G, i) OVLexicon_DecRef(G->Lexicon, i)
#define LexInc(G, i) OVLexicon_IncRef(G->Lexicon, i)
#define LexNumeric(i) (i)
/**
* Get the pointer to the internal string storage for reference `i`.
*/
inline const char * LexStr(PyMOLGlobals * G, const lexidx_t & i) {
return (i) ? OVLexicon_FetchCString(G->Lexicon, i) : "";
}
/**
* Lookup or insert new string `s` into the global lexicon and return
* it's numerical reference. Will always return 0 for the empty string.
*/
inline lexidx_t LexIdx(PyMOLGlobals * G, pymol::zstring_view s) {
return s && !s.empty() ? OVLexicon_GetFromCString(G->Lexicon, s.c_str()).word : 0;
}
/**
* Assignment (i = j) with reference count update for old and new value.
*/
inline void LexAssign(PyMOLGlobals * G, lexidx_t& i, const lexidx_t& j) {
if (i != j) {
LexDec(G, i);
i = j;
LexInc(G, i);
}
}
inline void LexAssign(PyMOLGlobals * G, lexidx_t& i, const char * s) {
LexDec(G, i);
i = LexIdx(G, s);
}
#define LEX_BORROW_NOTFOUND -1
/**
* Lookup string `s` without inserting or incrementing the ref count. If
* `s` is not in the lexicon, return -1.
*/
inline lexidx_t LexBorrow(PyMOLGlobals * G, const char * s) {
auto result = OVLexicon_BorrowFromCString(G->Lexicon, s);
return (result.status == OVstatus_SUCCESS) ? result.word : LEX_BORROW_NOTFOUND;
}
|