github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/collection/lscq/types_gen.go (about)

     1  // Copyright 2021 ByteDance Inc.
     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.apache.org/licenses/LICENSE-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  //go:build ignore
    16  // +build ignore
    17  
    18  package main
    19  
    20  import (
    21  	"bytes"
    22  	"go/format"
    23  	"io/ioutil"
    24  	"os"
    25  	"strings"
    26  )
    27  
    28  var license = `// Copyright 2021 ByteDance Inc.
    29  //
    30  // Licensed under the Apache License, Version 2.0 (the "License");
    31  // you may not use this file except in compliance with the License.
    32  // You may obtain a copy of the License at
    33  //
    34  //     http://www.apache.org/licenses/LICENSE-2.0
    35  //
    36  // Unless required by applicable law or agreed to in writing, software
    37  // distributed under the License is distributed on an "AS IS" BASIS,
    38  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    39  // See the License for the specific language governing permissions and
    40  // limitations under the License.
    41  `
    42  
    43  func main() {
    44  	f, err := os.Open("lscq.go")
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  	filedata, err := ioutil.ReadAll(f)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	w := new(bytes.Buffer)
    54  	w.WriteString(license + `// Code generated by go run types_gen.go; DO NOT EDIT.` + "\r\n")
    55  	w.WriteString(string(filedata)[strings.Index(string(filedata), "package lscq") : strings.Index(string(filedata), ")\n")+1])
    56  	// ts := []string{"Float32", "Float64", "Int64", "Int32", "Int16", "Int", "Uint64", "Uint32", "Uint16", "Uint"} // all types need to be converted
    57  	ts := []string{"Uint64"} // all types need to be converted
    58  	for _, upper := range ts {
    59  		lower := strings.ToLower(upper)
    60  		data := string(filedata)
    61  		// Remove header(imported packages).
    62  		data = data[strings.Index(data, ")\n")+1:]
    63  		// Common cases.
    64  		data = strings.Replace(data, "atomic.StorePointer((*unsafe.Pointer)(ent.data), nil)", "", -1)
    65  		data = strings.Replace(data, "NewPointer", "New"+upper, -1)
    66  		data = strings.Replace(data, "data unsafe.Pointer", "data "+lower, -1)
    67  		data = strings.Replace(data, "data  unsafe.Pointer", "data "+lower, -1)
    68  		data = strings.Replace(data, "pointerSCQ", lower+"SCQ", -1)
    69  		data = strings.Replace(data, "PointerSCQ", upper+"SCQ", -1)
    70  		data = strings.Replace(data, "pointerQueue", lower+"Queue", -1)
    71  		data = strings.Replace(data, "PointerQueue", upper+"Queue", -1)
    72  		data = strings.Replace(data, "scqNodePointer", "scqNode"+upper, -1)
    73  		data = strings.Replace(data, "compareAndSwapSCQNodePointer", "compareAndSwapSCQNode"+upper, -1)
    74  		data = strings.Replace(data, "loadSCQNodePointer", "loadSCQNode"+upper, -1)
    75  		data = strings.Replace(data, "pointerSCQPool", lower+"SCQPool", -1)
    76  		data = strings.Replace(data, "atomicWriteBarrier(&entAddr.data)", "", -1)
    77  		w.WriteString(data)
    78  		w.WriteString("\r\n")
    79  	}
    80  
    81  	out, err := format.Source(w.Bytes())
    82  	if err != nil {
    83  		panic(err)
    84  	}
    85  	if err := ioutil.WriteFile("types.go", out, 0660); err != nil {
    86  		panic(err)
    87  	}
    88  }
    89  
    90  func lowerSlice(s []string) []string {
    91  	n := make([]string, len(s))
    92  	for i, v := range s {
    93  		n[i] = strings.ToLower(v)
    94  	}
    95  	return n
    96  }
    97  
    98  func inSlice(s []string, val string) bool {
    99  	for _, v := range s {
   100  		if v == val {
   101  			return true
   102  		}
   103  	}
   104  	return false
   105  }