Click here to Skip to main content
15,891,607 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I don't know why I hate statics so much Pin
0x01AA11-Dec-21 5:00
mve0x01AA11-Dec-21 5:00 
GeneralRe: I don't know why I hate statics so much Pin
David O'Neil11-Dec-21 5:27
professionalDavid O'Neil11-Dec-21 5:27 
GeneralRe: I don't know why I hate statics so much Pin
honey the codewitch11-Dec-21 5:48
mvahoney the codewitch11-Dec-21 5:48 
GeneralRe: I don't know why I hate statics so much Pin
David O'Neil11-Dec-21 7:44
professionalDavid O'Neil11-Dec-21 7:44 
GeneralRe: I don't know why I hate statics so much Pin
honey the codewitch11-Dec-21 10:04
mvahoney the codewitch11-Dec-21 10:04 
GeneralRe: I don't know why I hate statics so much Pin
PIEBALDconsult11-Dec-21 5:54
mvePIEBALDconsult11-Dec-21 5:54 
GeneralRe: I don't know why I hate statics so much Pin
Greg Utas11-Dec-21 6:17
professionalGreg Utas11-Dec-21 6:17 
GeneralRe: I don't know why I hate statics so much Pin
honey the codewitch11-Dec-21 6:23
mvahoney the codewitch11-Dec-21 6:23 
I have to be very careful with this code. Anything I do to de-staticize it can potentially impact performance severely, and also complicate it since it uses a lot of function pointers that it passes around. I'd have to add state parameters to get back to the class instance if it wasn't backed statically.

So what I'm doing is like this:

C++
#pragma once
#include <stddef.h>
#include <stdint.h>
#include "nes_core.hpp"

namespace nes {
    struct apu_context;
    struct apu_ext;
    enum struct apu_filter_type
    {
        none,
        low_pass,
        weighted
    };

    class apu final {
        static void initialize(double base_freq, int sample_rate, int refresh_rate, int sample_bits);
        static void deinitialize();
        static void set(double base_freq, int sample_rate, int refresh_rate, int sample_bits);
        static void process(void *buffer, int num_samples);
        static void reset();
        static void ext(apu_context *ctx, apu_ext *ext);
        static void filter(apu_filter_type type);
        static void channel(int chan, bool enabled);
        static uint8_t read(uint32_t address);
        static void write(uint32_t address, uint8_t value);
        static apu_context* context();
        static void context(apu_context* value);
    };
    class apu_context {
    protected:
        inline apu_context() {}
    };
    class apu_ext {
    protected:
        inline apu_ext() {}
    };
    
}


And then in my CPP I have:
C++
#include "nes_platform.h"
#include <nes_apu.hpp>
#include "nes_apu_defines.h"
#include "nes_reader_writer.h"
#include "nes_apu_context.h"
namespace nes
{
    enum
    {
        APU_FILTER_NONE,
        APU_FILTER_LOWPASS,
        APU_FILTER_WEIGHTED
    };
    void apu::initialize(double base_freq, int sample_rate, int refresh_rate, int sample_bits) {
...


All of those .h files are "private" includes that don't exist under the "/include" folder but the "/src" folder since they are actually part of the implementation.

That way:

- I can keep most of the implementation static for performance and to simplify callbacks
- I can keep 75% of the structure of the existing C code.
- I hide all the nasty in the CPP and H files away from the user's namespace
- I can very easily wrap it later with a real singleton class if i want
Real programmers use butterflies

GeneralRe: I don't know why I hate statics so much Pin
Gerry Schmitz11-Dec-21 6:37
mveGerry Schmitz11-Dec-21 6:37 
GeneralRe: I don't know why I hate statics so much Pin
Gary R. Wheeler11-Dec-21 10:23
Gary R. Wheeler11-Dec-21 10:23 
GeneralRe: I don't know why I hate statics so much Pin
honey the codewitch11-Dec-21 10:27
mvahoney the codewitch11-Dec-21 10:27 
GeneralRe: I don't know why I hate statics so much Pin
honey the codewitch12-Dec-21 4:51
mvahoney the codewitch12-Dec-21 4:51 
GeneralRe: I don't know why I hate statics so much Pin
Super Lloyd11-Dec-21 17:22
Super Lloyd11-Dec-21 17:22 
GeneralRe: I don't know why I hate statics so much Pin
honey the codewitch12-Dec-21 4:48
mvahoney the codewitch12-Dec-21 4:48 
GeneralRe: I don't know why I hate statics so much Pin
Super Lloyd12-Dec-21 5:58
Super Lloyd12-Dec-21 5:58 
GeneralRe: I don't know why I hate statics so much Pin
honey the codewitch12-Dec-21 6:02
mvahoney the codewitch12-Dec-21 6:02 
GeneralRe: I don't know why I hate statics so much Pin
11917640 Member 11-Dec-21 19:39
11917640 Member 11-Dec-21 19:39 
GeneralRe: I don't know why I hate statics so much Pin
PIEBALDconsult12-Dec-21 4:19
mvePIEBALDconsult12-Dec-21 4:19 
GeneralRe: I don't know why I hate statics so much Pin
honey the codewitch12-Dec-21 4:48
mvahoney the codewitch12-Dec-21 4:48 
GeneralHave you ever wondered what the words to "Non Je Ne Regrette Rien" actually mean? Pin
OriginalGriff11-Dec-21 1:43
mveOriginalGriff11-Dec-21 1:43 
GeneralRe: Have you ever wondered what the words to "Non Je Ne Regrette Rien" actually mean? Pin
RickZeeland11-Dec-21 2:06
mveRickZeeland11-Dec-21 2:06 
GeneralRe: Have you ever wondered what the words to "Non Je Ne Regrette Rien" actually mean? Pin
Super Lloyd11-Dec-21 17:22
Super Lloyd11-Dec-21 17:22 
GeneralRe: Have you ever wondered what the words to "Non Je Ne Regrette Rien" actually mean? Pin
Daniel Pfeffer12-Dec-21 1:19
professionalDaniel Pfeffer12-Dec-21 1:19 
JokeThings to think about PinPopular
Mike Hankey10-Dec-21 23:11
mveMike Hankey10-Dec-21 23:11 
GeneralRe: Things to think about Pin
Chris C-B10-Dec-21 23:51
Chris C-B10-Dec-21 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.