Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:45

0001 #pragma once
0002 /**
0003 sprop.h
0004 =========
0005 
0006 Curiously when using a constexpr std::array for the PROP::
0007 
0008     static constexpr std::array<sprop, 8> PROP = 
0009     {{
0010         { 0,0,"RINDEX" },
0011         { 0,1,"ABSLENGTH" },
0012         { 0,2,"RAYLEIGH" },
0013         { 0,3,"REEMISSIONPROB" },
0014         { 1,0,"GROUPVEL" },
0015         { 1,1,"SPARE11"  },
0016         { 1,2,"SPARE12"  },
0017         { 1,3,"SPARE13"  },
0018     }};
0019 
0020 Find that had to do the below in the cc to avoid link errors::
0021 
0022     constexpr std::array<sprop,8> sprop_Material::PROP ;  
0023 
0024 To avoid that have rejigged to extract the PROP from 
0025 a multiline constexpr string instead of the array.  
0026 **/
0027 
0028 #include <string>
0029 #include <vector>
0030 #include <array>
0031 #include <iomanip>
0032 #include <cassert>
0033 
0034 #include "sstr.h"
0035 
0036 struct sprop
0037 {
0038     enum { NUM_PAYLOAD_GRP = 2, NUM_PAYLOAD_VAL = 4, NUM_MATSUR = 4  } ;
0039 
0040     int  group ; 
0041     int  prop ;  
0042     char name[16] ; 
0043     double def ; 
0044 
0045     std::string desc() const ; 
0046     bool parse(const char* str) ; 
0047     bool match(int g, int v) const ; 
0048 
0049     static void Parse(std::vector<sprop>& prop, const char* lines ); 
0050     static std::string Desc(const std::vector<sprop>& prop); 
0051     static void GetNames(std::vector<std::string>& pnames, const std::vector<sprop>& prop, const char* skip_prefix ); 
0052     static const sprop* FindProp(const std::vector<sprop>& prop, const char* pname ); 
0053     static const sprop* Find(const std::vector<sprop>& prop, int g, int v); 
0054 }; 
0055 
0056 inline std::string sprop::desc() const 
0057 {
0058     std::stringstream ss ; 
0059     ss << "(" << group << "," << prop << ") " 
0060        << std::setw(20) << name 
0061        << "[" << std::setw(15) << std::scientific << def << "]"  ; 
0062     std::string s = ss.str(); 
0063     return s ; 
0064 }
0065 inline bool sprop::parse(const char* str) 
0066 {
0067     std::stringstream ss(str) ; 
0068     ss >> group >> prop >> name >> def ; 
0069     if (ss.fail()) return false ; 
0070     return true ; 
0071 }
0072 inline bool sprop::match(int g, int v) const 
0073 {
0074     return group == g && prop == v ; 
0075 }
0076 
0077 inline void sprop::Parse(std::vector<sprop>& prop, const char* lines ) // static 
0078 {
0079     std::stringstream ss;  
0080     ss.str(lines) ;
0081     std::string s;
0082     while (std::getline(ss, s, '\n'))
0083     {
0084         const char* line = s.c_str() ; 
0085         sprop p ; 
0086         bool ok = p.parse(line) ; 
0087         //std::cout << "[" << line << "]" << " " << ( ok ? p.desc() : "FAIL" ) << std::endl ; 
0088         if(ok) prop.push_back(p) ; 
0089     }
0090 }
0091 inline std::string sprop::Desc(const std::vector<sprop>& prop)  // static 
0092 {
0093     std::stringstream ss ; 
0094     for(unsigned i=0 ; i < prop.size() ; i++) ss << prop[i].desc() << std::endl; 
0095     std::string s = ss.str(); 
0096     return s ; 
0097 }
0098 inline void sprop::GetNames(std::vector<std::string>& pnames, const std::vector<sprop>& prop, const char* skip_prefix )  // static
0099 {
0100     for(unsigned i=0 ; i < prop.size() ; i++) 
0101     {
0102         const char* name = prop[i].name ; 
0103         if(sstr::MatchStart(name, skip_prefix) == false ) pnames.push_back(name) ; 
0104     }
0105 }
0106 inline const sprop* sprop::FindProp(const std::vector<sprop>& prop, const char* pname)
0107 {
0108     const sprop* p = nullptr ; 
0109     for(unsigned i=0 ; i < prop.size() ; i++) if(strcmp(prop[i].name, pname)==0) p = &prop[i] ; 
0110     return p ; 
0111 }
0112 inline const sprop* sprop::Find(const std::vector<sprop>& prop, int g, int v)
0113 {
0114     assert( g > -1 && g < NUM_PAYLOAD_GRP ); 
0115     assert( v > -1 && v < NUM_PAYLOAD_VAL ); 
0116     const sprop* p = nullptr ; 
0117     for(unsigned i=0 ; i < prop.size() ; i++) if(prop[i].match(g,v)) p = &prop[i] ; 
0118     return p ; 
0119 }
0120 
0121